Skip to content
Draft
10 changes: 6 additions & 4 deletions contextual_orchestrator/cost_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def _seed_dimension_catalog(self) -> None:
ph = self._placeholder()
cur = self._conn.cursor()
for order, (name, label, _column) in enumerate(ATTRIBUTION_DIMENSION_CATALOG):
cur.execute(
cur.execute( # nosemgrep -- sqlalchemy-execute-raw-query FP: only the DB-API placeholder char is interpolated; the value is bound.
f"SELECT 1 FROM cost_attribution_dimensions WHERE dimension_name = {ph}", # nosec B608 - ph is a DB-API placeholder.
(name,),
)
Expand All @@ -602,7 +602,7 @@ def append(self, record: UsageRecord) -> None:
placeholders = ", ".join(ph for _ in _USAGE_COLUMNS)
columns = ", ".join(_USAGE_COLUMNS)
cur = self._conn.cursor()
cur.execute(
cur.execute( # nosemgrep -- sqlalchemy-execute-raw-query FP: columns are the fixed _USAGE_COLUMNS constant; values are bound.
f"INSERT INTO llm_usage_records ({columns}) VALUES ({placeholders})", # nosec B608 - columns are fixed _USAGE_COLUMNS.
tuple(row.get(column) for column in _USAGE_COLUMNS),
)
Expand All @@ -622,7 +622,7 @@ def query(self, start: Optional[int] = None, end: Optional[int] = None) -> List[
where = f" WHERE {' AND '.join(clauses)}" if clauses else ""
columns = ", ".join(_USAGE_COLUMNS)
cur = self._conn.cursor()
cur.execute(f"SELECT {columns} FROM llm_usage_records{where}", tuple(params)) # nosec B608 - columns and clauses are fixed.
cur.execute(f"SELECT {columns} FROM llm_usage_records{where}", tuple(params)) # nosec B608 - columns and clauses are fixed. # nosemgrep -- sqlalchemy-execute-raw-query FP: fixed columns and clause templates; all values are bound.
return [dict(zip(_USAGE_COLUMNS, values)) for values in cur.fetchall()]


Expand Down Expand Up @@ -653,7 +653,9 @@ def __init__(
) -> None:
self.price_book = price_book
self.telemetry_sink = telemetry_sink or NoopUsageTelemetrySink()
base_store = store or InMemoryLedgerStore()
# Use `is None`, not truthiness: InMemoryLedgerStore defines __len__, so an
# empty injected store is falsy and `store or ...` would silently discard it.
base_store = store if store is not None else InMemoryLedgerStore()
should_wrap = bool(non_blocking_store)
if should_wrap:
self.store: LedgerStore = NonBlockingLedgerStore(
Expand Down
97 changes: 60 additions & 37 deletions contextual_orchestrator/orchestrator.py

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions fuzz/requirements-atheris.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Atheris coverage-guided job deps (Python 3.11). Compile: uv pip compile fuzz/requirements-atheris.in --generate-hashes --python-version 3.11 --universal -o fuzz/requirements-atheris.txt
# Atheris coverage-guided job deps. atheris is published per-interpreter: the
# repo fuzz job runs CPython 3.11, where the newest published wheel is 3.0.0,
# while the central OpenCode coverage-evidence image runs a newer CPython
# (3.13+) where only 3.1.0 is published. Pin per interpreter with environment
# markers so a single hash lock satisfies both --require-hashes installs.
# Compile: uv pip compile fuzz/requirements-atheris.in --generate-hashes --python-version 3.11 --universal -o fuzz/requirements-atheris.txt
pip
atheris==3.0.0
atheris==3.0.0; python_version < "3.13"
atheris==3.1.0; python_version >= "3.13"
7 changes: 6 additions & 1 deletion fuzz/requirements-atheris.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# This file was autogenerated by uv via the following command:
# uv pip compile fuzz/requirements-atheris.in --generate-hashes --python-version 3.11 --universal -o fuzz/requirements-atheris.txt
atheris==3.0.0 \
atheris==3.0.0 ; python_full_version < '3.13' \
--hash=sha256:1f0929c7bc3040f3fe4102e557718734190cf2d7718bbb8e3ce6d3eb56ef5bb3 \
--hash=sha256:510e502c57b6dc615fb174066407af620d4c7f73cf08a782c86e7761bf12c4eb \
--hash=sha256:8a5c8a781467c187da40fd29139784193e2647058831f837f675d0bb8cbd8746 \
--hash=sha256:a402cdca8a650d1371050b1f9552eb4cdc488d2db64950d603c4560318365eac
# via -r fuzz/requirements-atheris.in
atheris==3.1.0 ; python_full_version >= '3.13' \
--hash=sha256:315a0b5c819852b1ffe1ca72efc389c7724881f2c33e4aacb8c6bcec49bd5011 \
--hash=sha256:ec5e11f21a4c197fe91f7aea2b2de88e623c73a21fc07b105ac6329a1588457b \
--hash=sha256:f8a9f51ce8369026e8eb7b7174835e8c4c85a1a6db5d9add36c15100779d2a39
# via -r fuzz/requirements-atheris.in
pip==26.1.2 \
--hash=sha256:382ff9f685ee3bc25864f820aa50505825f10f5458ffff07e30a6d96e5715cab \
--hash=sha256:f49cd134c61cf2fd75e0ce2676db03e4054504a5a4986d00f8299ae632dc4605
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ db = [
]
fuzz = [
"atheris==3.0.0; python_version < '3.13'",
"atheris==3.1.0; python_version >= '3.13'",
]

[tool.contextual_orchestrator]
Expand Down
254 changes: 254 additions & 0 deletions tests/test_batch_routing_embeddings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
"""Embeddings batch routing: heuristic embedding, local + pg backends, helpers.

Covers the offline embeddings path — ``heuristic_embedding``, the in-process
``LocalEmbeddingBatchBackend``, the ``PgLlmBatchEmbeddingBackend`` (via an async
fake client), and the module helpers — with no Postgres and no external service.
"""

from __future__ import annotations

from pathlib import Path
import sys

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from contextual_orchestrator.batch_routing import ( # noqa: E402
BatchJob,
BatchRequest,
EmbeddingBatchRequest,
LocalEmbeddingBatchBackend,
PgLlmBatchBackend,
PgLlmBatchEmbeddingBackend,
RoutingHints,
_extract_answer,
_extract_embedding,
build_embeddings_jsonl_body,
cheapest_upstream,
heuristic_embedding,
)


# --- module helpers ---------------------------------------------------------


def test_cheapest_upstream_returns_none_for_no_candidates() -> None:
"""Cheapest upstream returns none for no candidates."""
# empty candidates short-circuit before the price book is consulted
assert cheapest_upstream([], None) is None


def test_extract_answer_empty_choices_is_blank() -> None:
"""Extract answer empty choices is blank."""
assert _extract_answer({"choices": []}) == ""
assert _extract_answer({}) == ""


def test_extract_embedding_reads_first_vector_or_empty() -> None:
"""Extract embedding reads first vector or empty."""
assert _extract_embedding({"data": [{"embedding": [0.1, 0.2, 0.3]}]}) == [0.1, 0.2, 0.3]
assert _extract_embedding({}) == []


def test_heuristic_embedding_is_deterministic_and_ranged() -> None:
"""Heuristic embedding is deterministic and ranged."""
vector = heuristic_embedding("hello", dimension=8)
assert len(vector) == 8
assert all(-1.0 <= value <= 1.0 for value in vector)
assert heuristic_embedding("hello", dimension=8) == vector


def test_heuristic_embedding_rejects_non_positive_dimension() -> None:
"""Heuristic embedding rejects non positive dimension."""
with pytest.raises(ValueError):
heuristic_embedding("hello", dimension=0)


def test_embedding_request_to_jsonl_line_shape() -> None:
"""Embedding request to jsonl line shape."""
line = EmbeddingBatchRequest(input_text="hi", model="embed-x", custom_id="e1").to_jsonl_line(
"/v1/embeddings"
)
assert line["custom_id"] == "e1"
assert line["url"] == "/v1/embeddings"
assert line["body"] == {"model": "embed-x", "input": "hi"}


def test_build_embeddings_jsonl_body_is_newline_delimited() -> None:
"""Build embeddings jsonl body is newline delimited."""
body = build_embeddings_jsonl_body(
[
EmbeddingBatchRequest(input_text="hi", model="embed-x", custom_id="e1"),
EmbeddingBatchRequest(input_text="yo", model="embed-x", custom_id="e2"),
]
)
assert body.count("\n") == 1
assert '"custom_id": "e1"' in body


# --- local embeddings backend ----------------------------------------------


def test_local_embedding_backend_token_fallback_counts_words() -> None:
"""Local embedding backend token fallback counts words."""
backend = LocalEmbeddingBatchBackend(dimension=4) # no token_counter -> word fallback
job = backend.submit(
[EmbeddingBatchRequest(input_text="one two three", model="embed-x", custom_id="e1")]
)
assert backend.poll(job)["is_complete"] is True
items = backend.retrieve(job)
assert len(items) == 1
assert items[0].prompt_tokens == 3
assert len(items[0].embedding) == 4


class _FakeTokenCounter:
"""Token counter returning a fixed count, exercising the counted path."""

def count_text(self, text: str, model: str) -> int:
"""Return a constant token count regardless of input."""
return 42


def test_local_embedding_backend_uses_injected_token_counter() -> None:
"""Local embedding backend uses injected token counter."""
backend = LocalEmbeddingBatchBackend(token_counter=_FakeTokenCounter(), dimension=4)
job = backend.submit(
[EmbeddingBatchRequest(input_text="anything", model="embed-x", custom_id="e1")]
)
assert backend.retrieve(job)[0].prompt_tokens == 42


def test_local_embedding_backend_retrieve_unknown_job_is_empty() -> None:
"""Local embedding backend retrieve unknown job is empty."""
backend = LocalEmbeddingBatchBackend()
unknown = BatchJob(job_id="missing", backend="local", status="completed", request_count=0)
assert backend.retrieve(unknown) == []


# --- pg-llm-batch embeddings backend ---------------------------------------


class _FakeEmbeddingClient:
"""Async pg-llm-batch client fake returning one embedding response."""

def __init__(self) -> None:
self.calls: list[str] = []

async def upload_jsonl(self, file_path, endpoint_alias, purpose="batch"):
"""Record the call and return a stub uploaded-file id."""
self.calls.append("upload_jsonl")
return {"id": "file-emb"}

async def create_batch_job(
self, input_file_id, endpoint_alias, endpoint="/v1/embeddings", metadata=None
):
"""Record the call and return a stub batch-job id."""
self.calls.append("create_batch_job")
assert input_file_id == "file-emb"
return {"id": "batch-emb", "status": "validating"}

async def get_batch_status(self, batch_id, endpoint_alias):
"""Return a completed status."""
self.calls.append("get_batch_status")
return {"status": "completed", "is_complete": True, "progress_percentage": 100}

async def download_results(self, batch_id, endpoint_alias):
"""Return one embedding response body."""
self.calls.append("download_results")
return {
"success": True,
"responses": [
{
"custom_id": "e1",
"response": {
"body": {"data": [{"embedding": [0.5, 0.25]}], "usage": {"prompt_tokens": 7}}
},
}
],
}


def test_pg_embedding_backend_submit_poll_retrieve() -> None:
"""Pg embedding backend submit poll retrieve."""
client = _FakeEmbeddingClient()
backend = PgLlmBatchEmbeddingBackend(client, endpoint_alias="prod_gateway")
job = backend.submit(
[EmbeddingBatchRequest(input_text="embed me", model="embed-x", custom_id="e1")],
metadata={"routing_reason": "bulk"},
)
assert job.backend == "pg-llm-batch"
assert job.job_id == "batch-emb"
assert backend.poll(job)["is_complete"] is True
items = backend.retrieve(job)
assert len(items) == 1
assert items[0].custom_id == "e1"
assert items[0].embedding == [0.5, 0.25]
assert items[0].prompt_tokens == 7
assert items[0].model == "embed-x"
assert client.calls == ["upload_jsonl", "create_batch_job", "get_batch_status", "download_results"]


def test_pg_embedding_backend_incomplete_download_returns_empty() -> None:
"""Pg embedding backend incomplete download returns empty."""
class _IncompleteClient(_FakeEmbeddingClient):
async def download_results(self, batch_id, endpoint_alias):
"""Report an unsuccessful download."""
return {"success": False}

backend = PgLlmBatchEmbeddingBackend(_IncompleteClient())
job = backend.submit([EmbeddingBatchRequest(input_text="x", model="embed-x", custom_id="e1")])
assert backend.retrieve(job) == []


class _FakeAssembler:
"""Payload assembler stand-in that records the assembled JSONL lines."""

def __init__(self) -> None:
self.assembled = None

def assemble(self, lines) -> str:
"""Record the lines and return a stub file path."""
self.assembled = lines
return "file:///tmp/embeddings.jsonl"


def test_pg_embedding_backend_uses_payload_assembler_when_provided() -> None:
"""Pg embedding backend uses payload assembler when provided."""
assembler = _FakeAssembler()
backend = PgLlmBatchEmbeddingBackend(_FakeEmbeddingClient(), payload_assembler=assembler)
backend.submit([EmbeddingBatchRequest(input_text="hi", model="embed-x", custom_id="e1")])
assert assembler.assembled is not None
assert assembler.assembled[0]["custom_id"] == "e1"


def test_completions_backend_uses_payload_assembler_when_provided() -> None:
"""Completions backend uses payload assembler when provided."""
assembler = _FakeAssembler()
backend = PgLlmBatchBackend(_FakeEmbeddingClient(), payload_assembler=assembler)
backend.submit(
[BatchRequest(messages=[{"role": "user", "content": "hi"}], custom_id="a", model="gpt-x")]
)
assert assembler.assembled is not None
assert assembler.assembled[0]["custom_id"] == "a"


# --- routing hints from a loose mapping -------------------------------------


def test_routing_hints_from_mapping_normalizes_values() -> None:
"""Routing hints from mapping normalizes values."""
hints = RoutingHints.from_mapping(
{"channel": "Batch", "latency_tolerant": True, "priority": "Bulk"}
)
assert hints.channel == "batch"
assert hints.latency_tolerant is True
assert hints.priority == "bulk"


def test_routing_hints_from_mapping_defaults_when_empty() -> None:
"""Routing hints from mapping defaults when empty."""
hints = RoutingHints.from_mapping(None)
assert hints.channel is None
assert hints.priority == "normal"
Loading
Loading