diff --git a/.github/workflows/freetoken-swap-daemon.yml b/.github/workflows/freetoken-swap-daemon.yml new file mode 100644 index 000000000..e517392ed --- /dev/null +++ b/.github/workflows/freetoken-swap-daemon.yml @@ -0,0 +1,173 @@ +# What: set name to FreeToken swap daemon; why: GitHub displays this label in checks and operators use it to identify the daemon lane or step. +name: FreeToken swap daemon + +# What: set on to its nested mapping; why: GitHub evaluates these events to decide whether the daemon lane is eligible to run. +on: + # What: set pull request to its nested mapping; why: changes targeting the integration branch must pass the daemon contract before merge. + pull_request: + # What: set branches to [main]; why: only pull requests aimed at main receive this automatic validation. + branches: [main] + # What: set workflow dispatch to its nested mapping; why: maintainers can rerun the public lane without altering the candidate commit. + workflow_dispatch: + +# What: set permissions to its nested mapping; why: the workflow token receives only the capabilities declared in this mapping. +permissions: + # What: set contents to read; why: checkout can read repository content while pull-request code receives no write token. + contents: read + +# What: configure concurrency as a nested section; why: GitHub consumes concurrency to preserve this lane's repository gate, ordered execution, or bounded result reporting. +concurrency: + # What: set group to freetoken-swap-daemon-${{ github.ref }}; why: runs for different refs do not cancel one another, while stale runs for one ref share a key. + group: freetoken-swap-daemon-${{ github.ref }} + # What: set cancel in progress to true; why: a newer commit supersedes an older run for the same workflow and ref. + cancel-in-progress: true + +# What: set jobs to its nested mapping; why: GitHub treats each nested entry as an independently gated validation job. +jobs: + # What: set daemon linux to its nested mapping; why: this job isolates the torch-free daemon contract on the hosted Linux process model. + daemon-linux: + # This is a hosted, secret-free smoke lane. Never route pull-request code to + # the repository's self-hosted engine builder or any protected runtime. + # What: set if to github.repository == 'dbourdea/FreeToken'; why: fork or renamed-repository execution is rejected before hosted work begins. + if: github.repository == 'dbourdea/FreeToken' + # What: set runs on to ubuntu-latest; why: the daemon suite exercises Linux process behavior on an isolated hosted runner. + runs-on: ubuntu-latest + # What: set timeout minutes to 10; why: a stalled install or child-process test cannot consume the runner beyond the bounded window. + timeout-minutes: 10 + # What: set steps to its nested mapping; why: the runner preserves checkout, dependency installation, testing, reporting, and final gating in this order. + steps: + # What: set uses to actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd; why: the immutable action revision prevents an upstream tag change from altering checkout behavior. + - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1 + # What: set name to Install torch-free daemon test dependencies; why: GitHub displays this label in checks and operators use it to identify the daemon lane or step. + - name: Install torch-free daemon test dependencies + # What: set run to |; why: the runner executes this exact command or block scalar as the step's behavior. + # What: preserve the exact python m pip install disable pip version check dependency-install command; why: the hosted lane executes this byte-preserved command to install the torch-free packages required by the daemon suite. + # What: add pytest's constraint to the shared pip command; why: the hosted lane needs the supported test runner without unrelated production stacks. + # What: add FastAPI's constraint to the shared pip command; why: daemon API tests construct the control and routing application. + # What: add HTTPX's constraint to the shared pip command; why: the test client exercises in-process HTTP routes and streaming behavior. + # What: add Pydantic's constraint to the shared pip command; why: FastAPI request and response models require the supported validation layer. + # What: add Uvicorn's constraint to the shared pip command; why: daemon startup imports the ASGI server without requiring GPU packages. + run: | + python -m pip install --disable-pip-version-check \ + 'pytest>=8,<9' \ + 'fastapi>=0.115,<1' \ + 'httpx>=0.27,<1' \ + 'pydantic>=2.9,<3' \ + 'uvicorn>=0.30,<1' + # What: set name to Run daemon suite, including disposable Linux child gates; why: GitHub displays this label in checks and operators use it to identify the daemon lane or step. + - name: Run daemon suite, including disposable Linux child gates + # What: set id to daemon-tests; why: later reporting and gate expressions address this step outcome through the stable identifier. + id: daemon-tests + # What: set continue on error to true; why: the reporting step can inspect JUnit output before the final gate restores failure status. + continue-on-error: true + # What: set env to its nested mapping; why: the step receives only the report path and prior-step outcome inputs needed by its command. + env: + # What: add the repository's python directory to pytest imports; why: the daemon suite loads freetoken directly without installing torch-heavy runtime dependencies. + PYTHONPATH: python + # What: set run to python -m pytest tests/daemon -q --junitxml="$RUNNER_TEMP/daemon.xml"; why: the runner executes this exact command or block scalar as the step's behavior. + run: python -m pytest tests/daemon -q --junitxml="$RUNNER_TEMP/daemon.xml" + # What: set name to Report bounded test result; why: GitHub displays this label in checks and operators use it to identify the daemon lane or step. + - name: Report bounded test result + # What: run the reporting step regardless of earlier outcomes; why: JUnit notices and errors remain visible even when the daemon test step failed or was cancelled. + if: always() + # What: set env to its nested mapping; why: the step receives only the report path and prior-step outcome inputs needed by its command. + env: + # What: preserve the exact report runner temp daemon xml reporter fragment; why: the inline script consumes this byte-preserved fragment as part of its JUnit parse, annotation, or final failure decision. + REPORT: ${{ runner.temp }}/daemon.xml + # What: preserve the exact outcome steps daemon tests outcome reporter fragment; why: the inline script consumes this byte-preserved fragment as part of its JUnit parse, annotation, or final failure decision. + OUTCOME: ${{ steps.daemon-tests.outcome }} + # What: set run to |; why: the runner executes this exact command or block scalar as the step's behavior. + # What: start the embedded Python reporter; why: the shell step needs a bounded script to parse JUnit XML and restore the test outcome. + # What: import os inside the inline reporter; why: the reporter directly uses os to read environment state, exit status, or JUnit XML. + # What: import sys inside the inline reporter; why: the reporter directly uses sys to read environment state, exit status, or JUnit XML. + # What: import xml.etree.ElementTree inside the inline reporter; why: the reporter directly uses xml etree element tree to read environment state, exit status, or JUnit XML. + # What: compute reporter state root from et parse os environ report getroot; why: the later JUnit summary or failure gate reads root to determine its annotation and exit behavior. + # What: compute reporter state suites from root if root tag testsuite else root findall; why: the later JUnit summary or failure gate reads suites to determine its annotation and exit behavior. + # What: start the JUnit outcome-count mapping; why: the reporter aggregates tests, failures, errors, and skips across every testsuite node. + # What: sum one JUnit outcome attribute across suites; why: multi-suite reports need a single count for the workflow notice and failure diagnosis. + # What: enumerate the four JUnit outcome counters; why: the summary reports total tests and distinguishes failed, errored, and skipped cases. + # What: finish the JUnit count-comprehension; why: all four counters must be aggregated before the workflow emits its summary notice. + # What: emit the print workflow annotation; why: GitHub surfaces this notice or error to identify the bounded daemon result without exposing raw private artifacts. + # What: compute reporter state notice title from daemon linux suite; why: the later JUnit summary or failure gate reads notice title to determine its annotation and exit behavior. + # What: compute reporter state join f key from value for key value in counts items; why: the later JUnit summary or failure gate reads join f key to determine its annotation and exit behavior. + # What: finish the summary formatting expression; why: the notice must include every computed JUnit counter in one readable message. + # What: compute reporter state failures from the JUnit expression delimiter; why: the later JUnit summary or failure gate reads failures to determine its annotation and exit behavior. + # What: iterate for case in root iter testcase in the reporter; why: the inline reporter inspects every bounded suite, testcase, or escaped annotation component before deciding the result. + # What: compute reporter state node from case find failure; why: the later JUnit summary or failure gate reads node to determine its annotation and exit behavior. + # What: test whether the case lacks a failure node; why: the reporter then falls back to an error node so both JUnit failure categories are covered. + # What: compute reporter state node from case find error; why: the later JUnit summary or failure gate reads node to determine its annotation and exit behavior. + # What: test whether the case lacks both failure and error nodes; why: successful and skipped cases do not need GitHub error annotations. + # What: skip test cases without failure or error nodes; why: successful and skipped cases do not need GitHub error annotations. + # What: compute reporter state test id from f case get classname case get name strip; why: the later JUnit summary or failure gate reads test id to determine its annotation and exit behavior. + # What: compute reporter state message from node get message or test failed splitlines; why: the later JUnit summary or failure gate reads message to determine its annotation and exit behavior. + # What: compute reporter state detail from next; why: the later JUnit summary or failure gate reads detail to determine its annotation and exit behavior. + # What: start selecting the first nonempty failure-detail line; why: annotations need a concise diagnostic instead of the entire traceback payload. + # What: normalize each candidate diagnostic line before selection; why: whitespace-only lines must not become the visible failure detail. + # What: iterate for line in node text or splitlines in the reporter; why: the inline reporter inspects every bounded suite, testcase, or escaped annotation component before deciding the result. + # What: gate the reporter on if line lstrip startswith; why: the inline reporter emits errors or exits only when this parsed JUnit or step-outcome predicate requires it. + # What: preserve the exact group boundary around if line lstrip startswith and the JUnit expression delimiter reporter fragment; why: the inline script consumes this byte-preserved fragment as part of its JUnit parse, annotation, or final failure decision. + # What: preserve the exact group boundary around the JUnit expression delimiter and the JUnit expression delimiter reporter fragment; why: the inline script consumes this byte-preserved fragment as part of its JUnit parse, annotation, or final failure decision. + # What: preserve the exact group boundary around the JUnit expression delimiter and if detail reporter fragment; why: the inline script consumes this byte-preserved fragment as part of its JUnit parse, annotation, or final failure decision. + # What: gate the reporter on if detail; why: the inline reporter emits errors or exits only when this parsed JUnit or step-outcome predicate requires it. + # What: compute reporter state message from f message detail; why: the later JUnit summary or failure gate reads message to determine its annotation and exit behavior. + # What: iterate for old new in r d in the reporter; why: the inline reporter inspects every bounded suite, testcase, or escaped annotation component before deciding the result. + # What: compute reporter state message from message replace old new; why: the later JUnit summary or failure gate reads message to determine its annotation and exit behavior. + # What: preserve the exact failures append test id message reporter fragment; why: the inline script consumes this byte-preserved fragment as part of its JUnit parse, annotation, or final failure decision. + # What: gate the reporter on if os environ outcome success; why: the inline reporter emits errors or exits only when this parsed JUnit or step-outcome predicate requires it. + # What: gate the reporter on if not failures; why: the inline reporter emits errors or exits only when this parsed JUnit or step-outcome predicate requires it. + # What: emit the print workflow annotation; why: GitHub surfaces this notice or error to identify the bounded daemon result without exposing raw private artifacts. + # What: compute reporter state error title from daemon linux suite; why: the later JUnit summary or failure gate reads error title to determine its annotation and exit behavior. + # What: preserve the exact pytest failed without a junit failure reporter fragment; why: the inline script consumes this byte-preserved fragment as part of its JUnit parse, annotation, or final failure decision. + # What: preserve the exact group boundary around pytest failed without a junit failure and for test id message in failures reporter fragment; why: the inline script consumes this byte-preserved fragment as part of its JUnit parse, annotation, or final failure decision. + # What: iterate for test id message in failures in the reporter; why: the inline reporter inspects every bounded suite, testcase, or escaped annotation component before deciding the result. + # What: emit the print f error title test id message workflow annotation; why: GitHub surfaces this notice or error to identify the bounded daemon result without exposing raw private artifacts. + # What: exit the reporter with failure status; why: the workflow must remain red after reporting an unsuccessful daemon test step. + # What: preserve the exact py reporter fragment; why: the inline script consumes this byte-preserved fragment as part of its JUnit parse, annotation, or final failure decision. + run: | + python - <<'PY' + import os + import sys + import xml.etree.ElementTree as ET + + root = ET.parse(os.environ["REPORT"]).getroot() + suites = [root] if root.tag == "testsuite" else root.findall("testsuite") + counts = { + key: sum(int(suite.get(key, "0")) for suite in suites) + for key in ("tests", "failures", "errors", "skipped") + } + print( + "::notice title=daemon Linux suite::" + + ", ".join(f"{key}={value}" for key, value in counts.items()) + ) + failures = [] + for case in root.iter("testcase"): + node = case.find("failure") + if node is None: + node = case.find("error") + if node is None: + continue + test_id = f"{case.get('classname', '')}.{case.get('name', '')}".strip(".") + message = (node.get("message") or "test failed").splitlines()[0][:500] + detail = next( + ( + line.lstrip()[1:].strip() + for line in (node.text or "").splitlines() + if line.lstrip().startswith(">") + ), + "", + ) + if detail: + message = f"{message}; {detail}"[:500] + for old, new in (("%", "%25"), ("\r", "%0D"), ("\n", "%0A")): + message = message.replace(old, new) + failures.append((test_id, message)) + if os.environ["OUTCOME"] != "success": + if not failures: + print( + "::error title=daemon Linux suite::" + "pytest failed without a JUnit failure record" + ) + for test_id, message in failures[:10]: + print(f"::error title={test_id}::{message}") + sys.exit(1) + PY diff --git a/README.md b/README.md index 2a56a0865..c8315f831 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ For More details: - [Quick start](https://github.com/FlashML-org/FreeToken/blob/main/docs/quickstart.md) - [Supported models](https://github.com/FlashML-org/FreeToken/blob/main/docs/models.md) - [CLI reference](https://github.com/FlashML-org/FreeToken/blob/main/docs/cli.md) +- [freetoken-swap named model switching](docs/freetoken-swap.md) ## Citation diff --git a/_bmad-output/implementation-artifacts/spec-document-all-created-code.md b/_bmad-output/implementation-artifacts/spec-document-all-created-code.md new file mode 100644 index 000000000..3e37474db --- /dev/null +++ b/_bmad-output/implementation-artifacts/spec-document-all-created-code.md @@ -0,0 +1,193 @@ +--- +title: 'Document every branch-created code line' +type: 'chore' +created: '2026-09-15' +status: 'done' +route: 'full' +review_loop_iteration: 3 +baseline_commit: 'a5846c0847cb371313b9ad7ceb93a1933a48d967' +context: [] +--- + + + +## Intent + +**Problem:** The `feat/freetoken-swap` implementation contains substantial code whose individual lines do not all explain both their operation and their purpose. The user requires all code created to date, and all future code, to carry those explanations. + +**Approach:** Use merge base `9ef3651309fe4058672f2cc92069238dea06be1b` as the ownership boundary. Add an adjacent, meaningful native-language comment for every nonblank executable or configuration line introduced after that base, explaining what the line does and why it exists, without changing runtime behavior. + +## Boundaries & Constraints + +**Always:** Cover branch-created Python runtime, benchmark, test, embedded HTML/CSS/JavaScript, workflow YAML, example YAML/TOML, and `pyproject.toml` additions. Preserve shebang placement, module docstrings, decorators, multiline grammar, exact protocol fixtures, exception text, serialized bytes, and public behavior. Explain syntax-only delimiters at their nearest valid structural boundary. Comments themselves do not require recursive comments. Retain the draft PR and private-artifact policy. + +**Never:** Modify the pinned llama-swap checkout, protected runtime/service/model/GPU state, upstream-owned pre-base logic, generated `_bmad/` files, or prose-only documentation merely to inflate coverage. Do not merge PRs, activate services, publish raw artifacts, or substitute generic comments that fail to identify both action and rationale. + +## I/O & Edge-Case Matrix + +| Scenario | Input / State | Expected Output / Behavior | Error Handling | +|----------|--------------|---------------------------|----------------| +| Python statement | Branch-added executable line | Adjacent `#` comment states action and purpose | Compilation/tests catch grammar or behavior changes | +| Embedded web code | HTML/CSS/JavaScript inside Python literal | Native embedded comment documents each safe line | Exact payload/string fixtures remain byte-identical when comments would alter semantics | +| YAML/TOML/config | Branch-added nonblank setting or command | Adjacent format-valid comment explains value and reason | Parser/workflow validation catches invalid syntax | +| Grammar-sensitive content | Docstring, multiline literal, backslash continuation, decorator, or fixture bytes | Explain at nearest valid boundary without mutating value or attachment | Preserve original content and document exception structurally | + + + +## Code Map + +- `.github/workflows/freetoken-swap-daemon.yml`, `examples/freetoken-swap.{toml,yaml}`, `pyproject.toml` -- branch-created operational configuration requiring native comments. +- `benchmarks/swap/*.py` -- three opt-in qualification harnesses; preserve fail-closed maintenance gates and private artifacts. +- `python/freetoken/daemon/{activity,app,catalog,client,inference_proxy,metrics,osproc,performance,proxy,readiness,router,serve_manager,server}.py` and `python/freetoken/server/control_api.py` -- production delta, including embedded router UI. +- `tests/daemon/*.py` -- branch-created behavioral and qualification coverage; preserve exact assertions and fixtures. +- `docs/*.md`, `README.md`, `python/freetoken/daemon/README.md` -- prose evidence, not executable code; do not mechanically annotate. + +## Comment Quality Contract + +- Every explanation must be specific to the documented line and its immediate enclosing symbol. The `what` clause names the semantic effect, not merely the token or delimiter; the `why` clause names the concrete consumer, invariant, failure path, or state transition that requires it. +- Imports must name at least one actual consumer or operation that needs the imported symbol. Runtime lines must connect to their concrete lifecycle/API/data-flow role. Reusing one module-wide rationale across unrelated lines is prohibited. +- Structural delimiters must identify the call, collection, signature, or branch they complete and why that construct must remain grouped. Do not emit generic “close expression,” “invoke with supplied arguments,” “set from,” or equivalent templates without the concrete semantic role. +- Never truncate a comment with `...`, embed historical physical line numbers, or copy secrets/private deployment values. Exact literals and block scalars are described at a stable enclosing boundary without changing their bytes. +- Tests must identify whether a line arranges a condition, performs the behavior, or asserts the outcome, plus the regression/failure mode it protects. Workflow keys must explain their individual operational choice. Example numeric/boolean values must be labeled illustrative and explain their tradeoff rather than imply universal suitability. +- Review every generated explanation against the underlying symbol/caller before accepting coverage. Form/count checks alone are insufficient. +- Never use textual-neighbor clauses such as “between X and Y,” the placeholders “declared structural boundary” or “literal fixture value,” or hedged consumers such as “parser, serializer, or API consumer.” Keep each generated explanation at or below 320 characters so imports and dense expressions remain reviewable. +- Describe `raise` as propagating a failure, `try` as establishing a handler boundary, and `except` as the actual handling behavior. Name the concrete behavior of route, middleware, property, classmethod, dataclass, and lifecycle decorators rather than collapsing them into one decorator template. +- Distinguish runtime accumulators from test fixtures, and distinguish GitHub workflow/JUnit concepts from model-router, llama-swap, and `ft serve` configuration. Test assertions must state the expected behavior or regression, not “assert the assert” or “exact condition.” +- Assertion rationales must describe failure when the asserted predicate is false. Side-effect calls such as `sleep`, `extend`, mutation, notification, and cleanup must not claim their `None` return is consumed. New exception construction is described as raising or signaling, not propagating an existing exception. +- Assignments and returns must name the concrete invariant, normalized value, state transition, or caller contract where it is available; tokenized expressions plus “later consumed” are insufficient. TOML table headers establish namespaces, and each example setting must explain its own operational tradeoff. +- Do not clip string or argument text to an unterminated fragment. Embedded reporter comments must explain aggregation, escaping, annotation, or failure-gating roles at stable scalar boundaries. + +## Tasks & Acceptance + +**Execution:** +- [x] Inventory added hunks from the pinned merge base and produce a deterministic coverage list by file and language. +- [x] Build symbol- and usage-aware explanations satisfying the Comment Quality Contract for every eligible Python and embedded web-code line while preserving grammar and exact-value fixtures. +- [x] Add key/value-specific explanations satisfying the Comment Quality Contract for every eligible workflow/configuration line using YAML or TOML syntax. +- [x] Review the complete diff for generic/repeated filler, factual errors, truncation, stale line references, accidental executable changes, secrets/private metadata, and untouched upstream code. +- [x] Measure and record source-size/import-parse impact, run deterministic verification, and prepare only the intended documentation delta for review; commit, push, and exact-head GitHub verification follow the mandatory review step. + +**Acceptance Criteria:** +- Given the merge-base diff, when every introduced executable/configuration line is inspected, then it has an adjacent meaningful explanation of both what it does and why, or is covered at the nearest valid boundary because inline insertion would change grammar or exact data. +- Given the pre-comment commit and final commit, when executable behavior and public outputs are compared through the existing suite, then all daemon tests pass with no logic regression. +- Given the final implementation state, when pre-publication review begins, then exactly the intended product files and this spec are staged, generated `_bmad/` runtime files are excluded, and commit/push/exact-head GitHub verification remain explicit post-review deliverables. + +## Implementation Notes + +- The pinned merge-base inventory found 11,714 eligible nonblank code/configuration lines across 31 tracked files; the transformation reported 11,714 covered lines. +- Python comments use tokenizer/AST context to distinguish definitions, calls, assignments, parameter defaults, keyword arguments, control flow, literals, and structural continuations. Multiline strings and explicit backslash continuations are documented at their nearest safe boundary. +- YAML block-scalar payloads remain byte-for-byte unchanged; one boundary comment per owned payload line is placed before the scalar key. TOML/YAML comments otherwise sit adjacent to their settings. +- A one-use refinement helper was created during implementation and deleted before review; it is not part of the repository diff. +- Final local evidence: Python compilation passed; stripping only the new comments reproduced the exact baseline bytes for all 31 product files; Python ASTs for 27 edited files remained identical; all four YAML/TOML files parsed; `tests/daemon` completed with 345 passed and 7 expected platform skips; `git diff --check` passed; prose-only documentation remained unchanged; and the privacy-pattern scan found no candidate secrets. +- The fail-closed coverage audit found exactly 11,714 valid what/why comments for 11,714 eligible branch-created lines. It found no missing coverage, non-comment byte differences, malformed explanations, truncated expressions, physical multiline line references, or rejected generic phrases. +- After loop-2 shortening, the 31 product files increased from 680,262 bytes to 3,137,738 bytes (2,457,476 bytes; 4.613x). A fresh five-run median parse of all 27 Python files increased from 337.129 ms to 459.453 ms (1.363x) on this host; this local microbenchmark does not claim runtime request-path overhead. +- Loop-2 final evidence: all 11,714 explanations are 320 characters or fewer; prior rejected phrases and placeholders have zero hits; the cited workflow, routing-group, API-key, readiness, malformed-path, runtime-accumulator, and test-action errors were corrected; exact non-comment bytes and Python ASTs still match baseline; compilation, TOML/YAML parsing, and `git diff --check` pass; and `tests/daemon` again reports 345 passed and 7 platform skips. +- Loop-3 final evidence: exact 11,714-line coverage and 320-character maximum remain intact; assertion truth, side-effect calls, pass handling, keyword-only syntax, exception origin, TOML namespaces/settings, clipped qualifier options, and reporter boundaries were corrected; non-comment bytes and ASTs still match baseline; compilation and `git diff --check` pass; and `tests/daemon` again reports 345 passed and 7 skips. +- Post-review patch evidence: the final cited sentinel, signal, readiness-loop, checkpoint, dependency-continuation, JUnit fallback, and `None`-assertion defects were corrected. The final invariant audit again reports 31 files, 11,714 comments, a 320-character maximum, zero non-comment/AST mismatches, valid TOML/YAML, and a clean `git diff --check`; the latest full suite remains 345 passed and 7 skipped. +- Commit, push, and exact-head hosted CI verification remain pending until the mandatory review workflow permits remote operations. + +## Spec Change Log + +- 2026-09-15 review loop 1 — Trigger: independent reviewers found the first derivation counted comments but allowed syntax restatement, repeated module-wide rationales, factual errors, truncated expressions, stale physical-line references, weak workflow/example/test explanations, and unacknowledged source bloat. Amendment: added the Comment Quality Contract, reset affected tasks, and required symbol/usage-aware generation, factual review, and cost measurement. Known-bad state avoided: 11,714 formally present but predominantly template-generated comments that reduce readability or misdescribe behavior. KEEP: pinned merge-base ownership; exact one-for-one eligible-line inventory; unchanged multiline literal/YAML scalar bytes; no protected-runtime access; AST/config semantic equivalence; privacy scan; full daemon suite; generated `_bmad/` exclusion. +- 2026-09-15 review loop 2 — Trigger: independent review found remaining systemic cross-domain templates, reversed exception-flow descriptions, vague decorator/literal/assertion explanations, unstable neighbor clauses, and individual comments up to 1,791 characters. Amendment: prohibited the observed placeholders and neighbor clauses, capped explanation length, required concrete exception/decorator/test semantics, and required strict workflow/router/fixture domain separation. Known-bad state avoided: formally complete coverage that still misleads maintainers about failure propagation, GitHub reporting, model routing, and expected test outcomes. KEEP: all loop-1 constraints; exact 11,714-line coverage; zero executable/config byte changes after comment stripping; 345-pass/7-skip suite; exact literals and block scalars; measured 5.308x source-size and 1.117x parse-time ratios. +- 2026-09-15 review loop 3 — Trigger: review found assertions with inverted truth semantics, side-effect calls documented as consumed return values, generic assignment/return rationales, TOML tables mislabeled as arguments, repeated unrelated example-setting rationales, and clipped argument text. Amendment: added explicit truth, side-effect, exception-origin, assignment/return, table/value, clipping, and embedded-reporter rules. Known-bad state avoided: comments that pass phrase scans while inventing data flow or hiding safety/configuration intent. KEEP: all prior constraints and verified coverage/byte-equivalence/test evidence; corrected workflow gate, readiness, API-key, routing-group, malformed-path, and runtime-accumulator explanations; 320-character limit. + +## Review Triage Log + +- Blind-1 — `medium`, `bad_spec`: verified syntax-only comments such as `app.py` delimiter explanations do not state the construct's semantic role; grouped into systemic comment-quality re-derivation. +- Blind-2 — `medium`, `bad_spec`: verified imports reuse a generic app-wide rationale instead of naming actual consumers; grouped into systemic comment-quality re-derivation. +- Blind-3 — `medium`, `bad_spec`: verified `/ready` comments incorrectly describe stop/accounting controls, risking maintainer misunderstanding of supervisor readiness; grouped into systemic comment-quality re-derivation. +- Blind-4 — `medium`, `bad_spec`: verified `fresh_health` comments incorrectly mention streaming cleanup rather than bypassing prior-generation cache; grouped into systemic comment-quality re-derivation. +- Blind-5 — `medium`, `bad_spec`: verified 28 comments truncate decisive expression text with `...`; grouped into systemic comment-quality re-derivation. +- Blind-6 — `medium`, `bad_spec`: verified workflow comments repeat a lane-wide rationale and omit the repository gate, timeout, pinned action, and result-reporting reasons; grouped into systemic comment-quality re-derivation. +- Blind-7 — `medium`, `bad_spec`: verified YAML scalar boundary comments embed physical line numbers that become stale after edits; grouped into systemic comment-quality re-derivation. +- Blind-8 — `medium`, `bad_spec`: verified example settings repeat “safe configuration” without explaining illustrative values/tradeoffs; grouped into systemic comment-quality re-derivation. +- Blind-9 — `medium`, `bad_spec`: verified test comments repeat “remains enforced” without arrange/act/assert role or protected failure mode; grouped into systemic comment-quality re-derivation. +- Blind-10 — `false`, `reject`: the user instructed this assistant to comment future code but did not request a repository linter or CI policy; absence of enforcement code is not a defect in this change. +- Blind-11 — `false`, `reject`: workflow parsed values were byte/structure-equivalent to baseline and exact-head hosted CI remains a publication gate, so a new `actionlint` dependency is not needed to validate comment-only edits. +- Blind-12 — `false`, `reject`: embedded HTML/CSS/JavaScript literal bytes were unchanged and Python AST constant equality proved that fact, so browser-side validation is not required for this comment-only boundary documentation. +- Blind-13 — `false`, `reject`: verification commands, baseline commit, counts, and expected results are recorded and independently reproducible; committing raw transient logs was neither requested nor safe/necessary. +- Blind-14 — `medium`, `bad_spec`: verified the 2.6 MB source expansion has developer/import/distribution costs that were not measured or acknowledged; retained as a separate measurement requirement in re-derivation. +- Blind-15 — `high`, `bad_spec`: verified predominant generated templates conflict directly with the approved semantic-comment example and falsely satisfy checked acceptance boxes; grouped into systemic comment-quality re-derivation. +- Edge-1 — `medium`, `bad_spec`: verified the workflow-name explanation repeats the global CI rationale rather than why the display name identifies this lane; duplicate root cause retained as its own verdict row, then grouped with systemic quality findings. +- Edge-2 — `false`, `reject`: `AM` is expected because Step 4 requires changing status to `in-review` without staging; the final status will be staged before commit, so reviewed and committed specs will not diverge. +- Verification-gap — no findings reported. +- Loop2-Blind-1 — `false`, `reject`: `git diff --check` passed against the worktree; CRLF bytes existed only in the temporary review-diff serialization and are not trailing whitespace in product files. +- Loop2-Blind-2 — `false`, `reject`: replacing per-line documentation with only selective comments would violate the human-owned frozen intent requiring every branch-created code/configuration line to be explained. +- Loop2-Blind-3 — `false`, `reject`: the comments adjacent to multiline docstrings describe the underlying string fragments that Python exposes through introspection; they do not claim the `#` comment itself is part of `__doc__`. The separate “declared structural boundary” wording defect is accepted below. +- Loop2-Blind-4 — `medium`, `bad_spec`: verified `.github/workflows/freetoken-swap-daemon.yml` attributes repository gating to `if: always()` instead of its real purpose of preserving result reporting after prior outcomes. +- Loop2-Blind-5 — `medium`, `bad_spec`: verified the workflow attributes `PYTHONPATH` to the JUnit reporter rather than pytest imports in the daemon-test step. +- Loop2-Blind-6 — `medium`, `bad_spec`: verified both TOML `group = "interactive"` explanations incorrectly describe GitHub ref concurrency instead of shared model routing/exclusivity policy. +- Loop2-Blind-7 — `medium`, `bad_spec`: verified example YAML model commands incorrectly use JUnit-reporter rationales for `ft serve` arguments. +- Loop2-Blind-8 — `medium`, `bad_spec`: verified `wait_for_ready` describes `while True` as candidate iteration rather than bounded readiness polling with explicit terminal conditions. +- Loop2-Blind-9 — `medium`, `bad_spec`: verified `wait_for_ready` describes `sleep()` as feeding a return rather than pacing injectable polling. +- Loop2-Blind-10 — `medium`, `bad_spec`: verified the `wait_for_ready` return annotation explanation does not identify the heterogeneous readiness-result mapping contract. +- Loop2-Blind-11 — `medium`, `bad_spec`: verified the `/ready` return explanation omits the supervisor-facing 200/503 acceptance contract. +- Loop2-Blind-12 — `medium`, `bad_spec`: verified runtime accumulator `parts = []` is mislabeled as a fixture. +- Loop2-Blind-13 — `medium`, `bad_spec`: verified the canary `model` field uses a hedged generic consumer rather than its concrete routed-model selection role. +- Loop2-Blind-14 — `medium`, `bad_spec`: verified test assertions use circular tokenized prose and omit the expected behavior or regression they protect. +- Loop2-Blind-15 — `medium`, `bad_spec`: verified the spec's checked semantic-review task and no-defect audit claim were contradicted by current examples; tasks were reset for re-derivation. +- Loop2-Blind-16 — `low`, `patch`: the verification command retained an unresolved `` placeholder; replace it with baseline commit `a5846c0847cb371313b9ad7ceb93a1933a48d967` before completion. +- Loop2-Blind-17 — `low`, `reject`: source and parse costs are measured explicitly, while editor/indexer, formatter, merge-conflict, and distribution effects lack a deterministic repository check and do not change the user-mandated per-line scope. +- Loop2-Edge-1 — `medium`, `bad_spec`: verified `app.py` documents malformed percent-escape rejection (`return None`) as a literal fixture return. +- Loop2-Edge-2 — `medium`, `bad_spec`: verified workflow JUnit aggregation lines retain placeholder structural explanations rather than tests/failures/errors summation semantics. +- Loop2-Edge-3 — `medium`, `bad_spec`: carried Loop2-Blind-4; the `if: always()` rationale is factually wrong at the same location. +- Loop2-Edge-4 — `medium`, `bad_spec`: verified example TOML `api_keys` discusses model memory/startup tradeoffs rather than router authentication and credential replacement. +- Loop2-Edge-5 — `false`, `reject`: carried Edge-2; Step 4 explicitly prohibits staging while constructing and reviewing the diff, so an empty index and untracked in-review spec are expected until review succeeds. +- Loop2-Extra-1 — `medium`, `bad_spec`: verified `test_catalog.py` misclassifies the `wait_for_ready(...)` action assignment as function-signature binding. +- Loop2-Extra-2 — `medium`, `bad_spec`: verified hundreds of “declared structural boundary” explanations fail to name the expression or collection being completed. +- Loop2-Extra-3 — `medium`, `bad_spec`: verified generic `raise`, `try`, and decorator templates reverse or obscure concrete control flow and registration behavior. +- Loop2-Extra-4 — `medium`, `bad_spec`: verified comments up to 1,791 characters and 1,009 textual-neighbor clauses are unstable and make dense imports unreadable; a 320-character limit and neighbor-clause prohibition were added. +- Loop2-Verification-1 — `medium`, `bad_spec`: carried Loop2-Blind-4; independent verification-gap review confirmed the same false `if: always()` rationale and reported no additional gaps. +- Loop3-Blind-1 — `medium`, `bad_spec`: verified assertion comments described failure when a true predicate “violates” an invariant; rationales now require the predicate to remain true and identify false as the failure state. +- Loop3-Blind-2 — `medium`, `bad_spec`: verified `time.sleep(1)` was documented as supplying a later exception; it now explicitly paces bounded health polling. +- Loop3-Blind-3 — `medium`, `bad_spec`: verified `raw.extend()` was documented as a consumed return value; it now describes mutation of the accumulated stream buffer. +- Loop3-Blind-4 — `medium`, `bad_spec`: verified `pass` was mislabeled as predicate evaluation; pass lines now document suppression of the anticipated handled exception. +- Loop3-Blind-5 — `medium`, `bad_spec`: verified the bare signature `*` was mislabeled as a predicate fragment; it now documents the keyword-only API constraint. +- Loop3-Blind-6 — `medium`, `bad_spec`: verified three function definitions used “execute def” boilerplate; definitions now describe declaration and caller reuse, with the hostname helper's safety contract retained in its adjacent docstring. +- Loop3-Blind-7 — `low`, `bad_spec`: verified newly constructed errors were called propagated exceptions; generated wording now says they are raised for the caller. +- Loop3-Blind-8 — `medium`, `bad_spec`: verified broad assignment templates often omitted the reason for normalization or state production; the strengthened contract now requires the concrete invariant where available. +- Loop3-Blind-9 — `medium`, `bad_spec`: verified broad return templates only said callers depend on results; the strengthened contract now requires the caller guarantee where available. +- Loop3-Blind-10 — `medium`, `bad_spec`: verified unrelated TOML settings shared one model/memory/port rationale; each example key now has a setting-specific operational tradeoff. +- Loop3-Blind-11 — `medium`, `bad_spec`: verified TOML table headers were mislabeled as command arguments; they now describe the namespace each table opens. +- Loop3-Blind-12 — `medium`, `bad_spec`: verified three qualifier comments clipped option/help or encoding text into unterminated fragments; those lines now document the complete option or artifact-write behavior. +- Loop3-Blind-13 — `medium`, `bad_spec`: verified embedded reporter delimiter comments did not explain their aggregation and diagnostic roles; cited boundaries now name count aggregation, summary formatting, case filtering, and detail selection. +- Loop3-Blind-14 — `false`, `reject`: carried Loop2-Blind-3; comments outside multiline strings describe the underlying docstring fragments without altering the introspected string bytes. +- Loop3-Blind-15 — `medium`, `bad_spec`: verified checked completion claims were premature while the above factual defects remained; tasks were reset during correction and require another independent review. +- Loop3-Edge-1 — `medium`, `bad_spec`: carried Loop3-Blind-2; the sleep data-flow claim was false and was corrected. +- Loop3-Edge-2 — `medium`, `bad_spec`: carried Loop3-Blind-2; the same line invented consumption of a `None` return. +- Loop3-Edge-3 — `medium`, `bad_spec`: carried Loop3-Blind-11; `[router]` opens a TOML table rather than representing a command argument. +- Loop3-Edge-4 — `medium`, `bad_spec`: carried Loop3-Blind-15; the clean-review claim was premature while the sleep defect remained. +- Loop3-Edge-5 — `false`, `reject`: carried Edge-2 and Loop2-Edge-5; the review workflow prohibits staging until independent review succeeds. +- Loop3-Verification — no verification gaps reported. +- Loop4-Blind-1 — `medium`, `patch`: verified the llama-swap validation comment remained clipped; directly replaced it with the complete pre-launch validation purpose. +- Loop4-Blind-2 — `false`, `reject`: carried Loop2-Blind-3 and Loop3-Blind-14; adjacent comments describe underlying docstring fragments without claiming comments are part of `__doc__`. +- Loop4-Blind-3 — `medium`, `patch`: verified two `None` capture assertions were mislabeled as delimiters; directly replaced them with eviction and retention semantics. +- Loop4-Blind-4 — `medium`, `patch`: verified the signal handler described a newly raised `KeyboardInterrupt` as propagated; directly documented signal-to-interruption conversion. +- Loop4-Blind-5 — `medium`, `patch`: verified SIGTERM registration was explained through the neighboring SIGHUP call; directly documented its restoration-path purpose. +- Loop4-Blind-6 — `medium`, `patch`: verified `MAX_JOBS` was tied to config validation instead of native-extension compilation; directly documented the two-job build cap. +- Loop4-Blind-7 — `medium`, `patch`: verified `proc = None` was mislabeled as fixture state; directly documented the conditional-cleanup sentinel. +- Loop4-Blind-8 — `medium`, `patch`: verified `config +=` omitted the generated model stanza role; directly documented command, readiness, proxy, and optional-TTL sequencing. +- Loop4-Blind-9 — `medium`, `patch`: verified the main `while True` omitted its readiness terminal conditions; directly documented listing success, process exit, and deadline expiry. +- Loop4-Blind-10 — `medium`, `patch`: verified `break` was mislabeled as predicate structure; directly documented successful readiness-loop exit. +- Loop4-Blind-11 — `medium`, `patch`: verified the half-second sleep was tied to a later trial loop; directly documented readiness retry backoff. +- Loop4-Blind-12 — `medium`, `patch`: verified three `save()` calls inherited unrelated neighboring expressions; directly documented trial, cancellation, and restoration checkpoints. +- Loop4-Blind-13 — `medium`, `patch`: verified pip continuation lines were called separate commands; directly documented each constraint as part of the shared install command. +- Loop4-Blind-14 — `medium`, `patch`: verified clean-review evidence was premature for the cited lines; this patch and post-patch verification supersede that claim. +- Loop4-Edge-1 — `medium`, `patch`: verified `observed = None` was mislabeled as fixture state; directly documented the not-yet-fetched statistics sentinel. +- Loop4-Edge-2 — `medium`, `patch`: verified reporter fallback was described as terminal handling; directly documented failure-node to error-node fallback. +- Loop4-Edge-3 — `medium`, `patch`: verified the hostname return contract remained generic; directly documented that the returned identity passed the exact-host safety gate. +- Loop4-Edge-4 — `false`, `reject`: carried Edge-2, Loop2-Edge-5, and Loop3-Edge-5; staging is intentionally prohibited until this review and patch verification finish. +- Loop4-Verification — no verification gaps reported. + +## Design Notes + +Comments should name concrete roles rather than restating syntax. For example, prefer “Normalize the requested alias so all lifecycle locks share one canonical identity” over “Assign the canonical variable.” A structural closing line may be explained by the comment attached to the construct it closes. + +## Verification + +**Commands:** +- `python -m compileall -q python/freetoken/daemon python/freetoken/server benchmarks/swap tests/daemon` -- expected: all edited Python parses. +- `python -m pytest tests/daemon -q` -- expected: complete local daemon suite passes with only platform-qualified skips. +- `git diff --check` -- expected: no whitespace or patch errors. +- `git diff --exit-code a5846c0847cb371313b9ad7ceb93a1933a48d967 -- docs README.md python/freetoken/daemon/README.md` -- expected: prose evidence remains unchanged. +- Public GitHub API check for the pushed exact head -- expected: `FreeToken swap daemon` completes successfully and PR #1 remains draft. diff --git a/benchmarks/swap/qualify.py b/benchmarks/swap/qualify.py new file mode 100644 index 000000000..0e7075e21 --- /dev/null +++ b/benchmarks/swap/qualify.py @@ -0,0 +1,569 @@ +"""Opt-in Linux maintenance-window qualification against a real llama-swap binary. + +Artifacts contain local operational paths and raw model output. Keep them private. +This script never changes the protected service's configuration or enablement. +""" +# What: document opt in linux maintenance window qualification against a in the qualify docstring; why: introspection and maintainers read this exact docstring fragment to understand qualify behavior without executing it. +# What: document artifacts contain local operational paths and in the qualify docstring; why: introspection and maintainers read this exact docstring fragment to understand qualify behavior without executing it. +# What: document this script never changes the protected in the qualify docstring; why: introspection and maintainers read this exact docstring fragment to understand qualify behavior without executing it. +# What: preserve the paragraph boundary in the the qualify docstring; why: introspection and maintainers read this paragraph break to understand qualify behavior without executing it. + +# What: import argparse for main using argparse; why: main uses argparse argument parser, making that imported dependency available to its named operation. +import argparse +# What: import json for cancellation canary using json; why: cancellation_canary uses json loads, making that imported dependency available to its named operation. +import json +# What: import os for main using os; why: main uses os environ copy, making that imported dependency available to its named operation. +import os +# What: import path for main using pathlib and path; why: main uses path, making that imported dependency available to its named operation. +from pathlib import Path +# What: import signal for main using signal; why: main uses signal signal, making that imported dependency available to its named operation. +import signal +# What: import socket for require expected hostname using socket; why: require_expected_hostname uses socket gethostname, making that imported dependency available to its named operation. +import socket +# What: import subprocess for main using subprocess; why: main uses subprocess run, making that imported dependency available to its named operation. +import subprocess +# What: import sys for module initialization using sys; why: module initialization uses sys exit, making that imported dependency available to its named operation. +import sys +# What: import time for cancellation canary using time; why: cancellation_canary uses time monotonic, making that imported dependency available to its named operation. +import time +# What: import urllib error for http using urllib and error; why: http uses urllib request request, making that imported dependency available to its named operation. +import urllib.error +# What: import urllib request for http using urllib and request; why: http uses urllib request request, making that imported dependency available to its named operation. +import urllib.request +# What: import thread pool executor for main using concurrent and futures and thread pool executor; why: main uses thread pool executor, making that imported dependency available to its named operation. +from concurrent.futures import ThreadPoolExecutor + + +# What: define require_expected_hostname and its declared inputs; why: callers use require_expected_hostname to perform the behavior named by this helper without duplicating its boundary checks. +def require_expected_hostname(expected: str, *, actual: str | None = None) -> str: + """Fail closed unless the operator names this exact maintenance host. + + The mismatch deliberately omits both values so a copied error cannot publish + a private machine name. The approved public hardware label is documented + separately and is not assumed to equal the operating-system hostname. + """ + # What: document fail closed unless the operator names in the require_expected_hostname docstring; why: introspection and maintainers read this exact docstring fragment to understand require expected hostname behavior without executing it. + # What: document the mismatch deliberately omits both values in the require_expected_hostname docstring; why: introspection and maintainers read this exact docstring fragment to understand require expected hostname behavior without executing it. + # What: document a private machine name the approved in the require_expected_hostname docstring; why: introspection and maintainers read this exact docstring fragment to understand require expected hostname behavior without executing it. + # What: document separately and is not assumed to in the require_expected_hostname docstring; why: introspection and maintainers read this exact docstring fragment to understand require expected hostname behavior without executing it. + # What: preserve the paragraph boundary in the the require_expected_hostname docstring; why: introspection and maintainers read this paragraph break to understand require expected hostname behavior without executing it. + # What: compute actual from actual and gethostname and socket; why: if not expected or x00 in later reads actual, so require_expected_hostname must retain the computed value under that name. + actual = socket.gethostname() if actual is None else actual + # What: gate on expected and actual before runtime error; why: require_expected_hostname admits runtime error only for this predicate and excludes the opposite state. + if not expected or "\x00" in expected or actual != expected: + # What: raise RuntimeError for the caller; why: require_expected_hostname stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError( + # What: execute qualification host does not match the operator supplied expected hostname; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + "qualification host does not match the operator-supplied expected hostname" + # What: complete the RuntimeError call with ordered positional inputs; why: require_expected_hostname groups the supplied clauses as one RuntimeError call before its value is consumed. + ) + # What: return the hostname that passed exact-host validation; why: callers use this confirmed identity before any maintenance side effect is allowed. + return actual + + +# What: define http around url and body and timeout; why: its direct callers call http for http and rely on this exact input and result contract. +def http(url, body=None, timeout=30): + # What: compute data from body and encode and dumps and json; why: request urllib request request url data data headers later reads data, so http must retain the computed value under that name. + data = None if body is None else json.dumps(body).encode() + # What: map the content type field as application and json; why: http carries content type through request into with urllib request urlopen request timeout timeout as response. + request = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"}) + # What: enter the urllib.request.urlopen managed context before return response read; why: http releases this resource or lock after return response read on both success and failure paths. + with urllib.request.urlopen(request, timeout=timeout) as response: + # What: return read and response from http; why: http exposes read and response so its caller can continue with the function\'s computed outcome. + return response.read() + + +# What: define wait_health around url and seconds; why: its direct callers call wait_health for wait health and rely on this exact input and result contract. +def wait_health(url, seconds): + # What: compute deadline from seconds and monotonic and time; why: while time monotonic deadline later reads deadline, so wait_health must retain the computed value under that name. + deadline = time.monotonic() + seconds + # What: iterate across deadline and monotonic and time to perform doc and loads and oserror and value error and json; why: wait_health repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: establish the handler boundary for the protected operation; why: wait_health routes failures to oserror and value error while preserving cleanup and success flow. + try: + # What: compute doc from loads and json and http and url and 3; why: if doc get status ok later reads doc, so wait_health must retain the computed value under that name. + doc = json.loads(http(url, timeout=3)) + # What: gate on get and doc before doc; why: wait_health admits doc only for this predicate and excludes the opposite state. + if doc.get("status") == "ok": + # What: return doc from wait_health; why: wait_health exposes doc so its caller can continue with the function\'s computed outcome. + return doc + # What: handle oserror and value error by pass; why: wait_health converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, ValueError): + # What: ignore the anticipated exception handled by this branch; why: wait_health continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: pause one second between health probes; why: wait_health avoids a busy retry loop while retaining a bounded readiness deadline. + time.sleep(1) + # What: raise TimeoutError for the caller; why: wait_health stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError("health did not become ready") + + +# What: define canary around url and model and stream; why: its direct callers call canary for canary and rely on this exact input and result contract. +def canary(url, model, stream=False): + # What: compute body from model and stream and model and messages and temperature; why: body stream options include usage later reads body, so canary must retain the computed value under that name. + body = { + # What: map the model field as model; why: canary sends this field through body so the router selects the canonical model or alias for upstream dispatch. + "model": model, + # What: map the role field as user; why: canary carries role through body into body stream options include usage true. + "messages": [{"role": "user", "content": "What is 2 + 2? Reply with only the single digit."}], + # What: map the temperature field as 0; why: canary carries temperature through body into body stream options include usage true. + "temperature": 0, "max_tokens": 32, "stream": stream, + # What: map the enable thinking field as false; why: canary carries enable thinking through body into body stream options include usage true. + "chat_template_kwargs": {"enable_thinking": False}, + # What: complete the body mapping with model and messages and temperature and max tokens and stream; why: canary groups the supplied clauses as one body mapping before its value is consumed. + } + # What: gate on stream before body; why: canary admits body only for this predicate and excludes the opposite state. + if stream: + # What: map the include usage field as true; why: canary carries include usage through body entry into raw http url v1 chat completions body. + body["stream_options"] = {"include_usage": True} + # What: compute raw from http and body and url and v1 and chat; why: assert b data done in raw later reads raw, so canary must retain the computed value under that name. + raw = http(url + "/v1/chat/completions", body, timeout=660) + # What: gate on stream before parts; why: canary admits parts only for this predicate and excludes the opposite state. + if stream: + # What: initialize parts as an empty runtime accumulator; why: canary appends or maps entries into it during parts append choice get delta get content or before consuming the aggregate. + parts = [] + # What: assert that b data done is present in raw; why: canary requires b data done is present in raw to be true, so a false result stops the invalid state. + assert b"data: [DONE]" in raw, "SSE completion marker missing" + # What: iterate across splitlines and decode and raw to perform doc and choice and startswith and line and loads; why: canary repeats the body only while or for the loop header admits an iteration. + for line in raw.decode().splitlines(): + # What: gate on startswith and line before doc and loads and json and line; why: canary admits doc and loads and json and line only for this predicate and excludes the opposite state. + if line.startswith("data: ") and line != "data: [DONE]": + # What: compute doc from loads and json and line and 6; why: for choice in doc get choices later reads doc, so canary must retain the computed value under that name. + doc = json.loads(line[6:]) + # What: iterate across get and doc to perform append and parts and get and choice; why: canary repeats the body only while or for the loop header admits an iteration. + for choice in doc.get("choices", []): + # What: preserve the exact parts append choice get delta get content or literal fragment; why: canary passes this fragment verbatim through parts.append(choice.get("delta", {}).get("content") or ""), because changing it would alter a protocol payload, serialized fixture, or public message. + parts.append(choice.get("delta", {}).get("content") or "") + # What: compute content from join and parts and value; why: content doc choices message get content later reads content, so canary must retain the computed value under that name. + content = "".join(parts) + # What: select the remaining branch that performs doc json loads raw; why: canary covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute doc from loads and raw and json; why: content doc choices message get content later reads doc, so canary must retain the computed value under that name. + doc = json.loads(raw) + # What: compute content from get and doc and value and content and message; why: return raw content strip later reads content, so canary must retain the computed value under that name. + content = doc["choices"][0]["message"].get("content") or "" + # What: return raw and strip and content from canary; why: canary exposes raw and strip and content so its caller can continue with the function\'s computed outcome. + return raw, content.strip() + + +# What: define cancellation_canary around url and model and seconds; why: its direct callers call cancellation_canary for cancellation canary and rely on this exact input and result contract. +def cancellation_canary(url, model, *, seconds=30): + """Close a live SSE response, then require same-process terminal abort evidence. + + Active reaching zero alone is insufficient: TTL restart and normal completion + can also produce that observation. Check instance identity and completed count. + """ + # What: document close a live sse response then in the cancellation_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand cancellation canary behavior without executing it. + # What: document active reaching zero alone is insufficient in the cancellation_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand cancellation canary behavior without executing it. + # What: document can also produce that observation check in the cancellation_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand cancellation canary behavior without executing it. + # What: preserve the paragraph boundary in the the cancellation_canary docstring; why: introspection and maintainers read this paragraph break to understand cancellation canary behavior without executing it. + # What: compute stats url from model and url and v1 and stats and upstream; why: before json loads http stats url later reads stats url, so cancellation_canary must retain the computed value under that name. + stats_url = url + "/upstream/" + model + "/v1/stats" + # What: compute before from loads and json and http and stats url; why: instance before get instance id later reads before, so cancellation_canary must retain the computed value under that name. + before = json.loads(http(stats_url)) + # What: compute instance from get and before and instance id; why: assert instance backend instance identity missing later reads instance, so cancellation_canary must retain the computed value under that name. + instance = before.get("instance_id") + # What: assert that instance; why: cancellation_canary requires instance to be true, so a false result stops the invalid state. + assert instance, "backend instance identity missing" + # What: assert that before requests active equals 0; why: cancellation_canary requires before requests active equals 0 to be true, so a false result stops the invalid state. + assert before["requests"]["active"] == 0, "cancellation test requires an idle backend" + # What: map the model field as model; why: cancellation_canary sends this field through body so the router selects the canonical model or alias for upstream dispatch. + body = {"model": model, "stream": True, "max_tokens": 1024, "temperature": 0, + # What: map the role field as user; why: cancellation_canary carries role through body into data json dumps body encode. + "messages": [{"role": "user", "content": + # What: apply the count from to writing every number portion of body; why: cancellation_canary uses this clause to evaluate body as one grouped value. + "Count from 1 to 1000, writing every number on a separate line. Do not summarize."}], + # What: map the enable thinking field as false; why: cancellation_canary carries enable thinking through body into data json dumps body encode. + "chat_template_kwargs": {"enable_thinking": False}} + # What: compute request from request and request and url and urllib; why: with urllib request urlopen request timeout as response later reads request, so cancellation_canary must retain the computed value under that name. + request = urllib.request.Request(url + "/v1/chat/completions", + # What: supply data to operation.encode; why: cancellation_canary binds this encode and dumps and body and json value to operation.encode's data input. + data=json.dumps(body).encode(), + # What: map the content type field as application and json; why: cancellation_canary carries content type through request into with urllib request urlopen request timeout 660 as response. + headers={"Content-Type": "application/json"}) + # What: compute raw from bytearray; why: raw extend line later reads raw, so cancellation_canary must retain the computed value under that name. + raw = bytearray() + # What: compute started from monotonic and time; why: after after first content seconds first content started later reads started, so cancellation_canary must retain the computed value under that name. + started = time.monotonic() + # What: initialize the observed-statistics sentinel to no result; why: cancellation_canary can distinguish not-yet-fetched state from a completed statistics response. + observed = None + # What: enter the urllib.request.urlopen managed context before for line in response; why: cancellation_canary releases this resource or lock after for line in response on both success and failure paths. + with urllib.request.urlopen(request, timeout=660) as response: + # Read incrementally. Reading the entire body would only test completion. + # What: iterate across response to perform extend and line and raw; why: cancellation_canary repeats the body only while or for the loop header admits an iteration. + for line in response: + # What: append the received stream line to the raw response buffer; why: cancellation_canary tracks accumulated bytes before triggering its disconnect threshold. + raw.extend(line) + # What: gate on len and raw before runtime error; why: cancellation_canary admits runtime error only for this predicate and excludes the opposite state. + if len(raw) > 1024 * 1024: + # What: raise RuntimeError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("stream exceeded cancellation capture limit") + # What: gate on strip and line before runtime error; why: cancellation_canary admits runtime error only for this predicate and excludes the opposite state. + if line.strip() == b"data: [DONE]": + # What: raise RuntimeError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("stream completed before cancellation") + # What: gate on startswith and line before the computed value; why: cancellation_canary admits the computed value only for this predicate and excludes the opposite state. + if not line.startswith(b"data: "): + # What: apply the continue portion of the enclosing predicate; why: this clause remains in cancellation_canary\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: compute doc from loads and json and line and 6; why: if any choice get delta get content later reads doc, so cancellation_canary must retain the computed value under that name. + doc = json.loads(line[6:]) + # What: gate on any and get and choice and doc before first content and monotonic and time; why: cancellation_canary admits first content and monotonic and time only for this predicate and excludes the opposite state. + if any(choice.get("delta", {}).get("content") for choice in doc.get("choices", [])): + # What: compute first content from monotonic and time; why: after after first content seconds first content started later reads first content, so cancellation_canary must retain the computed value under that name. + first_content = time.monotonic() + # What: compute observed from loads and json and http and stats url; why: assert observed instance id instance backend restarted later reads observed, so cancellation_canary must retain the computed value under that name. + observed = json.loads(http(stats_url)) + # What: assert that observed instance id equals instance; why: cancellation_canary requires observed instance id equals instance to be true, so a false result stops the invalid state. + assert observed["instance_id"] == instance, "backend restarted before disconnect" + # What: assert that observed requests active exceeds 0; why: cancellation_canary requires observed requests active exceeds 0 to be true, so a false result stops the invalid state. + assert observed["requests"]["active"] > 0, "generation already finished before disconnect" + # What: leave the stream loop after enough response bytes arrive; why: cancellation can now be triggered against a live partial response. + break + # What: select the remaining branch that performs raise runtime error stream ended without a; why: cancellation_canary covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: raise RuntimeError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("stream ended without a content delta") + # What: compute disconnected from monotonic and time; why: deadline disconnected seconds later reads disconnected, so cancellation_canary must retain the computed value under that name. + disconnected = time.monotonic() + # What: compute deadline from disconnected and seconds; why: if time monotonic deadline later reads deadline, so cancellation_canary must retain the computed value under that name. + deadline = disconnected + seconds + # What: poll cancellation statistics until a terminal result; why: the loop ends after cancellation evidence or its explicit deadline. + while True: + # What: compute after from loads and json and http and stats url; why: assert after instance id instance backend restart later reads after, so cancellation_canary must retain the computed value under that name. + after = json.loads(http(stats_url)) + # What: assert that after instance id equals instance; why: cancellation_canary requires after instance id equals instance to be true, so a false result stops the invalid state. + assert after["instance_id"] == instance, "backend restart cannot count as cancellation" + # What: gate on after before after and before; why: cancellation_canary admits after and before only for this predicate and excludes the opposite state. + if after["requests"]["active"] == 0: + # What: require after requests completed == before requests completed; why: the qualifier stops immediately when this protected invariant is false. + # What: require after requests completed == before requests completed; why: the qualifier stops immediately when this protected invariant is false. + assert after["requests"]["completed"] == before["requests"]["completed"], \ + "normal completion cannot count as cancellation" + # What: map the passed field as true; why: cancellation_canary carries passed into return bytes(raw), {"passed": True, "before": before, "during": observed. + return bytes(raw), {"passed": True, "before": before, "during": observed, + # What: map the after field as after; why: cancellation_canary carries after into "after": after, "firstContentSeconds": first_content - started. + "after": after, "firstContentSeconds": first_content - started, + # What: map the abort seconds field as disconnected and monotonic and time; why: cancellation_canary carries abort seconds into "abortSeconds": time.monotonic() - disconnected}. + "abortSeconds": time.monotonic() - disconnected} + # What: gate on deadline and monotonic and time before timeout error; why: cancellation_canary admits timeout error only for this predicate and excludes the opposite state. + if time.monotonic() >= deadline: + # What: raise TimeoutError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError("disconnected request did not reach terminal abort") + # What: call time.sleep with 0 25; why: cancellation_canary invokes time.sleep while performing the enclosing return; the call advances that operation through its result or side effect. + time.sleep(0.25) + + +# What: define main around the current object state; why: its direct callers call main for main and rely on this exact input and result contract. +def main(): + # What: compute parser from argument parser and argparse and doc; why: parser add argument name required later reads parser, so main must retain the computed value under that name. + parser = argparse.ArgumentParser(description=__doc__) + # What: iterate across the computed value to perform add argument and parser and name; why: main repeats the body only while or for the loop header admits an iteration. + for name in ("source", "python", "llama-swap", "model-a", "model-b", "artifacts", "protected-service", "protected-url", "expected-hostname"): + # What: preserve the exact parser add argument name required literal fragment; why: main passes this fragment verbatim through parser.add_argument("--" + name, required=True), because changing it would alter a protocol payload, serialized fixture, or public message. + parser.add_argument("--" + name, required=True) + # What: register the parser add argument allow maintenance action store true required True command-line option; why: main validates this operator input before starting the qualification sequence. + parser.add_argument("--allow-maintenance", action="store_true", required=True) + # What: register the parser add argument port type int default 1960 command-line option; why: main validates this operator input before starting the qualification sequence. + parser.add_argument("--port", type=int, default=1960) + # What: preserve the exact parser add argument start port type int default literal fragment; why: main passes this fragment verbatim through parser.add_argument("--start-port", type=int, default=1961), because changing it would alter a protocol payload, serialized fixture, or public message. + parser.add_argument("--start-port", type=int, default=1961) + # What: add the --extended switch for concurrency and idle-eviction checks; why: operators opt into the longer qualification cases instead of running them by default. + parser.add_argument("--extended", action="store_true", help="Also test concurrent requests and idle eviction") + # What: add the --cancellation switch for live SSE disconnect checks; why: operators explicitly request the disruptive cancellation-and-recovery qualification path. + parser.add_argument("--cancellation", action="store_true", help="Also qualify live SSE disconnect and recovery") + # What: compute args from parse args and parser; why: require expected hostname args expected hostname later reads args, so main must retain the computed value under that name. + args = parser.parse_args() + # What: call require_expected_hostname with expected hostname and args; why: main invokes require_expected_hostname while performing artifacts path args artifacts; the call advances that operation through its result or side effect. + require_expected_hostname(args.expected_hostname) + # What: compute artifacts from path and artifacts and args; why: artifacts mkdir parents exist ok later reads artifacts, so main must retain the computed value under that name. + artifacts = Path(args.artifacts) + # What: supply parents to artifacts.mkdir; why: main binds this true value to artifacts.mkdir's parents input. + artifacts.mkdir(parents=True, exist_ok=False) + # What: map the trials field as the fixture input; why: main carries trials through status into artifacts result json write text json dumps status indent 2. + status = {"trials": [], "restored": False} + + # What: define save around the current object state; why: its direct callers call save for save and rely on this exact input and result contract. + def save(): + # What: write the current qualification status as indented UTF-8 JSON; why: operators need a durable result artifact even when a later qualification phase fails. + (artifacts / "result.json").write_text(json.dumps(status, indent=2), encoding="utf-8") + + # What: compute service from sudo and n and systemctl; why: subprocess run service is active quiet args protected service check later reads service, so main must retain the computed value under that name. + service = ["sudo", "-n", "systemctl"] + # What: execute subprocess run service is active quiet args protected service check True; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run(service + ["is-active", "--quiet", args.protected_service], check=True) + # What: compute status entry from wait health and protected url and args and 10 and health; why: status trials append row later reads status entry, so main must retain the computed value under that name. + status["baselineHealth"] = wait_health(args.protected_url + "/health", 10) + # What: compute baseline models from loads and json and http and protected url; why: protected model baseline models data id later reads baseline models, so main must retain the computed value under that name. + baseline_models = json.loads(http(args.protected_url + "/v1/models")) + # What: compute protected model from baseline models and id and 0 and data; why: raw content canary args protected url protected model later reads protected model, so main must retain the computed value under that name. + protected_model = baseline_models["data"][0]["id"] + # What: evaluate and capture raw content canary args protected url protected model; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + raw, content = canary(args.protected_url, protected_model) + # What: execute artifacts baseline json write bytes raw; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + (artifacts / "baseline.json").write_bytes(raw) + # What: gate on content before runtime error; why: main admits runtime error only for this predicate and excludes the opposite state. + if content != "4": + # What: raise RuntimeError for the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("protected-service baseline canary did not return 4; no maintenance performed") + + # What: compute env from copy and environ and os; why: env pythonpath str path args source python later reads env, so main must retain the computed value under that name. + env = os.environ.copy() + # What: compute env entry from str and path and source and args and python; why: env path str path home local bin later reads env entry, so main must retain the computed value under that name. + env["PYTHONPATH"] = str(Path(args.source) / "python") + # What: compute env entry from pathsep and get and str and os; why: env torch extensions dir str artifacts torch extensions later reads env entry, so main must retain the computed value under that name. + env["PATH"] = str(Path.home() / ".local/bin") + os.pathsep + env.get("PATH", "") + # Avoid sharing extension binaries or abandoned build locks across revisions. + # What: compute env entry from str and artifacts and torch extensions; why: env max jobs later reads env entry, so main must retain the computed value under that name. + env["TORCH_EXTENSIONS_DIR"] = str(artifacts / "torch-extensions") + # What: cap native-extension compilation at two parallel jobs; why: the later kernel preflight must not exhaust the qualification host while building extensions. + env["MAX_JOBS"] = "2" + # What: compute config from start port and args and health check timeout and global ttl and unload timeout; why: config f alias cmd json dumps command later reads config, so main must retain the computed value under that name. + config = ["healthCheckTimeout: 600", "globalTTL: 0", "unloadTimeout: 45", "logToStdout: both", f"startPort: {args.start_port}", "models:"] + # What: import shlex for main using shlex; why: main uses shlex join, making that imported dependency available to its named operation. + import shlex + + # What: iterate across model a and model b and args to perform command and join and shlex and python and model; why: main repeats the body only while or for the loop header admits an iteration. + for alias, model in (("model-a", args.model_a), ("model-b", args.model_b)): + # What: compute command from join and shlex and python and model; why: config f alias cmd json dumps command later reads command, so main must retain the computed value under that name. + command = shlex.join([ + # What: apply the args python m freetoken cli serve model model portion of command; why: main uses this clause to evaluate command as one grouped value. + args.python, "-m", "freetoken.cli", "serve", "--model", model, + # What: apply the host port port served model name model id portion of command; why: main uses this clause to evaluate command as one grouped value. + "--host", "127.0.0.1", "--port", "${PORT}", "--served-model-name", "${MODEL_ID}", + # What: apply the max seq len override num tokens max prefill length portion of command; why: main uses this clause to evaluate command as one grouped value. + "--max-seq-len-override", "4096", "--num-tokens", "4096", "--max-prefill-length", "512", + # What: apply the max running requests graph memory ratio portion of command; why: main uses this clause to evaluate command as one grouped value. + "--max-running-requests", "1", "--graph", "1", "--memory-ratio", "0.75", + # What: apply the attention backend triton moe backend fused disable pynccl portion of command; why: main uses this clause to evaluate command as one grouped value. + "--attention-backend", "triton", "--moe-backend", "fused", "--disable-pynccl", + # What: complete the shlex.join call with python; why: main groups the supplied clauses as one shlex.join call before its value is consumed. + ]) + # What: append this model command, readiness endpoint, and proxy stanza; why: the generated llama-swap configuration needs a complete entry before optional TTL settings. + config += [f" {alias}:", " cmd: " + json.dumps(command), " checkEndpoint: /ready", " proxy: http://127.0.0.1:${PORT}"] + # What: gate on extended and args before append and config; why: main admits append and config only for this predicate and excludes the opposite state. + if args.extended: + # What: preserve the exact config append ttl literal fragment; why: main passes this fragment verbatim through config.append(" ttl: 5"), because changing it would alter a protocol payload, serialized fixture, or public message. + config.append(" ttl: 5") + # What: compute config path from artifacts and models and yaml; why: config path write text n join config n encoding later reads config path, so main must retain the computed value under that name. + config_path = artifacts / "models.yaml" + # What: preserve the exact config path write text n join config n encoding literal fragment; why: main passes this fragment verbatim through config_path.write_text("\n".join(config) + "\n", encoding="utf-8"), because changing it would alter a protocol payload, serialized fixture, or public message. + config_path.write_text("\n".join(config) + "\n", encoding="utf-8") + # What: run llama-swap configuration validation against the generated file; why: qualification fails before launch when the temporary routing configuration is invalid. + subprocess.run([args.llama_swap, "-config", str(config_path), "-validate"], env=env, check=True) + # What: preserve the exact print native kernel preflight started flush literal fragment; why: main passes this fragment verbatim through print("NATIVE_KERNEL_PREFLIGHT_STARTED", flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("NATIVE_KERNEL_PREFLIGHT_STARTED", flush=True) + # What: open the with artifacts kernel build log open wb as build log resource scope; why: the qualification operation releases this resource when the guarded block exits. + with (artifacts / "kernel-build.log").open("wb") as build_log: + # What: execute subprocess run args python c from freetoken kernel gguf import module module print NATIVE KERNEL READY; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run([args.python, "-c", "from freetoken.kernel.gguf import _module; _module(); print('NATIVE_KERNEL_READY')"], + # What: supply env to subprocess.run; why: main binds this env value to subprocess.run's env input. + env=env, cwd=args.source, stdout=build_log, stderr=subprocess.STDOUT, check=True, timeout=600) + # What: initialize the child-process sentinel to no process; why: cleanup can test whether llama-swap started before attempting termination. + proc = None + # What: compute maintenance from false; why: maintenance later reads maintenance, so main must retain the computed value under that name. + maintenance = False + + # What: define interrupted around the current object state; why: its direct callers call interrupted for interrupted and rely on this exact input and result contract. + def interrupted(*_): + # What: convert a termination signal into KeyboardInterrupt; why: the normal interruption path then performs restoration and child cleanup. + raise KeyboardInterrupt + + # What: register the interruption handler for SIGTERM; why: service-manager termination must enter the qualifier restoration path. + signal.signal(signal.SIGTERM, interrupted) + # What: register the interruption handler for SIGHUP; why: session loss must enter the same restoration path. + signal.signal(signal.SIGHUP, interrupted) + # What: establish the handler boundary for the protected operation; why: main routes failures to base exception while preserving cleanup and success flow. + try: + # Set the restore obligation before the stop, including partial failures. + # What: compute maintenance from true; why: if maintenance later reads maintenance, so main must retain the computed value under that name. + maintenance = True + # What: execute subprocess run service stop args protected service check True timeout 90; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run(service + ["stop", args.protected_service], check=True, timeout=90) + # What: preserve the exact print maintenance started flush literal fragment; why: main passes this fragment verbatim through print("MAINTENANCE_STARTED", flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("MAINTENANCE_STARTED", flush=True) + # What: enter the operation.open managed context before proc subprocess popen args llama swap config str config path; why: main releases this resource or lock after proc subprocess popen args llama swap config str config path on both success and failure paths. + with (artifacts / "swap.log").open("wb") as log: + # What: compute proc from popen and subprocess and llama swap and env; why: if time monotonic deadline or proc poll is later reads proc, so main must retain the computed value under that name. + proc = subprocess.Popen([args.llama_swap, "-config", str(config_path), "-listen", f"127.0.0.1:{args.port}"], + # What: supply env to subprocess.Popen; why: main binds this env value to subprocess.Popen's env input. + env=env, stdout=log, stderr=subprocess.STDOUT, start_new_session=True) + # What: compute base from port and args and http; why: listing json loads http base v1 models later reads base, so main must retain the computed value under that name. + base = f"http://127.0.0.1:{args.port}" + # What: compute deadline from monotonic and time and 20; why: if time monotonic deadline or proc poll is later reads deadline, so main must retain the computed value under that name. + deadline = time.monotonic() + 20 + # What: retry model-list readiness until a terminal condition; why: the loop exits on a valid listing and raises on process exit or deadline expiry. + while True: + # What: establish the handler boundary for the protected operation; why: main routes failures to oserror and value error while preserving cleanup and success flow. + try: + # What: compute listing from loads and json and http and base and v1; why: assert item id for item in later reads listing, so main must retain the computed value under that name. + listing = json.loads(http(base + "/v1/models", timeout=2)) + # What: assert that item id for item in listing equals model a model b; why: main requires item id for item in listing equals model a model b to be true, so a false result stops the invalid state. + assert {item["id"] for item in listing["data"]} == {"model-a", "model-b"} + # What: leave the readiness loop after a valid listing; why: both expected models are visible and qualification can begin. + break + # What: handle oserror and value error by if time monotonic at least deadline or proc poll; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, ValueError): + # What: gate on deadline and monotonic and poll and time and proc before the computed value; why: main admits the computed value only for this predicate and excludes the opposite state. + if time.monotonic() >= deadline or proc.poll() is not None: + # What: re-propagate the active failure to the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: wait half a second before retrying the model-list request; why: readiness polling needs backoff instead of a busy loop. + time.sleep(0.5) + # What: iterate across enumerate to perform started and monotonic and time; why: main repeats the body only while or for the loop header admits an iteration. + for index, (alias, streaming) in enumerate((("model-a", False), ("model-b", True), ("model-a", True))): + # What: compute started from monotonic and time; why: row model alias stream streaming seconds later reads started, so main must retain the computed value under that name. + started = time.monotonic() + # What: preserve the exact print f trial started index alias flush literal fragment; why: main passes this fragment verbatim through print(f"TRIAL_STARTED {index} {alias}", flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print(f"TRIAL_STARTED {index} {alias}", flush=True) + # What: compute raw and content from canary and base and alias and streaming; why: artifacts f trial index response write bytes later reads raw and content, so main must retain the computed value under that name. + raw, content = canary(base, alias, streaming) + # What: preserve the exact artifacts f trial index response write bytes literal fragment; why: main passes this fragment verbatim through (artifacts / f"trial-{index}.response").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / f"trial-{index}.response").write_bytes(raw) + # What: map the model field as alias; why: main sends this field through row so the router selects the canonical model or alias for upstream dispatch. + row = {"model": alias, "stream": streaming, "seconds": time.monotonic() - started, "content": content, "passed": content == "4"} + # What: preserve the exact status trials append row literal fragment; why: main passes this fragment verbatim through status["trials"].append(row), because changing it would alter a protocol payload, serialized fixture, or public message. + status["trials"].append(row) + # What: checkpoint the completed trial in the result artifact; why: evidence survives if a later trial or cleanup step fails. + save() + # What: preserve the exact print trial result json dumps row flush literal fragment; why: main passes this fragment verbatim through print("TRIAL_RESULT " + json.dumps(row), flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("TRIAL_RESULT " + json.dumps(row), flush=True) + # What: gate on row before runtime error; why: main admits runtime error only for this predicate and excludes the opposite state. + if not row["passed"]: + # What: raise RuntimeError for the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("deterministic quality gate failed") + # What: gate on cancellation and args before raw and cancellation and cancellation canary and base; why: main admits raw and cancellation and cancellation canary and base only for this predicate and excludes the opposite state. + if args.cancellation: + # What: compute raw and cancellation from cancellation canary and base and model a; why: artifacts cancelled prefix sse write bytes raw later reads raw and cancellation, so main must retain the computed value under that name. + raw, cancellation = cancellation_canary(base, "model-a") + # What: preserve the exact artifacts cancelled prefix sse write bytes raw literal fragment; why: main passes this fragment verbatim through (artifacts / "cancelled-prefix.sse").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / "cancelled-prefix.sse").write_bytes(raw) + # What: compute status entry from cancellation; why: status cancellation recovery passed later reads status entry, so main must retain the computed value under that name. + status["cancellation"] = cancellation + # What: checkpoint the cancellation result before recovery trials; why: disconnect evidence survives if post-cancellation validation fails. + save() + # What: iterate across enumerate to perform raw and content and canary and base and alias; why: main repeats the body only while or for the loop header admits an iteration. + for index, alias in enumerate(("model-a", "model-b", "model-a")): + # What: compute raw and content from canary and base and alias and true; why: artifacts f after cancel index alias sse later reads raw and content, so main must retain the computed value under that name. + raw, content = canary(base, alias, True) + # What: preserve the exact artifacts f after cancel index alias sse literal fragment; why: main passes this fragment verbatim through (artifacts / f"after-cancel-{index}-{alias}.sse").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / f"after-cancel-{index}-{alias}.sse").write_bytes(raw) + # What: assert that content equals 4; why: main requires content equals 4 to be true, so a false result stops the invalid state. + assert content == "4", "post-cancellation routing failed" + # What: compute status entry from true; why: status concurrent passed later reads status entry, so main must retain the computed value under that name. + status["cancellationRecoveryPassed"] = True + # What: preserve the exact print cancellation recovery ok flush literal fragment; why: main passes this fragment verbatim through print("CANCELLATION_RECOVERY_OK", flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("CANCELLATION_RECOVERY_OK", flush=True) + # What: gate on extended and args before names and clients and futures and print and thread pool executor; why: main admits names and clients and futures and print and thread pool executor only for this predicate and excludes the opposite state. + if args.extended: + # What: iterate across the computed value to perform clients and futures and thread pool executor and index and future; why: main repeats the body only while or for the loop header admits an iteration. + for names in (("model-a", "model-a"), ("model-a", "model-b")): + # What: enter the ThreadPoolExecutor managed context before futures clients submit canary base name for; why: main releases this resource or lock after futures clients submit canary base name for on both success and failure paths. + with ThreadPoolExecutor(2) as clients: + # What: compute futures from submit and canary and base and name; why: for index future in enumerate futures later reads futures, so main must retain the computed value under that name. + futures = [clients.submit(canary, base, name, True) for name in names] + # What: iterate across enumerate and futures to perform raw and content and result and future; why: main repeats the body only while or for the loop header admits an iteration. + for index, future in enumerate(futures): + # What: compute raw and content from result and future; why: artifacts f concurrent join names index later reads raw and content, so main must retain the computed value under that name. + raw, content = future.result() + # What: preserve the exact artifacts f concurrent join names index literal fragment; why: main passes this fragment verbatim through (artifacts / f"concurrent-{'-'.join(names)}-{index}.sse").write_bytes(ra, because changing it would alter a protocol payload, serialized fixture, or public me. + (artifacts / f"concurrent-{'-'.join(names)}-{index}.sse").write_bytes(raw) + # What: assert that content equals 4; why: main requires content equals 4 to be true, so a false result stops the invalid state. + assert content == "4", "concurrent quality gate failed" + # What: assert that b usage is present in raw; why: main requires b usage is present in raw to be true, so a false result stops the invalid state. + assert b'"usage"' in raw, "streamed usage block missing" + # What: preserve the exact print concurrent ok join names flush literal fragment; why: main passes this fragment verbatim through print("CONCURRENT_OK " + ",".join(names), flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("CONCURRENT_OK " + ",".join(names), flush=True) + # What: compute status entry from true; why: status idle eviction passed later reads status entry, so main must retain the computed value under that name. + status["concurrentPassed"] = True + # What: compute deadline from monotonic and time and 30; why: while time monotonic deadline later reads deadline, so main must retain the computed value under that name. + deadline = time.monotonic() + 30 + # What: iterate across deadline and monotonic and time to perform running and loads and json and http and base; why: main repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: compute running from loads and json and http and base and running; why: if running get running later reads running, so main must retain the computed value under that name. + running = json.loads(http(base + "/running")) + # What: gate on get and running before status; why: main admits status only for this predicate and excludes the opposite state. + if running.get("running") == []: + # What: compute status entry from true; why: assert status get idle eviction passed idle ttl did later reads status entry, so main must retain the computed value under that name. + status["idleEvictionPassed"] = True + # What: leave the idle-eviction loop after unload; why: the engine is no longer running and the eviction gate is satisfied. + break + # What: pause one second before checking idle eviction again; why: the qualifier gives asynchronous unload work time to complete without busy-waiting. + time.sleep(1) + # What: assert that status get idle eviction passed; why: main requires status get idle eviction passed to be true, so a false result stops the invalid state. + assert status.get("idleEvictionPassed"), "idle TTL did not unload the models" + # What: preserve the exact print idle eviction ok flush literal fragment; why: main passes this fragment verbatim through print("IDLE_EVICTION_OK", flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("IDLE_EVICTION_OK", flush=True) + # What: handle base exception by status error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException as exc: + # What: compute status entry from repr and exc; why: status cleanup error repr exc later reads status entry, so main must retain the computed value under that name. + status["error"] = repr(exc) + # What: preserve the exact print qualification failed repr exc flush literal fragment; why: main passes this fragment verbatim through print("QUALIFICATION_FAILED " + repr(exc), flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("QUALIFICATION_FAILED " + repr(exc), flush=True) + # What: run if proc is not on every exit path; why: main performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: gate on proc before oserror and timeout expired and poll and killpg and pid; why: main admits oserror and timeout expired and poll and killpg and pid only for this predicate and excludes the opposite state. + if proc is not None: + # What: establish the handler boundary for the protected operation; why: main routes failures to oserror and timeout expired and subprocess while preserving cleanup and success flow. + try: + # What: gate on poll and proc before killpg and pid and sigterm and os and proc; why: main admits killpg and pid and sigterm and os and proc only for this predicate and excludes the opposite state. + if proc.poll() is None: + # What: call os.killpg with pid and proc and sigterm and signal; why: main invokes os.killpg while performing try; the call advances that operation through its result or side effect. + os.killpg(proc.pid, signal.SIGTERM) + # What: establish the handler boundary for the protected operation; why: main routes failures to timeout expired and subprocess while preserving cleanup and success flow. + try: + # What: supply timeout to proc.wait; why: main binds this 60 value to proc.wait's timeout input. + proc.wait(timeout=60) + # What: handle timeout expired and subprocess by os killpg proc pid signal sigkill; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except subprocess.TimeoutExpired: + # What: call os.killpg with pid and proc and sigkill and signal; why: main invokes os.killpg while performing proc wait timeout; the call advances that operation through its result or side effect. + os.killpg(proc.pid, signal.SIGKILL) + # What: supply timeout to proc.wait; why: main binds this 10 value to proc.wait's timeout input. + proc.wait(timeout=10) + # What: handle oserror and timeout expired and subprocess by status cleanup error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, subprocess.TimeoutExpired) as exc: + # What: compute status entry from repr and exc; why: status restored health wait health args protected url health later reads status entry, so main must retain the computed value under that name. + status["cleanupError"] = repr(exc) + # What: gate on maintenance before exception and run and status and wait health and raw; why: main admits exception and run and status and wait health and raw only for this predicate and excludes the opposite state. + if maintenance: + # What: establish the handler boundary for the protected operation; why: main routes failures to exception while preserving cleanup and success flow. + try: + # What: execute subprocess run service start args protected service check True timeout 180; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run(service + ["start", args.protected_service], check=True, timeout=180) + # What: compute status entry from wait health and protected url and args and 300 and health; why: status restored content later reads status entry, so main must retain the computed value under that name. + status["restoredHealth"] = wait_health(args.protected_url + "/health", 300) + # What: evaluate and capture raw content canary args protected url protected model; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + raw, content = canary(args.protected_url, protected_model) + # What: execute artifacts restored json write bytes raw; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + (artifacts / "restored.json").write_bytes(raw) + # What: compute status entry from content and 4; why: print restored str status restored flush later reads status entry, so main must retain the computed value under that name. + status["restored"] = content == "4" + # What: execute print RESTORED str status restored flush True; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + print("RESTORED " + str(status["restored"]), flush=True) + # What: handle exception by status restore error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: + # What: compute status entry from repr and exc; why: passed status restored and error not later reads status entry, so main must retain the computed value under that name. + status["restoreError"] = repr(exc) + # What: preserve the exact print restore failed repr exc flush literal fragment; why: main passes this fragment verbatim through print("RESTORE_FAILED " + repr(exc), flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("RESTORE_FAILED " + repr(exc), flush=True) + # What: checkpoint the final restoration state; why: the artifact records cleanup success or failure before exit status is computed. + save() + # What: compute passed from status and all and len and x and restored; why: and len status trials and all later reads passed, so main must retain the computed value under that name. + passed = (status["restored"] and "error" not in status and "cleanupError" not in status + # What: call all with x and status and passed and trials; why: main invokes all while performing if args extended; the call advances that operation through its result or side effect. + and len(status["trials"]) == 3 and all(x["passed"] for x in status["trials"])) + # What: gate on extended and args before passed and get and status; why: main admits passed and get and status only for this predicate and excludes the opposite state. + if args.extended: + # What: compute passed from passed and get and status and concurrent passed and idle eviction passed; why: passed passed and status get cancellation get later reads passed, so main must retain the computed value under that name. + passed = passed and status.get("concurrentPassed") and status.get("idleEvictionPassed") + # What: gate on cancellation and args before passed and get and status; why: main admits passed and get and status only for this predicate and excludes the opposite state. + if args.cancellation: + # What: compute passed from passed and get and status and passed and cancellation recovery passed; why: return if passed else later reads passed, so main must retain the computed value under that name. + passed = passed and status.get("cancellation", {}).get("passed") and status.get("cancellationRecoveryPassed") + # What: return passed and 0 and 1 from main; why: main exposes passed and 0 and 1 so its caller can continue with the function\'s computed outcome. + return 0 if passed else 1 + + +# What: gate on name before exit and sys and main; why: qualify admits exit and sys and main only for this predicate and excludes the opposite state. +if __name__ == "__main__": + # What: call sys.exit with main; why: qualify invokes sys.exit while performing the enclosing return; the call advances that operation through its result or side effect. + sys.exit(main()) diff --git a/benchmarks/swap/qualify_native_recovery.py b/benchmarks/swap/qualify_native_recovery.py new file mode 100644 index 000000000..c10ee5c25 --- /dev/null +++ b/benchmarks/swap/qualify_native_recovery.py @@ -0,0 +1,304 @@ +"""Opt-in real-model daemon recovery test. Raw artifacts must remain private.""" +# What: document opt in real model daemon recovery test raw in the qualify_native_recovery docstring; why: introspection and maintainers read this exact docstring fragment to understand qualify native recovery behavior without executing it. + +# What: import argparse for main using argparse; why: main uses argparse argument parser, making that imported dependency available to its named operation. +import argparse +# What: import from concurrent futures import ThreadPoolExecutor; why: this module calls or annotates these symbols in the branch-created operations below. +from concurrent.futures import ThreadPoolExecutor +# What: import json for main using json; why: main uses json dumps, making that imported dependency available to its named operation. +import json +# What: import os for main using os; why: main uses os environ copy, making that imported dependency available to its named operation. +import os +# What: import path for main using pathlib and path; why: main uses path, making that imported dependency available to its named operation. +from pathlib import Path +# What: import signal for main using signal; why: main uses signal signal, making that imported dependency available to its named operation. +import signal +# What: import subprocess for main using subprocess; why: main uses subprocess run, making that imported dependency available to its named operation. +import subprocess +# What: import sys for module initialization using sys; why: module initialization uses sys exit, making that imported dependency available to its named operation. +import sys + +# What: import canary and http and require expected hostname and wait health for main using qualify and canary and http and require expected hostname and wait health; why: main uses canary and http and require expected hostname and wait health, making that imported dependency available to its named operation. +from qualify import canary, http, require_expected_hostname, wait_health + + +# What: define main around the current object state; why: its direct callers call main for main and rely on this exact input and result contract. +def main(): + # What: compute parser from argument parser and argparse and doc; why: parser add argument name required later reads parser, so main must retain the computed value under that name. + parser = argparse.ArgumentParser(description=__doc__) + # What: iterate across the computed value to perform add argument and parser and name; why: main repeats the body only while or for the loop header admits an iteration. + for name in ("source", "daemon-source", "python", "model", "extensions-dir", + # What: apply the protected service protected url artifacts expected hostname portion of the enclosing predicate; why: this clause remains in main\'s enclosing expression so its grouping and evaluation order stay intact. + "protected-service", "protected-url", "artifacts", "expected-hostname"): + # What: register the parser add argument name required True command-line option; why: main validates this operator input before starting the qualification sequence. + parser.add_argument("--" + name, required=True) + # What: register the parser add argument allow maintenance action store true required True command-line option; why: main validates this operator input before starting the qualification sequence. + parser.add_argument("--allow-maintenance", action="store_true", required=True) + # What: register the parser add argument port type int default 1963 command-line option; why: main validates this operator input before starting the qualification sequence. + parser.add_argument("--port", type=int, default=1963) + # What: compute args from parse args and parser; why: require expected hostname args expected hostname later reads args, so main must retain the computed value under that name. + args = parser.parse_args() + # What: call require_expected_hostname with expected hostname and args; why: main invokes require_expected_hostname while performing sys path insert str path args daemon source python; the call advances that operation through its result or side effect. + require_expected_hostname(args.expected_hostname) + # What: preserve the exact sys path insert str path args daemon source python literal fragment; why: main passes this fragment verbatim through sys.path.insert(0, str(Path(args.daemon_source) / "python")), because changing it would alter a protocol payload, serialized fixture, or public message. + sys.path.insert(0, str(Path(args.daemon_source) / "python")) + # What: import test client for main using fastapi and testclient and test client; why: main uses test client, making that imported dependency available to its named operation. + from fastapi.testclient import TestClient + # What: import build app for main using freetoken and daemon and app and build app; why: main uses build app, making that imported dependency available to its named operation. + from freetoken.daemon.app import build_app + # What: import model catalog for main using freetoken and daemon and catalog and model catalog; why: main uses model catalog load, making that imported dependency available to its named operation. + from freetoken.daemon.catalog import ModelCatalog + # What: import log ring for main using freetoken and daemon and logring and log ring; why: main uses log ring, making that imported dependency available to its named operation. + from freetoken.daemon.logring import LogRing + # What: import serve state store for main using freetoken and daemon and pidfile and serve state store; why: main uses serve state store, making that imported dependency available to its named operation. + from freetoken.daemon.pidfile import ServeStateStore + # What: import serve probe for main using freetoken and daemon and proxy and serve probe; why: main uses serve probe, making that imported dependency available to its named operation. + from freetoken.daemon.proxy import ServeProbe + # What: import popen child and serve manager for spawn and main using freetoken and daemon and serve manager and popen child and serve manager; why: spawn and main uses popen child and serve manager, making that imported dependency available to its named operation. + from freetoken.daemon.serve_manager import PopenChild, ServeManager + + # What: compute artifacts from path and artifacts and args; why: artifacts mkdir parents exist ok later reads artifacts, so main must retain the computed value under that name. + artifacts = Path(args.artifacts) + # What: supply parents to artifacts.mkdir; why: main binds this true value to artifacts.mkdir's parents input. + artifacts.mkdir(parents=True, exist_ok=False) + # What: map the passed field as false; why: main carries passed through status into status baseline health wait health args protected url health 10. + status = {"passed": False, "restored": False} + # What: compute service from sudo and n and systemctl; why: subprocess run service is active quiet args protected service check later reads service, so main must retain the computed value under that name. + service = ["sudo", "-n", "systemctl"] + # What: execute subprocess run service is active quiet args protected service check True; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run(service + ["is-active", "--quiet", args.protected_service], check=True) + # What: compute status entry from wait health and protected url and args and 10 and health; why: status initial response json later reads status entry, so main must retain the computed value under that name. + status["baselineHealth"] = wait_health(args.protected_url + "/health", 10) + # What: compute protected model from loads and json and http and protected url; why: raw content canary args protected url protected model later reads protected model, so main must retain the computed value under that name. + protected_model = json.loads(http(args.protected_url + "/v1/models"))["data"][0]["id"] + # What: evaluate and capture raw content canary args protected url protected model; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + raw, content = canary(args.protected_url, protected_model) + # What: execute artifacts baseline json write bytes raw; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + (artifacts / "baseline.json").write_bytes(raw) + # What: assert that content equals 4; why: main requires content equals 4 to be true, so a false result stops the invalid state. + assert content == "4", "baseline failed; no maintenance performed" + # What: compute env from copy and environ and os; why: env pythonpath str path args source python later reads env, so main must retain the computed value under that name. + env = os.environ.copy() + # What: compute env entry from str and path and source and args and python; why: env torch extensions dir args extensions dir later reads env entry, so main must retain the computed value under that name. + env["PYTHONPATH"] = str(Path(args.source) / "python") + # What: compute env entry from extensions dir and args; why: env max jobs later reads env entry, so main must retain the computed value under that name. + env["TORCH_EXTENSIONS_DIR"] = args.extensions_dir + # What: compute env entry from 2; why: cwd args source env env stdout log later reads env entry, so main must retain the computed value under that name. + env["MAX_JOBS"] = "2" + # What: open the with artifacts kernel preflight log open wb as log resource scope; why: the qualification operation releases this resource when the guarded block exits. + with (artifacts / "kernel-preflight.log").open("wb") as log: + # What: execute subprocess run args python c from freetoken kernel gguf import module module; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run([args.python, "-c", "from freetoken.kernel.gguf import _module; _module()"], + # What: supply cwd to subprocess.run; why: main binds this source and args value to subprocess.run's cwd input. + cwd=args.source, env=env, stdout=log, stderr=subprocess.STDOUT, + # What: supply check to subprocess.run; why: main binds this true value to subprocess.run's check input. + check=True, timeout=600) + # Deliberately corrupt test artifact, never an existing model file. + # What: compute bad model from artifacts and invalid test model and gguf; why: bad model write bytes b invalid gguf test fixture later reads bad model, so main must retain the computed value under that name. + bad_model = artifacts / "invalid-test-model.gguf" + # What: call bad_model.write_bytes with the named fixture input; why: main invokes bad_model.write_bytes while performing common host served model name native recovery; the call advances that operation through its result or side effect. + bad_model.write_bytes(b"INVALID_GGUF_TEST_FIXTURE") + # What: compute common from host and 127 0 0 1 and served model name and native recovery and max seq len override; why: f ready timeout s nargs json dumps common n later reads common, so main must retain the computed value under that name. + common = ["--host", "127.0.0.1", "--served-model-name", "native-recovery", + # What: apply the max seq len override num tokens portion of common; why: main uses this clause to evaluate common as one grouped value. + "--max-seq-len-override", "4096", "--num-tokens", "4096", + # What: apply the max prefill length max running requests portion of common; why: main uses this clause to evaluate common as one grouped value. + "--max-prefill-length", "512", "--max-running-requests", "1", + # What: apply the graph memory ratio attention backend triton portion of common; why: main uses this clause to evaluate common as one grouped value. + "--graph", "1", "--memory-ratio", "0.75", "--attention-backend", "triton", + # What: apply the moe backend fused disable pynccl portion of common; why: main uses this clause to evaluate common as one grouped value. + "--moe-backend", "fused", "--disable-pynccl"] + # What: compute catalog path from artifacts and models and toml; why: catalog path write text n join later reads catalog path, so main must retain the computed value under that name. + catalog_path = artifacts / "models.toml" + # What: preserve the exact catalog path write text n join literal fragment; why: main passes this fragment verbatim through catalog_path.write_text("\n".join(, because changing it would alter a protocol payload, serialized fixture, or public message. + catalog_path.write_text("\n".join( + # What: preserve the exact f models name nmodel json dumps str literal fragment; why: main passes this fragment verbatim through f"[models.{name}]\nmodel = {json.dumps(str(model))}\nport = {args.port}\, because changing it would alter a protocol payload, serialized fixture, or public message. + # What: preserve the exact f ready timeout s nargs json dumps common n literal fragment; why: main passes this fragment verbatim through f"[models.{name}]\nmodel = {json.dumps(str(model))}\nport = {args.port}\, because changing it would alter a protocol payload, serialized fixture, or public message. + f"[models.{name}]\nmodel = {json.dumps(str(model))}\nport = {args.port}\n" + f"ready_timeout_s = 600\nargs = {json.dumps(common)}\n" + # What: preserve the exact for name model in good args model literal fragment; why: main passes this fragment verbatim through for name, model in (("good", args.model), ("bad", bad_model))), encoding, because changing it would alter a protocol payload, serialized fixture, or public message. + for name, model in (("good", args.model), ("bad", bad_model))), encoding="utf-8") + # What: initialize children as an empty runtime accumulator; why: main appends or maps entries into it during log path artifacts f engine len children log before consuming the aggregate. + children = [] + + # What: define spawn around model and port and launch args; why: its direct callers call spawn for spawn and rely on this exact input and result contract. + def spawn(model, port, launch_args): + # What: compute log path from artifacts and len and children and engine and log; why: with log path open wb as log later reads log path, so spawn must retain the computed value under that name. + log_path = artifacts / f"engine-{len(children)}.log" + # What: enter the log_path.open managed context before proc subprocess popen args python m freetoken cli serve; why: spawn releases this resource or lock after proc subprocess popen args python m freetoken cli serve on both success and failure paths. + with log_path.open("wb") as log: + # What: compute proc from popen and subprocess and python and model; why: child popen child proc str log path later reads proc, so spawn must retain the computed value under that name. + proc = subprocess.Popen([args.python, "-m", "freetoken.cli", "serve", + # What: call str with port; why: spawn invokes str while performing cwd args source env env stdout log; the call advances that operation through its result or side effect. + "--model", model, "--port", str(port), *launch_args], + # What: supply cwd to subprocess.Popen; why: spawn binds this source and args value to subprocess.Popen's cwd input. + cwd=args.source, env=env, stdout=log, stderr=subprocess.STDOUT, + # What: supply stdin to subprocess.Popen; why: spawn binds this devnull and subprocess value to subprocess.Popen's stdin input. + stdin=subprocess.DEVNULL, start_new_session=True) + # What: compute child from popen child and proc and str and log path; why: children append child later reads child, so spawn must retain the computed value under that name. + child = PopenChild(proc, str(log_path)) + # What: call children.append with child; why: spawn invokes children.append while performing return child; the call advances that operation through its result or side effect. + children.append(child) + # What: return child from spawn; why: spawn exposes child so its caller can continue with the function\'s computed outcome. + return child + + # What: compute probe from serve probe; why: prepare stop probe prepare stop read stats probe fresh stats later reads probe, so main must retain the computed value under that name. + probe = ServeProbe() + # What: compute ring from log ring; why: manager serve manager ring store spawn fn spawn later reads ring, so main must retain the computed value under that name. + ring = LogRing() + # What: compute store from serve state store and str and artifacts and serve and json; why: manager serve manager ring store spawn fn spawn later reads store, so main must retain the computed value under that name. + store = ServeStateStore(str(artifacts / "serve.json")) + # What: compute manager from serve manager and ring and store and spawn; why: app build app manager manager ring ring later reads manager, so main must retain the computed value under that name. + manager = ServeManager(ring, store, spawn_fn=spawn, apply_oom=False, + # What: supply prepare stop to ServeManager; why: main binds this prepare stop and probe value to ServeManager's prepare stop input. + prepare_stop=probe.prepare_stop, read_stats=probe.fresh_stats, + # What: supply grace s to ServeManager; why: main binds this 30 value to ServeManager's grace s input. + grace_s=30, reap_wait_s=15) + # What: compute maintenance from false; why: maintenance later reads maintenance, so main must retain the computed value under that name. + maintenance = False + + # What: define interrupt around the current object state; why: its direct callers call interrupt for interrupt and rely on this exact input and result contract. + def interrupt(*_): + # What: propagate the active failure to the caller; why: interrupt stops this rejected path before it can mutate state, dispatch work, or report success. + raise KeyboardInterrupt + + # What: call signal.signal with sigterm and signal and interrupt; why: main invokes signal.signal while performing signal signal signal sighup interrupt; the call advances that operation through its result or side effect. + signal.signal(signal.SIGTERM, interrupt) + # What: call signal.signal with sighup and signal and interrupt; why: main invokes signal.signal while performing try; the call advances that operation through its result or side effect. + signal.signal(signal.SIGHUP, interrupt) + # What: establish the handler boundary for the protected operation; why: main routes failures to base exception while preserving cleanup and success flow. + try: + # What: compute maintenance from true; why: if maintenance later reads maintenance, so main must retain the computed value under that name. + maintenance = True + # What: execute subprocess run service stop args protected service check True timeout 90; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run(service + ["stop", args.protected_service], check=True, timeout=90) + # What: preserve the exact print native maintenance started flush literal fragment; why: main passes this fragment verbatim through print("NATIVE_MAINTENANCE_STARTED", flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("NATIVE_MAINTENANCE_STARTED", flush=True) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app manager manager ring ring; why: main releases this resource or lock after app build app manager manager ring ring on both success and failure paths. + with ThreadPoolExecutor(2) as lifecycle, ThreadPoolExecutor(2) as proxy: + # What: declare the pid input for main; why: main consumes pid during signature binding, so callers must bind it with the other signature inputs. + app = build_app(manager=manager, ring=ring, probe=probe, footprint_fn=lambda pid: {}, + # What: supply lifecycle pool to build_app; why: main binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, + # What: supply catalog to ModelCatalog.load; why: main binds this load and model catalog and str and catalog path value to ModelCatalog.load's catalog input. + catalog=ModelCatalog.load(str(catalog_path))) + # What: enter the TestClient managed context before response client post engine start profile json name; why: main releases this resource or lock after response client post engine start profile json name on both success and failure paths. + with TestClient(app) as client: + # What: map the name field as good; why: main carries name through response into status initial response json. + response = client.post("/engine/start-profile", json={"name": "good"}) + # What: compute status entry from json and response; why: status failed switch response json later reads status entry, so main must retain the computed value under that name. + status["initial"] = response.json() + # What: assert that response status code equals 200 and response json readiness ready; why: main requires response status code equals 200 and response json readiness ready to be true, so a false result stops the invalid state. + assert response.status_code == 200 and response.json()["readiness"]["ready"] + # What: compute raw and content from canary and port and args and native recovery and http; why: artifacts before failure json write bytes raw later reads raw and content, so main must retain the computed value under that name. + raw, content = canary(f"http://127.0.0.1:{args.port}", "native-recovery") + # What: preserve the exact artifacts before failure json write bytes raw literal fragment; why: main passes this fragment verbatim through (artifacts / "before-failure.json").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / "before-failure.json").write_bytes(raw) + # What: assert that content equals 4; why: main requires content equals 4 to be true, so a false result stops the invalid state. + assert content == "4" + # What: preserve the exact print native baseline ok flush literal fragment; why: main passes this fragment verbatim through print("NATIVE_BASELINE_OK", flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("NATIVE_BASELINE_OK", flush=True) + # What: map the name field as bad; why: main carries name through response into status failed switch response json. + response = client.post("/engine/switch-profile", json={"name": "bad"}) + # What: compute status entry from json and response; why: status accounting manager pending accounting later reads status entry, so main must retain the computed value under that name. + status["failedSwitch"] = response.json() + # What: assert that response status code equals 503; why: main requires response status code equals 503 to be true, so a false result stops the invalid state. + assert response.status_code == 503, "failed model must not report success" + # What: compute rollback from json and response and rollback; why: assert rollback launched and rollback readiness later reads rollback, so main must retain the computed value under that name. + rollback = response.json()["rollback"] + # What: assert that rollback launched and rollback readiness ready; why: main requires rollback launched and rollback readiness ready to be true, so a false result stops the invalid state. + assert rollback["launched"] and rollback["readiness"]["ready"] + # What: assert that len children equals 3 and children 1 proc poll not in; why: main requires len children equals 3 and children 1 proc poll not in to be true, so a false result stops the invalid state. + assert len(children) == 3 and children[1].proc.poll() not in (None, 0) + # What: require GGUF magic invalid in artifacts engine 1 log read text errors replace; why: the qualifier stops immediately when this protected invariant is false. + # What: require GGUF magic invalid in artifacts engine 1 log read text errors replace; why: the qualifier stops immediately when this protected invariant is false. + assert "GGUF magic invalid" in (artifacts / "engine-1.log").read_text(errors="replace"), \ + "replacement must fail for the intended invalid-GGUF reason" + # What: assert that store load model equals args model; why: main requires store load model equals args model to be true, so a false result stops the invalid state. + assert store.load().model == args.model + # What: compute raw and content from canary and port and args and native recovery and true; why: artifacts after recovery sse write bytes raw later reads raw and content, so main must retain the computed value under that name. + raw, content = canary(f"http://127.0.0.1:{args.port}", "native-recovery", True) + # What: preserve the exact artifacts after recovery sse write bytes raw literal fragment; why: main passes this fragment verbatim through (artifacts / "after-recovery.sse").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / "after-recovery.sse").write_bytes(raw) + # What: assert that content equals 4; why: main requires content equals 4 to be true, so a false result stops the invalid state. + assert content == "4", "restored model failed generation" + # What: compute status entry from pending accounting and manager; why: for row in status accounting previous later reads status entry, so main must retain the computed value under that name. + status["accounting"] = manager.pending_accounting() + # What: require any row get drainComplete and not row get degraded; why: the qualifier stops immediately when this protected invariant is false. + assert any(row.get("drainComplete") and not row.get("degraded") + # What: execute for row in status accounting previous engine receipt must be sealed; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + for row in status["accounting"]), "previous engine receipt must be sealed" + # What: require any row get reason == engine crashed and row get degraded; why: the qualifier stops immediately when this protected invariant is false. + assert any(row.get("reason") == "engine-crashed" and row.get("degraded") + # What: execute for row in status accounting loader failure must retain explicit crash accounting; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + for row in status["accounting"]), "loader failure must retain explicit crash accounting" + # What: compute status entry from true; why: status error repr exc later reads status entry, so main must retain the computed value under that name. + status["passed"] = True + # What: preserve the exact print native model recovery ok flush literal fragment; why: main passes this fragment verbatim through print("NATIVE_MODEL_RECOVERY_OK", flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("NATIVE_MODEL_RECOVERY_OK", flush=True) + # What: handle base exception by status error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException as exc: + # What: compute status entry from repr and exc; why: status cleanup error repr exc later reads status entry, so main must retain the computed value under that name. + status["error"] = repr(exc) + # What: preserve the exact print native recovery failed repr exc flush literal fragment; why: main passes this fragment verbatim through print("NATIVE_RECOVERY_FAILED " + repr(exc), flush=True), because changing it would alter a protocol payload, serialized fixture, or public message. + print("NATIVE_RECOVERY_FAILED " + repr(exc), flush=True) + # What: run try on every exit path; why: main performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: establish the handler boundary for the protected operation; why: main routes failures to exception while preserving cleanup and success flow. + try: + # What: supply force to manager.stop; why: main binds this true value to manager.stop's force input. + manager.stop(force=True) + # What: handle exception by status cleanup error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: + # What: compute status entry from repr and exc; why: status cleanup error repr exc later reads status entry, so main must retain the computed value under that name. + status["cleanupError"] = repr(exc) + # What: iterate across children to perform process lookup error and oserror and timeout expired and killpg and pid; why: main repeats the body only while or for the loop header admits an iteration. + for child in children: + # What: establish the handler boundary for the protected operation; why: main routes failures to oserror and timeout expired and subprocess while preserving cleanup and success flow. + try: + # What: establish the handler boundary for the protected operation; why: main routes failures to process lookup error while preserving cleanup and success flow. + try: + # What: call os.killpg with pid and child and sigkill and signal; why: main invokes os.killpg while performing except process lookup error; the call advances that operation through its result or side effect. + os.killpg(child.pid, signal.SIGKILL) + # What: handle process lookup error by pass; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except ProcessLookupError: + # What: ignore the anticipated exception handled by this branch; why: interrupt continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: gate on poll and proc and child before wait and proc and child; why: main admits wait and proc and child only for this predicate and excludes the opposite state. + if child.proc.poll() is None: + # What: supply timeout to child.proc.wait; why: main binds this 15 value to child.proc.wait's timeout input. + child.proc.wait(timeout=15) + # What: handle oserror and timeout expired and subprocess by status cleanup error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, subprocess.TimeoutExpired) as exc: + # What: compute status entry from repr and exc; why: status restored health wait health args protected url health later reads status entry, so main must retain the computed value under that name. + status["cleanupError"] = repr(exc) + # What: gate on maintenance before exception and run and status and wait health and raw; why: main admits exception and run and status and wait health and raw only for this predicate and excludes the opposite state. + if maintenance: + # What: establish the handler boundary for the protected operation; why: main routes failures to exception while preserving cleanup and success flow. + try: + # What: execute subprocess run service start args protected service check True timeout 180; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run(service + ["start", args.protected_service], check=True, timeout=180) + # What: execute status restoredHealth wait health args protected url health 300; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + status["restoredHealth"] = wait_health(args.protected_url + "/health", 300) + # What: evaluate and capture raw content canary args protected url protected model; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + raw, content = canary(args.protected_url, protected_model) + # What: execute artifacts restored json write bytes raw; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + (artifacts / "restored.json").write_bytes(raw) + # What: compute status entry from content and 4; why: print restored str status restored flush later reads status entry, so main must retain the computed value under that name. + status["restored"] = content == "4" + # What: execute print RESTORED str status restored flush True; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + print("RESTORED " + str(status["restored"]), flush=True) + # What: handle exception by status restore error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: + # What: compute status entry from repr and exc; why: artifacts result json write text json dumps status indent later reads status entry, so main must retain the computed value under that name. + status["restoreError"] = repr(exc) + # What: preserve the exact artifacts result json write text json dumps status indent literal fragment; why: main passes this fragment verbatim through (artifacts / "result.json").write_text(json.dumps(status, indent=2), enc, because changing it would alter a protocol payload, serialized fixture, or public mess. + (artifacts / "result.json").write_text(json.dumps(status, indent=2), encoding="utf-8") + # What: return status and 0 and 1 and passed and restored from main; why: main exposes status and 0 and 1 and passed and restored so its caller can continue with the function\'s computed outcome. + return 0 if status["passed"] and status["restored"] and "cleanupError" not in status else 1 + + +# What: gate on name before exit and sys and main; why: qualify_native_recovery admits exit and sys and main only for this predicate and excludes the opposite state. +if __name__ == "__main__": + # What: call sys.exit with main; why: qualify_native_recovery invokes sys.exit while performing the enclosing return; the call advances that operation through its result or side effect. + sys.exit(main()) diff --git a/benchmarks/swap/qualify_native_router.py b/benchmarks/swap/qualify_native_router.py new file mode 100644 index 000000000..4d27ad003 --- /dev/null +++ b/benchmarks/swap/qualify_native_router.py @@ -0,0 +1,2611 @@ +"""Opt-in native freetoken-swap routing benchmark for an approved Linux window. + +It keeps raw requests, responses, daemon logs, catalog paths, and host details +inside a newly created private artifact directory. It never changes protected +service enablement or configuration, and always attempts restoration after a +maintenance stop. This is evidence collection, not a production launcher. +""" +# What: document opt in native freetoken swap routing benchmark for in the qualify_native_router docstring; why: introspection and maintainers read this exact docstring fragment to understand qualify native router behavior without executing it. +# What: document it keeps raw requests responses daemon in the qualify_native_router docstring; why: introspection and maintainers read this exact docstring fragment to understand qualify native router behavior without executing it. +# What: document inside a newly created private artifact in the qualify_native_router docstring; why: introspection and maintainers read this exact docstring fragment to understand qualify native router behavior without executing it. +# What: document service enablement or configuration and always in the qualify_native_router docstring; why: introspection and maintainers read this exact docstring fragment to understand qualify native router behavior without executing it. +# What: document maintenance stop this is evidence collection in the qualify_native_router docstring; why: introspection and maintainers read this exact docstring fragment to understand qualify native router behavior without executing it. +# What: preserve the paragraph boundary in the the qualify_native_router docstring; why: introspection and maintainers read this paragraph break to understand qualify native router behavior without executing it. + +# What: enable postponed evaluation of annotations; why: type hints in qualify_native_router can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import argparse for main using argparse; why: main uses argparse argument parser, making that imported dependency available to its named operation. +import argparse +# What: import base64 for control plane canary using base64; why: control_plane_canary uses base64 b64encode, making that imported dependency available to its named operation. +import base64 +# What: import json for request json using json; why: request_json uses json loads, making that imported dependency available to its named operation. +import json +# What: import os for stop process group using os; why: stop_process_group uses os killpg, making that imported dependency available to its named operation. +import os +# What: import path for upstream model rewrite canary using pathlib and path; why: upstream_model_rewrite_canary uses the path annotation in upstream model rewrite canary, making that imported dependency available to its named operation. +from pathlib import Path +# What: import secrets for main using secrets; why: main uses secrets token urlsafe, making that imported dependency available to its named operation. +import secrets +# What: import signal for stop process group using signal; why: stop_process_group uses signal sigterm, making that imported dependency available to its named operation. +import signal +# What: import socket for require expected hostname using socket; why: require_expected_hostname uses socket gethostname, making that imported dependency available to its named operation. +import socket +# What: import subprocess for stop process group using subprocess; why: stop_process_group uses subprocess timeout expired, making that imported dependency available to its named operation. +import subprocess +# What: import sys for main using sys; why: main uses sys platform startswith, making that imported dependency available to its named operation. +import sys +# What: import threading for concurrent canaries using threading; why: concurrent_canaries uses threading lock, making that imported dependency available to its named operation. +import threading +# What: import time for canary using time; why: canary uses time monotonic, making that imported dependency available to its named operation. +import time +# What: import urllib error for request json using urllib and error; why: request_json uses urllib request request, making that imported dependency available to its named operation. +import urllib.error +# What: import urllib request for request json using urllib and request; why: request_json uses urllib request request, making that imported dependency available to its named operation. +import urllib.request + + +# What: compute native auth base from the named fixture input; why: global native auth base native api key later reads native auth base, so qualify_native_router must retain the computed value under that name. +_NATIVE_AUTH_BASE: str | None = None +# What: compute native api key from the named fixture input; why: global native auth base native api key later reads native api key, so qualify_native_router must retain the computed value under that name. +_NATIVE_API_KEY: str | None = None + + +# What: define require_expected_hostname and its declared inputs; why: callers use require_expected_hostname to perform the behavior named by this helper without duplicating its boundary checks. +def require_expected_hostname(expected: str, *, actual: str | None = None) -> str: + """Require an exact operator-supplied host without disclosing either name.""" + # What: document require an exact operator supplied host without in the require_expected_hostname docstring; why: introspection and maintainers read this exact docstring fragment to understand require expected hostname behavior without executing it. + # What: evaluate and capture actual socket gethostname if actual is None else actual; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + actual = socket.gethostname() if actual is None else actual + # What: gate on expected and actual before runtime error; why: require_expected_hostname admits runtime error only for this predicate and excludes the opposite state. + if not expected or "\x00" in expected or actual != expected: + # What: raise RuntimeError for the caller; why: require_expected_hostname stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError( + # What: execute qualification host does not match the operator supplied expected hostname; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + "qualification host does not match the operator-supplied expected hostname" + # What: complete the RuntimeError call with ordered positional inputs; why: require_expected_hostname groups the supplied clauses as one RuntimeError call before its value is consumed. + ) + # What: return actual from require_expected_hostname; why: require_expected_hostname exposes actual so its caller can continue with the function\'s computed outcome. + return actual + + +# What: define configure_native_auth around base and api key; why: its direct callers call configure_native_auth for configure native auth and rely on this exact input and result contract. +def configure_native_auth(base: str, api_key: str) -> None: + """Scope private router credentials to the exact temporary daemon origin.""" + # What: document scope private router credentials to the in the configure_native_auth docstring; why: introspection and maintainers read this exact docstring fragment to understand configure native auth behavior without executing it. + # What: apply the global native auth base native api key portion of the enclosing predicate; why: this clause remains in configure_native_auth\'s enclosing expression so its grouping and evaluation order stay intact. + global _NATIVE_AUTH_BASE, _NATIVE_API_KEY + # What: compute native auth base from rstrip and base and value; why: the enclosing return or state update later reads native auth base, so configure_native_auth must retain the computed value under that name. + _NATIVE_AUTH_BASE = base.rstrip("/") + # What: compute native api key from api key; why: the enclosing return or state update later reads native api key, so configure_native_auth must retain the computed value under that name. + _NATIVE_API_KEY = api_key + + +# What: define _native_headers around url; why: its direct callers call _native_headers for native headers and rely on this exact input and result contract. +def _native_headers(url: str) -> dict[str, str]: + # What: gate on native auth base and native api key and url and startswith before native api key; why: _native_headers admits native api key only for this predicate and excludes the opposite state. + if ( + # What: apply the native auth base is not portion of the enclosing predicate; why: this clause remains in _native_headers\'s enclosing expression so its grouping and evaluation order stay intact. + _NATIVE_AUTH_BASE is not None + # What: apply the and native api key is not portion of the enclosing predicate; why: this clause remains in _native_headers\'s enclosing expression so its grouping and evaluation order stay intact. + and _NATIVE_API_KEY is not None + # What: call url.startswith with native auth base and value; why: _native_headers consumes the url.startswith return value while evaluating and (url == _NATIVE_AUTH_BASE or url.startswith(_NATIVE_AUTH_BASE + "/"). + and (url == _NATIVE_AUTH_BASE or url.startswith(_NATIVE_AUTH_BASE + "/")) + # What: complete the enclosing predicate with if native auth base is not and native api key is not and; why: _native_headers groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: map the authorization field as native api key and bearer; why: _native_headers carries authorization into return {"Authorization": f"Bearer {_NATIVE_API_KEY}"}. + return {"Authorization": f"Bearer {_NATIVE_API_KEY}"} + # What: return no value from _native_headers; why: _native_headers returns no value to callers that depend on its completed result. + return {} + + +# What: define request_json around url and body and timeout and method; why: its direct callers call request_json for request json and rely on this exact input and result contract. +def request_json( + # What: declare the url input for request_json; why: request_json consumes url during url, so callers must bind it with the other signature inputs. + url: str, + # What: declare the body input for request_json; why: request_json consumes body during data if body is else json dumps, so callers must bind it with the other signature inputs. + body: dict | None = None, + # What: mark the remaining parameters as keyword-only; why: request_json prevents callers from confusing adjacent lifecycle and timing arguments. + *, + # What: declare the timeout input for request_json; why: request_json consumes timeout during with urllib request urlopen request timeout timeout as, so callers must bind it with the other signature inputs. + timeout: float = 30, + # What: declare the method input for request_json; why: request_json consumes method during method method, so callers must bind it with the other signature inputs. + method: str | None = None, +# What: complete the enclosing predicate collection with bytes and dict; why: request_json groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. +) -> tuple[bytes, dict]: + # What: compute data from body and encode and dumps and json and utf 8; why: data data later reads data, so request_json must retain the computed value under that name. + data = None if body is None else json.dumps(body).encode("utf-8") + # What: compute request from request and url and request and data; why: with urllib request urlopen request timeout timeout as later reads request, so request_json must retain the computed value under that name. + request = urllib.request.Request( + # What: apply the url portion of request; why: request_json uses this clause to evaluate request as one grouped value. + url, + # What: supply data to urllib.request.Request; why: request_json binds this data value to urllib.request.Request's data input. + data=data, + # What: map the content type field as application and json; why: request_json carries content type through request into with urllib request urlopen request timeout timeout as response. + headers={"Content-Type": "application/json", **_native_headers(url)}, + # What: supply method to urllib.request.Request; why: request_json binds this method value to urllib.request.Request's method input. + method=method, + # What: complete the urllib.request.Request call with data and headers and method; why: request_json groups the supplied clauses as one urllib.request.Request call before its value is consumed. + ) + # What: enter the urllib.request.urlopen managed context before raw response read; why: request_json releases this resource or lock after raw response read on both success and failure paths. + with urllib.request.urlopen(request, timeout=timeout) as response: + # What: compute raw from read and response; why: return raw json loads raw later reads raw, so request_json must retain the computed value under that name. + raw = response.read() + # What: return raw and loads and json from request_json; why: request_json exposes raw and loads and json so its caller can continue with the function\'s computed outcome. + return raw, json.loads(raw) + + +# What: define request_bytes around url and timeout; why: its direct callers call request_bytes for request bytes and rely on this exact input and result contract. +def request_bytes(url: str, *, timeout: float = 30) -> bytes: + # What: compute request from request and url and request and urllib; why: with urllib request urlopen request timeout timeout as later reads request, so request_bytes must retain the computed value under that name. + request = urllib.request.Request(url, headers=_native_headers(url)) + # What: enter the urllib.request.urlopen managed context before return response read; why: request_bytes releases this resource or lock after return response read on both success and failure paths. + with urllib.request.urlopen(request, timeout=timeout) as response: + # What: return read and response from request_bytes; why: request_bytes exposes read and response so its caller can continue with the function\'s computed outcome. + return response.read() + + +# What: define wait_json around url and seconds; why: its direct callers call wait_json for wait json and rely on this exact input and result contract. +def wait_json(url: str, *, seconds: float) -> dict: + # What: compute deadline from seconds and monotonic and time; why: while time monotonic deadline later reads deadline, so wait_json must retain the computed value under that name. + deadline = time.monotonic() + seconds + # What: compute last from the named fixture input; why: last exc later reads last, so wait_json must retain the computed value under that name. + last: Exception | None = None + # What: iterate across deadline and monotonic and time to perform oserror and value error and httperror and last and exc; why: wait_json repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: establish the handler boundary for the protected operation; why: wait_json routes failures to oserror and value error and httperror and error and urllib while preserving cleanup and success flow. + try: + # What: return request json and url and 1 and 3 from wait_json; why: wait_json exposes request json and url and 1 and 3 so its caller can continue with the function\'s computed outcome. + return request_json(url, timeout=3)[1] + # What: handle oserror and value error and httperror and error and urllib by last exc; why: wait_json converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, ValueError, urllib.error.HTTPError) as exc: + # What: compute last from exc; why: raise timeout error f endpoint did not later reads last, so wait_json must retain the computed value under that name. + last = exc + # What: call time.sleep with 0 25; why: wait_json invokes time.sleep while performing raise timeout error f endpoint did not; the call advances that operation through its result or side effect. + time.sleep(0.25) + # What: raise TimeoutError for the caller; why: wait_json stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError(f"endpoint did not become available: {last!r}") + + +# What: define canary around url and model and direct; why: its direct callers call canary for canary and rely on this exact input and result contract. +def canary(url: str, model: str, *, direct: bool) -> tuple[bytes, dict]: + """Make one deterministic request and retain raw bytes only in private artifacts.""" + # What: document make one deterministic request and retain in the canary docstring; why: introspection and maintainers read this exact docstring fragment to understand canary behavior without executing it. + # What: compute body from model and model and messages and temperature and max tokens; why: data json dumps body encode utf 8 later reads body, so canary must retain the computed value under that name. + body = { + # What: map the model field as model; why: canary sends this field through body so the router selects the canonical model or alias for upstream dispatch. + "model": model, + # What: map the role field as user; why: canary carries role through body into data json dumps body encode utf 8. + "messages": [{"role": "user", "content": "What is 2 + 2? Reply with only the single digit."}], + # What: map the temperature field as 0; why: canary carries temperature through body into data json dumps body encode utf 8. + "temperature": 0, + # What: map the max tokens field as 32; why: canary carries max tokens through body into data json dumps body encode utf 8. + "max_tokens": 32, + # What: map the stream field as true; why: canary carries stream through body into data json dumps body encode utf 8. + "stream": True, + # What: map the include usage field as true; why: canary carries include usage through body into data json dumps body encode utf 8. + "stream_options": {"include_usage": True}, + # What: map the enable thinking field as false; why: canary carries enable thinking through body into data json dumps body encode utf 8. + "chat_template_kwargs": {"enable_thinking": False}, + # What: complete the body mapping with model and messages and temperature and max tokens and stream; why: canary groups the supplied clauses as one body mapping before its value is consumed. + } + # What: compute request from request and request and url and urllib; why: with urllib request urlopen request timeout as response later reads request, so canary must retain the computed value under that name. + request = urllib.request.Request( + # What: apply the url v1 chat completions portion of request; why: canary uses this clause to evaluate request as one grouped value. + url + "/v1/chat/completions", + # What: supply data to operation.encode; why: canary binds this encode and dumps and body and json and utf 8 value to operation.encode's data input. + data=json.dumps(body).encode("utf-8"), + # What: map the content type field as application and json; why: canary carries content type through request into with urllib request urlopen request timeout 660 as response. + headers={"Content-Type": "application/json", **_native_headers(url)}, + # What: complete the urllib.request.Request call with data and headers; why: canary groups the supplied clauses as one urllib.request.Request call before its value is consumed. + ) + # What: compute raw from bytearray; why: raw extend chunk later reads raw, so canary must retain the computed value under that name. + raw = bytearray() + # What: initialize content as an empty runtime accumulator; why: canary appends or maps entries into it during value choice get delta get content or before consuming the aggregate. + content: list[str] = [] + # What: compute started from monotonic and time; why: observed s time monotonic started later reads started, so canary must retain the computed value under that name. + started = time.monotonic() + # What: compute first byte s from the named fixture input; why: if first byte s is later reads first byte s, so canary must retain the computed value under that name. + first_byte_s: float | None = None + # What: compute first token s from the named fixture input; why: if value and first token s is later reads first token s, so canary must retain the computed value under that name. + first_token_s: float | None = None + # What: compute completion tokens from the named fixture input; why: if isinstance usage dict and isinstance later reads completion tokens, so canary must retain the computed value under that name. + completion_tokens: int | None = None + # What: compute response models from set; why: response models add response model later reads response models, so canary must retain the computed value under that name. + response_models: set[str] = set() + # What: enter the urllib.request.urlopen managed context before for chunk in response; why: canary releases this resource or lock after for chunk in response on both success and failure paths. + with urllib.request.urlopen(request, timeout=660) as response: + # What: iterate across response to perform observed s and float; why: canary repeats the body only while or for the loop header admits an iteration. + for chunk in response: + # What: compute observed s from the named fixture input; why: observed s time monotonic started later reads observed s, so canary must retain the computed value under that name. + observed_s: float | None = None + # What: gate on first byte s before observed s and started and monotonic and time; why: canary admits observed s and started and monotonic and time only for this predicate and excludes the opposite state. + if first_byte_s is None: + # What: compute observed s from started and monotonic and time; why: first byte s observed s later reads observed s, so canary must retain the computed value under that name. + observed_s = time.monotonic() - started + # What: compute first byte s from observed s; why: if first byte s is or first token s is later reads first byte s, so canary must retain the computed value under that name. + first_byte_s = observed_s + # What: call raw.extend with chunk; why: canary invokes raw.extend while performing if len raw; the call advances that operation through its result or side effect. + raw.extend(chunk) + # What: gate on len and raw before runtime error; why: canary admits runtime error only for this predicate and excludes the opposite state. + if len(raw) > 8 * 1024 * 1024: + # What: raise RuntimeError for the caller; why: canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("canary response exceeded private capture bound") + # What: gate on startswith and chunk and strip before event and loads and json and chunk; why: canary admits event and loads and json and chunk only for this predicate and excludes the opposite state. + if chunk.startswith(b"data: ") and chunk.strip() != b"data: [DONE]": + # What: compute event from loads and json and chunk and 6; why: response model event get model later reads event, so canary must retain the computed value under that name. + event = json.loads(chunk[6:]) + # What: compute response model from get and event and model; why: if isinstance response model str later reads response model, so canary must retain the computed value under that name. + response_model = event.get("model") + # What: gate on isinstance and response model and str before add and response model and response models; why: canary admits add and response model and response models only for this predicate and excludes the opposite state. + if isinstance(response_model, str): + # What: call response_models.add with response model; why: canary invokes response_models.add while performing usage event get usage; the call advances that operation through its result or side effect. + response_models.add(response_model) + # What: compute usage from get and event and usage; why: if isinstance usage dict and isinstance later reads usage, so canary must retain the computed value under that name. + usage = event.get("usage") + # What: gate on isinstance and usage and dict and int and get before completion tokens and usage; why: canary admits completion tokens and usage only for this predicate and excludes the opposite state. + if isinstance(usage, dict) and isinstance(usage.get("completion_tokens"), int): + # What: compute completion tokens from usage and completion tokens; why: if not isinstance completion tokens int or later reads completion tokens, so canary must retain the computed value under that name. + completion_tokens = usage["completion_tokens"] + # What: iterate across get and event to perform value and get and choice; why: canary repeats the body only while or for the loop header admits an iteration. + for choice in event.get("choices", []): + # What: compute value from get and choice and value and content and delta; why: if value and first token s is later reads value, so canary must retain the computed value under that name. + value = choice.get("delta", {}).get("content") or "" + # What: gate on value and first token s before observed s and started and monotonic and time; why: canary admits observed s and started and monotonic and time only for this predicate and excludes the opposite state. + if value and first_token_s is None: + # What: gate on observed s before observed s and started and monotonic and time; why: canary admits observed s and started and monotonic and time only for this predicate and excludes the opposite state. + if observed_s is None: + # What: compute observed s from started and monotonic and time; why: first token s observed s later reads observed s, so canary must retain the computed value under that name. + observed_s = time.monotonic() - started + # What: compute first token s from observed s; why: if first byte s is or first token s is later reads first token s, so canary must retain the computed value under that name. + first_token_s = observed_s + # What: call content.append with value; why: canary invokes content.append while performing duration s time monotonic started; the call advances that operation through its result or side effect. + content.append(value) + # What: compute duration s from started and monotonic and time; why: if first byte s is or first token s is later reads duration s, so canary must retain the computed value under that name. + duration_s = time.monotonic() - started + # What: compute answer from strip and join and content and value; why: if answer later reads answer, so canary must retain the computed value under that name. + answer = "".join(content).strip() + # What: gate on raw before runtime error; why: canary admits runtime error only for this predicate and excludes the opposite state. + if b"data: [DONE]" not in raw: + # What: raise RuntimeError for the caller; why: canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("SSE completion marker missing") + # What: gate on answer before runtime error; why: canary admits runtime error only for this predicate and excludes the opposite state. + if answer != "4": + # What: raise RuntimeError for the caller; why: canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("deterministic quality gate failed") + # What: gate on completion tokens and isinstance and int before runtime error; why: canary admits runtime error only for this predicate and excludes the opposite state. + if not isinstance(completion_tokens, int) or completion_tokens <= 0: + # What: raise RuntimeError for the caller; why: canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("streamed completion usage missing") + # What: gate on first byte s and first token s and duration s before runtime error; why: canary admits runtime error only for this predicate and excludes the opposite state. + if first_byte_s is None or first_token_s is None or duration_s <= first_token_s: + # What: raise RuntimeError for the caller; why: canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("stream timing did not permit token-throughput measurement") + # What: compute decode s from duration s and first token s; why: completion tokens per second completion tokens decode s later reads decode s, so canary must retain the computed value under that name. + decode_s = duration_s - first_token_s + # What: compute completion tokens per second from completion tokens and decode s; why: completion tokens per second completion tokens per second later reads completion tokens per second, so canary must retain the computed value under that name. + completion_tokens_per_second = completion_tokens / decode_s + # What: gate on len and response models before runtime error; why: canary admits runtime error only for this predicate and excludes the opposite state. + if len(response_models) > 1: + # What: raise RuntimeError for the caller; why: canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("SSE completion reported inconsistent upstream model names") + # What: return bytes and raw and model and first byte s from canary; why: canary exposes bytes and raw and model and first byte s so its caller can continue with the function\'s computed outcome. + return bytes(raw), { + # What: map the route field as direct and direct and native router; why: canary carries route into "route": "direct" if direct else "native_router". + "route": "direct" if direct else "native_router", + # What: map the model field as model; why: canary sends this field through "model": model so the router selects the canonical model or alias for upstream dispatch. + "model": model, + # What: map the response model field as next and iter and response models; why: canary carries response model into "responseModel": next(iter(response_models), None). + "responseModel": next(iter(response_models), None), + # What: map the first byte seconds field as first byte s; why: canary carries first byte seconds into "firstByteSeconds": first_byte_s. + "firstByteSeconds": first_byte_s, + # What: map the first token seconds field as first token s; why: canary carries first token seconds into "firstTokenSeconds": first_token_s. + "firstTokenSeconds": first_token_s, + # What: map the duration seconds field as duration s; why: canary carries duration seconds into "durationSeconds": duration_s. + "durationSeconds": duration_s, + # What: map the decode seconds field as decode s; why: canary carries decode seconds into "decodeSeconds": decode_s. + "decodeSeconds": decode_s, + # What: map the completion tokens field as completion tokens; why: canary carries completion tokens into "completionTokens": completion_tokens. + "completionTokens": completion_tokens, + # What: map the completion tokens per second field as completion tokens per second; why: canary carries completion tokens per second into "completionTokensPerSecond": completion_tokens_per_second. + "completionTokensPerSecond": completion_tokens_per_second, + # What: map the response bytes field as len and raw; why: canary carries response bytes into "responseBytes": len(raw). + "responseBytes": len(raw), + # What: map the passed field as true; why: canary carries passed into "passed": True. + "passed": True, + # What: complete the enclosing predicate collection with bytes and raw and model and first byte s and first token s and duration s; why: canary groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + } + + +# What: define upstream_model_rewrite_canary around base and artifacts; why: its direct callers call upstream_model_rewrite_canary for upstream model rewrite canary and rely on this exact input and result contract. +def upstream_model_rewrite_canary(base: str, artifacts: Path) -> dict: + """Prove an alias is rewritten upstream without changing routing identity.""" + # What: document prove an alias is rewritten upstream in the upstream_model_rewrite_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand upstream model rewrite canary behavior without executing it. + # What: compute and before from request json and base and router and status; why: value after request json base router status later reads and before, so upstream_model_rewrite_canary must retain the computed value under that name. + _, before = request_json(base + "/router/status") + # What: compute prior activations from get and before and activations; why: if before get active profile model a or not later reads prior activations, so upstream_model_rewrite_canary must retain the computed value under that name. + prior_activations = before.get("activations") + # What: gate on get and isinstance and prior activations and int and before before runtime error; why: upstream_model_rewrite_canary admits runtime error only for this predicate and excludes the opposite state. + if before.get("activeProfile") != "model-a" or not isinstance(prior_activations, int): + # What: raise RuntimeError for the caller; why: upstream_model_rewrite_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("upstream model rewrite canary requires resident model-a") + # What: compute raw and completion from canary and base and compat and model a and false; why: artifacts upstream model rewrite sse write bytes raw later reads raw and completion, so upstream_model_rewrite_canary must retain the computed value under that name. + raw, completion = canary(base, "compat/model-a", direct=False) + # What: compute and after from request json and base and router and status; why: the enclosing return or state update later reads and after, so upstream_model_rewrite_canary must retain the computed value under that name. + _, after = request_json(base + "/router/status") + # What: gate on prior activations and get and completion and after before runtime error; why: upstream_model_rewrite_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call completion.get with response model; why: upstream_model_rewrite_canary invokes completion.get while performing or after get active profile model a; the call advances that operation through its result or side effect. + completion.get("responseModel") != "model-a" + # What: call after.get with active profile; why: upstream_model_rewrite_canary invokes after.get while performing or after get active requests; the call advances that operation through its result or side effect. + or after.get("activeProfile") != "model-a" + # What: call after.get with active requests; why: upstream_model_rewrite_canary invokes after.get while performing or after get activations prior activations; the call advances that operation through its result or side effect. + or after.get("activeRequests") != 0 + # What: call after.get with activations; why: upstream_model_rewrite_canary consumes the after.get return value while evaluating or after.get("activations") != prior_activations. + or after.get("activations") != prior_activations + # What: complete the enclosing predicate with if completion get response model differs from model a or after get active profile; why: upstream_model_rewrite_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: upstream_model_rewrite_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("alias was not rewritten upstream with stable routing residency") + # What: preserve the exact artifacts upstream model rewrite sse write bytes raw literal fragment; why: upstream_model_rewrite_canary passes this fragment verbatim through (artifacts / "upstream-model-rewrite.sse").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public m. + (artifacts / "upstream-model-rewrite.sse").write_bytes(raw) + # What: return; why: the caller consumes this value as the function’s success-path result. + return { + # What: map the requested model field as compat and model a; why: upstream_model_rewrite_canary carries requested model into "requestedModel": "compat/model-a". + "requestedModel": "compat/model-a", + # What: map the upstream response model field as model a; why: upstream_model_rewrite_canary carries upstream response model into "upstreamResponseModel": "model-a". + "upstreamResponseModel": "model-a", + # What: map the resident profile field as model a; why: upstream_model_rewrite_canary carries resident profile into "residentProfile": "model-a". + "residentProfile": "model-a", + # What: map the activation delta field as 0; why: upstream_model_rewrite_canary carries activation delta into "activationDelta": 0. + "activationDelta": 0, + # What: map the passed field as true; why: upstream_model_rewrite_canary carries passed into "passed": True. + "passed": True, + # What: complete the enclosing predicate mapping with requested model and upstream response model and resident profile and activation delta and passed; why: upstream_model_rewrite_canary groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + +# What: define validate_loading_feedback around raw and expected; why: its direct callers call validate_loading_feedback for validate loading feedback and rely on this exact input and result contract. +def validate_loading_feedback(raw: bytes, *, expected: bool) -> dict: + """Require the private SSE capture to match the expected router loading state.""" + # What: document require the private sse capture to in the validate_loading_feedback docstring; why: introspection and maintainers read this exact docstring fragment to understand validate loading feedback behavior without executing it. + # What: initialize reasoning as an empty runtime accumulator; why: validate_loading_feedback appends or maps entries into it during reasoning append value before consuming the aggregate. + reasoning: list[str] = [] + # What: iterate across splitlines and raw to perform line and startswith; why: validate_loading_feedback repeats the body only while or for the loop header admits an iteration. + for line in raw.splitlines(): + # What: gate on line and startswith before the computed value; why: validate_loading_feedback admits the computed value only for this predicate and excludes the opposite state. + if not line.startswith(b"data: ") or line == b"data: [DONE]": + # What: apply the continue portion of the enclosing predicate; why: this clause remains in validate_loading_feedback\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: establish the handler boundary for the protected operation; why: validate_loading_feedback routes failures to unicode decode error and jsondecode error and json while preserving cleanup and success flow. + try: + # What: compute event from loads and json and line and 6; why: for choice in event get choices later reads event, so validate_loading_feedback must retain the computed value under that name. + event = json.loads(line[6:]) + # What: handle unicode decode error and jsondecode error and json by continue; why: validate_loading_feedback converts that failure into this concrete recovery, response, or cleanup behavior. + except (UnicodeDecodeError, json.JSONDecodeError): + # What: apply the continue portion of the enclosing predicate; why: this clause remains in validate_loading_feedback\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: iterate across get and event to perform delta and isinstance and choice and dict and get; why: validate_loading_feedback repeats the body only while or for the loop header admits an iteration. + for choice in event.get("choices", []): + # What: compute delta from isinstance and choice and dict and get and delta; why: value delta get reasoning content if isinstance delta later reads delta, so validate_loading_feedback must retain the computed value under that name. + delta = choice.get("delta", {}) if isinstance(choice, dict) else {} + # What: compute value from isinstance and delta and dict and get and reasoning content; why: if isinstance value str later reads value, so validate_loading_feedback must retain the computed value under that name. + value = delta.get("reasoning_content") if isinstance(delta, dict) else None + # What: gate on isinstance and value and str before append and value and reasoning; why: validate_loading_feedback admits append and value and reasoning only for this predicate and excludes the opposite state. + if isinstance(value, str): + # What: call reasoning.append with value; why: validate_loading_feedback invokes reasoning.append while performing combined join reasoning; the call advances that operation through its result or side effect. + reasoning.append(value) + # What: compute combined from join and reasoning and value; why: observed freetoken swap loading model in combined later reads combined, so validate_loading_feedback must retain the computed value under that name. + combined = "".join(reasoning) + # What: compute observed from combined and freetoken swap and loading and model; why: if observed expected later reads observed, so validate_loading_feedback must retain the computed value under that name. + observed = "freetoken-swap loading model:" in combined + # What: gate on observed and expected before state and expected; why: validate_loading_feedback admits state and expected only for this predicate and excludes the opposite state. + if observed != expected: + # What: compute state from expected and missing and unexpected; why: raise runtime error f router loading feedback later reads state, so validate_loading_feedback must retain the computed value under that name. + state = "missing" if expected else "unexpected" + # What: raise RuntimeError for the caller; why: validate_loading_feedback stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError(f"router loading feedback was {state} for this qualification trial") + # What: map the expected field as expected; why: validate_loading_feedback carries expected into return {"expected": expected, "observed": observed, "passed": True}. + return {"expected": expected, "observed": observed, "passed": True} + + +# What: define concurrent_canaries around base and model and seconds; why: its direct callers call concurrent_canaries for concurrent canaries and rely on this exact input and result contract. +def concurrent_canaries(base: str, model: str, *, seconds: float = 180) -> tuple[list[tuple[bytes, dict]], dict]: + """Run two same-profile streams and prove they did not trigger a model swap.""" + # What: document run two same profile streams and prove in the concurrent_canaries docstring; why: introspection and maintainers read this exact docstring fragment to understand concurrent canaries behavior without executing it. + # What: compute and before from request json and base and router and status; why: value after request json base router status later reads and before, so concurrent_canaries must retain the computed value under that name. + _, before = request_json(base + "/router/status") + # What: compute prior activations from get and before and activations; why: if before get active profile model or not later reads prior activations, so concurrent_canaries must retain the computed value under that name. + prior_activations = before.get("activations") + # What: gate on model and get and isinstance and prior activations and int before runtime error; why: concurrent_canaries admits runtime error only for this predicate and excludes the opposite state. + if before.get("activeProfile") != model or not isinstance(prior_activations, int): + # What: raise RuntimeError for the caller; why: concurrent_canaries stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("same-model concurrency requires an already active profile") + # What: initialize results as an empty runtime accumulator; why: concurrent_canaries appends or maps entries into it during results append value before consuming the aggregate. + results: list[tuple[bytes, dict]] = [] + # What: initialize errors as an empty runtime accumulator; why: concurrent_canaries appends or maps entries into it during errors append exc before consuming the aggregate. + errors: list[BaseException] = [] + # What: compute lock from lock and threading; why: with lock later reads lock, so concurrent_canaries must retain the computed value under that name. + lock = threading.Lock() + # What: compute gate from barrier and threading and 3; why: gate wait timeout seconds later reads gate, so concurrent_canaries must retain the computed value under that name. + gate = threading.Barrier(3) + + # What: define run_one around the current object state; why: its direct callers call run_one for run one and rely on this exact input and result contract. + def run_one() -> None: + # What: establish the handler boundary for the protected operation; why: run_one routes failures to base exception while preserving cleanup and success flow. + try: + # What: supply timeout to gate.wait; why: run_one binds this seconds value to gate.wait's timeout input. + gate.wait(timeout=seconds) + # What: compute value from canary and base and model and false; why: results append value later reads value, so run_one must retain the computed value under that name. + value = canary(base, model, direct=False) + # What: enter the lock managed context before results append value; why: run_one releases this resource or lock after results append value on both success and failure paths. + with lock: + # What: call results.append with value; why: run_one invokes results.append while performing except base exception as exc; the call advances that operation through its result or side effect. + results.append(value) + # What: handle base exception by with lock; why: run_one converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException as exc: + # What: enter the lock managed context before errors append exc; why: run_one releases this resource or lock after errors append exc on both success and failure paths. + with lock: + # What: call errors.append with exc; why: run_one invokes errors.append while performing the enclosing return; the call advances that operation through its result or side effect. + errors.append(exc) + + # What: compute workers from thread and index and threading and run one; why: for worker in workers later reads workers, so concurrent_canaries must retain the computed value under that name. + workers = [threading.Thread(target=run_one, name=f"native-router-concurrent-{index}", daemon=True) + # What: call range with 2; why: concurrent_canaries invokes range while performing for worker in workers; the call advances that operation through its result or side effect. + for index in range(2)] + # What: iterate across workers to perform start and worker; why: concurrent_canaries repeats the body only while or for the loop header admits an iteration. + for worker in workers: + # What: call worker.start with the declared inputs; why: concurrent_canaries invokes worker.start while performing gate wait timeout seconds; the call advances that operation through its result or side effect. + worker.start() + # What: supply timeout to gate.wait; why: concurrent_canaries binds this seconds value to gate.wait's timeout input. + gate.wait(timeout=seconds) + # What: iterate across workers to perform join and seconds and worker; why: concurrent_canaries repeats the body only while or for the loop header admits an iteration. + for worker in workers: + # What: call worker.join with seconds; why: concurrent_canaries invokes worker.join while performing if any worker is alive for worker in; the call advances that operation through its result or side effect. + worker.join(seconds) + # What: gate on any and is alive and worker and workers before timeout error; why: concurrent_canaries admits timeout error only for this predicate and excludes the opposite state. + if any(worker.is_alive() for worker in workers): + # What: raise TimeoutError for the caller; why: concurrent_canaries stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError("same-model concurrent streams did not finish") + # What: gate on errors before runtime error and errors; why: concurrent_canaries admits runtime error and errors only for this predicate and excludes the opposite state. + if errors: + # What: raise RuntimeError for the caller; why: concurrent_canaries stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("same-model concurrent stream failed") from errors[0] + # What: compute and after from request json and base and router and status; why: or not all row get passed is later reads and after, so concurrent_canaries must retain the computed value under that name. + _, after = request_json(base + "/router/status") + # What: gate on model and prior activations and len and results and all before runtime error; why: concurrent_canaries admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call len with results; why: concurrent_canaries invokes len while performing or not all row get passed is; the call advances that operation through its result or side effect. + len(results) != 2 + # What: call all with results and get and value and row and true; why: concurrent_canaries invokes all while performing or after get active requests; the call advances that operation through its result or side effect. + or not all(row.get("passed") is True for _, row in results) + # What: call after.get with active requests; why: concurrent_canaries invokes after.get while performing or after get active profile model; the call advances that operation through its result or side effect. + or after.get("activeRequests") != 0 + # What: call after.get with active profile; why: concurrent_canaries invokes after.get while performing or after get activations prior activations; the call advances that operation through its result or side effect. + or after.get("activeProfile") != model + # What: call after.get with activations; why: concurrent_canaries consumes the after.get return value while evaluating or after.get("activations") != prior_activations. + or after.get("activations") != prior_activations + # What: complete the enclosing predicate with if len results differs from 2 or not all; why: concurrent_canaries groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: concurrent_canaries stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("same-model concurrency changed native routing residency") + # What: return results and model and route and model and requests from concurrent_canaries; why: concurrent_canaries exposes results and model and route and model and requests so its caller can continue with the function\'s computed outcome. + return results, { + # What: map the route field as native router; why: concurrent_canaries carries route into "route": "native_router". + "route": "native_router", + # What: map the model field as model; why: concurrent_canaries sends this field through "model": model so the router selects the canonical model or alias for upstream dispatch. + "model": model, + # What: map the requests field as 2; why: concurrent_canaries carries requests into "requests": 2. + "requests": 2, + # What: map the activation delta field as 0; why: concurrent_canaries carries activation delta into "activationDelta": 0. + "activationDelta": 0, + # What: map the active requests after field as 0; why: concurrent_canaries carries active requests after into "activeRequestsAfter": 0. + "activeRequestsAfter": 0, + # What: map the passed field as true; why: concurrent_canaries carries passed into "passed": True. + "passed": True, + # What: complete the enclosing predicate collection with results and model and route and model and requests and activation delta; why: concurrent_canaries groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + } + + +# What: define cancellation_canary around base and model and seconds; why: its direct callers call cancellation_canary for cancellation canary and rely on this exact input and result contract. +def cancellation_canary(base: str, model: str, *, seconds: float = 90) -> tuple[bytes, dict]: + """Prove native router cancellation reaches idle without a normal completion credit. + + The raw partial SSE remains a private artifact. The returned observation is + deliberately limited to lifecycle counters and timing-safe booleans. + """ + # What: document prove native router cancellation reaches idle in the cancellation_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand cancellation canary behavior without executing it. + # What: document the raw partial sse remains a in the cancellation_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand cancellation canary behavior without executing it. + # What: document deliberately limited to lifecycle counters and in the cancellation_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand cancellation canary behavior without executing it. + # What: preserve the paragraph boundary in the the cancellation_canary docstring; why: introspection and maintainers read this paragraph break to understand cancellation canary behavior without executing it. + # What: compute request id from native qualification cancel; why: content type application json x ft request id request id later reads request id, so cancellation_canary must retain the computed value under that name. + request_id = "native-qualification-cancel" + # What: compute and before from request json and base and router and status; why: value cancelled request json base f router later reads and before, so cancellation_canary must retain the computed value under that name. + _, before = request_json(base + "/router/status") + # What: compute prior cancellations from get and before and cancellations; why: if not isinstance prior cancellations int or later reads prior cancellations, so cancellation_canary must retain the computed value under that name. + prior_cancellations = before.get("cancellations") + # What: compute prior terminal from get and before and terminal streams; why: if not isinstance prior cancellations int or later reads prior terminal, so cancellation_canary must retain the computed value under that name. + prior_terminal = before.get("terminalStreams") + # What: gate on isinstance and prior cancellations and int and prior terminal before runtime error; why: cancellation_canary admits runtime error only for this predicate and excludes the opposite state. + if not isinstance(prior_cancellations, int) or not isinstance(prior_terminal, int): + # What: raise RuntimeError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router status lacks cancellation counters") + # What: compute body from model and model and messages and temperature and max tokens; why: base v1 chat completions data json dumps later reads body, so cancellation_canary must retain the computed value under that name. + body = { + # What: map the model field as model; why: cancellation_canary sends this field through body so the router selects the canonical model or alias for upstream dispatch. + "model": model, + # What: map the role field as user; why: cancellation_canary carries role through body into base v1 chat completions data json dumps body. + "messages": [{"role": "user", "content": "Count upward slowly and do not stop."}], + # What: map the temperature field as 0; why: cancellation_canary carries temperature through body into base v1 chat completions data json dumps body. + "temperature": 0, + # What: map the max tokens field as 2048; why: cancellation_canary carries max tokens through body into base v1 chat completions data json dumps body. + "max_tokens": 2048, + # What: map the stream field as true; why: cancellation_canary carries stream through body into base v1 chat completions data json dumps body. + "stream": True, + # What: complete the body mapping with model and messages and temperature and max tokens and stream; why: cancellation_canary groups the supplied clauses as one body mapping before its value is consumed. + } + # What: compute request from request and request and base and urllib; why: with urllib request urlopen request timeout seconds as later reads request, so cancellation_canary must retain the computed value under that name. + request = urllib.request.Request( + # What: supply data to operation.encode; why: cancellation_canary binds this encode and dumps and body and json and utf 8 value to operation.encode's data input. + base + "/v1/chat/completions", data=json.dumps(body).encode("utf-8"), + # What: supply headers to urllib.request.Request; why: cancellation_canary binds this request id and native headers and base and content type and x ft request id value to urllib.request.Request's headers input. + headers={ + # What: map the content type field as application and json; why: cancellation_canary carries content type through request into with urllib request urlopen request timeout seconds as response. + "Content-Type": "application/json", "X-FT-Request-ID": request_id, + # What: call _native_headers with base; why: cancellation_canary consumes the _native_headers return value while evaluating **_native_headers(base). + **_native_headers(base), + # What: complete the request mapping with content type and x ft request id; why: cancellation_canary groups the supplied clauses as one request mapping before its value is consumed. + }, + # What: complete the urllib.request.Request call with data and headers; why: cancellation_canary groups the supplied clauses as one urllib.request.Request call before its value is consumed. + ) + # What: compute raw from bytearray; why: raw extend chunk later reads raw, so cancellation_canary must retain the computed value under that name. + raw = bytearray() + # What: compute first chunk from event and threading; why: first chunk set later reads first chunk, so cancellation_canary must retain the computed value under that name. + first_chunk = threading.Event() + # What: compute finished from event and threading; why: finished set later reads finished, so cancellation_canary must retain the computed value under that name. + finished = threading.Event() + # What: initialize errors as an empty runtime accumulator; why: cancellation_canary appends or maps entries into it during errors append exc before consuming the aggregate. + errors: list[BaseException] = [] + + # What: define consume around the current object state; why: its direct callers call consume for consume and rely on this exact input and result contract. + def consume() -> None: + # What: establish the handler boundary for the protected operation; why: consume routes failures to exception while preserving cleanup and success flow. + try: + # What: enter the urllib.request.urlopen managed context before for chunk in response; why: consume releases this resource or lock after for chunk in response on both success and failure paths. + with urllib.request.urlopen(request, timeout=seconds) as response: + # What: iterate across response to perform extend and chunk and raw; why: consume repeats the body only while or for the loop header admits an iteration. + for chunk in response: + # What: call raw.extend with chunk; why: consume invokes raw.extend while performing first chunk set; the call advances that operation through its result or side effect. + raw.extend(chunk) + # What: call first_chunk.set with the declared inputs; why: consume invokes first_chunk.set while performing except exception as exc cancellation may; the call advances that operation through its result or side effect. + first_chunk.set() + # What: handle exception by errors append exc; why: consume converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: # cancellation may close a blocking HTTP read + # What: call errors.append with exc; why: consume invokes errors.append while performing finally; the call advances that operation through its result or side effect. + errors.append(exc) + # What: run finished set on every exit path; why: consume performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: call finished.set with the declared inputs; why: consume invokes finished.set while performing the enclosing return; the call advances that operation through its result or side effect. + finished.set() + + # What: compute worker from thread and threading and consume and native router cancel and true; why: worker start later reads worker, so cancellation_canary must retain the computed value under that name. + worker = threading.Thread(target=consume, name="native-router-cancel", daemon=True) + # What: compute started from monotonic and time; why: duration seconds time monotonic started later reads started, so cancellation_canary must retain the computed value under that name. + started = time.monotonic() + # What: call worker.start with the declared inputs; why: cancellation_canary invokes worker.start while performing if not first chunk wait seconds; the call advances that operation through its result or side effect. + worker.start() + # What: gate on wait and seconds and first chunk before timeout error; why: cancellation_canary admits timeout error only for this predicate and excludes the opposite state. + if not first_chunk.wait(seconds): + # What: raise TimeoutError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError("cancellation stream produced no first chunk") + # What: compute and cancelled from request json and base and request id and 30 and router; why: the enclosing return or state update later reads and cancelled, so cancellation_canary must retain the computed value under that name. + _, cancelled = request_json(base + f"/router/requests/{request_id}/cancel", {}, timeout=30) + # What: map the cancelled field as true; why: cancellation_canary carries cancelled through if cancelled != {"cancelled": True, "id": request_id} into raise runtime error router did not acknowledge the. + if cancelled != {"cancelled": True, "id": request_id}: + # What: raise RuntimeError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router did not acknowledge the active cancellation request") + # What: gate on wait and seconds and finished before timeout error; why: cancellation_canary admits timeout error only for this predicate and excludes the opposite state. + if not finished.wait(seconds): + # What: raise TimeoutError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError("cancelled stream did not close") + # What: compute deadline from seconds and monotonic and time; why: while time monotonic deadline later reads deadline, so cancellation_canary must retain the computed value under that name. + deadline = time.monotonic() + seconds + # What: compute status from the named fixture input; why: status request json base router status timeout later reads status, so cancellation_canary must retain the computed value under that name. + status: dict | None = None + # What: iterate across deadline and monotonic and time to perform status and request json and base; why: cancellation_canary repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: compute status from request json and base and 1 and router and status; why: if status get active requests later reads status, so cancellation_canary must retain the computed value under that name. + status = request_json(base + "/router/status", timeout=3)[1] + # What: gate on get and status before the computed value; why: cancellation_canary admits the computed value only for this predicate and excludes the opposite state. + if status.get("activeRequests") == 0: + # What: apply the break portion of the enclosing predicate; why: this clause remains in cancellation_canary\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: call time.sleep with 0 1; why: cancellation_canary invokes time.sleep while performing if status is or status get active requests; the call advances that operation through its result or side effect. + time.sleep(0.1) + # What: gate on status and get before timeout error; why: cancellation_canary admits timeout error only for this predicate and excludes the opposite state. + if status is None or status.get("activeRequests") != 0: + # What: raise TimeoutError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError("router did not return to idle after cancellation") + # What: gate on get and prior cancellations and status before runtime error; why: cancellation_canary admits runtime error only for this predicate and excludes the opposite state. + if status.get("cancellations") != prior_cancellations + 1: + # What: raise RuntimeError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router cancellation counter did not increment") + # What: gate on prior terminal and get and status before runtime error; why: cancellation_canary admits runtime error only for this predicate and excludes the opposite state. + if status.get("terminalStreams") != prior_terminal: + # What: raise RuntimeError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("cancelled stream was credited as a normal completion") + # What: gate on raw before runtime error; why: cancellation_canary admits runtime error only for this predicate and excludes the opposite state. + if b"data: [DONE]" in raw: + # What: raise RuntimeError for the caller; why: cancellation_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("cancelled stream reached a normal terminal event") + # What: return bytes and raw and model and request id from cancellation_canary; why: cancellation_canary exposes bytes and raw and model and request id so its caller can continue with the function\'s computed outcome. + return bytes(raw), { + # What: map the route field as native router; why: cancellation_canary carries route into "route": "native_router". + "route": "native_router", + # What: map the model field as model; why: cancellation_canary sends this field through "model": model so the router selects the canonical model or alias for upstream dispatch. + "model": model, + # What: map the request id field as request id; why: cancellation_canary carries request id into "requestId": request_id. + "requestId": request_id, + # What: map the duration seconds field as started and monotonic and time; why: cancellation_canary carries duration seconds into "durationSeconds": time.monotonic() - started. + "durationSeconds": time.monotonic() - started, + # What: map the response bytes field as len and raw; why: cancellation_canary carries response bytes into "responseBytes": len(raw). + "responseBytes": len(raw), + # What: map the cancellation incremented field as true; why: cancellation_canary carries cancellation incremented into "cancellationIncremented": True. + "cancellationIncremented": True, + # What: map the normal completion credited field as false; why: cancellation_canary carries normal completion credited into "normalCompletionCredited": False. + "normalCompletionCredited": False, + # What: map the stream read error field as errors and repr and 0; why: cancellation_canary carries stream read error into "streamReadError": repr(errors[0]) if errors else None. + "streamReadError": repr(errors[0]) if errors else None, + # What: map the passed field as true; why: cancellation_canary carries passed into "passed": True. + "passed": True, + # What: complete the enclosing predicate collection with bytes and raw and model and request id and started and len; why: cancellation_canary groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + } + + +# What: define conflicting_request_canary around base and active model and waiting model and seconds; why: its direct callers call conflicting_request_canary for conflicting request canary and rely on this exact input and result contract. +def conflicting_request_canary( + # What: declare the base input for conflicting_request_canary; why: conflicting_request_canary consumes base during restored raw restored row canary base active model direct, so callers must bind it with the other signature inputs. + base: str, active_model: str, waiting_model: str, *, seconds: float = 180 +# What: complete the enclosing predicate collection with bytes and bytes and bytes and dict; why: conflicting_request_canary groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. +) -> tuple[bytes, bytes, bytes, dict]: + """Hold A, prove B queues, cancel A, then complete B and restore A.""" + # What: document hold a prove b queues cancel in the conflicting_request_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand conflicting request canary behavior without executing it. + # What: compute request id from native qualification conflict; why: content type application json x ft request id request id later reads request id, so conflicting_request_canary must retain the computed value under that name. + request_id = "native-qualification-conflict" + # What: compute and before from request json and base and router and status; why: value after waiting request json base router status later reads and before, so conflicting_request_canary must retain the computed value under that name. + _, before = request_json(base + "/router/status") + # What: compute prior activations from get and before and activations; why: if before get active profile active model or not later reads prior activations, so conflicting_request_canary must retain the computed value under that name. + prior_activations = before.get("activations") + # What: gate on active model and get and isinstance and prior activations and int before runtime error; why: conflicting_request_canary admits runtime error only for this predicate and excludes the opposite state. + if before.get("activeProfile") != active_model or not isinstance(prior_activations, int): + # What: raise RuntimeError for the caller; why: conflicting_request_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("conflicting-request qualification requires active model A") + # What: compute body from active model and model and messages and temperature and max tokens; why: base v1 chat completions data json dumps later reads body, so conflicting_request_canary must retain the computed value under that name. + body = { + # What: map the model field as active model; why: conflicting_request_canary sends this field through body so the router selects the canonical model or alias for upstream dispatch. + "model": active_model, + # What: map the role field as user; why: conflicting_request_canary carries role through body into base v1 chat completions data json dumps body. + "messages": [{"role": "user", "content": "Count upward slowly and do not stop."}], + # What: map the temperature field as 0; why: conflicting_request_canary carries temperature through body into base v1 chat completions data json dumps body. + "temperature": 0, "max_tokens": 2048, "stream": True, + # What: complete the body mapping with model and messages and temperature and max tokens and stream; why: conflicting_request_canary groups the supplied clauses as one body mapping before its value is consumed. + } + # What: compute request from request and request and base and urllib; why: with urllib request urlopen request timeout seconds as later reads request, so conflicting_request_canary must retain the computed value under that name. + request = urllib.request.Request( + # What: supply data to operation.encode; why: conflicting_request_canary binds this encode and dumps and body and json and utf 8 value to operation.encode's data input. + base + "/v1/chat/completions", data=json.dumps(body).encode("utf-8"), + # What: supply headers to urllib.request.Request; why: conflicting_request_canary binds this request id and native headers and base and content type and x ft request id value to urllib.request.Request's headers input. + headers={ + # What: map the content type field as application and json; why: conflicting_request_canary carries content type through request into with urllib request urlopen request timeout seconds as response. + "Content-Type": "application/json", "X-FT-Request-ID": request_id, + # What: call _native_headers with base; why: conflicting_request_canary consumes the _native_headers return value while evaluating **_native_headers(base). + **_native_headers(base), + # What: complete the request mapping with content type and x ft request id; why: conflicting_request_canary groups the supplied clauses as one request mapping before its value is consumed. + }, + # What: complete the urllib.request.Request call with data and headers; why: conflicting_request_canary groups the supplied clauses as one urllib.request.Request call before its value is consumed. + ) + # What: compute active raw from bytearray; why: active raw extend chunk later reads active raw, so conflicting_request_canary must retain the computed value under that name. + active_raw = bytearray() + # What: compute first chunk from event and threading; why: first chunk set later reads first chunk, so conflicting_request_canary must retain the computed value under that name. + first_chunk = threading.Event() + # What: compute active finished from event and threading; why: active finished set later reads active finished, so conflicting_request_canary must retain the computed value under that name. + active_finished = threading.Event() + # What: initialize waiting result as an empty runtime accumulator; why: conflicting_request_canary appends or maps entries into it during waiting result append canary base waiting model direct false before consuming the aggregate. + waiting_result: list[tuple[bytes, dict]] = [] + # What: initialize active errors as an empty runtime accumulator; why: conflicting_request_canary appends or maps entries into it during active errors append exc before consuming the aggregate. + active_errors: list[BaseException] = [] + # What: initialize waiting errors as an empty runtime accumulator; why: conflicting_request_canary appends or maps entries into it during waiting errors append exc before consuming the aggregate. + waiting_errors: list[BaseException] = [] + + # What: define consume_active around the current object state; why: its direct callers call consume_active for consume active and rely on this exact input and result contract. + def consume_active() -> None: + # What: establish the handler boundary for the protected operation; why: consume_active routes failures to exception while preserving cleanup and success flow. + try: + # What: enter the urllib.request.urlopen managed context before for chunk in response; why: consume_active releases this resource or lock after for chunk in response on both success and failure paths. + with urllib.request.urlopen(request, timeout=seconds) as response: + # What: iterate across response to perform extend and chunk and active raw; why: consume_active repeats the body only while or for the loop header admits an iteration. + for chunk in response: + # What: call active_raw.extend with chunk; why: consume_active invokes active_raw.extend while performing first chunk set; the call advances that operation through its result or side effect. + active_raw.extend(chunk) + # What: call first_chunk.set with the declared inputs; why: consume_active invokes first_chunk.set while performing except exception as exc; the call advances that operation through its result or side effect. + first_chunk.set() + # What: handle exception by active errors append exc; why: consume_active converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: + # What: call active_errors.append with exc; why: consume_active invokes active_errors.append while performing finally; the call advances that operation through its result or side effect. + active_errors.append(exc) + # What: run active finished set on every exit path; why: consume_active performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: call active_finished.set with the declared inputs; why: consume_active invokes active_finished.set while performing the enclosing return; the call advances that operation through its result or side effect. + active_finished.set() + + # What: define consume_waiting around the current object state; why: its direct callers call consume_waiting for consume waiting and rely on this exact input and result contract. + def consume_waiting() -> None: + # What: establish the handler boundary for the protected operation; why: consume_waiting routes failures to base exception while preserving cleanup and success flow. + try: + # What: supply direct to waiting_result.append; why: consume_waiting binds this false value to waiting_result.append's direct input. + waiting_result.append(canary(base, waiting_model, direct=False)) + # What: handle base exception by waiting errors append exc; why: consume_waiting converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException as exc: + # What: call waiting_errors.append with exc; why: consume_waiting invokes waiting_errors.append while performing the enclosing return; the call advances that operation through its result or side effect. + waiting_errors.append(exc) + + # What: compute active worker from thread and threading and consume active and true; why: active worker start later reads active worker, so conflicting_request_canary must retain the computed value under that name. + active_worker = threading.Thread(target=consume_active, daemon=True) + # What: call active_worker.start with the declared inputs; why: conflicting_request_canary invokes active_worker.start while performing if not first chunk wait seconds; the call advances that operation through its result or side effect. + active_worker.start() + # What: gate on wait and seconds and first chunk before timeout error; why: conflicting_request_canary admits timeout error only for this predicate and excludes the opposite state. + if not first_chunk.wait(seconds): + # What: raise TimeoutError for the caller; why: conflicting_request_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError("active conflicting stream produced no first chunk") + # What: compute waiting worker from thread and threading and consume waiting and true; why: waiting worker start later reads waiting worker, so conflicting_request_canary must retain the computed value under that name. + waiting_worker = threading.Thread(target=consume_waiting, daemon=True) + # What: call waiting_worker.start with the declared inputs; why: conflicting_request_canary invokes waiting_worker.start while performing deadline time monotonic seconds; the call advances that operation through its result or side effect. + waiting_worker.start() + # What: compute deadline from seconds and monotonic and time; why: while time monotonic deadline later reads deadline, so conflicting_request_canary must retain the computed value under that name. + deadline = time.monotonic() + seconds + # What: compute queued from the named fixture input; why: queued request json base router status timeout later reads queued, so conflicting_request_canary must retain the computed value under that name. + queued: dict | None = None + # What: iterate across deadline and monotonic and time to perform queued and request json and base; why: conflicting_request_canary repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: compute queued from request json and base and 1 and router and status; why: if queued get queued requests later reads queued, so conflicting_request_canary must retain the computed value under that name. + queued = request_json(base + "/router/status", timeout=3)[1] + # What: gate on get and queued before the computed value; why: conflicting_request_canary admits the computed value only for this predicate and excludes the opposite state. + if queued.get("queuedRequests") == 1: + # What: apply the break portion of the enclosing predicate; why: this clause remains in conflicting_request_canary\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: call time.sleep with 0 1; why: conflicting_request_canary invokes time.sleep while performing if; the call advances that operation through its result or side effect. + time.sleep(0.1) + # What: gate on queued and active model and get before runtime error; why: conflicting_request_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call queued.get with queued requests; why: conflicting_request_canary invokes queued.get while performing or queued get active profile active model; the call advances that operation through its result or side effect. + queued is None or queued.get("queuedRequests") != 1 + # What: call queued.get with active profile; why: conflicting_request_canary invokes queued.get while performing or queued get active requests; the call advances that operation through its result or side effect. + or queued.get("activeProfile") != active_model + # What: call queued.get with active requests; why: conflicting_request_canary invokes queued.get while performing or queued get active identity matches engine is not; the call advances that operation through its result or side effect. + or queued.get("activeRequests") != 1 + # What: call queued.get with active identity matches engine; why: conflicting_request_canary consumes the queued.get return value while evaluating or queued.get("activeIdentityMatchesEngine") is not True. + or queued.get("activeIdentityMatchesEngine") is not True + # What: complete the enclosing predicate with if queued is or queued get queued requests differs from 1; why: conflicting_request_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: conflicting_request_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("waiting model did not queue behind the active stream") + # What: gate on request id and request json and base before runtime error; why: conflicting_request_canary admits runtime error only for this predicate and excludes the opposite state. + if request_json(base + f"/router/requests/{request_id}/cancel", {}, timeout=30)[1] != { + # What: map the cancelled field as true; why: conflicting_request_canary carries cancelled into "cancelled": True, "id": request_id. + "cancelled": True, "id": request_id, + # What: apply the grouped expression portion of the enclosing predicate; why: this clause remains in conflicting_request_canary\'s enclosing expression so its grouping and evaluation order stay intact. + }: + # What: raise RuntimeError for the caller; why: conflicting_request_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("active conflicting stream cancellation was not acknowledged") + # What: gate on wait and seconds and active finished before timeout error; why: conflicting_request_canary admits timeout error only for this predicate and excludes the opposite state. + if not active_finished.wait(seconds): + # What: raise TimeoutError for the caller; why: conflicting_request_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError("active conflicting stream did not close") + # What: call waiting_worker.join with seconds; why: conflicting_request_canary invokes waiting_worker.join while performing if waiting worker is alive or waiting errors or len; the call advances that operation through its result or side effect. + waiting_worker.join(seconds) + # What: gate on waiting errors and is alive and waiting worker and len and waiting result before runtime error; why: conflicting_request_canary admits runtime error only for this predicate and excludes the opposite state. + if waiting_worker.is_alive() or waiting_errors or len(waiting_result) != 1: + # What: propagate raise RuntimeError waiting model did not complete after active stream cancellation as a qualification failure; why: callers must not continue after this violated precondition or observed result. + raise RuntimeError("waiting model did not complete after active-stream cancellation") + # What: compute waiting raw and waiting row from waiting result and 0; why: return bytes active raw waiting raw restored raw later reads waiting raw and waiting row, so conflicting_request_canary must retain the computed value under that name. + waiting_raw, waiting_row = waiting_result[0] + # What: compute and after waiting from request json and base and router and status; why: value restored request json base router status later reads and after waiting, so conflicting_request_canary must retain the computed value under that name. + _, after_waiting = request_json(base + "/router/status") + # What: gate on waiting model and get and prior activations and waiting row and after waiting before runtime error; why: conflicting_request_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call waiting_row.get with passed; why: conflicting_request_canary invokes waiting_row.get while performing or after waiting get active profile waiting model; the call advances that operation through its result or side effect. + waiting_row.get("passed") is not True + # What: call after_waiting.get with active profile; why: conflicting_request_canary invokes after_waiting.get while performing or after waiting get active requests; the call advances that operation through its result or side effect. + or after_waiting.get("activeProfile") != waiting_model + # What: call after_waiting.get with active requests; why: conflicting_request_canary invokes after_waiting.get while performing or after waiting get activations prior activations; the call advances that operation through its result or side effect. + or after_waiting.get("activeRequests") != 0 + # What: call after_waiting.get with activations; why: conflicting_request_canary consumes the after_waiting.get return value while evaluating or after_waiting.get("activations") != prior_activations + 1. + or after_waiting.get("activations") != prior_activations + 1 + # What: complete the enclosing predicate with if waiting row get passed is not true or after waiting get active profile; why: conflicting_request_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: conflicting_request_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("waiting model did not receive exactly one post-drain activation") + # What: compute restored raw and restored row from canary and base and active model and false; why: return bytes active raw waiting raw restored raw later reads restored raw and restored row, so conflicting_request_canary must retain the computed value under that name. + restored_raw, restored_row = canary(base, active_model, direct=False) + # What: compute and restored from request json and base and router and status; why: the enclosing return or state update later reads and restored, so conflicting_request_canary must retain the computed value under that name. + _, restored = request_json(base + "/router/status") + # What: gate on get and prior activations and restored row and restored before runtime error; why: conflicting_request_canary admits runtime error only for this predicate and excludes the opposite state. + if restored_row.get("passed") is not True or restored.get("activations") != prior_activations + 2: + # What: raise RuntimeError for the caller; why: conflicting_request_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("conflicting-request qualification did not restore model A") + # What: gate on active raw before runtime error; why: conflicting_request_canary admits runtime error only for this predicate and excludes the opposite state. + if b"data: [DONE]" in active_raw: + # What: raise RuntimeError for the caller; why: conflicting_request_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("active conflicting stream completed normally instead of being cancelled") + # What: return waiting raw and restored raw and bytes and active raw from conflicting_request_canary; why: conflicting_request_canary exposes waiting raw and restored raw and bytes and active raw so its caller can continue with the function\'s computed outcome. + return bytes(active_raw), waiting_raw, restored_raw, { + # What: map the active profile field as active model; why: conflicting_request_canary carries active profile into "activeProfile": active_model, "waitingProfile": waiting_model. + "activeProfile": active_model, "waitingProfile": waiting_model, + # What: map the queued behind active field as true; why: conflicting_request_canary carries queued behind active into "queuedBehindActive": True, "activeIdentityPreservedWhileQueued": True. + "queuedBehindActive": True, "activeIdentityPreservedWhileQueued": True, + # What: map the activation delta field as 2; why: conflicting_request_canary carries activation delta into "activationDelta": 2, "restoredProfile": active_model, "passed": True. + "activationDelta": 2, "restoredProfile": active_model, "passed": True, + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + } + + +# What: define stop_process_group around proc; why: its direct callers call stop_process_group for stop process group and rely on this exact input and result contract. +def stop_process_group(proc: subprocess.Popen[bytes]) -> None: + # What: gate on poll and proc before the computed value; why: stop_process_group admits the computed value only for this predicate and excludes the opposite state. + if proc.poll() is not None: + # What: return no value from stop_process_group; why: stop_process_group returns no value to callers that depend on its completed result. + return + # What: call os.killpg with pid and proc and sigterm and signal; why: stop_process_group invokes os.killpg while performing try; the call advances that operation through its result or side effect. + os.killpg(proc.pid, signal.SIGTERM) + # What: establish the handler boundary for the protected operation; why: stop_process_group routes failures to timeout expired and subprocess while preserving cleanup and success flow. + try: + # What: supply timeout to proc.wait; why: stop_process_group binds this 45 value to proc.wait's timeout input. + proc.wait(timeout=45) + # What: handle timeout expired and subprocess by os killpg proc pid signal sigkill; why: stop_process_group converts that failure into this concrete recovery, response, or cleanup behavior. + except subprocess.TimeoutExpired: + # What: call os.killpg with pid and proc and sigkill and signal; why: stop_process_group invokes os.killpg while performing proc wait timeout; the call advances that operation through its result or side effect. + os.killpg(proc.pid, signal.SIGKILL) + # What: supply timeout to proc.wait; why: stop_process_group binds this 10 value to proc.wait's timeout input. + proc.wait(timeout=10) + + +# What: define validate_routed_trial around router and alias and prior activations and expected delta; why: its direct callers call validate_routed_trial for validate routed trial and rely on this exact input and result contract. +def validate_routed_trial(router: dict, *, alias: str, prior_activations: int, expected_delta: int) -> int: + """Prove that a labeled routed benchmark actually used its intended state. + + Timings alone cannot distinguish a warm request from an accidental reload. + The bounded router state makes each performance label auditable without + retaining a prompt or model path in the public summary. + """ + # What: document prove that a labeled routed benchmark in the validate_routed_trial docstring; why: introspection and maintainers read this exact docstring fragment to understand validate routed trial behavior without executing it. + # What: document timings alone cannot distinguish a warm in the validate_routed_trial docstring; why: introspection and maintainers read this exact docstring fragment to understand validate routed trial behavior without executing it. + # What: document the bounded router state makes each in the validate_routed_trial docstring; why: introspection and maintainers read this exact docstring fragment to understand validate routed trial behavior without executing it. + # What: document retaining a prompt or model path in the validate_routed_trial docstring; why: introspection and maintainers read this exact docstring fragment to understand validate routed trial behavior without executing it. + # What: preserve the paragraph boundary in the the validate_routed_trial docstring; why: introspection and maintainers read this paragraph break to understand validate routed trial behavior without executing it. + # What: compute activations from get and router and activations; why: if not isinstance activations int or later reads activations, so validate_routed_trial must retain the computed value under that name. + activations = router.get("activations") + # What: gate on alias and get and router before runtime error; why: validate_routed_trial admits runtime error only for this predicate and excludes the opposite state. + if router.get("activeProfile") != alias or router.get("activeRequests") != 0: + # What: raise RuntimeError for the caller; why: validate_routed_trial stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("routed trial did not settle on the expected idle profile") + # What: gate on activations and isinstance and int and prior activations and expected delta before runtime error; why: validate_routed_trial admits runtime error only for this predicate and excludes the opposite state. + if not isinstance(activations, int) or activations != prior_activations + expected_delta: + # What: raise RuntimeError for the caller; why: validate_routed_trial stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("routed trial activation count did not match its scenario") + # What: return activations from validate_routed_trial; why: validate_routed_trial exposes activations so its caller can continue with the function\'s computed outcome. + return activations + + +# What: define valid_periodic_performance around performance; why: its direct callers call valid_periodic_performance for valid periodic performance and rely on this exact input and result contract. +def valid_periodic_performance(performance: dict) -> bool: + # What: compute rows from get and performance and sys stats; why: or not isinstance rows list later reads rows, so valid_periodic_performance must retain the computed value under that name. + rows = performance.get("sys_stats") + # What: gate on get and isinstance and rows and list and all before the computed value; why: valid_periodic_performance admits the computed value only for this predicate and excludes the opposite state. + if ( + # What: call performance.get with enabled; why: valid_periodic_performance invokes performance.get while performing or performance get gpu stats; the call advances that operation through its result or side effect. + performance.get("enabled") is not True + # What: call performance.get with gpu stats; why: valid_periodic_performance invokes performance.get while performing or not isinstance rows list; the call advances that operation through its result or side effect. + or performance.get("gpu_stats") != [] + # What: call isinstance with rows and list; why: valid_periodic_performance invokes isinstance while performing or not len rows; the call advances that operation through its result or side effect. + or not isinstance(rows, list) + # What: call len with rows; why: valid_periodic_performance invokes len while performing or not all; the call advances that operation through its result or side effect. + or not 1 <= len(rows) <= 720 + # What: call all with row and rows and isinstance and dict; why: valid_periodic_performance invokes all while performing isinstance row dict; the call advances that operation through its result or side effect. + or not all( + # What: call isinstance with row and dict; why: valid_periodic_performance invokes isinstance while performing and row get scope engine process tree; the call advances that operation through its result or side effect. + isinstance(row, dict) + # What: call row.get with scope; why: valid_periodic_performance invokes row.get while performing and not any key in row; the call advances that operation through its result or side effect. + and row.get("scope") == "engine-process-tree" + # What: call any with key and row and pids and model and path; why: valid_periodic_performance invokes any while performing for row in rows; the call advances that operation through its result or side effect. + and not any(key in row for key in ("pids", "model", "path", "command")) + # What: apply the for row in rows portion of the enclosing predicate; why: this clause remains in valid_periodic_performance\'s enclosing expression so its grouping and evaluation order stay intact. + for row in rows + # What: complete the all call with row; why: valid_periodic_performance groups the supplied clauses as one all call before its value is consumed. + ) + # What: complete the enclosing predicate with if performance get enabled is not true or performance get gpu stats; why: valid_periodic_performance groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: return false from valid_periodic_performance; why: valid_periodic_performance exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: compute latest from rows and 1; why: latest get ram available is later reads latest, so valid_periodic_performance must retain the computed value under that name. + latest = rows[-1] + # What: return isinstance and int and all and get from valid_periodic_performance; why: valid_periodic_performance exposes isinstance and int and all and get so its caller can continue with the function\'s computed outcome. + return ( + # What: call latest.get with ram available; why: valid_periodic_performance invokes latest.get while performing and latest get vram available is; the call advances that operation through its result or side effect. + latest.get("ram_available") is True + # What: call latest.get with vram available; why: valid_periodic_performance invokes latest.get while performing and isinstance latest get ram bytes int; the call advances that operation through its result or side effect. + and latest.get("vram_available") is True + # What: call isinstance with get and latest and ram bytes and int; why: valid_periodic_performance invokes isinstance while performing and latest ram bytes; the call advances that operation through its result or side effect. + and isinstance(latest.get("ram_bytes"), int) + # What: apply the and latest ram bytes portion of the enclosing predicate; why: this clause remains in valid_periodic_performance\'s enclosing expression so its grouping and evaluation order stay intact. + and latest["ram_bytes"] > 0 + # What: call isinstance with get and latest and vram bytes and int; why: valid_periodic_performance invokes isinstance while performing and latest vram bytes; the call advances that operation through its result or side effect. + and isinstance(latest.get("vram_bytes"), int) + # What: apply the and latest vram bytes portion of the enclosing predicate; why: this clause remains in valid_periodic_performance\'s enclosing expression so its grouping and evaluation order stay intact. + and latest["vram_bytes"] > 0 + # What: call all with key and isinstance and str and latest; why: valid_periodic_performance invokes all while performing isinstance latest get key str and latest; the call advances that operation through its result or side effect. + and all( + # What: call isinstance with get and key and latest and str; why: valid_periodic_performance invokes isinstance while performing for key in timestamp ram source vram source; the call advances that operation through its result or side effect. + isinstance(latest.get(key), str) and latest[key] + # What: apply the for key in timestamp ram source vram source portion of the enclosing predicate; why: this clause remains in valid_periodic_performance\'s enclosing expression so its grouping and evaluation order stay intact. + for key in ("timestamp", "ram_source", "vram_source") + # What: complete the all call with key; why: valid_periodic_performance groups the supplied clauses as one all call before its value is consumed. + ) + # What: complete the valid_periodic_performance signature with performance; why: valid_periodic_performance groups the supplied clauses as one valid_periodic_performance signature before its value is consumed. + ) + + +# What: define control_plane_canary around base and artifacts; why: its direct callers call control_plane_canary for control plane canary and rely on this exact input and result contract. +def control_plane_canary(base: str, artifacts: Path) -> dict: + """Qualify authenticated management, metrics, and bounded router-log access.""" + # What: document qualify authenticated management metrics and bounded in the control_plane_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand control plane canary behavior without executing it. + # What: initialize unauthorized as an empty runtime accumulator; why: control_plane_canary appends or maps entries into it during unauthorized path exc code before consuming the aggregate. + unauthorized: dict[str, int] = {} + # What: compute protected paths from router and status and v1 and models and models; why: for path in protected paths later reads protected paths, so control_plane_canary must retain the computed value under that name. + protected_paths = ("/router/status", "/v1/models", "/models", "/api/performance") + # What: iterate across protected paths to perform request and request and base and path and urllib; why: control_plane_canary repeats the body only while or for the loop header admits an iteration. + for path in protected_paths: + # What: compute request from request and request and base and path; why: with urllib request urlopen request timeout later reads request, so control_plane_canary must retain the computed value under that name. + request = urllib.request.Request(base + path) + # What: establish the handler boundary for the protected operation; why: control_plane_canary routes failures to httperror and error and urllib while preserving cleanup and success flow. + try: + # What: enter the urllib.request.urlopen managed context before pass; why: control_plane_canary releases this resource or lock after pass on both success and failure paths. + with urllib.request.urlopen(request, timeout=10): + # What: ignore the anticipated exception handled by this branch; why: control_plane_canary continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: handle httperror and error and urllib by unauthorized path exc code; why: control_plane_canary converts that failure into this concrete recovery, response, or cleanup behavior. + except urllib.error.HTTPError as exc: + # What: compute unauthorized entry from code and exc; why: if unauthorized path for path in later reads unauthorized entry, so control_plane_canary must retain the computed value under that name. + unauthorized[path] = exc.code + # What: call exc.close with the declared inputs; why: control_plane_canary invokes exc.close while performing else; the call advances that operation through its result or side effect. + exc.close() + # What: select the remaining branch that performs raise runtime error f unauthenticated request unexpectedly; why: control_plane_canary covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError(f"unauthenticated request unexpectedly succeeded: {path}") + # What: gate on unauthorized and path and protected paths before runtime error; why: control_plane_canary admits runtime error only for this predicate and excludes the opposite state. + if unauthorized != {path: 401 for path in protected_paths}: + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("native router did not reject unauthenticated control and inference") + + # What: gate on native auth base and native api key and rstrip and base before runtime error; why: control_plane_canary admits runtime error only for this predicate and excludes the opposite state. + if _NATIVE_AUTH_BASE != base.rstrip("/") or _NATIVE_API_KEY is None: + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("native router credentials are not scoped to the qualification origin") + # What: compute basic from decode and b64encode and base64 and encode; why: basic authorization f basic basic later reads basic, so control_plane_canary must retain the computed value under that name. + basic = base64.b64encode(f"operator:{_NATIVE_API_KEY}".encode()).decode() + # What: initialize alternate auth raw as an empty runtime accumulator; why: control_plane_canary appends or maps entries into it during alternate auth raw name raw before consuming the aggregate. + alternate_auth_raw: dict[str, bytes] = {} + # What: iterate across native api key and basic to perform request and request and base and headers and urllib; why: control_plane_canary repeats the body only while or for the loop header admits an iteration. + for name, headers in ( + # What: map the authorization field as basic and basic; why: control_plane_canary carries authorization through ("basic", {"Authorization": f"Basic {basic}"}) into raise runtime error models is not equivalent to. + ("basic", {"Authorization": f"Basic {basic}"}), + # What: map the x api key field as native api key; why: control_plane_canary carries x api key through ("x-api-key", {"X-Api-Key": _NATIVE_API_KEY}) into raise runtime error models is not equivalent to. + ("x-api-key", {"X-Api-Key": _NATIVE_API_KEY}), + # What: complete the enclosing predicate collection with basic and basic and authorization and basic and native api key and x api key and x api key; why: control_plane_canary groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + ): + # What: compute request from request and request and base and headers; why: with urllib request urlopen request timeout as response later reads request, so control_plane_canary must retain the computed value under that name. + request = urllib.request.Request(base + "/router/status", headers=headers) + # What: enter the urllib.request.urlopen managed context before raw response read; why: control_plane_canary releases this resource or lock after raw response read on both success and failure paths. + with urllib.request.urlopen(request, timeout=10) as response: + # What: compute raw from read and response; why: status json loads raw later reads raw, so control_plane_canary must retain the computed value under that name. + raw = response.read() + # What: compute status from loads and raw and json; why: if status get active profile model a later reads status, so control_plane_canary must retain the computed value under that name. + status = json.loads(raw) + # What: gate on get and status before runtime error and name; why: control_plane_canary admits runtime error and name only for this predicate and excludes the opposite state. + if status.get("activeProfile") != "model-a": + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError(f"{name} authentication did not expose exact model-a residency") + # What: compute alternate auth raw entry from raw; why: for name raw in alternate auth raw items later reads alternate auth raw entry, so control_plane_canary must retain the computed value under that name. + alternate_auth_raw[name] = raw + + # What: compute models raw and models from request json and base and v1 and models and 10; why: artifacts control v1 models json write bytes models raw later reads models raw and models, so control_plane_canary must retain the computed value under that name. + models_raw, models = request_json(base + "/v1/models", timeout=10) + # What: compute and models alias from request json and base and models and 10; why: value namespaced stats request json later reads and models alias, so control_plane_canary must retain the computed value under that name. + _, models_alias = request_json(base + "/models", timeout=10) + # What: compute and namespaced stats from request json and base and upstream and compat and model a; why: the enclosing return or state update later reads and namespaced stats, so control_plane_canary must retain the computed value under that name. + _, namespaced_stats = request_json( + # What: supply timeout to request_json; why: control_plane_canary binds this 10 value to request_json's timeout input. + base + "/upstream/compat/model-a/v1/stats", timeout=10 + # What: complete the request_json call with timeout; why: control_plane_canary groups the supplied clauses as one request_json call before its value is consumed. + ) + # What: compute routed raw and routed from request json and base and router and models and 10; why: artifacts control router models json write bytes routed raw later reads routed raw and routed, so control_plane_canary must retain the computed value under that name. + routed_raw, routed = request_json(base + "/router/models", timeout=10) + # What: compute profiles raw and profiles from request json and base and router and profiles and 10; why: artifacts control router profiles json write bytes profiles raw later reads profiles raw and profiles, so control_plane_canary must retain the computed value under that name. + profiles_raw, profiles = request_json(base + "/router/profiles", timeout=10) + # What: compute performance raw from the named fixture input; why: performance raw performance request json base api performance later reads performance raw, so control_plane_canary must retain the computed value under that name. + performance_raw = b"" + # What: initialize performance as an empty runtime accumulator; why: control_plane_canary appends or maps entries into it during performance raw performance request json base api performance timeout before consuming the aggregate. + performance: dict = {} + # What: compute performance deadline from monotonic and time and 15; why: if time monotonic performance deadline later reads performance deadline, so control_plane_canary must retain the computed value under that name. + performance_deadline = time.monotonic() + 15 + # What: iterate across the computed value to perform performance raw and performance and request json and base; why: control_plane_canary repeats the body only while or for the loop header admits an iteration. + while True: + # What: compute performance raw and performance from request json and base and api and performance and 10; why: control_plane_canary consumes performance raw and performance during artifacts control performance json write bytes performance raw, so performance raw and performance value receives the computed val. + performance_raw, performance = request_json(base + "/api/performance", timeout=10) + # What: gate on valid periodic performance and performance before the computed value; why: control_plane_canary admits the computed value only for this predicate and excludes the opposite state. + if valid_periodic_performance(performance): + # What: apply the break portion of the enclosing predicate; why: this clause remains in control_plane_canary\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: gate on performance deadline and monotonic and time before runtime error; why: control_plane_canary admits runtime error only for this predicate and excludes the opposite state. + if time.monotonic() >= performance_deadline: + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("periodic performance lacked a positive owned-process sample") + # What: call time.sleep with 0 25; why: control_plane_canary invokes time.sleep while performing metrics raw request bytes base metrics timeout; the call advances that operation through its result or side effect. + time.sleep(0.25) + # What: compute metrics raw from request bytes and base and metrics and 10; why: or b freetoken swap admissions total not in metrics raw later reads metrics raw, so control_plane_canary must retain the computed value under that name. + metrics_raw = request_bytes(base + "/metrics", timeout=10) + # What: compute model rows from get and models and data; why: for rows in model rows alias rows routed rows later reads model rows, so control_plane_canary must retain the computed value under that name. + model_rows = models.get("data") + # What: compute alias rows from get and models alias and data; why: for rows in model rows alias rows routed rows later reads alias rows, so control_plane_canary must retain the computed value under that name. + alias_rows = models_alias.get("data") + # What: compute routed rows from get and routed and data; why: for rows in model rows alias rows routed rows later reads routed rows, so control_plane_canary must retain the computed value under that name. + routed_rows = routed.get("data") + # What: compute profile rows from get and profiles and data; why: for rows in model rows alias rows routed rows later reads profile rows, so control_plane_canary must retain the computed value under that name. + profile_rows = profiles.get("data") + # What: compute routing profiles from get and profiles and routing profiles; why: item for item in routing profiles later reads routing profiles, so control_plane_canary must retain the computed value under that name. + routing_profiles = profiles.get("routingProfiles") + # What: gate on all and rows and isinstance and list and model rows before runtime error; why: control_plane_canary admits runtime error only for this predicate and excludes the opposite state. + if not all( + # What: call isinstance with rows and list; why: control_plane_canary invokes isinstance while performing for rows in model rows alias rows routed rows; the call advances that operation through its result or side effect. + isinstance(rows, list) and all(isinstance(item, dict) for item in rows) + # What: apply the for rows in model rows alias rows routed rows portion of the enclosing predicate; why: this clause remains in control_plane_canary\'s enclosing expression so its grouping and evaluation order stay intact. + for rows in (model_rows, alias_rows, routed_rows, profile_rows) + # What: complete the all call with rows; why: control_plane_canary groups the supplied clauses as one all call before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("authenticated native control-plane responses have invalid shapes") + # The pinned alias invokes the same handler independently, so request-time + # `created` values may differ by one second. Everything else must match. + # What: compute normalized models from k and v and item and model rows; why: if model envelope alias envelope or normalized models normalized alias later reads normalized models, so control_plane_canary must retain the computed value under that name. + normalized_models = [{k: v for k, v in item.items() if k != "created"} for item in model_rows] + # What: compute normalized alias from k and v and item and alias rows; why: if model envelope alias envelope or normalized models normalized alias later reads normalized alias, so control_plane_canary must retain the computed value under that name. + normalized_alias = [{k: v for k, v in item.items() if k != "created"} for item in alias_rows] + # What: compute model envelope from k and v and items and models and data; why: if model envelope alias envelope or normalized models normalized alias later reads model envelope, so control_plane_canary must retain the computed value under that name. + model_envelope = {k: v for k, v in models.items() if k != "data"} + # What: compute alias envelope from k and v and items and models alias and data; why: if model envelope alias envelope or normalized models normalized alias later reads alias envelope, so control_plane_canary must retain the computed value under that name. + alias_envelope = {k: v for k, v in models_alias.items() if k != "data"} + # What: gate on model envelope and alias envelope and normalized models and normalized alias before runtime error; why: control_plane_canary admits runtime error only for this predicate and excludes the opposite state. + if model_envelope != alias_envelope or normalized_models != normalized_alias: + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("/models is not equivalent to the /v1/models compatibility listing") + # What: compute aliases from sorted and item and model rows and isinstance; why: not model a model b compat model a preferred model later reads aliases, so control_plane_canary must retain the computed value under that name. + aliases = sorted(item["id"] for item in model_rows if isinstance(item.get("id"), str)) + # What: compute routed names from sorted and item and routed rows and isinstance; why: or routed names profile names later reads routed names, so control_plane_canary must retain the computed value under that name. + routed_names = sorted( + # What: call isinstance with get and item and name and str; why: control_plane_canary consumes the isinstance return value while evaluating item["name"] for item in routed_rows if isinstance(item.get("name"), str. + item["name"] for item in routed_rows if isinstance(item.get("name"), str) + # What: complete the sorted call with item; why: control_plane_canary groups the supplied clauses as one sorted call before its value is consumed. + ) + # What: compute profile names from sorted and item and profile rows and isinstance; why: or routed names profile names later reads profile names, so control_plane_canary must retain the computed value under that name. + profile_names = sorted( + # What: call isinstance with get and item and name and str; why: control_plane_canary consumes the isinstance return value while evaluating item["name"] for item in profile_rows if isinstance(item.get("name"), st. + item["name"] for item in profile_rows if isinstance(item.get("name"), str) + # What: complete the sorted call with item; why: control_plane_canary groups the supplied clauses as one sorted call before its value is consumed. + ) + # What: compute coding profile from isinstance and routing profiles and list and next; why: or not isinstance coding profile dict later reads coding profile, so control_plane_canary must retain the computed value under that name. + coding_profile = next( + # What: complete the next call with item; why: control_plane_canary groups the supplied clauses as one next call before its value is consumed. + ( + # What: apply the item for item in routing profiles portion of coding profile; why: control_plane_canary uses this clause to evaluate coding profile as one grouped value. + item for item in routing_profiles + # What: call isinstance with item and dict; why: control_plane_canary consumes the isinstance return value while evaluating if isinstance(item, dict) and item.get("name") == "coding". + if isinstance(item, dict) and item.get("name") == "coding" + # What: complete the next call with item; why: control_plane_canary groups the supplied clauses as one next call before its value is consumed. + ), + # What: apply the grouped expression portion of coding profile; why: control_plane_canary uses this clause to evaluate coding profile as one grouped value. + None, + # What: call isinstance with routing profiles and list; why: control_plane_canary invokes isinstance while performing resident item get name for item in; the call advances that operation through its result or side effect. + ) if isinstance(routing_profiles, list) else None + # What: compute resident from get and item and routed rows and name and resident; why: or resident model a later reads resident, so control_plane_canary must retain the computed value under that name. + resident = [item.get("name") for item in routed_rows if item.get("resident")] + # What: compute routed a from next and item and routed rows and get and model a; why: or not isinstance routed a dict later reads routed a, so control_plane_canary must retain the computed value under that name. + routed_a = next((item for item in routed_rows if item.get("name") == "model-a"), None) + # What: compute listed a from next and item and model rows and get and model a; why: or not isinstance listed a dict later reads listed a, so control_plane_canary must retain the computed value under that name. + listed_a = next((item for item in model_rows if item.get("id") == "model-a"), None) + # What: compute listed alias a from next and item and model rows and get and compat; why: or not isinstance listed alias a dict later reads listed alias a, so control_plane_canary must retain the computed value under that name. + listed_alias_a = next( + # What: call item.get with id; why: control_plane_canary consumes the item.get return value while evaluating (item for item in model_rows if item.get("id") == "compat/model-a"), Non. + (item for item in model_rows if item.get("id") == "compat/model-a"), None + # What: complete the next call with item; why: control_plane_canary groups the supplied clauses as one next call before its value is consumed. + ) + # What: gate on routed names and profile names and resident and metrics raw and issubset before runtime error; why: control_plane_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call operation.issubset with aliases; why: control_plane_canary invokes operation.issubset while performing or routed names profile names; the call advances that operation through its result or side effect. + not {"model-a", "model-b", "compat/model-a", "preferred-model"}.issubset(aliases) + # What: apply the or routed names profile names portion of the enclosing predicate; why: this clause remains in control_plane_canary\'s enclosing expression so its grouping and evaluation order stay intact. + or routed_names != profile_names + # What: call operation.issubset with routed names; why: control_plane_canary invokes operation.issubset while performing or resident model a; the call advances that operation through its result or side effect. + or not {"model-a", "model-b"}.issubset(routed_names) + # What: apply the or resident model a portion of the enclosing predicate; why: this clause remains in control_plane_canary\'s enclosing expression so its grouping and evaluation order stay intact. + or resident != ["model-a"] + # What: call profiles.get with active profile; why: control_plane_canary invokes profiles.get while performing or profiles get active routing profile is not; the call advances that operation through its result or side effect. + or profiles.get("activeProfile") != "model-a" + # What: call profiles.get with active routing profile; why: control_plane_canary invokes profiles.get while performing or not isinstance routing profiles list; the call advances that operation through its result or side effect. + or profiles.get("activeRoutingProfile") is not None + # What: call isinstance with routing profiles and list; why: control_plane_canary invokes isinstance while performing or not isinstance coding profile dict; the call advances that operation through its result or side effect. + or not isinstance(routing_profiles, list) + # What: call isinstance with coding profile and dict; why: control_plane_canary invokes isinstance while performing or coding profile get pins; the call advances that operation through its result or side effect. + or not isinstance(coding_profile, dict) + # What: call coding_profile.get with pins; why: control_plane_canary invokes coding_profile.get while performing disabled model profile model preferred model; the call advances that operation through its result or side effect. + or coding_profile.get("pins") != { + # What: map the disabled model field as the fixture input; why: control_plane_canary carries disabled model through "disabled-model": None, "profile-model": "preferred-model" into raise runtime error router log stream lacked the. + "disabled-model": None, "profile-model": "preferred-model", + # What: complete the enclosing predicate mapping with disabled model and profile model; why: control_plane_canary groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + # What: call isinstance with routed a and dict; why: control_plane_canary invokes isinstance while performing or routed a get check endpoint ready; the call advances that operation through its result or side effect. + or not isinstance(routed_a, dict) + # What: call routed_a.get with check endpoint; why: control_plane_canary invokes routed_a.get while performing or routed a get use model name model a; the call advances that operation through its result or side effect. + or routed_a.get("checkEndpoint") != "/ready" + # What: call routed_a.get with use model name; why: control_plane_canary invokes routed_a.get while performing or routed a get upstream timeout s; the call advances that operation through its result or side effect. + or routed_a.get("useModelName") != "model-a" + # What: call routed_a.get with upstream timeout s; why: control_plane_canary invokes routed_a.get while performing or routed a get display name qualification model a; the call advances that operation through its result or side effect. + or routed_a.get("upstreamTimeoutS") != 659 + # What: call routed_a.get with display name; why: control_plane_canary invokes routed_a.get while performing or routed a get metadata tier qualification type; the call advances that operation through its result or side effect. + or routed_a.get("displayName") != "Qualification model A" + # What: map the tier field as qualification; why: control_plane_canary carries tier through or routed_a.get("metadata") != {"tier": "qualification", "type": "operat into raise runtime error router log stream lacked the. + or routed_a.get("metadata") != {"tier": "qualification", "type": "operator"} + # What: call isinstance with listed a and dict; why: control_plane_canary invokes isinstance while performing or listed a get name qualification model a; the call advances that operation through its result or side effect. + or not isinstance(listed_a, dict) + # What: call listed_a.get with name; why: control_plane_canary invokes listed_a.get while performing or listed a get meta get freetoken; the call advances that operation through its result or side effect. + or listed_a.get("name") != "Qualification model A" + # What: call operation.get with freetoken; why: control_plane_canary invokes operation.get while performing aliases compat model a tier qualification type; the call advances that operation through its result or side effect. + or listed_a.get("meta", {}).get("freetoken") != { + # What: map the aliases field as compat and model a; why: control_plane_canary carries aliases through "aliases": ["compat/model-a"], "tier": "qualification", "type": "model" into raise runtime error router log stream lacked the. + "aliases": ["compat/model-a"], "tier": "qualification", "type": "model", + # What: complete the enclosing predicate mapping with aliases and tier and type; why: control_plane_canary groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + # What: call isinstance with listed alias a and dict; why: control_plane_canary invokes isinstance while performing or listed alias a get name qualification model a; the call advances that operation through its result or side effect. + or not isinstance(listed_alias_a, dict) + # What: call listed_alias_a.get with name; why: control_plane_canary invokes listed_alias_a.get while performing or listed alias a get meta get freetoken; the call advances that operation through its result or side effect. + or listed_alias_a.get("name") != "Qualification model A" + # What: call operation.get with freetoken; why: control_plane_canary invokes operation.get while performing model id model a tier qualification type alias; the call advances that operation through its result or side effect. + or listed_alias_a.get("meta", {}).get("freetoken") != { + # What: map the model id field as model a; why: control_plane_canary carries model id through "modelID": "model-a", "tier": "qualification", "type": "alias" into raise runtime error router log stream lacked the. + "modelID": "model-a", "tier": "qualification", "type": "alias", + # What: complete the enclosing predicate mapping with model id and tier and type; why: control_plane_canary groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + # What: call isinstance with namespaced stats and dict; why: control_plane_canary invokes isinstance while performing or b freetoken swap admissions total not in metrics raw; the call advances that operation through its result or side effect. + or not isinstance(namespaced_stats, dict) + # What: apply the or b freetoken swap admissions total not in metrics raw portion of the enclosing predicate; why: this clause remains in control_plane_canary\'s enclosing expression so its grouping and evaluation order stay intact. + or b"freetoken_swap_admissions_total" not in metrics_raw + # What: complete the enclosing predicate with if not model a model b compat model a preferred model issubset aliases; why: control_plane_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("authenticated native control-plane responses are inconsistent") + + # What: compute log request from request and request and base and urllib; why: with urllib request urlopen log request timeout as response later reads log request, so control_plane_canary must retain the computed value under that name. + log_request = urllib.request.Request( + # What: supply headers to _native_headers; why: control_plane_canary binds this native headers and base value to _native_headers's headers input. + base + "/router/logs?since=0", headers=_native_headers(base) + # What: complete the urllib.request.Request call with headers; why: control_plane_canary groups the supplied clauses as one urllib.request.Request call before its value is consumed. + ) + # What: compute log frame from bytearray; why: while len log frame later reads log frame, so control_plane_canary must retain the computed value under that name. + log_frame = bytearray() + # What: enter the urllib.request.urlopen managed context before if response headers get content type text event stream; why: control_plane_canary releases this resource or lock after if response headers get content type text event stream on both success and failure paths. + with urllib.request.urlopen(log_request, timeout=10) as response: + # What: gate on get content type and headers and response before runtime error; why: control_plane_canary admits runtime error only for this predicate and excludes the opposite state. + if response.headers.get_content_type() != "text/event-stream": + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router log endpoint did not return SSE") + # What: iterate across len and log frame to perform line and readline and response; why: control_plane_canary repeats the body only while or for the loop header admits an iteration. + while len(log_frame) <= 64 * 1024: + # What: compute line from readline and response; why: if not line later reads line, so control_plane_canary must retain the computed value under that name. + line = response.readline() + # What: gate on line before the computed value; why: control_plane_canary admits the computed value only for this predicate and excludes the opposite state. + if not line: + # What: apply the break portion of the enclosing predicate; why: this clause remains in control_plane_canary\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: call log_frame.extend with line; why: control_plane_canary invokes log_frame.extend while performing if b management loaded in log frame; the call advances that operation through its result or side effect. + log_frame.extend(line) + # What: gate on log frame before the computed value; why: control_plane_canary admits the computed value only for this predicate and excludes the opposite state. + if b"management_loaded" in log_frame: + # What: apply the break portion of the enclosing predicate; why: this clause remains in control_plane_canary\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: gate on log frame and len before runtime error; why: control_plane_canary admits runtime error only for this predicate and excludes the opposite state. + if len(log_frame) > 64 * 1024 or b"management_loaded" not in log_frame: + # What: raise RuntimeError for the caller; why: control_plane_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router log stream lacked the bounded management event") + + # What: preserve the exact artifacts control v1 models json write bytes models raw literal fragment; why: control_plane_canary passes this fragment verbatim through (artifacts / "control-v1-models.json").write_bytes(models_raw), because changing it would alter a protocol payload, serialized fixture, or public mess. + (artifacts / "control-v1-models.json").write_bytes(models_raw) + # What: preserve the exact artifacts control router models json write bytes routed raw literal fragment; why: control_plane_canary passes this fragment verbatim through (artifacts / "control-router-models.json").write_bytes(routed_raw), because changing it would alter a protocol payload, serialized fixture, or pub. + (artifacts / "control-router-models.json").write_bytes(routed_raw) + # What: preserve the exact artifacts control router profiles json write bytes profiles raw literal fragment; why: control_plane_canary passes this fragment verbatim through (artifacts / "control-router-profiles.json").write_bytes(profiles_raw), because changing it would alter a protocol payload, serialized fixture. + (artifacts / "control-router-profiles.json").write_bytes(profiles_raw) + # What: preserve the exact artifacts control performance json write bytes performance raw literal fragment; why: control_plane_canary passes this fragment verbatim through (artifacts / "control-performance.json").write_bytes(performance_raw), because changing it would alter a protocol payload, serialized fixture. + (artifacts / "control-performance.json").write_bytes(performance_raw) + # What: preserve the exact artifacts control metrics prom write bytes metrics raw literal fragment; why: control_plane_canary passes this fragment verbatim through (artifacts / "control-metrics.prom").write_bytes(metrics_raw), because changing it would alter a protocol payload, serialized fixture, or public messag. + (artifacts / "control-metrics.prom").write_bytes(metrics_raw) + # What: preserve the exact artifacts control router log sse write bytes log frame literal fragment; why: control_plane_canary passes this fragment verbatim through (artifacts / "control-router-log.sse").write_bytes(log_frame), because changing it would alter a protocol payload, serialized fixture, or public messag. + (artifacts / "control-router-log.sse").write_bytes(log_frame) + # What: iterate across items and alternate auth raw to perform write bytes and raw and artifacts and name; why: control_plane_canary repeats the body only while or for the loop header admits an iteration. + for name, raw in alternate_auth_raw.items(): + # What: preserve the exact artifacts f control auth name json write bytes literal fragment; why: control_plane_canary passes this fragment verbatim through (artifacts / f"control-auth-{name}.json").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / f"control-auth-{name}.json").write_bytes(raw) + # What: return; why: the caller consumes this value as the function’s success-path result. + return { + # What: map the unauthenticated control rejected field as true; why: control_plane_canary carries unauthenticated control rejected into "unauthenticatedControlRejected": True. + "unauthenticatedControlRejected": True, + # What: map the unauthenticated inference rejected field as true; why: control_plane_canary carries unauthenticated inference rejected into "unauthenticatedInferenceRejected": True. + "unauthenticatedInferenceRejected": True, + # What: map the alias count field as len and aliases; why: control_plane_canary carries alias count into "aliasCount": len(aliases). + "aliasCount": len(aliases), + # What: map the selector listed field as aliases and preferred model; why: control_plane_canary carries selector listed into "selectorListed": "preferred-model" in aliases. + "selectorListed": "preferred-model" in aliases, + # What: map the profile count field as len and profile names; why: control_plane_canary carries profile count into "profileCount": len(profile_names). + "profileCount": len(profile_names), + # What: map the routing profile listed field as true; why: control_plane_canary carries routing profile listed into "routingProfileListed": True. + "routingProfileListed": True, + # What: map the configured readiness target verified field as true; why: control_plane_canary carries configured readiness target verified into "configuredReadinessTargetVerified": True. + "configuredReadinessTargetVerified": True, + # What: map the configured upstream model name verified field as true; why: control_plane_canary carries configured upstream model name verified into "configuredUpstreamModelNameVerified": True. + "configuredUpstreamModelNameVerified": True, + # What: map the configured upstream timeout verified field as true; why: control_plane_canary carries configured upstream timeout verified into "configuredUpstreamTimeoutVerified": True. + "configuredUpstreamTimeoutVerified": True, + # What: map the configured model metadata verified field as true; why: control_plane_canary carries configured model metadata verified into "configuredModelMetadataVerified": True. + "configuredModelMetadataVerified": True, + # What: map the resident profile field as model a; why: control_plane_canary carries resident profile into "residentProfile": "model-a". + "residentProfile": "model-a", + # What: map the model list alias verified field as true; why: control_plane_canary carries model list alias verified into "modelListAliasVerified": True. + "modelListAliasVerified": True, + # What: map the namespaced upstream verified field as true; why: control_plane_canary carries namespaced upstream verified into "namespacedUpstreamVerified": True. + "namespacedUpstreamVerified": True, + # What: map the api key forms verified field as bearer and basic and x api key; why: control_plane_canary carries api key forms verified into "apiKeyFormsVerified": ["bearer", "basic", "x-api-key"]. + "apiKeyFormsVerified": ["bearer", "basic", "x-api-key"], + # What: map the metrics available field as true; why: control_plane_canary carries metrics available into "metricsAvailable": True. + "metricsAvailable": True, + # What: map the periodic performance available field as true; why: control_plane_canary carries periodic performance available into "periodicPerformanceAvailable": True. + "periodicPerformanceAvailable": True, + # What: map the router log sse available field as true; why: control_plane_canary carries router log sse available into "routerLogSseAvailable": True. + "routerLogSseAvailable": True, + # What: map the passed field as true; why: control_plane_canary carries passed into "passed": True. + "passed": True, + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + } + + +# What: define selector_canary around base and artifacts; why: its direct callers call selector_canary for selector canary and rely on this exact input and result contract. +def selector_canary(base: str, artifacts: Path) -> dict: + """Prove a warm virtual ID reuses the resident target without a swap.""" + # What: document prove a warm virtual id reuses in the selector_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand selector canary behavior without executing it. + # What: compute and before from request json and base and router and status; why: value after request json base router status later reads and before, so selector_canary must retain the computed value under that name. + _, before = request_json(base + "/router/status") + # What: compute prior activations from get and before and activations; why: if before get active profile model a or not later reads prior activations, so selector_canary must retain the computed value under that name. + prior_activations = before.get("activations") + # What: gate on get and isinstance and prior activations and int and before before runtime error; why: selector_canary admits runtime error only for this predicate and excludes the opposite state. + if before.get("activeProfile") != "model-a" or not isinstance(prior_activations, int): + # What: raise RuntimeError for the caller; why: selector_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("warm selector canary requires resident model-a") + # What: compute raw and completion from canary and base and preferred model and false; why: artifacts warm selector sse write bytes raw later reads raw and completion, so selector_canary must retain the computed value under that name. + raw, completion = canary(base, "preferred-model", direct=False) + # What: compute and after from request json and base and router and status; why: the enclosing return or state update later reads and after, so selector_canary must retain the computed value under that name. + _, after = request_json(base + "/router/status") + # What: gate on prior activations and get and completion and after before runtime error; why: selector_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call completion.get with passed; why: selector_canary invokes completion.get while performing or after get active profile model a; the call advances that operation through its result or side effect. + completion.get("passed") is not True + # What: call after.get with active profile; why: selector_canary invokes after.get while performing or after get active requests; the call advances that operation through its result or side effect. + or after.get("activeProfile") != "model-a" + # What: call after.get with active requests; why: selector_canary invokes after.get while performing or after get activations prior activations; the call advances that operation through its result or side effect. + or after.get("activeRequests") != 0 + # What: call after.get with activations; why: selector_canary consumes the after.get return value while evaluating or after.get("activations") != prior_activations. + or after.get("activations") != prior_activations + # What: complete the enclosing predicate with if completion get passed is not true or after get active profile; why: selector_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: selector_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("warm selector did not reuse the resident target") + # What: preserve the exact artifacts warm selector sse write bytes raw literal fragment; why: selector_canary passes this fragment verbatim through (artifacts / "warm-selector.sse").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / "warm-selector.sse").write_bytes(raw) + # What: return strategy and resolved profile and activation delta and passed and warm from selector_canary; why: selector_canary exposes strategy and resolved profile and activation delta and passed and warm so its caller can continue with the function\'s computed outcome. + return { + # What: map the strategy field as warm; why: selector_canary carries strategy into "strategy": "warm". + "strategy": "warm", + # What: map the resolved profile field as model a; why: selector_canary carries resolved profile into "resolvedProfile": "model-a". + "resolvedProfile": "model-a", + # What: map the activation delta field as 0; why: selector_canary carries activation delta into "activationDelta": 0. + "activationDelta": 0, + # What: map the passed field as true; why: selector_canary carries passed into "passed": True. + "passed": True, + # What: complete the enclosing predicate mapping with strategy and resolved profile and activation delta and passed; why: selector_canary groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + +# What: define routing_profile_canary around base and artifacts; why: its direct callers call routing_profile_canary for routing profile canary and rely on this exact input and result contract. +def routing_profile_canary(base: str, artifacts: Path) -> dict: + """Prove an active profile pin composes through a warm selector, then clear it.""" + # What: document prove an active profile pin composes in the routing_profile_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand routing profile canary behavior without executing it. + # What: compute and before from request json and base and router and status; why: value activated request json later reads and before, so routing_profile_canary must retain the computed value under that name. + _, before = request_json(base + "/router/status") + # What: compute prior activations from get and before and activations; why: if before get active profile model a or not later reads prior activations, so routing_profile_canary must retain the computed value under that name. + prior_activations = before.get("activations") + # What: gate on get and isinstance and prior activations and int and before before runtime error; why: routing_profile_canary admits runtime error only for this predicate and excludes the opposite state. + if before.get("activeProfile") != "model-a" or not isinstance(prior_activations, int): + # What: raise RuntimeError for the caller; why: routing_profile_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("routing profile canary requires resident model-a") + # What: compute raw from the named fixture input; why: raw completion canary base profile model direct later reads raw, so routing_profile_canary must retain the computed value under that name. + raw = b"" + # What: compute listed raw from the named fixture input; why: listed raw listed request json base v1 models later reads listed raw, so routing_profile_canary must retain the computed value under that name. + listed_raw = b"" + # What: establish the handler boundary for the protected operation; why: routing_profile_canary routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: compute and activated from request json and base and router and profiles and active; why: value after request json base router status later reads and activated, so routing_profile_canary must retain the computed value under that name. + _, activated = request_json( + # What: map the name field as coding; why: routing_profile_canary carries name through and activated into value after request json base router status. + base + "/router/profiles/active", {"name": "coding"}, method="PUT" + # What: complete the request_json call with method; why: routing_profile_canary groups the supplied clauses as one request_json call before its value is consumed. + ) + # What: map the active field as coding; why: routing_profile_canary carries active through if activated != {"active": "coding"} into raise runtime error routing profile pin did not. + if activated != {"active": "coding"}: + # What: raise RuntimeError for the caller; why: routing_profile_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("routing profile activation was not acknowledged") + # What: compute listed raw and listed from request json and base and v1 and models and 10; why: artifacts routing profile models json write bytes listed raw later reads listed raw and listed, so routing_profile_canary must retain the computed value under that name. + listed_raw, listed = request_json(base + "/v1/models", timeout=10) + # What: compute listed ids from get and item and isinstance and dict; why: if profile model not in listed ids or later reads listed ids, so routing_profile_canary must retain the computed value under that name. + listed_ids = { + # What: call item.get with id; why: routing_profile_canary consumes the item.get return value while evaluating item.get("id") for item in listed.get("data", []) if isinstance(item, di. + item.get("id") for item in listed.get("data", []) if isinstance(item, dict) + # What: complete the listed_ids expression with listed ids item get id for item in listed get data if; why: routing_profile_canary groups the supplied clauses as one listed_ids expression before its value is consumed. + } + # What: gate on listed ids before runtime error; why: routing_profile_canary admits runtime error only for this predicate and excludes the opposite state. + if "profile-model" not in listed_ids or "disabled-model" in listed_ids: + # What: raise RuntimeError for the caller; why: routing_profile_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("active routing profile model listing is inconsistent") + # What: compute raw and completion from canary and base and profile model and false; why: artifacts routing profile sse write bytes raw later reads raw and completion, so routing_profile_canary must retain the computed value under that name. + raw, completion = canary(base, "profile-model", direct=False) + # What: compute and after from request json and base and router and status; why: value cleared request json later reads and after, so routing_profile_canary must retain the computed value under that name. + _, after = request_json(base + "/router/status") + # What: gate on prior activations and get and completion and after before runtime error; why: routing_profile_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call completion.get with passed; why: routing_profile_canary invokes completion.get while performing or after get active routing profile coding; the call advances that operation through its result or side effect. + completion.get("passed") is not True + # What: call after.get with active routing profile; why: routing_profile_canary invokes after.get while performing or after get active profile model a; the call advances that operation through its result or side effect. + or after.get("activeRoutingProfile") != "coding" + # What: call after.get with active profile; why: routing_profile_canary invokes after.get while performing or after get active requests; the call advances that operation through its result or side effect. + or after.get("activeProfile") != "model-a" + # What: call after.get with active requests; why: routing_profile_canary invokes after.get while performing or after get activations prior activations; the call advances that operation through its result or side effect. + or after.get("activeRequests") != 0 + # What: call after.get with activations; why: routing_profile_canary consumes the after.get return value while evaluating or after.get("activations") != prior_activations. + or after.get("activations") != prior_activations + # What: complete the enclosing predicate with if completion get passed is not true or after get active routing profile; why: routing_profile_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: routing_profile_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("routing profile pin did not reuse the resident selector target") + # What: run value cleared request json on every exit path; why: routing_profile_canary performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: compute and cleared from request json and base and router and profiles and active; why: the enclosing return or state update later reads and cleared, so routing_profile_canary must retain the computed value under that name. + _, cleared = request_json( + # What: map the name field as the fixture input; why: routing_profile_canary carries name through and cleared into the enclosing return or state update. + base + "/router/profiles/active", {"name": None}, method="PUT" + # What: complete the request_json call with method; why: routing_profile_canary groups the supplied clauses as one request_json call before its value is consumed. + ) + # What: map the active field as the fixture input; why: routing_profile_canary carries active into if cleared != {"active": None}. + if cleared != {"active": None}: + # What: raise RuntimeError for the caller; why: routing_profile_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("routing profile was not cleared after its canary") + # What: preserve the exact artifacts routing profile sse write bytes raw literal fragment; why: routing_profile_canary passes this fragment verbatim through (artifacts / "routing-profile.sse").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / "routing-profile.sse").write_bytes(raw) + # What: preserve the exact artifacts routing profile models json write bytes listed raw literal fragment; why: routing_profile_canary passes this fragment verbatim through (artifacts / "routing-profile-models.json").write_bytes(listed_raw), because changing it would alter a protocol payload, serialized fixture, or. + (artifacts / "routing-profile-models.json").write_bytes(listed_raw) + # What: return; why: the caller consumes this value as the function’s success-path result. + return { + # What: map the profile activated field as true; why: routing_profile_canary carries profile activated into "profileActivated": True. + "profileActivated": True, + # What: map the profile cleared field as true; why: routing_profile_canary carries profile cleared into "profileCleared": True. + "profileCleared": True, + # What: map the selector composed field as true; why: routing_profile_canary carries selector composed into "selectorComposed": True. + "selectorComposed": True, + # What: map the resolved profile field as model a; why: routing_profile_canary carries resolved profile into "resolvedProfile": "model-a". + "resolvedProfile": "model-a", + # What: map the activation delta field as 0; why: routing_profile_canary carries activation delta into "activationDelta": 0. + "activationDelta": 0, + # What: map the passed field as true; why: routing_profile_canary carries passed into "passed": True. + "passed": True, + # What: complete the enclosing predicate mapping with profile activated and profile cleared and selector composed and resolved profile and activation delta; why: routing_profile_canary groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + +# What: define capture_hardware around base and artifacts and label; why: its direct callers call capture_hardware for capture hardware and rely on this exact input and result contract. +def capture_hardware(base: str, artifacts: Path, label: str) -> dict: + """Keep per-trial process and memory observations in the private artifact set.""" + # What: document keep per trial process and memory observations in the capture_hardware docstring; why: introspection and maintainers read this exact docstring fragment to understand capture hardware behavior without executing it. + # What: compute raw and hardware from request json and base and router and hardware; why: artifacts f label hardware json write bytes raw later reads raw and hardware, so capture_hardware must retain the computed value under that name. + raw, hardware = request_json(base + "/router/hardware") + # What: compute engine from get and hardware and engine; why: if not isinstance engine dict or later reads engine, so capture_hardware must retain the computed value under that name. + engine = hardware.get("engine") + # What: compute memory from get and hardware and memory; why: if not isinstance engine dict or later reads memory, so capture_hardware must retain the computed value under that name. + memory = hardware.get("memory") + # What: gate on isinstance and engine and dict and memory before runtime error; why: capture_hardware admits runtime error only for this predicate and excludes the opposite state. + if not isinstance(engine, dict) or not isinstance(memory, dict): + # What: raise RuntimeError for the caller; why: capture_hardware stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router hardware observation has an invalid shape") + # What: gate on get and isinstance and int and engine before runtime error; why: capture_hardware admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call engine.get with running; why: capture_hardware invokes engine.get while performing or not isinstance engine get pid int; the call advances that operation through its result or side effect. + not engine.get("running") + # What: call isinstance with get and engine and pid and int; why: capture_hardware invokes isinstance while performing or engine pid; the call advances that operation through its result or side effect. + or not isinstance(engine.get("pid"), int) + # What: apply the or engine pid portion of the enclosing predicate; why: this clause remains in capture_hardware\'s enclosing expression so its grouping and evaluation order stay intact. + or engine["pid"] <= 0 + # What: call isinstance with get and engine and port and int; why: capture_hardware invokes isinstance while performing or not engine port; the call advances that operation through its result or side effect. + or not isinstance(engine.get("port"), int) + # What: apply the or not engine port portion of the enclosing predicate; why: this clause remains in capture_hardware\'s enclosing expression so its grouping and evaluation order stay intact. + or not 1 <= engine["port"] <= 65535 + # What: complete the enclosing predicate with if not engine get running or not isinstance engine get pid; why: capture_hardware groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: capture_hardware stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router hardware observation does not identify a running engine") + # What: gate on all and isinstance and int and key and get before runtime error; why: capture_hardware admits runtime error only for this predicate and excludes the opposite state. + if not all(isinstance(memory.get(key), int) for key in ("ramBytes", "vramBytes")): + # What: raise RuntimeError for the caller; why: capture_hardware stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router hardware observation lacks byte measurements") + # What: gate on get and memory before runtime error; why: capture_hardware admits runtime error only for this predicate and excludes the opposite state. + if memory.get("ramAvailable") is not True or memory.get("vramAvailable") is not True: + # What: raise RuntimeError for the caller; why: capture_hardware stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router hardware observation contains unavailable memory measurements") + # What: gate on memory before runtime error; why: capture_hardware admits runtime error only for this predicate and excludes the opposite state. + if memory["ramBytes"] <= 0 or memory["vramBytes"] <= 0: + # What: raise RuntimeError for the caller; why: capture_hardware stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router hardware observation contains non-positive memory measurements") + # What: gate on all and key and isinstance and str and memory before runtime error; why: capture_hardware admits runtime error only for this predicate and excludes the opposite state. + if not all(isinstance(memory.get(key), str) and memory[key] for key in ("ramSource", "vramSource")): + # What: raise RuntimeError for the caller; why: capture_hardware stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router hardware observation lacks memory measurement sources") + # What: preserve the exact artifacts f label hardware json write bytes raw literal fragment; why: capture_hardware passes this fragment verbatim through (artifacts / f"{label}.hardware.json").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / f"{label}.hardware.json").write_bytes(raw) + # What: return hardware from capture_hardware; why: capture_hardware exposes hardware so its caller can continue with the function\'s computed outcome. + return hardware + + +# What: define validate_re_adoption around before and after and router; why: its direct callers call validate_re_adoption for validate re adoption and rely on this exact input and result contract. +def validate_re_adoption(before: dict, after: dict, router: dict) -> dict: + """Validate that a replacement daemon bound, rather than replaced, one engine.""" + # What: document validate that a replacement daemon bound in the validate_re_adoption docstring; why: introspection and maintainers read this exact docstring fragment to understand validate re adoption behavior without executing it. + # What: compute old pid and old port from get and before and pid and port; why: or not isinstance old pid int or later reads old pid and old port, so validate_re_adoption must retain the computed value under that name. + old_pid, old_port = before.get("pid"), before.get("port") + # What: gate on old pid and get and isinstance and int and old port before runtime error; why: validate_re_adoption admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call before.get with running; why: validate_re_adoption invokes before.get while performing or not isinstance old pid int or; the call advances that operation through its result or side effect. + not before.get("running") + # What: call isinstance with old pid and int; why: validate_re_adoption invokes isinstance while performing or not isinstance old port int or; the call advances that operation through its result or side effect. + or not isinstance(old_pid, int) or old_pid <= 0 + # What: call isinstance with old port and int; why: validate_re_adoption consumes the isinstance return value while evaluating or not isinstance(old_port, int) or not 1 <= old_port <= 65535. + or not isinstance(old_port, int) or not 1 <= old_port <= 65535 + # What: complete the enclosing predicate with if not before get running or not isinstance old pid int; why: validate_re_adoption groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: validate_re_adoption stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("pre-restart engine identity is invalid") + # What: gate on old pid and old port and get and after and router before runtime error; why: validate_re_adoption admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call after.get with pid; why: validate_re_adoption invokes after.get while performing or after get port old port; the call advances that operation through its result or side effect. + after.get("pid") != old_pid + # What: call after.get with port; why: validate_re_adoption invokes after.get while performing or after get adopted is not; the call advances that operation through its result or side effect. + or after.get("port") != old_port + # What: call after.get with adopted; why: validate_re_adoption invokes after.get while performing or router get active profile model a; the call advances that operation through its result or side effect. + or after.get("adopted") is not True + # What: call router.get with active profile; why: validate_re_adoption invokes router.get while performing or router get active identity matches engine is not; the call advances that operation through its result or side effect. + or router.get("activeProfile") != "model-a" + # What: call router.get with active identity matches engine; why: validate_re_adoption invokes router.get while performing or router get activations; the call advances that operation through its result or side effect. + or router.get("activeIdentityMatchesEngine") is not True + # What: call router.get with activations; why: validate_re_adoption consumes the router.get return value while evaluating or router.get("activations") != 0. + or router.get("activations") != 0 + # What: complete the enclosing predicate with if after get pid differs from old pid or after get port; why: validate_re_adoption groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: validate_re_adoption stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("replacement daemon did not bind the exact adopted residency") + # What: return profile and same pid and same port and manager adopted and activation delta from validate_re_adoption; why: validate_re_adoption exposes profile and same pid and same port and manager adopted and activation delta so its caller can continue with the function\'s computed outcome. + return { + # What: map the profile field as model a; why: validate_re_adoption carries profile into "profile": "model-a", "samePid": True, "samePort": True. + "profile": "model-a", "samePid": True, "samePort": True, + # What: map the manager adopted field as true; why: validate_re_adoption carries manager adopted into "managerAdopted": True, "activationDelta": 0. + "managerAdopted": True, "activationDelta": 0, + # What: complete the enclosing predicate mapping with profile and same pid and same port and manager adopted and activation delta; why: validate_re_adoption groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + +# What: define require_listener_closed around port; why: its direct callers call require_listener_closed for require listener closed and rely on this exact input and result contract. +def require_listener_closed(port: int) -> None: + """Fail the qualification if a temporary engine listener survived cleanup.""" + # What: document fail the qualification if a temporary in the require_listener_closed docstring; why: introspection and maintainers read this exact docstring fragment to understand require listener closed behavior without executing it. + # What: enter the socket.socket managed context before connection settimeout; why: require_listener_closed releases this resource or lock after connection settimeout on both success and failure paths. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as connection: + # What: call connection.settimeout with 1; why: require_listener_closed invokes connection.settimeout while performing if connection connect ex port; the call advances that operation through its result or side effect. + connection.settimeout(1) + # What: gate on connect ex and connection and port before runtime error; why: require_listener_closed admits runtime error only for this predicate and excludes the opposite state. + if connection.connect_ex(("127.0.0.1", port)) == 0: + # What: raise RuntimeError for the caller; why: require_listener_closed stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("temporary engine listener remains reachable after cleanup") + + +# What: define require_listener_open around port; why: its direct callers call require_listener_open for require listener open and rely on this exact input and result contract. +def require_listener_open(port: int) -> None: + """Require a detached test-owned engine to remain reachable for re-adoption.""" + # What: document require a detached test owned engine to in the require_listener_open docstring; why: introspection and maintainers read this exact docstring fragment to understand require listener open behavior without executing it. + # What: enter the socket.socket managed context before connection settimeout; why: require_listener_open releases this resource or lock after connection settimeout on both success and failure paths. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as connection: + # What: call connection.settimeout with 1; why: require_listener_open invokes connection.settimeout while performing if connection connect ex port; the call advances that operation through its result or side effect. + connection.settimeout(1) + # What: gate on connect ex and connection and port before runtime error; why: require_listener_open admits runtime error only for this predicate and excludes the opposite state. + if connection.connect_ex(("127.0.0.1", port)) != 0: + # What: raise RuntimeError for the caller; why: require_listener_open stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("detached engine listener did not survive daemon restart") + + +# What: define stop_detached_engine around pid and port; why: its direct callers call stop_detached_engine for stop detached engine and rely on this exact input and result contract. +def stop_detached_engine(pid: int, port: int) -> None: + """Best-effort cleanup for the exact test-owned engine during a restart gap.""" + # What: document best effort cleanup for the exact test owned in the stop_detached_engine docstring; why: introspection and maintainers read this exact docstring fragment to understand stop detached engine behavior without executing it. + # What: establish the handler boundary for the protected operation; why: stop_detached_engine routes failures to process lookup error while preserving cleanup and success flow. + try: + # What: call os.killpg with pid and sigterm and signal; why: stop_detached_engine invokes os.killpg while performing except process lookup error; the call advances that operation through its result or side effect. + os.killpg(pid, signal.SIGTERM) + # What: handle process lookup error by return; why: stop_detached_engine converts that failure into this concrete recovery, response, or cleanup behavior. + except ProcessLookupError: + # What: return no value from stop_detached_engine; why: stop_detached_engine returns no value to callers that depend on its completed result. + return + # What: compute deadline from monotonic and time and 15; why: while time monotonic deadline later reads deadline, so stop_detached_engine must retain the computed value under that name. + deadline = time.monotonic() + 15 + # What: iterate across deadline and monotonic and time to perform runtime error and require listener closed and port and sleep and time; why: stop_detached_engine repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: establish the handler boundary for the protected operation; why: stop_detached_engine routes failures to runtime error while preserving cleanup and success flow. + try: + # What: call require_listener_closed with port; why: stop_detached_engine invokes require_listener_closed while performing return; the call advances that operation through its result or side effect. + require_listener_closed(port) + # What: return no value from stop_detached_engine; why: stop_detached_engine returns no value to callers that depend on its completed result. + return + # What: handle runtime error by time sleep 0 1; why: stop_detached_engine converts that failure into this concrete recovery, response, or cleanup behavior. + except RuntimeError: + # What: call time.sleep with 0 1; why: stop_detached_engine invokes time.sleep while performing try; the call advances that operation through its result or side effect. + time.sleep(0.1) + # What: establish the handler boundary for the protected operation; why: stop_detached_engine routes failures to process lookup error while preserving cleanup and success flow. + try: + # What: call os.killpg with pid and sigkill and signal; why: stop_detached_engine invokes os.killpg while performing except process lookup error; the call advances that operation through its result or side effect. + os.killpg(pid, signal.SIGKILL) + # What: handle process lookup error by pass; why: stop_detached_engine converts that failure into this concrete recovery, response, or cleanup behavior. + except ProcessLookupError: + # What: ignore the anticipated exception handled by this branch; why: stop_detached_engine continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: compute deadline from monotonic and time and 5; why: while time monotonic deadline later reads deadline, so stop_detached_engine must retain the computed value under that name. + deadline = time.monotonic() + 5 + # What: iterate across deadline and monotonic and time to perform runtime error and require listener closed and port and sleep and time; why: stop_detached_engine repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: establish the handler boundary for the protected operation; why: stop_detached_engine routes failures to runtime error while preserving cleanup and success flow. + try: + # What: call require_listener_closed with port; why: stop_detached_engine invokes require_listener_closed while performing return; the call advances that operation through its result or side effect. + require_listener_closed(port) + # What: return no value from stop_detached_engine; why: stop_detached_engine returns no value to callers that depend on its completed result. + return + # What: handle runtime error by time sleep 0 1; why: stop_detached_engine converts that failure into this concrete recovery, response, or cleanup behavior. + except RuntimeError: + # What: call time.sleep with 0 1; why: stop_detached_engine invokes time.sleep while performing raise runtime error detached test owned engine survived; the call advances that operation through its result or side effect. + time.sleep(0.1) + # What: raise RuntimeError for the caller; why: stop_detached_engine stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("detached test-owned engine survived cleanup") + + +# What: define reload_conflict_canary around base and catalog path and model a and model b and api key; why: its direct callers call reload_conflict_canary for reload conflict canary and rely on this exact input and result contract. +def reload_conflict_canary( + # What: declare the base input for reload_conflict_canary; why: reload_conflict_canary consumes base during value status request json base router status, so callers must bind it with the other signature inputs. + base: str, catalog_path: Path, model_a: str, model_b: str, *, api_key: str | None = None +# What: complete the enclosing predicate with dict; why: reload_conflict_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. +) -> dict: + """Prove an active profile's scheduler policy cannot change under its engine.""" + # What: document prove an active profile s scheduler in the reload_conflict_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand reload conflict canary behavior without executing it. + # What: call catalog_path.write_text with native catalog text and model a and model b and api key and 1; why: reload_conflict_canary invokes catalog_path.write_text while performing native catalog text model a model b model a priority api key api key; the call advances that operation through its result or side effect. + catalog_path.write_text( + # What: supply model a priority to native_catalog_text; why: reload_conflict_canary binds this 1 value to native_catalog_text's model a priority input. + native_catalog_text(model_a, model_b, model_a_priority=1, api_key=api_key), + # What: preserve the exact encoding utf 8 literal fragment; why: reload_conflict_canary passes this fragment verbatim through encoding="utf-8", because changing it would alter a protocol payload, serialized fixture, or public message. + encoding="utf-8", + # What: complete the catalog_path.write_text call with encoding; why: reload_conflict_canary groups the supplied clauses as one catalog_path.write_text call before its value is consumed. + ) + # What: establish the handler boundary for the protected operation; why: reload_conflict_canary routes failures to httperror and error and urllib while preserving cleanup and success flow. + try: + # What: preserve the exact request json base router reload timeout literal fragment; why: reload_conflict_canary passes this fragment verbatim through request_json(base + "/router/reload", {}, timeout=30), because changing it would alter a protocol payload, serialized fixture, or public message. + request_json(base + "/router/reload", {}, timeout=30) + # What: handle httperror and error and urllib by if exc code differs from 409; why: reload_conflict_canary converts that failure into this concrete recovery, response, or cleanup behavior. + except urllib.error.HTTPError as exc: + # What: gate on code and exc before exc and runtime error; why: reload_conflict_canary admits exc and runtime error only for this predicate and excludes the opposite state. + if exc.code != 409: + # What: raise RuntimeError for the caller; why: reload_conflict_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("active catalog conflict returned the wrong status") from exc + # What: select the remaining branch that performs raise runtime error active catalog scheduler redefinition; why: reload_conflict_canary covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: raise RuntimeError for the caller; why: reload_conflict_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("active catalog scheduler redefinition was accepted") + # What: compute and status from request json and base and router and status and 30; why: the enclosing return or state update later reads and status, so reload_conflict_canary must retain the computed value under that name. + _, status = request_json(base + "/router/status", timeout=30) + # What: gate on get and status before runtime error; why: reload_conflict_canary admits runtime error only for this predicate and excludes the opposite state. + if status.get("activeProfile") != "model-a" or status.get("activeIdentityMatchesEngine") is not True: + # What: raise RuntimeError for the caller; why: reload_conflict_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("rejected catalog replacement changed active engine identity") + # What: return active profile and rejected status and active identity preserved and passed and model a from reload_conflict_canary; why: qualify_native_router needs this line to preserve the surrounding expression or collection structure. + return { + # What: map the active profile field as model a; why: reload_conflict_canary carries active profile into "activeProfile": "model-a". + "activeProfile": "model-a", + # What: map the rejected status field as 409; why: reload_conflict_canary carries rejected status into "rejectedStatus": 409. + "rejectedStatus": 409, + # What: map the active identity preserved field as true; why: reload_conflict_canary carries active identity preserved into "activeIdentityPreserved": True. + "activeIdentityPreserved": True, + # What: map the passed field as true; why: reload_conflict_canary carries passed into "passed": True. + "passed": True, + # What: complete the enclosing predicate mapping with active profile and rejected status and active identity preserved and passed; why: reload_conflict_canary groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + +# What: define failed_switch_canary around base and model and restored model; why: its direct callers call failed_switch_canary for failed switch canary and rely on this exact input and result contract. +def failed_switch_canary(base: str, model: str, restored_model: str) -> tuple[bytes, bytes, dict]: + """Require a failed disposable load to restore the prior resident engine.""" + # What: document require a failed disposable load to in the failed_switch_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand failed switch canary behavior without executing it. + # What: compute and before from request json and base and router and status; why: value pending before request json base accounting pending later reads and before, so failed_switch_canary must retain the computed value under that name. + _, before = request_json(base + "/router/status") + # What: compute and pending before from request json and base and accounting and pending; why: value after request json base router status later reads and pending before, so failed_switch_canary must retain the computed value under that name. + _, pending_before = request_json(base + "/accounting/pending") + # What: compute receipts before from get and pending before and receipts; why: if not isinstance receipts before list later reads receipts before, so failed_switch_canary must retain the computed value under that name. + receipts_before = pending_before.get("receipts") + # What: gate on isinstance and receipts before and list before runtime error; why: failed_switch_canary admits runtime error only for this predicate and excludes the opposite state. + if not isinstance(receipts_before, list): + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("accounting outbox response has an invalid shape") + # What: compute before ids from get and receipt and receipts before and isinstance; why: new receipts after ids before ids later reads before ids, so failed_switch_canary must retain the computed value under that name. + before_ids = { + # What: call receipt.get with receipt id; why: failed_switch_canary invokes receipt.get while performing if isinstance receipt dict and isinstance; the call advances that operation through its result or side effect. + receipt.get("receiptId") for receipt in receipts_before + # What: call isinstance with receipt and dict; why: failed_switch_canary consumes the isinstance return value while evaluating if isinstance(receipt, dict) and isinstance(receipt.get("receiptId"), st. + if isinstance(receipt, dict) and isinstance(receipt.get("receiptId"), str) + # What: complete the before_ids expression with before ids receipt get receipt id for receipt in receipts before if isinstance; why: failed_switch_canary groups the supplied clauses as one before_ids expression before its value is consumed. + } + # What: compute prior failures from get and before and activation failures; why: or not isinstance prior failures int later reads prior failures, so failed_switch_canary must retain the computed value under that name. + prior_failures = before.get("activationFailures") + # What: gate on restored model and get and isinstance and prior failures and int before runtime error; why: failed_switch_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call before.get with active profile; why: failed_switch_canary invokes before.get while performing or before get active identity matches engine is not; the call advances that operation through its result or side effect. + before.get("activeProfile") != restored_model + # What: call before.get with active identity matches engine; why: failed_switch_canary invokes before.get while performing or not isinstance prior failures int; the call advances that operation through its result or side effect. + or before.get("activeIdentityMatchesEngine") is not True + # What: call isinstance with prior failures and int; why: failed_switch_canary consumes the isinstance return value while evaluating or not isinstance(prior_failures, int). + or not isinstance(prior_failures, int) + # What: complete the enclosing predicate with if before get active profile differs from restored model or before get active identity matches engine; why: failed_switch_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("failed-switch qualification requires an exact healthy resident") + # What: compute failure raw from the named fixture input; why: failure raw exc read later reads failure raw, so failed_switch_canary must retain the computed value under that name. + failure_raw = b"" + # What: establish the handler boundary for the protected operation; why: failed_switch_canary routes failures to httperror and error and urllib while preserving cleanup and success flow. + try: + # What: map the name field as model; why: failed_switch_canary carries name into request_json(base + "/router/load", {"name": model}, timeout=90). + request_json(base + "/router/load", {"name": model}, timeout=90) + # What: handle httperror and error and urllib by failure raw exc read 1024 1024 1; why: failed_switch_canary converts that failure into this concrete recovery, response, or cleanup behavior. + except urllib.error.HTTPError as exc: + # What: compute failure raw from read and exc and 1 and 1024 and 1024; why: if exc code or len failure raw later reads failure raw, so failed_switch_canary must retain the computed value under that name. + failure_raw = exc.read(1024 * 1024 + 1) + # What: gate on code and exc and len and failure raw before exc and runtime error; why: failed_switch_canary admits exc and runtime error only for this predicate and excludes the opposite state. + if exc.code != 503 or len(failure_raw) > 1024 * 1024: + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("failed replacement returned an invalid bounded response") from exc + # What: select the remaining branch that performs raise runtime error disposable invalid model unexpectedly; why: failed_switch_canary covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("disposable invalid model unexpectedly activated") + # What: establish the handler boundary for the protected operation; why: failed_switch_canary routes failures to unicode decode error and jsondecode error and json while preserving cleanup and success flow. + try: + # What: compute failure from loads and failure raw and json; why: error failure get error if isinstance failure later reads failure, so failed_switch_canary must retain the computed value under that name. + failure = json.loads(failure_raw) + # What: handle unicode decode error and jsondecode error and json by raise runtime error failed replacement response was not; why: failed_switch_canary converts that failure into this concrete recovery, response, or cleanup behavior. + except (UnicodeDecodeError, json.JSONDecodeError) as exc: + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("failed replacement response was not JSON") from exc + # What: compute error from isinstance and failure and dict and get and error; why: if not isinstance error dict or later reads error, so failed_switch_canary must retain the computed value under that name. + error = failure.get("error") if isinstance(failure, dict) else None + # What: compute recovery from isinstance and failure and dict and get and recovery; why: if not isinstance recovery dict or later reads recovery, so failed_switch_canary must retain the computed value under that name. + recovery = failure.get("recovery") if isinstance(failure, dict) else None + # What: gate on isinstance and error and dict and get before runtime error; why: failed_switch_canary admits runtime error only for this predicate and excludes the opposite state. + if not isinstance(error, dict) or error.get("type") not in {"engine_not_ready", "switch_launch_failed"}: + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("failed replacement did not report a lifecycle failure") + # What: gate on isinstance and recovery and dict and get before runtime error; why: failed_switch_canary admits runtime error only for this predicate and excludes the opposite state. + if not isinstance(recovery, dict) or recovery.get("launched") is not True: + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("failed replacement did not report successful rollback launch") + # What: compute and after from request json and base and router and status and 30; why: value pending after request json base accounting pending later reads and after, so failed_switch_canary must retain the computed value under that name. + _, after = request_json(base + "/router/status", timeout=30) + # What: gate on restored model and get and prior failures and after before runtime error; why: failed_switch_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call after.get with active profile; why: failed_switch_canary invokes after.get while performing or after get active identity matches engine is not; the call advances that operation through its result or side effect. + after.get("activeProfile") != restored_model + # What: call after.get with active identity matches engine; why: failed_switch_canary invokes after.get while performing or after get active requests; the call advances that operation through its result or side effect. + or after.get("activeIdentityMatchesEngine") is not True + # What: call after.get with active requests; why: failed_switch_canary invokes after.get while performing or after get activation failures prior failures; the call advances that operation through its result or side effect. + or after.get("activeRequests") != 0 + # What: call after.get with activation failures; why: failed_switch_canary consumes the after.get return value while evaluating or after.get("activationFailures") != prior_failures + 1. + or after.get("activationFailures") != prior_failures + 1 + # What: complete the enclosing predicate with if after get active profile differs from restored model or after get active identity matches engine; why: failed_switch_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("failed replacement did not restore exact idle residency") + # What: compute and pending after from request json and base and accounting and pending; why: the enclosing return or state update later reads and pending after, so failed_switch_canary must retain the computed value under that name. + _, pending_after = request_json(base + "/accounting/pending") + # What: compute receipts after from get and pending after and receipts; why: if not isinstance receipts after list later reads receipts after, so failed_switch_canary must retain the computed value under that name. + receipts_after = pending_after.get("receipts") + # What: gate on isinstance and receipts after and list before runtime error; why: failed_switch_canary admits runtime error only for this predicate and excludes the opposite state. + if not isinstance(receipts_after, list): + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("post-failure accounting outbox response has an invalid shape") + # What: compute after ids from get and receipt and receipts after and isinstance; why: new receipts after ids before ids later reads after ids, so failed_switch_canary must retain the computed value under that name. + after_ids = { + # What: call receipt.get with receipt id; why: failed_switch_canary invokes receipt.get while performing if isinstance receipt dict and isinstance; the call advances that operation through its result or side effect. + receipt.get("receiptId") for receipt in receipts_after + # What: call isinstance with receipt and dict; why: failed_switch_canary consumes the isinstance return value while evaluating if isinstance(receipt, dict) and isinstance(receipt.get("receiptId"), st. + if isinstance(receipt, dict) and isinstance(receipt.get("receiptId"), str) + # What: complete the after_ids expression with after ids receipt get receipt id for receipt in receipts after if isinstance; why: failed_switch_canary groups the supplied clauses as one after_ids expression before its value is consumed. + } + # What: compute new receipts from after ids and before ids; why: if not new receipts later reads new receipts, so failed_switch_canary must retain the computed value under that name. + new_receipts = after_ids - before_ids + # What: gate on new receipts before runtime error; why: failed_switch_canary admits runtime error only for this predicate and excludes the opposite state. + if not new_receipts: + # What: raise RuntimeError for the caller; why: failed_switch_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("failed switch produced no new durable accounting receipt") + # What: compute restored raw and restored from canary and base and restored model and false; why: return failure raw restored raw later reads restored raw and restored, so failed_switch_canary must retain the computed value under that name. + restored_raw, restored = canary(base, restored_model, direct=False) + # What: return failure raw and restored raw and model and restored model from failed_switch_canary; why: failed_switch_canary exposes failure raw and restored raw and model and restored model so its caller can continue with the function\'s computed outcome. + return failure_raw, restored_raw, { + # What: map the failed profile field as model; why: failed_switch_canary carries failed profile into "failedProfile": model. + "failedProfile": model, + # What: map the restored profile field as restored model; why: failed_switch_canary carries restored profile into "restoredProfile": restored_model. + "restoredProfile": restored_model, + # What: map the failure type field as error and type; why: failed_switch_canary carries failure type into "failureType": error["type"]. + "failureType": error["type"], + # What: map the rollback launched field as true; why: failed_switch_canary carries rollback launched into "rollbackLaunched": True. + "rollbackLaunched": True, + # What: map the activation failure incremented field as true; why: failed_switch_canary carries activation failure incremented into "activationFailureIncremented": True. + "activationFailureIncremented": True, + # What: map the new accounting receipt count field as len and new receipts; why: failed_switch_canary carries new accounting receipt count into "newAccountingReceiptCount": len(new_receipts). + "newAccountingReceiptCount": len(new_receipts), + # What: map the restored completion passed field as get and restored and true and passed; why: failed_switch_canary carries restored completion passed into "restoredCompletionPassed": restored.get("passed") is True. + "restoredCompletionPassed": restored.get("passed") is True, + # What: map the passed field as get and restored and true and passed; why: failed_switch_canary carries passed into "passed": restored.get("passed") is True. + "passed": restored.get("passed") is True, + # What: complete the enclosing predicate collection with failure raw and restored raw and model and restored model and error and len; why: failed_switch_canary groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + } + + +# What: define persistent_capacity_canary around base and catalog path and model a and model b and api key; why: its direct callers call persistent_capacity_canary for persistent capacity canary and rely on this exact input and result contract. +def persistent_capacity_canary( + # What: declare the base input for persistent_capacity_canary; why: persistent_capacity_canary consumes base during value loaded a request json base router load, so callers must bind it with the other signature inputs. + base: str, catalog_path: Path, model_a: str, model_b: str, *, api_key: str | None = None +# What: complete the enclosing predicate collection with bytes and dict; why: persistent_capacity_canary groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. +) -> tuple[bytes, dict]: + """Prove a singleton persistent group reserves the sole resident slot.""" + # What: document prove a singleton persistent group reserves in the persistent_capacity_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand persistent capacity canary behavior without executing it. + # What: gate on get and request json and base before runtime error; why: persistent_capacity_canary admits runtime error only for this predicate and excludes the opposite state. + if request_json(base + "/router/unload", {}, timeout=45)[1].get("unloaded") is not True: + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("could not unload before persistent capacity qualification") + # What: call catalog_path.write_text; why: qualify_native_router needs this line to preserve the surrounding expression or collection structure. + catalog_path.write_text( + # What: supply persistent a to native_catalog_text; why: persistent_capacity_canary binds this true value to native_catalog_text's persistent a input. + native_catalog_text(model_a, model_b, persistent_a=True, api_key=api_key), + # What: preserve the exact encoding utf 8 literal fragment; why: persistent_capacity_canary passes this fragment verbatim through encoding="utf-8", because changing it would alter a protocol payload, serialized fixture, or public message. + encoding="utf-8", + # What: complete the catalog_path.write_text call with encoding; why: persistent_capacity_canary groups the supplied clauses as one catalog_path.write_text call before its value is consumed. + ) + # What: gate on get and request json and base before runtime error; why: persistent_capacity_canary admits runtime error only for this predicate and excludes the opposite state. + if request_json(base + "/router/reload", {}, timeout=30)[1].get("reloaded") is not True: + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("persistent catalog reload was not acknowledged") + # What: map the name field as model a; why: persistent_capacity_canary carries name through and loaded a into value still a request json base router status. + _, loaded_a = request_json(base + "/router/load", {"name": "model-a"}, timeout=660) + # What: compute router a and pid a from get and loaded a and router and pid; why: or not isinstance router a dict or later reads router a and pid a, so persistent_capacity_canary must retain the computed value under that name. + router_a, pid_a = loaded_a.get("router"), loaded_a.get("pid") + # What: gate on pid a and get and isinstance and int and router a before runtime error; why: persistent_capacity_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call loaded_a.get with profile; why: persistent_capacity_canary invokes loaded_a.get while performing or not isinstance router a dict or; the call advances that operation through its result or side effect. + loaded_a.get("profile") != "model-a" or not isinstance(pid_a, int) or pid_a <= 0 + # What: call isinstance with router a and dict; why: persistent_capacity_canary invokes isinstance while performing or router a get active identity matches engine is not; the call advances that operation through its result or side effect. + or not isinstance(router_a, dict) or router_a.get("persistent") is not True + # What: call router_a.get with active identity matches engine; why: persistent_capacity_canary consumes the router_a.get return value while evaluating or router_a.get("activeIdentityMatchesEngine") is not True. + or router_a.get("activeIdentityMatchesEngine") is not True + # What: complete the enclosing predicate with if loaded a get profile differs from model a or not isinstance; why: persistent_capacity_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("model-a did not occupy the persistent resident slot") + # What: compute rejection raw from the named fixture input; why: rejection raw exc read later reads rejection raw, so persistent_capacity_canary must retain the computed value under that name. + rejection_raw = b"" + # What: establish the handler boundary for the protected operation; why: persistent_capacity_canary routes failures to httperror and error and urllib while preserving cleanup and success flow. + try: + # What: map the name field as model b; why: persistent_capacity_canary carries name through request_json(base + "/router/load", {"name": "model-b"}, timeout=30) into raise runtime error persistent capacity conflict returned the. + request_json(base + "/router/load", {"name": "model-b"}, timeout=30) + # What: handle httperror and error and urllib by rejection raw exc read 1024 1024 1; why: persistent_capacity_canary converts that failure into this concrete recovery, response, or cleanup behavior. + except urllib.error.HTTPError as exc: + # What: compute rejection raw from read and exc and 1 and 1024 and 1024; why: if exc code or len rejection raw later reads rejection raw, so persistent_capacity_canary must retain the computed value under that name. + rejection_raw = exc.read(1024 * 1024 + 1) + # What: gate on code and exc and len and rejection raw before exc and runtime error; why: persistent_capacity_canary admits exc and runtime error only for this predicate and excludes the opposite state. + if exc.code != 409 or len(rejection_raw) > 1024 * 1024: + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("persistent capacity conflict returned an invalid response") from exc + # What: select the remaining branch that performs raise runtime error persistent resident allowed a; why: persistent_capacity_canary covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("persistent resident allowed a conflicting activation") + # What: establish the handler boundary for the protected operation; why: persistent_capacity_canary routes failures to unicode decode error and jsondecode error and json while preserving cleanup and success flow. + try: + # What: compute rejection from loads and rejection raw and json; why: if rejection get error get type capacity unavailable later reads rejection, so persistent_capacity_canary must retain the computed value under that name. + rejection = json.loads(rejection_raw) + # What: handle unicode decode error and jsondecode error and json by raise runtime error persistent capacity response was not; why: persistent_capacity_canary converts that failure into this concrete recovery, response, or cleanup behavior. + except (UnicodeDecodeError, json.JSONDecodeError) as exc: + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("persistent capacity response was not JSON") from exc + # What: gate on get and rejection before runtime error; why: persistent_capacity_canary admits runtime error only for this predicate and excludes the opposite state. + if rejection.get("error", {}).get("type") != "capacity_unavailable": + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("persistent capacity conflict returned the wrong error type") + # What: compute and still a from request json and base and router and status; why: value loaded b request json base router load later reads and still a, so persistent_capacity_canary must retain the computed value under that name. + _, still_a = request_json(base + "/router/status") + # What: gate on pid a and get and still a and request json and base before runtime error; why: persistent_capacity_canary admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call still_a.get with active profile; why: persistent_capacity_canary invokes still_a.get while performing or still a get active identity matches engine is not; the call advances that operation through its result or side effect. + still_a.get("activeProfile") != "model-a" + # What: call still_a.get with active identity matches engine; why: persistent_capacity_canary invokes still_a.get while performing or still a get persistent is not; the call advances that operation through its result or side effect. + or still_a.get("activeIdentityMatchesEngine") is not True + # What: call still_a.get with persistent; why: persistent_capacity_canary invokes still_a.get while performing or request json base engine status get; the call advances that operation through its result or side effect. + or still_a.get("persistent") is not True + # What: call operation.get with pid; why: persistent_capacity_canary consumes the operation.get return value while evaluating or request_json(base + "/engine/status")[1].get("pid") != pid_a. + or request_json(base + "/engine/status")[1].get("pid") != pid_a + # What: complete the enclosing predicate with if still a get active profile differs from model a or still a get active identity matches engine; why: persistent_capacity_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("persistent capacity rejection disturbed the resident engine") + # What: map the name field as model a; why: persistent_capacity_canary carries name into if request_json(base + "/router/unload", {"name": "model-a"}, timeout=45. + if request_json(base + "/router/unload", {"name": "model-a"}, timeout=45)[1].get("unloaded") is not True: + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("explicit persistent unload failed") + # What: map the name field as model b; why: persistent_capacity_canary carries name through and loaded b into the enclosing return or state update. + _, loaded_b = request_json(base + "/router/load", {"name": "model-b"}, timeout=660) + # What: gate on get and loaded b before runtime error; why: persistent_capacity_canary admits runtime error only for this predicate and excludes the opposite state. + if loaded_b.get("profile") != "model-b" or loaded_b.get("router", {}).get("activeIdentityMatchesEngine") is not True: + # What: raise RuntimeError for the caller; why: persistent_capacity_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("released persistent capacity did not admit model-b") + # What: return rejection raw; why: the caller consumes this value as the function’s success-path result. + return rejection_raw, { + # What: map the persistent profile field as model a; why: persistent_capacity_canary carries persistent profile into "persistentProfile": "model-a", "conflictingProfile": "model-b". + "persistentProfile": "model-a", "conflictingProfile": "model-b", + # What: map the rejected status field as 409; why: persistent_capacity_canary carries rejected status into "rejectedStatus": 409, "residentPidPreserved": True. + "rejectedStatus": 409, "residentPidPreserved": True, + # What: map the explicit unload released capacity field as true; why: persistent_capacity_canary carries explicit unload released capacity into "explicitUnloadReleasedCapacity": True, "passed": True. + "explicitUnloadReleasedCapacity": True, "passed": True, + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + } + + +# What: define ttl_eviction_canary around base and catalog path and model a and model b and seconds and api key; why: its direct callers call ttl_eviction_canary for ttl eviction canary and rely on this exact input and result contract. +def ttl_eviction_canary( + # What: declare the base input for ttl_eviction_canary; why: ttl_eviction_canary consumes base during value before request json base router status, so callers must bind it with the other signature inputs. + base: str, catalog_path: Path, model_a: str, model_b: str, *, seconds: float = 45, + # What: declare the api key input for ttl_eviction_canary; why: ttl_eviction_canary consumes api key during native catalog text model a model b ttl s api key api key, so callers must bind it with the other signature inputs. + api_key: str | None = None, +# What: complete the enclosing predicate with dict; why: ttl_eviction_canary groups the supplied clauses as one enclosing predicate expression before its value is consumed. +) -> dict: + """Exercise idle-TTL ownership cleanup against the temporary catalog only.""" + # What: document exercise idle ttl ownership cleanup against the in the ttl_eviction_canary docstring; why: introspection and maintainers read this exact docstring fragment to understand ttl eviction canary behavior without executing it. + # What: compute and before from request json and base and router and status; why: value unloaded request json base router unload later reads and before, so ttl_eviction_canary must retain the computed value under that name. + _, before = request_json(base + "/router/status") + # What: compute prior evictions from get and before and evictions; why: if not isinstance prior evictions int later reads prior evictions, so ttl_eviction_canary must retain the computed value under that name. + prior_evictions = before.get("evictions") + # What: gate on isinstance and prior evictions and int before runtime error; why: ttl_eviction_canary admits runtime error only for this predicate and excludes the opposite state. + if not isinstance(prior_evictions, int): + # What: raise RuntimeError for the caller; why: ttl_eviction_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("router status lacks eviction counter") + # What: compute and unloaded from request json and base and router and unload and 45; why: value reloaded request json base router reload later reads and unloaded, so ttl_eviction_canary must retain the computed value under that name. + _, unloaded = request_json(base + "/router/unload", {}, timeout=45) + # What: gate on get and unloaded before runtime error; why: ttl_eviction_canary admits runtime error only for this predicate and excludes the opposite state. + if unloaded.get("unloaded") is not True: + # What: raise RuntimeError for the caller; why: ttl_eviction_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("could not unload the prior resident before TTL qualification") + # What: call catalog_path.write_text with native catalog text and model a and model b and api key and 2; why: ttl_eviction_canary invokes catalog_path.write_text while performing native catalog text model a model b ttl s api key api key; the call advances that operation through its result or side effect. + catalog_path.write_text( + # What: preserve the exact native catalog text model a model b ttl s api key api key literal fragment; why: ttl_eviction_canary passes this fragment verbatim through native_catalog_text(model_a, model_b, ttl_s=2, api_key=api_key), encodin, because changing it would alter a protocol payload, serialized fixture. + native_catalog_text(model_a, model_b, ttl_s=2, api_key=api_key), encoding="utf-8" + # What: complete the catalog_path.write_text call with encoding; why: ttl_eviction_canary groups the supplied clauses as one catalog_path.write_text call before its value is consumed. + ) + # What: compute and reloaded from request json and base and router and reload and 30; why: value loaded request json base router load later reads and reloaded, so ttl_eviction_canary must retain the computed value under that name. + _, reloaded = request_json(base + "/router/reload", {}, timeout=30) + # What: gate on get and reloaded before runtime error; why: ttl_eviction_canary admits runtime error only for this predicate and excludes the opposite state. + if reloaded.get("reloaded") is not True: + # What: raise RuntimeError for the caller; why: ttl_eviction_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("temporary TTL catalog reload was not acknowledged") + # What: map the name field as model a; why: ttl_eviction_canary carries name through and loaded into the enclosing return or state update. + _, loaded = request_json(base + "/router/load", {"name": "model-a"}, timeout=660) + # What: compute port from get and loaded and port; why: if loaded get profile model a or not later reads port, so ttl_eviction_canary must retain the computed value under that name. + port = loaded.get("port") + # What: gate on get and isinstance and port and int and loaded before runtime error; why: ttl_eviction_canary admits runtime error only for this predicate and excludes the opposite state. + if loaded.get("profile") != "model-a" or not isinstance(port, int) or not 1 <= port <= 65535: + # What: raise RuntimeError for the caller; why: ttl_eviction_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("TTL qualification did not activate a concrete model-a engine") + # What: compute deadline from seconds and monotonic and time; why: while time monotonic deadline later reads deadline, so ttl_eviction_canary must retain the computed value under that name. + deadline = time.monotonic() + seconds + # What: compute status from the named fixture input; why: status request json base router status timeout later reads status, so ttl_eviction_canary must retain the computed value under that name. + status: dict | None = None + # What: iterate across deadline and monotonic and time to perform status and request json and base; why: ttl_eviction_canary repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: compute status from request json and base and 1 and router and status; why: if status get active profile is and status get later reads status, so ttl_eviction_canary must retain the computed value under that name. + status = request_json(base + "/router/status", timeout=3)[1] + # What: gate on get and prior evictions and status before the computed value; why: ttl_eviction_canary admits the computed value only for this predicate and excludes the opposite state. + if status.get("activeProfile") is None and status.get("evictions") == prior_evictions + 1: + # What: apply the break portion of the enclosing predicate; why: this clause remains in ttl_eviction_canary\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: call time.sleep with 0 1; why: ttl_eviction_canary invokes time.sleep while performing if status is or status get active profile; the call advances that operation through its result or side effect. + time.sleep(0.1) + # What: gate on status and get and prior evictions before timeout error; why: ttl_eviction_canary admits timeout error only for this predicate and excludes the opposite state. + if status is None or status.get("activeProfile") is not None or status.get("evictions") != prior_evictions + 1: + # What: raise TimeoutError for the caller; why: ttl_eviction_canary stops this rejected path before it can mutate state, dispatch work, or report success. + raise TimeoutError("idle TTL did not evict the temporary resident engine") + # What: call require_listener_closed with port; why: ttl_eviction_canary invokes require_listener_closed while performing return; the call advances that operation through its result or side effect. + require_listener_closed(port) + # What: return port and profile and ttl seconds and port and eviction incremented from ttl_eviction_canary; why: ttl_eviction_canary exposes port and profile and ttl seconds and port and eviction incremented so its caller can continue with the function\'s computed outcome. + return { + # What: map the profile field as model a; why: ttl_eviction_canary carries profile into "profile": "model-a". + "profile": "model-a", + # What: map the ttl seconds field as 2; why: ttl_eviction_canary carries ttl seconds into "ttlSeconds": 2. + "ttlSeconds": 2, + # What: map the port field as port; why: ttl_eviction_canary carries port into "port": port. + "port": port, + # What: map the eviction incremented field as true; why: ttl_eviction_canary carries eviction incremented into "evictionIncremented": True. + "evictionIncremented": True, + # What: map the listener closed field as true; why: ttl_eviction_canary carries listener closed into "listenerClosed": True. + "listenerClosed": True, + # What: map the passed field as true; why: ttl_eviction_canary carries passed into "passed": True. + "passed": True, + # What: complete the enclosing predicate mapping with profile and ttl seconds and port and eviction incremented and listener closed; why: ttl_eviction_canary groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + +# What: define native_catalog_text around model a and model b and ttl s and model a priority and invalid model and persistent a and api key and startup; why: its direct callers call native_catalog_text for native catalog text and rely on this exact input and result contract. +def native_catalog_text( + # What: declare the model a input for native_catalog_text; why: native_catalog_text consumes model a during for alias model in model a model a, so callers must bind it with the other signature inputs. + model_a: str, model_b: str, *, ttl_s: int = 0, model_a_priority: int = 0, + # What: declare the invalid model input for native_catalog_text; why: native_catalog_text consumes invalid model during if invalid model is not, so callers must bind it with the other signature inputs. + invalid_model: str | None = None, persistent_a: bool = False, + # What: declare the api key input for native_catalog_text; why: native_catalog_text consumes api key during if api key is not, so callers must bind it with the other signature inputs. + api_key: str | None = None, startup: bool = False, +# What: complete the enclosing predicate with str; why: native_catalog_text groups the supplied clauses as one enclosing predicate expression before its value is consumed. +) -> str: + """Return the allowlisted, dynamic-port catalog used by the private run.""" + # What: document return the allowlisted dynamic port catalog used in the native_catalog_text docstring; why: introspection and maintainers read this exact docstring fragment to understand native catalog text behavior without executing it. + # What: compute common args from host and 127 0 0 1 and served model name and model id and max seq len override; why: args json dumps common args replace model id alias later reads common args, so native_catalog_text must retain the computed value under that name. + common_args = [ + # What: apply the host served model name model id portion of common args; why: native_catalog_text uses this clause to evaluate common args as one grouped value. + "--host", "127.0.0.1", "--served-model-name", "${MODEL_ID}", + # What: apply the max seq len override num tokens max prefill length portion of common args; why: native_catalog_text uses this clause to evaluate common args as one grouped value. + "--max-seq-len-override", "4096", "--num-tokens", "4096", "--max-prefill-length", "512", + # What: apply the max running requests graph memory ratio portion of common args; why: native_catalog_text uses this clause to evaluate common args as one grouped value. + "--max-running-requests", "1", "--graph", "1", "--memory-ratio", "0.75", + # What: apply the attention backend triton moe backend fused disable pynccl portion of common args; why: native_catalog_text uses this clause to evaluate common args as one grouped value. + "--attention-backend", "triton", "--moe-backend", "fused", "--disable-pynccl", + # What: complete the common_args collection with host and 127 0 0 1 and served model name and model id; why: native_catalog_text groups the supplied clauses as one common_args collection before its value is consumed. + ] + # What: compute catalog from router and upstream timeout s and include aliases in list and true and send loading state; why: catalog f api keys json dumps api key later reads catalog, so native_catalog_text must retain the computed value under that name. + catalog = [ + # What: apply the router upstream timeout s include aliases in list true portion of catalog; why: native_catalog_text uses this clause to evaluate catalog as one grouped value. + "[router]", "upstream_timeout_s = 660", "include_aliases_in_list = true", + # What: apply the send loading state true performance every s portion of catalog; why: native_catalog_text uses this clause to evaluate catalog as one grouped value. + "send_loading_state = true", "performance_every_s = 5", "", + # What: complete the catalog collection with router and upstream timeout s and include aliases in list and true and send loading state and true; why: native_catalog_text groups the supplied clauses as one catalog collection before its value is consumed. + ] + # What: gate on api key before catalog and dumps and api key and json; why: native_catalog_text admits catalog and dumps and api key and json only for this predicate and excludes the opposite state. + if api_key is not None: + # What: compute catalog entry from dumps and api key and json and api keys and value; why: catalog later reads catalog entry, so native_catalog_text must retain the computed value under that name. + catalog[2:2] = [f"api_keys = [{json.dumps(api_key)}]"] + # What: gate on startup before catalog; why: native_catalog_text admits catalog only for this predicate and excludes the opposite state. + if startup: + # What: compute catalog entry from preload model and compat and model a and startup routing profile and coding; why: catalog extend later reads catalog entry, so native_catalog_text must retain the computed value under that name. + catalog[-1:-1] = [ + # What: apply the preload model compat model a portion of catalog entry; why: native_catalog_text uses this clause to evaluate catalog entry as one grouped value. + 'preload_model = "compat/model-a"', + # What: apply the startup routing profile coding portion of catalog entry; why: native_catalog_text uses this clause to evaluate catalog entry as one grouped value. + 'startup_routing_profile = "coding"', + # What: complete the catalog entry collection with preload model and compat and model a and startup routing profile and coding; why: native_catalog_text groups the supplied clauses as one catalog entry collection before its value is consumed. + ] + # What: call catalog.extend with selectors and preferred model and strategy and warm and targets; why: native_catalog_text invokes catalog.extend while performing selectors preferred model strategy warm; the call advances that operation through its result or side effect. + catalog.extend(( + # What: preserve the exact selectors preferred model strategy warm literal fragment; why: native_catalog_text passes this fragment verbatim through "[selectors.preferred-model]", 'strategy = "warm"', because changing it would alter a protocol payload, serialized fixture, or public message. + "[selectors.preferred-model]", 'strategy = "warm"', + # What: preserve the exact targets model b model a name preferred local literal fragment; why: native_catalog_text passes this fragment verbatim through 'targets = ["model-b", "model-a"]', 'name = "Preferred local model"', because changing it would alter a protocol payload, serialized fixture, or public messag. + 'targets = ["model-b", "model-a"]', 'name = "Preferred local model"', + # What: preserve the exact description reuses a ready target before literal fragment; why: native_catalog_text passes this fragment verbatim through 'description = "Reuses a ready target before the ordered cold fallback"', because changing it would alter a protocol payload, serialized fixture, or public messag. + 'description = "Reuses a ready target before the ordered cold fallback"', "", + # What: preserve the exact profiles coding description qualification routing profile literal fragment; why: native_catalog_text passes this fragment verbatim through "[profiles.coding]", 'description = "Qualification routing profile"', because changing it would alter a protocol payload, serialized fixture, or. + "[profiles.coding]", 'description = "Qualification routing profile"', + # What: preserve the exact profiles coding pins profile model preferred model literal fragment; why: native_catalog_text passes this fragment verbatim through "[profiles.coding.pins]", 'profile-model = "preferred-model"', because changing it would alter a protocol payload, serialized fixture, or public message. + "[profiles.coding.pins]", 'profile-model = "preferred-model"', + # What: preserve the exact disabled model literal fragment; why: native_catalog_text passes this fragment verbatim through 'disabled-model = ""', "", because changing it would alter a protocol payload, serialized fixture, or public message. + 'disabled-model = ""', "", + # What: complete the catalog.extend call with ordered positional inputs; why: native_catalog_text groups the supplied clauses as one catalog.extend call before its value is consumed. + )) + # What: gate on persistent a before extend and catalog; why: native_catalog_text admits extend and catalog only for this predicate and excludes the opposite state. + if persistent_a: + # What: call catalog.extend with router and groups and resident and members and model a; why: native_catalog_text invokes catalog.extend while performing router groups resident members model a swap false; the call advances that operation through its result or side effect. + catalog.extend(( + # What: preserve the exact router groups resident members model a swap false literal fragment; why: native_catalog_text passes this fragment verbatim through "[router.groups.resident]", 'members = ["model-a"]', "swap = false", because changing it would alter a protocol payload, serialized fixture, or publi. + "[router.groups.resident]", 'members = ["model-a"]', "swap = false", + # What: preserve the exact exclusive true persistent true literal fragment; why: native_catalog_text passes this fragment verbatim through "exclusive = true", "persistent = true", "", because changing it would alter a protocol payload, serialized fixture, or public message. + "exclusive = true", "persistent = true", "", + # What: complete the catalog.extend call with ordered positional inputs; why: native_catalog_text groups the supplied clauses as one catalog.extend call before its value is consumed. + )) + # What: iterate across model a and model b to perform profile lines and alias and ttl s and dumps and model; why: native_catalog_text repeats the body only while or for the loop header admits an iteration. + for alias, model in (("model-a", model_a), ("model-b", model_b)): + # What: compute profile lines from alias and ttl s and dumps and model; why: profile lines append group resident later reads profile lines, so native_catalog_text must retain the computed value under that name. + profile_lines = [ + # What: call json.dumps with model; why: native_catalog_text invokes json.dumps while performing check endpoint ready proxy http port; the call advances that operation through its result or side effect. + f"[models.{alias}]", f"model = {json.dumps(model)}", "port = 0", "ready_timeout_s = 600", + # What: apply the check endpoint ready proxy http port portion of profile lines; why: native_catalog_text uses this clause to evaluate profile lines as one grouped value. + 'check_endpoint = "/ready"', 'proxy = "http://127.0.0.1:${PORT}"', + # What: call json.dumps with alias; why: native_catalog_text invokes json.dumps while performing upstream timeout s; the call advances that operation through its result or side effect. + f"use_model_name = {json.dumps(alias)}", + # What: apply the upstream timeout s portion of profile lines; why: native_catalog_text uses this clause to evaluate profile lines as one grouped value. + "upstream_timeout_s = 659", + # What: apply the f ttl s ttl s f priority model a priority portion of profile lines; why: native_catalog_text uses this clause to evaluate profile lines as one grouped value. + f"ttl_s = {ttl_s}", f"priority = {model_a_priority if alias == 'model-a' else 0}", + # What: complete the profile_lines collection with alias and models and value and dumps and model and json and model and port and ready timeout s; why: native_catalog_text groups the supplied clauses as one profile_lines collection before its value is consumed. + ] + # What: gate on persistent a and alias before append and profile lines; why: native_catalog_text admits append and profile lines only for this predicate and excludes the opposite state. + if persistent_a and alias == "model-a": + # What: preserve the exact profile lines append group resident literal fragment; why: native_catalog_text passes this fragment verbatim through profile_lines.append('group = "resident"'), because changing it would alter a protocol payload, serialized fixture, or public message. + profile_lines.append('group = "resident"') + # What: gate on alias before append and profile lines; why: native_catalog_text admits append and profile lines only for this predicate and excludes the opposite state. + if alias == "model-a": + # What: preserve the exact profile lines append name qualification model a literal fragment; why: native_catalog_text passes this fragment verbatim through profile_lines.append('name = "Qualification model A"'), because changing it would alter a protocol payload, serialized fixture, or public message. + profile_lines.append('name = "Qualification model A"') + # What: preserve the exact profile lines append aliases compat model a literal fragment; why: native_catalog_text passes this fragment verbatim through profile_lines.append('aliases = ["compat/model-a"]'), because changing it would alter a protocol payload, serialized fixture, or public message. + profile_lines.append('aliases = ["compat/model-a"]') + # What: call profile_lines.extend with replace and alias and dumps and common args; why: native_catalog_text invokes profile_lines.extend while performing args json dumps common args replace model id alias; the call advances that operation through its result or side effect. + profile_lines.extend(( + # What: preserve the exact args json dumps common args replace model id alias literal fragment; why: native_catalog_text passes this fragment verbatim through "args = " + json.dumps(common_args).replace("${MODEL_ID}", alias), "", because changing it would alter a protocol payload, serialized fixture, or pu. + "args = " + json.dumps(common_args).replace("${MODEL_ID}", alias), "" + # What: complete the profile_lines.extend call with replace; why: native_catalog_text groups the supplied clauses as one profile_lines.extend call before its value is consumed. + )) + # What: gate on alias before extend and profile lines; why: native_catalog_text admits extend and profile lines only for this predicate and excludes the opposite state. + if alias == "model-a": + # What: call profile_lines.extend with models and model a and metadata and tier and qualification; why: native_catalog_text invokes profile_lines.extend while performing models model a metadata tier qualification; the call advances that operation through its result or side effect. + profile_lines.extend(( + # What: preserve the exact models model a metadata tier qualification literal fragment; why: native_catalog_text passes this fragment verbatim through "[models.model-a.metadata]", 'tier = "qualification"', because changing it would alter a protocol payload, serialized fixture, or public message. + "[models.model-a.metadata]", 'tier = "qualification"', + # What: preserve the exact type operator literal fragment; why: native_catalog_text passes this fragment verbatim through 'type = "operator"', "", because changing it would alter a protocol payload, serialized fixture, or public message. + 'type = "operator"', "", + # What: complete the profile_lines.extend call with ordered positional inputs; why: native_catalog_text groups the supplied clauses as one profile_lines.extend call before its value is consumed. + )) + # What: call catalog.extend with profile lines; why: native_catalog_text invokes catalog.extend while performing if invalid model is not; the call advances that operation through its result or side effect. + catalog.extend(profile_lines) + # What: gate on invalid model before extend and catalog and replace and dumps and invalid model; why: native_catalog_text admits extend and catalog and replace and dumps and invalid model only for this predicate and excludes the opposite state. + if invalid_model is not None: + # What: call catalog.extend with replace and dumps and invalid model and json; why: native_catalog_text invokes catalog.extend while performing models model invalid f model json dumps invalid model port; the call advances that operation through its result or side effect. + catalog.extend(( + # What: preserve the exact models model invalid f model json dumps invalid model port literal fragment; why: native_catalog_text passes this fragment verbatim through "[models.model-invalid]", f"model = {json.dumps(invalid_model)}", "port, because changing it would alter a protocol payload, serialized fixt. + "[models.model-invalid]", f"model = {json.dumps(invalid_model)}", "port = 0", + # What: preserve the exact ready timeout s check endpoint ready literal fragment; why: native_catalog_text passes this fragment verbatim through "ready_timeout_s = 15", 'check_endpoint = "/ready"', because changing it would alter a protocol payload, serialized fixture, or public message. + "ready_timeout_s = 15", 'check_endpoint = "/ready"', + # What: preserve the exact proxy http port ttl s literal fragment; why: native_catalog_text passes this fragment verbatim through 'proxy = "http://127.0.0.1:${PORT}"', "ttl_s = 0", because changing it would alter a protocol payload, serialized fixture, or public message. + 'proxy = "http://127.0.0.1:${PORT}"', "ttl_s = 0", + # What: preserve the exact args json dumps common args replace model id model invalid literal fragment; why: native_catalog_text passes this fragment verbatim through "args = " + json.dumps(common_args).replace("${MODEL_ID}", "model-invali, because changing it would alter a protocol payload, serialized fix. + "args = " + json.dumps(common_args).replace("${MODEL_ID}", "model-invalid"), "", + # What: complete the catalog.extend call with replace; why: native_catalog_text groups the supplied clauses as one catalog.extend call before its value is consumed. + )) + # What: return join and catalog and value from native_catalog_text; why: native_catalog_text exposes join and catalog and value so its caller can continue with the function\'s computed outcome. + return "\n".join(catalog) + + +# What: define main around the current object state; why: its direct callers call main for main and rely on this exact input and result contract. +def main() -> int: + # What: compute parser from argument parser and argparse and doc; why: parser add argument name required later reads parser, so main must retain the computed value under that name. + parser = argparse.ArgumentParser(description=__doc__) + # What: iterate across the computed value to perform add argument and parser and name; why: main repeats the body only while or for the loop header admits an iteration. + for name in ( + # What: apply the source python model a model b artifacts protected service portion of the enclosing predicate; why: this clause remains in main\'s enclosing expression so its grouping and evaluation order stay intact. + "source", "python", "model-a", "model-b", "artifacts", "protected-service", "protected-url", + # What: apply the expected hostname portion of the enclosing predicate; why: this clause remains in main\'s enclosing expression so its grouping and evaluation order stay intact. + "expected-hostname", + # What: complete the enclosing predicate collection with source and python and model a and model b; why: main groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + ): + # What: register the parser add argument name required True command-line option; why: main validates this operator input before starting the qualification sequence. + parser.add_argument("--" + name, required=True) + # What: register the parser add argument allow maintenance action store true required True command-line option; why: main validates this operator input before starting the qualification sequence. + parser.add_argument("--allow-maintenance", action="store_true", required=True) + # What: preserve the exact parser add argument daemon port type int default literal fragment; why: main passes this fragment verbatim through parser.add_argument("--daemon-port", type=int, default=1964), because changing it would alter a protocol payload, serialized fixture, or public message. + parser.add_argument("--daemon-port", type=int, default=1964) + # What: compute args from parse args and parser; why: require expected hostname args expected hostname later reads args, so main must retain the computed value under that name. + args = parser.parse_args() + # What: gate on startswith and platform and sys before system exit; why: main admits system exit only for this predicate and excludes the opposite state. + if not sys.platform.startswith("linux"): + # What: raise SystemExit for the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise SystemExit("native maintenance qualification requires Linux process-group semantics") + # What: call require_expected_hostname with expected hostname and args; why: main invokes require_expected_hostname while performing artifacts path args artifacts; the call advances that operation through its result or side effect. + require_expected_hostname(args.expected_hostname) + + # What: compute artifacts from path and artifacts and args; why: artifacts mkdir parents exist ok later reads artifacts, so main must retain the computed value under that name. + artifacts = Path(args.artifacts) + # What: supply parents to artifacts.mkdir; why: main binds this true value to artifacts.mkdir's parents input. + artifacts.mkdir(parents=True, exist_ok=False) + # What: map the trials field as the fixture input; why: main carries trials through result into artifacts result json write text json dumps result indent 2. + result: dict = {"trials": [], "restored": False} + + # What: define save around the current object state; why: its direct callers call save for save and rely on this exact input and result contract. + def save() -> None: + # What: preserve the exact artifacts result json write text json dumps result indent literal fragment; why: save passes this fragment verbatim through (artifacts / "result.json").write_text(json.dumps(result, indent=2), enc, because changing it would alter a protocol payload, serialized fixture, or public mess. + (artifacts / "result.json").write_text(json.dumps(result, indent=2), encoding="utf-8") + + # What: compute service from sudo and n and systemctl; why: subprocess run service is active quiet args protected service check later reads service, so main must retain the computed value under that name. + service = ["sudo", "-n", "systemctl"] + # What: execute subprocess run service is active quiet args protected service check True; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run(service + ["is-active", "--quiet", args.protected_service], check=True) + # What: compute baseline raw and baseline from request json and protected url and args and health and 10; why: artifacts protected baseline health json write bytes baseline raw later reads baseline raw and baseline, so main must retain the computed value under that name. + baseline_raw, baseline = request_json(args.protected_url + "/health", timeout=10) + # What: preserve the exact artifacts protected baseline health json write bytes baseline raw literal fragment; why: main passes this fragment verbatim through (artifacts / "protected-baseline-health.json").write_bytes(baseline_raw), because changing it would alter a protocol payload, serialized fixture, or public. + (artifacts / "protected-baseline-health.json").write_bytes(baseline_raw) + # What: gate on get and baseline before runtime error; why: main admits runtime error only for this predicate and excludes the opposite state. + if baseline.get("status") != "ok": + # What: raise RuntimeError for the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("protected service health baseline failed; no maintenance performed") + # What: compute and listing from request json and protected url and args and v1 and models; why: protected raw value canary args protected url protected model direct later reads and listing, so main must retain the computed value under that name. + _, listing = request_json(args.protected_url + "/v1/models", timeout=10) + # What: compute protected model from listing and id and 0 and data; why: protected raw value canary args protected url protected model direct later reads protected model, so main must retain the computed value under that name. + protected_model = listing["data"][0]["id"] + # What: compute protected raw and from canary and protected url and protected model and args and true; why: artifacts protected baseline response sse write bytes protected raw later reads protected raw and, so main must retain the computed value under that name. + protected_raw, _ = canary(args.protected_url, protected_model, direct=True) + # What: preserve the exact artifacts protected baseline response sse write bytes protected raw literal fragment; why: main passes this fragment verbatim through (artifacts / "protected-baseline-response.sse").write_bytes(protected_ra, because changing it would alter a protocol payload, serialized fixture, or publi. + (artifacts / "protected-baseline-response.sse").write_bytes(protected_raw) + + # What: compute env from copy and environ and os; why: env pythonpath str path args source python later reads env, so main must retain the computed value under that name. + env = os.environ.copy() + # What: compute env entry from str and path and source and args and python; why: env torch extensions dir str artifacts torch extensions later reads env entry, so main must retain the computed value under that name. + env["PYTHONPATH"] = str(Path(args.source) / "python") + # What: compute env entry from str and artifacts and torch extensions; why: env max jobs later reads env entry, so main must retain the computed value under that name. + env["TORCH_EXTENSIONS_DIR"] = str(artifacts / "torch-extensions") + # What: compute env entry from 2; why: cwd args source env env stdout log later reads env entry, so main must retain the computed value under that name. + env["MAX_JOBS"] = "2" + # What: compute catalog path from artifacts and models and toml; why: catalog path write text later reads catalog path, so main must retain the computed value under that name. + catalog_path = artifacts / "models.toml" + # What: compute invalid model from str and artifacts and intentionally missing model and gguf; why: args model a args model b invalid model invalid model api key native api key later reads invalid model, so main must retain the computed value under that name. + invalid_model = str(artifacts / "intentionally-missing-model.gguf") + # What: compute native api key from token urlsafe and secrets and 32; why: args model a args model b invalid model invalid model api key native api key later reads native api key, so main must retain the computed value under that name. + native_api_key = secrets.token_urlsafe(32) + # What: call catalog_path.write_text with native catalog text and model a and model b and args; why: main invokes catalog_path.write_text while performing native catalog text; the call advances that operation through its result or side effect. + catalog_path.write_text( + # What: call native_catalog_text with model a and args and model b and args; why: main invokes native_catalog_text while performing args model a args model b invalid model invalid model api key native api key; the call advances that operation through its result or side effect. + native_catalog_text( + # What: supply invalid model to native_catalog_text; why: main binds this invalid model value to native_catalog_text's invalid model input. + args.model_a, args.model_b, invalid_model=invalid_model, api_key=native_api_key + # What: complete the native_catalog_text call with invalid model and api key; why: main groups the supplied clauses as one native_catalog_text call before its value is consumed. + ), + # What: preserve the exact encoding utf 8 literal fragment; why: main passes this fragment verbatim through encoding="utf-8", because changing it would alter a protocol payload, serialized fixture, or public message. + encoding="utf-8", + # What: complete the catalog_path.write_text call with encoding; why: main groups the supplied clauses as one catalog_path.write_text call before its value is consumed. + ) + # What: enter the operation.open managed context before subprocess run; why: main releases this resource or lock after subprocess run on both success and failure paths. + with (artifacts / "kernel-preflight.log").open("wb") as log: + # What: call subprocess.run with python and args and c and from and freetoken; why: main invokes subprocess.run while performing args python c from freetoken kernel gguf import module; the call advances that operation through its result or side effect. + subprocess.run( + # What: preserve the exact args python c from freetoken kernel gguf import module literal fragment; why: main passes this fragment verbatim through [args.python, "-c", "from freetoken.kernel.gguf import _module. + [args.python, "-c", "from freetoken.kernel.gguf import _module; _module(); print('NATIVE_KERNEL_READY')"], + # What: supply cwd to subprocess.run; why: main binds this source and args value to subprocess.run's cwd input. + cwd=args.source, env=env, stdout=log, stderr=subprocess.STDOUT, check=True, timeout=600, + # What: complete the subprocess.run call with cwd and env and stdout and stderr and check; why: main groups the supplied clauses as one subprocess.run call before its value is consumed. + ) + + # What: compute daemon from the named fixture input; why: args python m freetoken cli daemon host later reads daemon, so main must retain the computed value under that name. + daemon: subprocess.Popen[bytes] | None = None + # What: compute detached engine from the named fixture input; why: detached engine old pid old port later reads detached engine, so main must retain the computed value under that name. + detached_engine: tuple[int, int] | None = None + # What: compute maintenance from false; why: maintenance later reads maintenance, so main must retain the computed value under that name. + maintenance = False + # What: compute final engine port from the named fixture input; why: final engine port direct row hardware engine port later reads final engine port, so main must retain the computed value under that name. + final_engine_port: int | None = None + # What: compute base from daemon port and args and http; why: configure native auth base native api key later reads base, so main must retain the computed value under that name. + base = f"http://127.0.0.1:{args.daemon_port}" + # What: call configure_native_auth with base and native api key; why: main invokes configure_native_auth while performing def launch daemon log stop serve on exit bool subprocess popen; the call advances that operation through its result or side effect. + configure_native_auth(base, native_api_key) + + # What: define launch_daemon around log and stop serve on exit; why: its direct callers call launch_daemon for launch daemon and rely on this exact input and result contract. + def launch_daemon(log, *, stop_serve_on_exit: bool) -> subprocess.Popen[bytes]: + # What: compute command from python and args and str and daemon port; why: command append stop serve on exit later reads command, so launch_daemon must retain the computed value under that name. + command = [ + # What: apply the args python m freetoken cli daemon host portion of command; why: launch_daemon uses this clause to evaluate command as one grouped value. + args.python, "-m", "freetoken.cli", "daemon", "--host", "127.0.0.1", + # What: call str with daemon port and args; why: launch_daemon invokes str while performing catalog str catalog path catalog watch interval no oom; the call advances that operation through its result or side effect. + "--port", str(args.daemon_port), "--state-dir", str(artifacts / "daemon-state"), + # What: call str with catalog path; why: launch_daemon consumes the str return value while evaluating "--catalog", str(catalog_path), "--catalog-watch-interval", "0", "--no-o. + "--catalog", str(catalog_path), "--catalog-watch-interval", "0", "--no-oom", + # What: complete the command collection with python and args and m and freetoken and cli and daemon; why: launch_daemon groups the supplied clauses as one command collection before its value is consumed. + ] + # What: gate on stop serve on exit before append and command; why: launch_daemon admits append and command only for this predicate and excludes the opposite state. + if stop_serve_on_exit: + # What: preserve the exact command append stop serve on exit literal fragment; why: launch_daemon passes this fragment verbatim through command.append("--stop-serve-on-exit"), because changing it would alter a protocol payload, serialized fixture, or public message. + command.append("--stop-serve-on-exit") + # What: return popen and command and subprocess and source from launch_daemon; why: launch_daemon exposes popen and command and subprocess and source so its caller can continue with the function\'s computed outcome. + return subprocess.Popen( + # What: supply cwd to subprocess.Popen; why: launch_daemon binds this source and args value to subprocess.Popen's cwd input. + command, cwd=args.source, env=env, stdout=log, stderr=subprocess.STDOUT, + # What: supply stdin to subprocess.Popen; why: launch_daemon binds this devnull and subprocess value to subprocess.Popen's stdin input. + stdin=subprocess.DEVNULL, start_new_session=True, + # What: complete the subprocess.Popen call with cwd and env and stdout and stderr and stdin; why: launch_daemon groups the supplied clauses as one subprocess.Popen call before its value is consumed. + ) + + # What: establish the handler boundary for the protected operation; why: main routes failures to base exception while preserving cleanup and success flow. + try: + # What: enter the operation.open managed context before daemon launch daemon log stop serve on exit; why: main releases this resource or lock after daemon launch daemon log stop serve on exit on both success and failure paths. + with (artifacts / "daemon.log").open("wb") as log: + # What: compute daemon from launch daemon and log and false; why: stop process group daemon later reads daemon, so main must retain the computed value under that name. + daemon = launch_daemon(log, stop_serve_on_exit=False) + # What: preserve the exact wait json base router status seconds literal fragment; why: main passes this fragment verbatim through wait_json(base + "/router/status", seconds=30), because changing it would alter a protocol payload, serialized fixture, or public message. + wait_json(base + "/router/status", seconds=30) + # What: compute maintenance from true; why: if maintenance later reads maintenance, so main must retain the computed value under that name. + maintenance = True + # What: execute subprocess run service stop args protected service check True timeout 90; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run(service + ["stop", args.protected_service], check=True, timeout=90) + + # Direct is intentionally measured against the native engine port after a router-owned load. + # What: map the name field as model a; why: main carries name through loaded raw and loaded into artifacts direct a load json write bytes loaded raw. + loaded_raw, loaded = request_json(base + "/router/load", {"name": "model-a"}, timeout=660) + # What: gate on get and isinstance and int and loaded before runtime error; why: main admits runtime error only for this predicate and excludes the opposite state. + if loaded.get("profile") != "model-a" or not isinstance(loaded.get("port"), int): + # What: raise RuntimeError for the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("native management load did not return a concrete model-a target") + # What: compute activation count from validate routed trial and loaded and router and model a and 0; why: activation count validate routed trial later reads activation count, so main must retain the computed value under that name. + activation_count = validate_routed_trial( + # What: supply alias to validate_routed_trial; why: main binds this model a value to validate_routed_trial's alias input. + loaded["router"], alias="model-a", prior_activations=0, expected_delta=1 + # What: complete the validate_routed_trial call with alias and prior activations and expected delta; why: main groups the supplied clauses as one validate_routed_trial call before its value is consumed. + ) + # What: compute result entry from control plane canary and base and artifacts; why: result upstream model rewrite upstream model rewrite canary base artifacts later reads result entry, so main must retain the computed value under that name. + result["controlPlane"] = control_plane_canary(base, artifacts) + # What: compute result entry from upstream model rewrite canary and base and artifacts; why: result selector selector canary base artifacts later reads result entry, so main must retain the computed value under that name. + result["upstreamModelRewrite"] = upstream_model_rewrite_canary(base, artifacts) + # What: compute result entry from selector canary and base and artifacts; why: result routing profile routing profile canary base artifacts later reads result entry, so main must retain the computed value under that name. + result["selector"] = selector_canary(base, artifacts) + # What: compute result entry from routing profile canary and base and artifacts; why: result trials append direct row later reads result entry, so main must retain the computed value under that name. + result["routingProfile"] = routing_profile_canary(base, artifacts) + # What: compute direct raw and direct row from canary and loaded and model a and http and true; why: artifacts direct a sse write bytes direct raw later reads direct raw and direct row, so main must retain the computed value under that name. + direct_raw, direct_row = canary(f"http://127.0.0.1:{loaded['port']}", "model-a", direct=True) + # What: preserve the exact artifacts direct a sse write bytes direct raw literal fragment; why: main passes this fragment verbatim through (artifacts / "direct-a.sse").write_bytes(direct_raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / "direct-a.sse").write_bytes(direct_raw) + # What: preserve the exact artifacts direct a load json write bytes loaded raw literal fragment; why: main passes this fragment verbatim through (artifacts / "direct-a.load.json").write_bytes(loaded_raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / "direct-a.load.json").write_bytes(loaded_raw) + # What: compute direct row entry from loaded and router; why: direct row expected activation delta later reads direct row entry, so main must retain the computed value under that name. + direct_row["router"] = loaded["router"] + # What: compute direct row entry from 1; why: direct row hardware capture hardware base artifacts direct a later reads direct row entry, so main must retain the computed value under that name. + direct_row["expectedActivationDelta"] = 1 + # What: compute direct row entry from capture hardware and base and artifacts and direct a; why: final engine port direct row hardware engine port later reads direct row entry, so main must retain the computed value under that name. + direct_row["hardware"] = capture_hardware(base, artifacts, "direct-a") + # What: compute final engine port from direct row and port and engine and hardware; why: final engine port row hardware engine port later reads final engine port, so main must retain the computed value under that name. + final_engine_port = direct_row["hardware"]["engine"]["port"] + # What: preserve the exact result trials append direct row literal fragment; why: main passes this fragment verbatim through result["trials"].append(direct_row), because changing it would alter a protocol payload, serialized fixture, or public message. + result["trials"].append(direct_row) + + # What: compute cancel raw and cancellation from cancellation canary and base and model a; why: artifacts cancel a partial sse write bytes cancel raw later reads cancel raw and cancellation, so main must retain the computed value under that name. + cancel_raw, cancellation = cancellation_canary(base, "model-a") + # What: preserve the exact artifacts cancel a partial sse write bytes cancel raw literal fragment; why: main passes this fragment verbatim through (artifacts / "cancel-a.partial.sse").write_bytes(cancel_raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / "cancel-a.partial.sse").write_bytes(cancel_raw) + # What: compute result entry from cancellation; why: result concurrency concurrency later reads result entry, so main must retain the computed value under that name. + result["cancellation"] = cancellation + + # What: compute concurrent rows and concurrency from concurrent canaries and base and model a; why: for index concurrent raw value in enumerate later reads concurrent rows and concurrency, so main must retain the computed value under that name. + concurrent_rows, concurrency = concurrent_canaries(base, "model-a") + # What: iterate across enumerate and concurrent rows to perform write bytes and concurrent raw and artifacts and index; why: main repeats the body only while or for the loop header admits an iteration. + for index, (concurrent_raw, _) in enumerate(concurrent_rows): + # What: preserve the exact artifacts f concurrent a index sse write bytes literal fragment; why: main passes this fragment verbatim through (artifacts / f"concurrent-a-{index}.sse").write_bytes(concurrent_raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / f"concurrent-a-{index}.sse").write_bytes(concurrent_raw) + # What: compute result entry from concurrency; why: result trials append row later reads result entry, so main must retain the computed value under that name. + result["concurrency"] = concurrency + # What: call save with the declared inputs; why: main invokes save while performing for label alias expected delta in; the call advances that operation through its result or side effect. + save() + + # What: iterate across the computed value to perform raw and row and canary and base and alias; why: main repeats the body only while or for the loop header admits an iteration. + for label, alias, expected_delta in ( + # What: apply the warm a model a portion of the enclosing predicate; why: this clause remains in main\'s enclosing expression so its grouping and evaluation order stay intact. + ("warm-a", "model-a", 0), + # What: apply the cold b model b portion of the enclosing predicate; why: this clause remains in main\'s enclosing expression so its grouping and evaluation order stay intact. + ("cold-b", "model-b", 1), + # What: apply the alternating a model a portion of the enclosing predicate; why: this clause remains in main\'s enclosing expression so its grouping and evaluation order stay intact. + ("alternating-a", "model-a", 1), + # What: complete the enclosing predicate collection with warm a and model a and 0 and cold b and model b and 1 and alternating a and model a and 1; why: main groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + ): + # What: compute raw and row from canary and base and alias and false; why: raw expected expected delta later reads raw and row, so main must retain the computed value under that name. + raw, row = canary(base, alias, direct=False) + # What: compute row entry from label; why: row loading feedback validate loading feedback later reads row entry, so main must retain the computed value under that name. + row["scenario"] = label + # What: compute row entry from validate loading feedback and raw and expected delta and 1; why: row router request json base router status later reads row entry, so main must retain the computed value under that name. + row["loadingFeedback"] = validate_loading_feedback( + # What: supply expected to validate_loading_feedback; why: main binds this expected delta and 1 value to validate_loading_feedback's expected input. + raw, expected=expected_delta == 1 + # What: complete the validate_loading_feedback call with expected; why: main groups the supplied clauses as one validate_loading_feedback call before its value is consumed. + ) + # What: compute row entry from request json and base and 1 and router and status; why: row router alias alias prior activations activation count later reads row entry, so main must retain the computed value under that name. + row["router"] = request_json(base + "/router/status")[1] + # What: compute activation count from validate routed trial and row and alias and activation count; why: row router alias alias prior activations activation count later reads activation count, so main must retain the computed value under that name. + activation_count = validate_routed_trial( + # What: supply alias to validate_routed_trial; why: main binds this alias value to validate_routed_trial's alias input. + row["router"], alias=alias, prior_activations=activation_count, + # What: supply expected delta to validate_routed_trial; why: main binds this expected delta value to validate_routed_trial's expected delta input. + expected_delta=expected_delta, + # What: complete the validate_routed_trial call with alias and prior activations and expected delta; why: main groups the supplied clauses as one validate_routed_trial call before its value is consumed. + ) + # What: compute row entry from expected delta; why: row hardware capture hardware base artifacts label later reads row entry, so main must retain the computed value under that name. + row["expectedActivationDelta"] = expected_delta + # What: preserve the exact artifacts f label sse write bytes raw literal fragment; why: main passes this fragment verbatim through (artifacts / f"{label}.sse").write_bytes(raw), because changing it would alter a protocol payload, serialized fixture, or public message. + (artifacts / f"{label}.sse").write_bytes(raw) + # What: preserve the exact artifacts f label metrics write bytes request bytes literal fragment; why: main passes this fragment verbatim through (artifacts / f"{label}.metrics").write_bytes(request_bytes(base + "/metr, because changing it would alter a protocol payload, serialized fixture, or public me. + (artifacts / f"{label}.metrics").write_bytes(request_bytes(base + "/metrics")) + # What: compute row entry from capture hardware and base and artifacts and label; why: final engine port row hardware engine port later reads row entry, so main must retain the computed value under that name. + row["hardware"] = capture_hardware(base, artifacts, label) + # What: compute final engine port from row and port and engine and hardware; why: final engine port result ttl port later reads final engine port, so main must retain the computed value under that name. + final_engine_port = row["hardware"]["engine"]["port"] + # What: preserve the exact result trials append row literal fragment; why: main passes this fragment verbatim through result["trials"].append(row), because changing it would alter a protocol payload, serialized fixture, or public message. + result["trials"].append(row) + # What: call save with the declared inputs; why: main invokes save while performing failure raw restored raw failed switch failed switch canary; the call advances that operation through its result or side effect. + save() + # What: evaluate and capture failure raw restored raw failed switch failed switch canary; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + failure_raw, restored_raw, failed_switch = failed_switch_canary( + # What: apply the base model invalid model a portion of failure raw and restored raw and failed switch; why: main uses this clause to evaluate failure raw and restored raw and failed switch as one grouped value. + base, "model-invalid", "model-a" + # What: complete the failed_switch_canary call with base; why: main groups the supplied clauses as one failed_switch_canary call before its value is consumed. + ) + # What: preserve the exact artifacts failed switch response json write bytes failure raw literal fragment; why: main passes this fragment verbatim through (artifacts / "failed-switch-response.json").write_bytes(failure_raw), because changing it would alter a protocol payload, serialized fixture, or public. + (artifacts / "failed-switch-response.json").write_bytes(failure_raw) + # What: preserve the exact artifacts failed switch restored a sse write bytes restored raw literal fragment; why: main passes this fragment verbatim through (artifacts / "failed-switch-restored-a.sse").write_bytes(restored_raw), because changing it would alter a protocol payload, serialized fixture, or pub. + (artifacts / "failed-switch-restored-a.sse").write_bytes(restored_raw) + # What: compute result entry from failed switch; why: result re adoption later reads result entry, so main must retain the computed value under that name. + result["failedSwitch"] = failed_switch + + # What: compute before restart raw and before restart from request json and base and engine and status; why: main consumes before restart raw and before restart during artifacts re adoption before engine json write bytes before restart raw, so before restart raw and before restart value receives the comput. + before_restart_raw, before_restart = request_json(base + "/engine/status") + # What: compute old pid and old port from get and before restart and pid and port; why: or not isinstance old pid int or later reads old pid and old port, so main must retain the computed value under that name. + old_pid, old_port = before_restart.get("pid"), before_restart.get("port") + # What: gate on old pid and get and isinstance and int and old port before runtime error; why: main admits runtime error only for this predicate and excludes the opposite state. + if ( + # What: call before_restart.get with running; why: main invokes before_restart.get while performing or not isinstance old pid int or; the call advances that operation through its result or side effect. + not before_restart.get("running") + # What: call isinstance with old pid and int; why: main invokes isinstance while performing or not isinstance old port int or; the call advances that operation through its result or side effect. + or not isinstance(old_pid, int) or old_pid <= 0 + # What: call isinstance with old port and int; why: main consumes the isinstance return value while evaluating or not isinstance(old_port, int) or not 1 <= old_port <= 65535. + or not isinstance(old_port, int) or not 1 <= old_port <= 65535 + # What: complete the enclosing predicate with if not before restart get running or not isinstance old pid int; why: main groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RuntimeError for the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("pre-restart engine identity is invalid") + # What: preserve the exact artifacts re adoption before engine json write bytes before restart raw literal fragme; why: main passes this fragment verbatim through (artifacts / "re-adoption-before-engine.json").write_bytes(before_restar, because changing it would alter a protocol payload, serialized fixture. + (artifacts / "re-adoption-before-engine.json").write_bytes(before_restart_raw) + # What: compute detached engine from old pid and old port; why: detached engine later reads detached engine, so main must retain the computed value under that name. + detached_engine = (old_pid, old_port) + # What: call catalog_path.write_text with native catalog text and model a and model b and args; why: main invokes catalog_path.write_text while performing native catalog text; the call advances that operation through its result or side effect. + catalog_path.write_text( + # What: call native_catalog_text with model a and args and model b and args; why: main invokes native_catalog_text while performing args model a args model b invalid model invalid model; the call advances that operation through its result or side effect. + native_catalog_text( + # What: supply invalid model to native_catalog_text; why: main binds this invalid model value to native_catalog_text's invalid model input. + args.model_a, args.model_b, invalid_model=invalid_model, + # What: supply api key to native_catalog_text; why: main binds this native api key value to native_catalog_text's api key input. + api_key=native_api_key, startup=True, + # What: complete the native_catalog_text call with invalid model and api key and startup; why: main groups the supplied clauses as one native_catalog_text call before its value is consumed. + ), + # What: preserve the exact encoding utf 8 literal fragment; why: main passes this fragment verbatim through encoding="utf-8", because changing it would alter a protocol payload, serialized fixture, or public message. + encoding="utf-8", + # What: complete the catalog_path.write_text call with encoding; why: main groups the supplied clauses as one catalog_path.write_text call before its value is consumed. + ) + # What: call stop_process_group with daemon; why: main invokes stop_process_group while performing daemon; the call advances that operation through its result or side effect. + stop_process_group(daemon) + # What: compute daemon from the named fixture input; why: daemon launch daemon log stop serve on exit later reads daemon, so main must retain the computed value under that name. + daemon = None + # What: call require_listener_open with old port; why: main invokes require_listener_open while performing daemon launch daemon log stop serve on exit; the call advances that operation through its result or side effect. + require_listener_open(old_port) + # What: compute daemon from launch daemon and log and true; why: if daemon is not later reads daemon, so main must retain the computed value under that name. + daemon = launch_daemon(log, stop_serve_on_exit=True) + # What: compute adopted router from wait json and base and router and status and 30; why: identity validate re adoption before restart adopted engine adopted router later reads adopted router, so main must retain the computed value under that name. + adopted_router = wait_json(base + "/router/status", seconds=30) + # What: compute adopted raw and adopted engine from request json and base and engine and status; why: artifacts re adoption after engine json write bytes adopted raw later reads adopted raw and adopted engine, so main must retain the computed value under that name. + adopted_raw, adopted_engine = request_json(base + "/engine/status") + # What: preserve the exact artifacts re adoption after engine json write bytes adopted raw literal fragment; why: main passes this fragment verbatim through (artifacts / "re-adoption-after-engine.json").write_bytes(adopted_raw), because changing it would alter a protocol payload, serialized fixture, or pub. + (artifacts / "re-adoption-after-engine.json").write_bytes(adopted_raw) + # What: compute identity from validate re adoption and before restart and adopted engine and adopted router; why: identity later reads identity, so main must retain the computed value under that name. + identity = validate_re_adoption(before_restart, adopted_engine, adopted_router) + # What: gate on get and adopted router before runtime error; why: main admits runtime error only for this predicate and excludes the opposite state. + if adopted_router.get("activeRoutingProfile") != "coding": + # What: raise RuntimeError for the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("startup routing profile was not activated") + # What: compute readopted raw and readopted completion from canary and base and model a and false; why: artifacts re adoption restored a sse write bytes readopted raw later reads readopted raw and readopted completion, so main must retain the computed value under that name. + readopted_raw, readopted_completion = canary(base, "model-a", direct=False) + # What: preserve the exact artifacts re adoption restored a sse write bytes readopted raw literal fragment; why: main passes this fragment verbatim through (artifacts / "re-adoption-restored-a.sse").write_bytes(readopted_raw), because changing it would alter a protocol payload, serialized fixture, or publi. + (artifacts / "re-adoption-restored-a.sse").write_bytes(readopted_raw) + # What: gate on get and request json and base before runtime error; why: main admits runtime error only for this predicate and excludes the opposite state. + if request_json(base + "/router/status")[1].get("activations") != 0: + # What: raise RuntimeError for the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("routed request replaced the re-adopted engine") + # What: compute result entry from identity and get and readopted completion and startup preload reused resident and startup routing profile; why: result conflicting request conflict later reads result entry, so main must retain the computed value under that name. + result["reAdoption"] = { + # What: apply the identity portion of result entry; why: main uses this clause to evaluate result entry as one grouped value. + **identity, + # What: map the startup preload reused resident field as true; why: main carries startup preload reused resident through result entry into result conflicting request conflict. + "startupPreloadReusedResident": True, + # What: map the startup routing profile field as coding; why: main carries startup routing profile through result entry into result conflicting request conflict. + "startupRoutingProfile": "coding", + # What: map the completion passed field as get and readopted completion and true and passed; why: main carries completion passed through result entry into result conflicting request conflict. + "completionPassed": readopted_completion.get("passed") is True, + # What: map the passed field as get and readopted completion and true and passed; why: main carries passed through result entry into result conflicting request conflict. + "passed": readopted_completion.get("passed") is True, + # What: complete the result entry mapping with startup preload reused resident and startup routing profile and completion passed and passed; why: main groups the supplied clauses as one result entry mapping before its value is consumed. + } + # What: compute detached engine from the named fixture input; why: if detached engine is later reads detached engine, so main must retain the computed value under that name. + detached_engine = None + # What: evaluate and capture conflict a raw conflict b raw conflict restored raw conflict conflicting request canary; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + conflict_a_raw, conflict_b_raw, conflict_restored_raw, conflict = conflicting_request_canary( + # What: apply the base model a model b portion of conflict a raw and conflict b raw and conflict restored raw and conflict; why: main uses this clause to evaluate conflict a raw and conflict b raw and conflict restored raw and conflict as one grouped value. + base, "model-a", "model-b" + # What: complete the conflicting_request_canary call with base; why: main groups the supplied clauses as one conflicting_request_canary call before its value is consumed. + ) + # What: preserve the exact artifacts conflict active a partial sse write bytes conflict a raw literal fragment; why: main passes this fragment verbatim through (artifacts / "conflict-active-a.partial.sse").write_bytes(conflict_a_raw, because changing it would alter a protocol payload, serialized fixture, o. + (artifacts / "conflict-active-a.partial.sse").write_bytes(conflict_a_raw) + # What: preserve the exact artifacts conflict waiting b sse write bytes conflict b raw literal fragment; why: main passes this fragment verbatim through (artifacts / "conflict-waiting-b.sse").write_bytes(conflict_b_raw), because changing it would alter a protocol payload, serialized fixture, or public mess. + (artifacts / "conflict-waiting-b.sse").write_bytes(conflict_b_raw) + # What: preserve the exact artifacts conflict restored a sse write bytes conflict restored raw literal fragment; why: main passes this fragment verbatim through (artifacts / "conflict-restored-a.sse").write_bytes(conflict_restored_ra, because changing it would alter a protocol payload, serialized fixture. + (artifacts / "conflict-restored-a.sse").write_bytes(conflict_restored_raw) + # What: compute result entry from conflict; why: result reload conflict reload conflict canary later reads result entry, so main must retain the computed value under that name. + result["conflictingRequest"] = conflict + # What: compute result entry from reload conflict canary and base and catalog path and model a; why: result persistent capacity persistent later reads result entry, so main must retain the computed value under that name. + result["reloadConflict"] = reload_conflict_canary( + # What: supply api key to reload_conflict_canary; why: main binds this native api key value to reload_conflict_canary's api key input. + base, catalog_path, args.model_a, args.model_b, api_key=native_api_key + # What: complete the reload_conflict_canary call with api key; why: main groups the supplied clauses as one reload_conflict_canary call before its value is consumed. + ) + # What: compute persistent raw and persistent from persistent capacity canary and base and catalog path and model; why: main consumes persistent raw and persistent during artifacts persistent capacity rejection json write bytes persistent raw, so persistent raw and persistent value receives the computed va. + persistent_raw, persistent = persistent_capacity_canary( + # What: supply api key to persistent_capacity_canary; why: main binds this native api key value to persistent_capacity_canary's api key input. + base, catalog_path, args.model_a, args.model_b, api_key=native_api_key + # What: complete the persistent_capacity_canary call with api key; why: main groups the supplied clauses as one persistent_capacity_canary call before its value is consumed. + ) + # What: preserve the exact artifacts persistent capacity rejection json write bytes persistent raw literal fragme; why: main passes this fragment verbatim through (artifacts / "persistent-capacity-rejection.json").write_bytes(persisten, because changing it would alter a protocol payload, serialized fixture. + (artifacts / "persistent-capacity-rejection.json").write_bytes(persistent_raw) + # What: compute result entry from persistent; why: result ttl ttl eviction canary later reads result entry, so main must retain the computed value under that name. + result["persistentCapacity"] = persistent + # What: compute result entry from ttl eviction canary and base and catalog path and model a; why: final engine port result ttl port later reads result entry, so main must retain the computed value under that name. + result["ttl"] = ttl_eviction_canary( + # What: supply api key to ttl_eviction_canary; why: main binds this native api key value to ttl_eviction_canary's api key input. + base, catalog_path, args.model_a, args.model_b, api_key=native_api_key + # What: complete the ttl_eviction_canary call with api key; why: main groups the supplied clauses as one ttl_eviction_canary call before its value is consumed. + ) + # What: compute final engine port from result and port and ttl; why: if final engine port is not later reads final engine port, so main must retain the computed value under that name. + final_engine_port = result["ttl"]["port"] + # What: call save with the declared inputs; why: main invokes save while performing result passed; the call advances that operation through its result or side effect. + save() + # What: compute result entry from all and len and get and result; why: len result trials later reads result entry, so main must retain the computed value under that name. + result["passed"] = ( + # What: call len with result and trials; why: main invokes len while performing and all x passed for x; the call advances that operation through its result or side effect. + len(result["trials"]) == 4 + # What: call all with x and result and passed and trials; why: main invokes all while performing and result get cancellation get passed is; the call advances that operation through its result or side effect. + and all(x["passed"] for x in result["trials"]) + # What: call operation.get with passed; why: main invokes operation.get while performing and result get concurrency get passed is; the call advances that operation through its result or side effect. + and result.get("cancellation", {}).get("passed") is True + # What: call operation.get with passed; why: main invokes operation.get while performing and result get ttl get passed is; the call advances that operation through its result or side effect. + and result.get("concurrency", {}).get("passed") is True + # What: call operation.get with passed; why: main invokes operation.get while performing and result get reload conflict get passed is; the call advances that operation through its result or side effect. + and result.get("ttl", {}).get("passed") is True + # What: call operation.get with passed; why: main invokes operation.get while performing and result get failed switch get passed is; the call advances that operation through its result or side effect. + and result.get("reloadConflict", {}).get("passed") is True + # What: call operation.get with passed; why: main invokes operation.get while performing and result get re adoption get passed is; the call advances that operation through its result or side effect. + and result.get("failedSwitch", {}).get("passed") is True + # What: call operation.get with passed; why: main invokes operation.get while performing and result get persistent capacity get passed is; the call advances that operation through its result or side effect. + and result.get("reAdoption", {}).get("passed") is True + # What: call operation.get with passed; why: main invokes operation.get while performing and result get conflicting request get passed is; the call advances that operation through its result or side effect. + and result.get("persistentCapacity", {}).get("passed") is True + # What: call operation.get with passed; why: main invokes operation.get while performing and result get control plane get passed is; the call advances that operation through its result or side effect. + and result.get("conflictingRequest", {}).get("passed") is True + # What: call operation.get with passed; why: main invokes operation.get while performing and result get selector get passed is; the call advances that operation through its result or side effect. + and result.get("controlPlane", {}).get("passed") is True + # What: call operation.get with passed; why: main invokes operation.get while performing and result get routing profile get passed is; the call advances that operation through its result or side effect. + and result.get("selector", {}).get("passed") is True + # What: call operation.get with passed; why: main consumes the operation.get return value while evaluating and result.get("routingProfile", {}).get("passed") is True. + and result.get("routingProfile", {}).get("passed") is True + # What: complete the result entry expression with result passed len result trials equals 4 and all; why: main groups the supplied clauses as one result entry expression before its value is consumed. + ) + # What: handle base exception by result error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException as exc: + # What: compute result entry from repr and exc; why: result cleanup error repr exc later reads result entry, so main must retain the computed value under that name. + result["error"] = repr(exc) + # What: run if daemon is not on every exit path; why: main performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: gate on daemon before request json and oserror and value error and httperror and base; why: main admits request json and oserror and value error and httperror and base only for this predicate and excludes the opposite state. + if daemon is not None: + # What: establish the handler boundary for the protected operation; why: main routes failures to oserror and value error and httperror and error and urllib while preserving cleanup and success flow. + try: + # What: preserve the exact request json base shutdown timeout literal fragment; why: main passes this fragment verbatim through request_json(base + "/shutdown", {}, timeout=45), because changing it would alter a protocol payload, serialized fixture, or public message. + request_json(base + "/shutdown", {}, timeout=45) + # What: handle oserror and value error and httperror and error and urllib by pass; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, ValueError, urllib.error.HTTPError): + # What: ignore the anticipated exception handled by this branch; why: launch_daemon continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: establish the handler boundary for the protected operation; why: main routes failures to oserror and timeout expired and subprocess and runtime error while preserving cleanup and success flow. + try: + # What: call stop_process_group with daemon; why: main invokes stop_process_group while performing if daemon poll is; the call advances that operation through its result or side effect. + stop_process_group(daemon) + # What: gate on poll and daemon before runtime error; why: main admits runtime error only for this predicate and excludes the opposite state. + if daemon.poll() is None: + # What: raise RuntimeError for the caller; why: main stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("temporary daemon process did not exit") + # What: gate on final engine port before require listener closed and final engine port; why: main admits require listener closed and final engine port only for this predicate and excludes the opposite state. + if final_engine_port is not None: + # What: call require_listener_closed with final engine port; why: main invokes require_listener_closed while performing except oserror subprocess timeout expired as exc; the call advances that operation through its result or side effect. + require_listener_closed(final_engine_port) + # What: handle oserror and timeout expired and subprocess by result cleanup error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, subprocess.TimeoutExpired) as exc: + # What: compute result entry from repr and exc; why: result cleanup error repr exc later reads result entry, so main must retain the computed value under that name. + result["cleanupError"] = repr(exc) + # What: handle runtime error by if detached engine is; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except RuntimeError as exc: + # What: gate on detached engine before result and repr and exc; why: main admits result and repr and exc only for this predicate and excludes the opposite state. + if detached_engine is None: + # What: compute result entry from repr and exc; why: result cleanup error repr detached exc later reads result entry, so main must retain the computed value under that name. + result["cleanupError"] = repr(exc) + # What: select the remaining branch that performs try; why: main covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: establish the handler boundary for the protected operation; why: main routes failures to oserror and runtime error while preserving cleanup and success flow. + try: + # What: call stop_detached_engine with detached engine; why: main invokes stop_detached_engine while performing detached engine; the call advances that operation through its result or side effect. + stop_detached_engine(*detached_engine) + # What: compute detached engine from the named fixture input; why: elif detached engine is not later reads detached engine, so main must retain the computed value under that name. + detached_engine = None + # What: handle oserror and runtime error by result cleanup error repr detached exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, RuntimeError) as detached_exc: + # What: compute result entry from repr and detached exc; why: result cleanup error repr exc later reads result entry, so main must retain the computed value under that name. + result["cleanupError"] = repr(detached_exc) + # What: gate on detached engine before stop detached engine and oserror and runtime error and detached engine and result; why: main admits stop detached engine and oserror and runtime error and detached engine and result only for this predicate and excludes the opposite state. + elif detached_engine is not None: + # What: establish the handler boundary for the protected operation; why: main routes failures to oserror and runtime error while preserving cleanup and success flow. + try: + # What: call stop_detached_engine with detached engine; why: main invokes stop_detached_engine while performing except oserror runtime error as exc; the call advances that operation through its result or side effect. + stop_detached_engine(*detached_engine) + # What: handle oserror and runtime error by result cleanup error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, RuntimeError) as exc: + # What: compute result entry from repr and exc; why: result restored later reads result entry, so main must retain the computed value under that name. + result["cleanupError"] = repr(exc) + # What: gate on maintenance before base exception and run and wait json and restored raw and value; why: main admits base exception and run and wait json and restored raw and value only for this predicate and excludes the opposite state. + if maintenance: + # What: establish the handler boundary for the protected operation; why: main routes failures to base exception while preserving cleanup and success flow. + try: + # What: execute subprocess run service start args protected service check True timeout 180; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + subprocess.run(service + ["start", args.protected_service], check=True, timeout=180) + # What: preserve the exact wait json args protected url health seconds literal fragment; why: main passes this fragment verbatim through wait_json(args.protected_url + "/health", seconds=300), because changing it would alter a protocol payload, serialized fixture, or public message. + wait_json(args.protected_url + "/health", seconds=300) + # What: compute restored raw and from canary and protected url and protected model and args and true; why: artifacts protected restored response sse write bytes restored raw later reads restored raw and, so main must retain the computed value under that name. + restored_raw, _ = canary(args.protected_url, protected_model, direct=True) + # What: preserve the exact artifacts protected restored response sse write bytes restored raw literal fragment; why: main passes this fragment verbatim through (artifacts / "protected-restored-response.sse").write_bytes(restored_raw, because changing it would alter a protocol payload, serialized fixtur. + (artifacts / "protected-restored-response.sse").write_bytes(restored_raw) + # What: compute result entry from true; why: result restore error repr exc later reads result entry, so main must retain the computed value under that name. + result["restored"] = True + # What: handle base exception by result restore error repr exc; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException as exc: + # What: compute result entry from repr and exc; why: return if result get passed and result later reads result entry, so main must retain the computed value under that name. + result["restoreError"] = repr(exc) + # What: call save with the declared inputs; why: main invokes save while performing return if result get passed and result; the call advances that operation through its result or side effect. + save() + # What: return get and result and 0 and 1 and passed from main; why: main exposes get and result and 0 and 1 and passed so its caller can continue with the function\'s computed outcome. + return 0 if result.get("passed") and result["restored"] and "cleanupError" not in result else 1 + + +# What: gate on name before system exit and main; why: qualify_native_router admits system exit and main only for this predicate and excludes the opposite state. +if __name__ == "__main__": + # What: raise SystemExit for the caller; why: qualify_native_router stops this rejected path before it can mutate state, dispatch work, or report success. + raise SystemExit(main()) diff --git a/docs/freetoken-swap-completion-audit.md b/docs/freetoken-swap-completion-audit.md new file mode 100644 index 000000000..dff188524 --- /dev/null +++ b/docs/freetoken-swap-completion-audit.md @@ -0,0 +1,148 @@ +# FreeToken swap completion audit + +This audit preserves the full integration goal. A draft PR and passing CPU tests +do not establish that every lifecycle behavior is qualified on real models. It +distinguishes historical evidence from current-branch evidence: neither is +silently promoted to proof for a later native-router implementation. + +## Historical combined-source verification + +- Swap source: `64dcc683d4e767fb4af8b7088ebb58564b1b7535`. +- AMD model-repair source: `de23ad6a9e74aecc72b9f6b9e81b8c3376ff2e60`. +- Git's clean merge-tree result: `c3c0ae54a09857b98bba83cfc75b91264e6eeb43`. +- The combined tree was archived into an isolated temporary directory on + GMKtek EVO-X2. Neither branch nor the live runtime was replaced by that tree. +- Historical Linux validation: 114 daemon, privacy, benchmark, and reproducibility tests + passed, including the real child-process recovery tests. No skips. +- Combined-tree Qwen validation: 21 grouped-output, SSM, and config tests passed. +- The protected llama.cpp service remained active throughout those CPU checks. + This archived combined tree is not the current `freetoken-swap` branch. + +Reproduce the combined-tree CPU suites from the extracted source, with its +`python` directory on `PYTHONPATH` and the required test dependencies installed: + +```bash +python -m pytest tests/daemon \ + tests/benchmarks/test_public_document_privacy.py \ + tests/benchmarks/test_gmk_evo_x2_benchmark.py \ + tests/reproduce/test_collect_host_manifest.py -q +python -m pytest tests/models/test_qwen36_gdn_grouped_output.py \ + tests/models/test_qwen35_gguf_ssm_a.py \ + tests/models/test_qwen35_gguf_config.py -q +``` + +## Current checkout verification + +- Read-only comparison reference: `mostlygeek/llama-swap` + `41ec321b6216d838488b2a7d936274ed227c0c5e`, whose `LICENSE.md` says MIT. +- Local deterministic verification at `39c3aaabd6fefd7dd462e85e8dfd2ba03be849ab` + on the current Windows checkout: 345 daemon + tests passed and 7 Linux-only tests were skipped. This proves CPU/HTTP + behavior only; it does not substitute for real-model evidence. +- GitHub-hosted Ubuntu verification at + `39c3aaabd6fefd7dd462e85e8dfd2ba03be849ab` (Actions run `34941311939`) + reported 352 tests with zero failures, errors, or skips. This includes the + fail-closed maintenance-host and measured-memory gates, AMD SMI parsing, + queued-disconnect ownership regression, and capability-metadata parser and + listing coverage, model display/metadata collision precedence, ordered + upstream-model/request-filter, per-profile upstream-timeout, safe + direct-upstream static-suffix cold-load guard, startup + preload/profile, and generated-alias coverage, and + pin/warm selector and runtime routing-profile parsing, routing, listing, + metadata, management-isolation, reload-reset, safe readiness/proxy-target, + and qualifier gates. + It also covers bounded body-free activity, authenticated aggregate and + capture retrieval APIs, capture-disabled defaults, serialized-byte and + per-response bounds, credential-header redaction, binary fidelity, eviction, + and exact cold-loading downstream SSE capture. Body-free activity recovery, + bounded corruption handling, atomic compaction, path-free persistence health, + memory-only capture restart behavior, hashed non-credential session grouping, + and explicit-on-click UI capture retrieval are also covered. + The bounded periodic-performance tests cover one-hour eviction, strict + RFC3339 filtering, authentication, disabled 503 behavior, generic probe + failure health, reload generation cleanup, and path/PID omission. + It also executes the + disposable process-group, readiness rollback, + re-adoption, dynamic-port, routed SSE, and cleanup tests that Windows skips. + It is current-branch Linux process evidence, not current-engine or GPU-model + qualification. +- No current-branch maintenance-window benchmark artifact has been published. + Raw paths, prompts, responses, logs, and host data must remain private. +- Current isolated combined-tree CPU verification used swap head + `39c3aaabd6fefd7dd462e85e8dfd2ba03be849ab` and draft AMD compatibility + head `c0534c6f38162cb2ddfd0193cd9bf1031613dde1`. Git produced the clean + synthetic tree `74a4f3b1649442d9d8c24576751d29f30e218d04` without checking out or + changing either branch. On Windows, 377 daemon/privacy/benchmark-contract/ + reproducibility tests passed with 7 expected Linux skips, and all 21 + grouped-output, SSM, and GGUF configuration tests passed in a disposable + Python 3.13 / torch 2.13 CPU environment. This proves source compatibility + only; no protected service, model, GPU, or runtime was inspected or changed. + +## Requirement evidence and gaps + +| Requirement | Evidence | Status | +| --- | --- | --- | +| Official source, license, and provenance | Read-only llama-swap reference pinned to `41ec321b6216d838488b2a7d936274ed227c0c5e`, MIT license; research report and configuration example | Documented and reverified locally | +| Model catalog and lifecycle controls | Validated TOML catalog, collision-safe slash-namespaced and colon-variant alternate IDs, ordered upstream-model/strip/hard/soft/by-ID JSON filters with protected routing identity, runtime and startup pin profiles, singleton native startup preload, pin/warm virtual selectors with listing metadata, unlisted model entries, safe configured readiness paths and manager-owned loopback proxy prefixes, authenticated profile endpoints, native process manager, longest-prefix direct-upstream resolution, and exact explicit/dynamic/omitted-default-port re-adoption. Spillover is rejected as incompatible with one-resident capacity. | Implemented and CPU/HTTP tested; profile/selector/readiness-target/upstream-model/startup live canaries remain required | +| Automatic model routing | Native `freetoken-swap` model-ID admission, readiness-gated activation, request-preserving proxying, cancellation, TTL eviction, reload, and deterministic HTTP tests; prior direct llama-swap runs remain comparison evidence only | Implemented and CPU/HTTP tested; current native real-engine qualification remains required | +| Readiness and API compatibility | Separate `/ready`, uncached generation-aware default health checks, safe profile-configured readiness paths, exact owned-port proxy targets with optional path prefixes, ordinary and SSE completions, side-effect-free sanitized browser preflight, authenticated model-list CORS, exact `/models` listing alias, public model entries with atomic loaded/activating/unloaded status, collision-safe display/JSON metadata, and declarative text/tool/context capability metadata matching the pinned listing fields | CPU/HTTP tested; current native real-engine evidence required | +| Streaming cold-load feedback | Global/per-profile safe configuration; atomic post-concurrency cold admission; reasoning and queue-position SSE; upstream continuation; in-band terminal errors; strict warm/route/stream bypass; explicit cancellation and disconnect cleanup | Deterministic HTTP and hosted Linux disposable-child gates passed; current GMKtek native execution required | +| Concurrency and unloading | Race-safe global/per-profile reservations, default and configured limits, immediate 429, canonical/alternate sharing, same-model and conflicting-model admission, concurrent cold dynamic binding, and idle eviction are deterministically tested | Current native real-engine verification required | +| Rollback protections | Launch/readiness recovery, newer lifecycle intent, accounting preservation, and current-branch hosted Linux real-child rollback/process-group cleanup passed; historical invalid-GGUF evidence is retained separately | Current-engine real-model recovery execution remains required | +| Client cancellation | Native opaque router request IDs, atomic duplicate-ID rejection before admission/upstream work, disconnect-aware admission, queued/connecting/active request list, explicit cancel endpoint across every owned phase, orphan socket close, lease release, and cancellation metrics. Failed, disconnected, or cancelled admission and failed upstream connection release ownership safely. | Deterministic HTTP tested; current native same-instance GPU verification required | +| Authentication and observability | Bearer, Basic-password, and `X-Api-Key` inference authentication with precedence and local termination; separate `X-FT-Token` lifecycle control; catalog-key-protected `/models` compatibility alias; configured aliases and profiles; Prometheus metrics; bounded router-log SSE; exact-origin qualification credentials | Deterministic HTTP tested; current native GMKtek control-plane execution required | +| Model compatibility | Mixed-format Qwen/GDN repair, tokenizer checks, exact-model contracts, prior live completion evidence, and 21 current isolated combined-tree model tests | Source-compatible at the recorded heads; current-engine real-model qualification remains required | +| Production protection | Isolated test paths, explicit maintenance gate, exact operator-supplied hostname required before artifacts or service inspection, historical restore/completion checks, no interruption during combined-tree checks | Maintained and fail-closed; no current protected workload was touched | +| Privacy | Generic GMKtek EVO-X2 label, sanitized public metadata and examples, privacy regressions, regenerated reviewed PDF | Current publication changes sanitized; historical copies not erased | +| FreeToken-only publication | Public GitHub recheck on 2026-09-15: draft PR 1 targets `main` from `feat/freetoken-swap`, and its public PR ref matched the branch head at recheck; draft PR 2 targets `amd-rocm-gfx1151` from `fix/qwen36-swap-compat` | Submitted, draft, not merged | + +The PRs target different base branches: PR 1 targets `main`; PR 2 targets +`amd-rocm-gfx1151`. Their current draft state and branch relationships were +rechecked on their public GitHub pages; current mergeability was not reverified, +and no authenticated mutation was attempted. The clean combined tree is +compatibility evidence, not an instruction to merge either PR or change the +repository's release strategy. PR 1 now has a secret-free GitHub-hosted Ubuntu +daemon check; PR 2 has no hosted check at this audit. The local and hosted test +results are separate evidence. PR 1's public description remains historical and +is not the authoritative record of current-branch qualification. + +## Historical maintenance-window evidence + +The following records describe an earlier approved window, not current-branch +completion proof: + +1. GPU stream cancellation reached terminal idle on the same backend, without + a normal-completion increment. Post-disconnect A-to-B-to-A streaming, + concurrency, and TTL unloading passed. +2. Native daemon recovery passed after the real loader rejected an invalid + GGUF fixture. The restored Qwen3.6 model reached readiness and generated the + expected answer. The failed switch correctly remained HTTP 503. +3. Both phases restored and health-checked the protected service, including a + verified completion. Final process/listener checks found no test runtime + remaining. The accounting gap for the crashed loader is explicitly degraded. + +The approved historical window is closed. No permanent production activation, +merge, or upstream submission was performed. Long-context quality, broad model +compatibility, direct-router automatic rollback, and long-duration endurance +remain explicitly unclaimed limitations. + +## Current completion gates + +The current native router is **not complete** until an approved GMKtek EVO-X2 +maintenance window runs the current branch's +`benchmarks/swap/qualify_native_router.py`, retains its raw artifacts privately, +and records sanitized direct, warm-routed, cold-routed, alternating-model, +resident-target warm-selector, +runtime-profile activation/composition/clear, +router-cancellation, same-model concurrency, conflicting-model queue/drain, +failed-switch rollback/accounting, +same-process re-adoption, active-reload-conflict, capacity-safe persistent residency, +TTL-eviction, unauthenticated 401, authenticated model/profile inventory, +Prometheus, bounded router-log, and positive available periodic owned-process +RAM/VRAM results without PID/model/path fields. It must also run Linux real-child tests on +the current branch, then restore and health-check the protected workload. No +merge, permanent service activation, or publication of raw artifacts is +authorized by this audit. + +See [integration behavior](freetoken-swap.md) and +[source research and live-test limitations](freetoken-swap-research.md). diff --git a/docs/freetoken-swap-native-qualification.md b/docs/freetoken-swap-native-qualification.md new file mode 100644 index 000000000..9b8948f6a --- /dev/null +++ b/docs/freetoken-swap-native-qualification.md @@ -0,0 +1,148 @@ +# Native freetoken-swap qualification runbook + +This runbook qualifies the FreeToken-owned router. It does not qualify the +separate llama-swap integration and it does not authorize production activation +or a merge. Use only an approved maintenance window on **GMKtek EVO-X2**. + +## Preconditions + +- An approved authentication method for the qualification host is available. +- The protected workload, its exact restoration procedure, and a deterministic + health request are recorded privately before any change. +- The current FreeToken feature branch is checked out in an isolated path. +- Model artifacts, tokenizer, native extension, and FreeToken runtime are + verified known-good for one short deterministic completion before swap tests. +- The native catalog has two aliases with distinct model paths, loopback ports, + bounded context settings, and no unreviewed `drop_fields` policy. +- `git status --short` is clean except for intentional qualification-only files. + +Do not copy private paths, addresses, hardware identifiers, prompts, raw model +output, API keys, or request headers into the public PR. + +## Baseline capture + +Capture privately, before starting the daemon: + +1. Protected workload health and one deterministic completion. +2. Current listener and process inventory for the selected temporary ports. +3. Host memory, accelerator-visible memory, and available system memory as + separate values. +4. FreeToken branch commit, catalog hash, model file hashes, Python version, + ROCm version, and runtime package versions. + +Abort before loading if the protected workload is unhealthy or the measured +capacity is lower than the documented gate. + +## Native router matrix + +Run each test through the daemon's stable URL, never by calling the engine port +directly. Keep request and response content private. Record status codes, +model alias, elapsed time, first-byte time, final duration, usage-derived completion tokens/second, router metrics, engine metrics, accounting receipt IDs, and cleanup result. + +| Test | Required observation | Pass condition | +| --- | --- | --- | +| Cold A | First request to alias A | Ready engine, valid ordinary completion, router activation increments | +| Warm A | Repeat alias A | Same engine PID, no activation increment, completion succeeds | +| Warm selector | Request the temporary warm selector while A is resident | Request is rewritten to A, completion succeeds, and activation remains unchanged | +| Runtime profile | Activate the temporary profile, request its pin through the warm selector, then clear it | Virtual pin is listed only while active, disabled pin stays omitted, completion uses resident A with zero activation, and the profile is cleared before later trials | +| Configured readiness target | Start and recheck temporary profiles through their configured `/ready` path | Control inventory reports `/ready`; activation and stable daemon readiness succeed without leaving the exact manager-owned port | +| Upstream model-name rewrite | Request temporary alias `compat/model-a` while A is resident and configured with `use_model_name = "model-a"` | The streamed response reports upstream model `model-a`, routing remains resident on A, active requests return to zero, and activation count is unchanged | +| Model-list display metadata | Inspect canonical A and `compat/model-a` through both public listing aliases and authenticated router inventory | Display name and operator metadata are present on both public IDs; canonical/alias identity metadata overwrites the conflicting operator `type`; inventory remains sanitized and consistent | +| Per-profile upstream timeout | Inspect the temporary profile inventory and complete routed ordinary/SSE canaries | Inventory reports the profile override and routed requests complete through that acquired-profile connection policy rather than the distinct global fallback | +| Startup preload and profile | After maintenance begins, restart the temporary daemon with alias A configured for preload and `coding` as startup profile while the exact A child remains available for re-adoption | Alias canonicalizes to A, startup profile is active, preload reuses the exact adopted PID/port with zero activation delta, and a routed completion succeeds | +| Cold B | Request alias B after A is idle | A receives durable stop receipt, B becomes ready, completion succeeds | +| A to B to A | Three routed requests | Each expected alias returns, no overlapping owned children, every replacement is ready | +| SSE | Stream an alias request | First event and terminal event arrive, final lease count is zero | +| Explicit cancel | Send a long stream with `X-FT-Request-ID`, call router cancel | Same engine reaches zero active leases, cancellation counter increments, no normal completion is credited | +| Same-model concurrency | Two A requests | Both complete, one resident engine, no unintended switch | +| Conflicting request | Keep A stream active then request B | B waits or receives documented capacity response, A is not killed mid-stream | +| TTL | Allow a nonpersistent idle profile to reach its TTL | Engine stops through accounting path, listener closes, eviction increments | +| Bad replacement | Select an intentionally invalid disposable fixture | HTTP failure is visible, previous engine recovery is attempted only when applicable, failed receipt is degraded rather than fabricated | +| Reload | Replace catalog with a valid idle change, then an invalid or active-profile redefinition | Valid change applies atomically; invalid and active redefinitions are refused without altering live ownership | +| Authentication and control plane | Probe inference, both public model-list paths, namespaced direct upstream, and management without credentials, then exercise Bearer, Basic-password, and `X-Api-Key` before inspecting aliases, profiles, metrics, and router-log SSE | Unauthenticated inference, `/v1/models`, `/models`, and management return 401; both listings are equivalent apart from request timestamps; the temporary `compat/model-a` alias reaches resident A's `/v1/stats`; all three key forms expose exact resident A; authenticated inventory is consistent; metrics and a bounded `management_loaded` event are available | + +## Separate performance evidence + +`benchmarks/swap/qualify_native_router.py` is the opt-in Linux harness for +collecting the four required comparisons in one approved maintenance window. +It starts a private native daemon with a private state directory and extension +cache and a validated dynamic-port TOML catalog. It generates a fresh private +router API key for the run and scopes that credential to the exact temporary +daemon origin; the protected service and direct engine comparison never receive +it. Before performance trials, it requires unauthenticated `/router/status` and +`/v1/models` and `/models` requests to return 401, verifies their normalized +listing equivalence plus Bearer, Basic-password, and `X-Api-Key`, then +authenticates alias, selector, model, profile, configured readiness target, +Prometheus, and bounded router-log SSE checks. Their raw responses and the key-bearing +catalog remain private. The harness then records private raw artifacts for: a direct request to the +router-owned engine port, warm-selector and runtime-profile composition canaries, a warm routed request, a cold routed swap to the +other model, and an alternating routed swap back. It requires streamed OpenAI usage, +then records first-byte time, final duration, completion tokens, and usage-derived +decode tokens/second at the client. Before the comparison sequence it also opens a +long routed stream, explicitly cancels its opaque request ID, and fails unless the +router returns to idle, increments cancellation telemetry, emits no normal terminal +completion credit, and the retained private partial SSE lacks `[DONE]`. It then runs +two simultaneous same-alias streams and fails unless both complete with zero +activation delta and one unchanged resident profile. It stores the corresponding +`/router/status` snapshot and Prometheus `/metrics` response for each routed +comparison, and fails if activation counters do not prove the advertised +warm/cold/alternating state. It then switches to a deliberately missing private +model fixture and requires HTTP 503, a successful rollback launch, restored exact +identity, an activation-failure increment, and a valid completion from the restored +model. The harness compares the private accounting outbox before and after that +failure and requires at least one new valid receipt ID, publishing only the count. +It also writes a private active-profile priority change and requires the router to +reject it with HTTP 409 while retaining exact active identity. +Before that reload check, the harness gracefully terminates the first daemon while +leaving its test-owned engine detached, starts a replacement daemon against the same +private state, and requires the manager's adopted flag, engine PID, engine port, and +router profile identity to match. A routed completion must succeed with zero router +activations before the replacement daemon becomes the final cleanup owner. +It then holds a long model-A stream while requesting model B, requires B to be +visibly queued with A still exact and active, cancels A, and requires exactly one +activation into B followed by one activation restoring A. The cancelled A prefix +must not contain a normal terminal marker. +The harness then reloads a singleton persistent group while no model is resident, +loads that profile, and requires a conflicting load to return HTTP 409 while the +same PID remains exact and persistent. Explicit unload must release the slot and +allow the conflicting profile to activate. +Finally, it explicitly unloads its temporary resident, atomically reloads the private +catalog with a two-second idle TTL, verifies TTL-driven eviction and listener closure, +and leaves no temporary engine for daemon cleanup. +The direct comparison retains its router-owned load receipt and activation +snapshot privately as well. +It also saves the authenticated-local `/router/hardware` process and memory +snapshot for every comparison privately. Each loaded-model snapshot must contain +positive Linux process-tree PSS and per-process GPU-memory values with explicit +available/source markers; an unavailable probe or compatibility zero fails the +run. The published result must remain a sanitized aggregate. + +The harness requires `--allow-maintenance`, the exact operating-system hostname +in `--expected-hostname`, a new empty `--artifacts` directory, two known-good +model paths, and the protected service's private restore endpoint. A hostname +mismatch fails before artifact creation, service inspection, or maintenance and +does not disclose either hostname. It then verifies the protected baseline, prebuilds kernels, +stops the protected service only after the native daemon is reachable, and +always attempts daemon cleanup and protected-workload restoration. Do not run +it on Windows or substitute a direct engine URL for the routed cases. Publish +only sanitized aggregate timings and explicit pass/fail results; raw responses, +paths, daemon logs, catalog, and host data remain private. +The harness also requires the final temporary engine listener to be closed +before it can report success. + +## Restoration and acceptance + +After testing: + +1. Stop temporary daemon and all test engines through their owned lifecycle. +2. Verify temporary listeners are closed and no test worker process remains. +3. Restore the protected workload exactly as captured. +4. Verify its health and deterministic completion. +5. Preserve raw logs and request content privately. Publish only sanitized + aggregate timings, pass or fail outcomes, anonymous hardware label, commit + hashes, and explicit limitations. + +The native route is not qualified by an open port, a green `/health`, or a +single start. Qualification requires the relevant end-to-end matrix evidence +above and protected-workload restoration. + diff --git a/docs/freetoken-swap-parity-matrix.md b/docs/freetoken-swap-parity-matrix.md new file mode 100644 index 000000000..dc2fdcdc0 --- /dev/null +++ b/docs/freetoken-swap-parity-matrix.md @@ -0,0 +1,120 @@ +# freetoken-swap parity matrix + +This is the implementation acceptance contract for native `freetoken-swap`. +It is based on the read-only official llama-swap reference at commit +`41ec321b6216d838488b2a7d936274ed227c0c5e`, MIT licensed. It does not copy +that project's code or authorize changes outside FreeToken. + +Status labels: + +- **Native**: implemented by FreeToken and behaviorally tested. +- **Integrated only**: available only when an unmodified llama-swap binary + supervises FreeToken. This is not native parity. +- **Missing**: applicable, not yet implemented. +- **Deferred**: a potentially applicable product expansion that is not part of + the current one-engine contract. It remains an open difference, not parity. +- **Inapplicable**: a stated backend-modality or one-engine architectural + constraint makes the behavior impossible or misleading. The constraint is + named explicitly rather than compatibility being claimed. + +The field- and route-level classifications behind this matrix are recorded in +[the pinned-source inventory](freetoken-swap-source-inventory.md). + +## Pinned-source inventory + +The following is a read-only source inventory, obtained with `git show` and +`git ls-tree` from the pinned commit rather than from the damaged local working +copy. It makes the scope of the comparison auditable without vendoring any +llama-swap code. + +| Reference source at `41ec321…` | Observed responsibility | Native classification and evidence | +| --- | --- | --- | +| `internal/server/server.go` (`modelPostJSONRoutes`, `modelPostFormRoutes`, `modelGetRoutes`, `routes`), `internal/server/api.go` (`handleListModels`), `internal/swaputil/http.go` (`FindModelInPath`, `EscapedPathSuffix`) | Model-dispatched OpenAI, Anthropic, embeddings, rerank, audio, images, SDAPI, ComfyUI and upstream routes; public model records and status; slash-namespaced longest-prefix upstream dispatch with escaped suffix preservation; list, health, unload, running, logs, metrics, UI, API group, browser CORS | Native text-generation routes, guarded namespaced passthrough, browser preflight/model-list CORS and atomic public loaded/activating/unloaded model status, plus a local management UI, are implemented and HTTP-tested. Embedding, rerank, image, speech, transcription, SDAPI and ComfyUI are **inapplicable** because FreeToken exposes no matching backend route. MCP and Tailcat remain explicitly deferred product surfaces. | +| `internal/config/{config,model_config,commands,filters,macros,selectors,profile,upstream,performance,peer,tailcat}.go`, `internal/server/{api,filters,selector,profiles}.go`, `docs/kb/guides/{api-integration/filters-and-request-rewriting,routing/profiles-and-selectors,model-runtime/capabilities-and-model-listings}.md` | YAML schema, command/macro expansion, request rewriting, runtime pin profiles, selectors, display and capability metadata, peers, hardware/performance policy, and global/per-model `sendLoadingState` | Native allowlisted TOML parser rejects commands/macros and unsafe owned options; aliases, dynamic ports, readiness, TTL, groups, priorities, keys, upstream timeout, safe ordered strip/hard/soft/by-ID JSON filters, runtime pin profiles, pin/warm selectors, model display/JSON metadata, global/per-model loading feedback and atomic reload are behavior-tested. Text input/output, tool-calling, and context declarations render the pinned listing fields but do not enable behavior. Unsupported backend modality and reranker claims fail closed. Spillover is inapplicable to one-resident capacity; macro, peer and Tailcat policy remain deferred or inapplicable rather than emulated unsafely. | +| `internal/router/{router,base,loading,group,matrix,matrix_solver,peer}.go`, `internal/router/scheduler/fifo.go` | Loading, queueing, group/matrix and peer routing | Native single-owner FIFO/priority coordinator, exclusive one-resident capacity, persistent-group protection, leases, eviction and cancellation are tested. Multi-resident matrix solving and peers are deferred: the declared one-engine supervisor cannot prove safe concurrent residency. | +| `internal/process/{process,process_command,runtime_*,treecleanup_*}.go` | Child launch, process identity, stop/reap/tree cleanup | Native `ServeManager` owns the child, durable state, exact identity/re-adoption, process-group cleanup, drain/abort accounting and rollback. On daemon reconstruction, the routing coordinator binds one unambiguous catalog profile to an exact explicit, dynamic, or omitted-default-port adopted identity; ambiguous or argument-mismatched identities fail closed. Deterministic and Linux actual-child recovery tests cover this boundary. | +| `internal/server/{auth,profiles,inflight,log,metrics,metrics_middleware,api,apigroup}.go`, `internal/logmon/*`, `internal/perf/*`, `internal/store/*` | API-key auth, profiles, inflight cancellation, log streams, Prometheus/activity/performance and persistence | Native Bearer, Basic-password, `X-Api-Key`, and dedicated control authentication, profiles, opaque cancellation, bounded engine/router logs, Prometheus lifecycle/queue/transport signals, durable accounting/activity, and a memory-only one-hour owned-process performance history are implemented. Token throughput, memory and extended performance evidence remain bounded live-test gates. | +| `internal/server/{ui,apimcp,captures,tailcat}.go`, `ui/*`, `internal/mcptools/*`, `internal/tailcat/*` | Browser UI, embedded MCP, captures and Tailcat | Native local management UI, restart-durable body-free activity, hashed session grouping, and opt-in bounded/redacted memory-only captures are implemented. MCP and Tailcat are **deferred** product expansions. | +| `internal/**/*_test.go`, `docs/kb/guides/**/*` | Reference behavioral tests and operator documentation | Native tests live in `tests/daemon`; the qualification runbook and completion audit separate deterministic, Linux and approved maintenance-window evidence. | + +| Pinned llama-swap capability | Current FreeToken state | Required native parity evidence | +| --- | --- | --- | +| Model catalog and aliases | Native TOML catalog with collision-safe slash-namespaced and colon-variant canonical/alternate model IDs, unlisted model entries, runtime pin profiles, pin/warm virtual selectors, global/per-model concurrency, validated model, port, args, readiness path, manager-owned loopback proxy target, optional upstream model-name override, unload, and global/per-profile upstream response timeouts. Model IDs use safe nonempty ASCII segments with a 128-character total cap; groups and each dotted filter-path segment retain their narrower grammar. `port = 0` requests a concrete kernel-selected loopback port for each activation. Model/profile/selector lookup, priority ticketing, head-of-queue port binding, and concurrency reservation are atomic with reload, which rejects admission/lifecycle races. | Deterministic tests prove namespaced/variant canonical and alternate routing, unsafe empty/traversal-like segment rejection, alternate-ID canonical residency, optional alias listing, hidden-model routing/list omission, alias unload, profile/selector validation, safe custom readiness and proxy-prefix targets, upstream model-name and timeout validation/inventory, remote/explicit-port/query/fragment/traversal rejection, concurrent cold dynamic-target sharing, allocation-failure cleanup, dynamic-port residency stability, atomic lookup/port binding, queued-request profile snapshot, reload profile reset, and a fresh target after a swap; a Linux real-child test exercises fresh dynamic ports across eviction/reactivation. TLS and pooled-connection timeout knobs are inapplicable to fresh plain-HTTP loopback targets. | +| Virtual model selectors | **Native, applicable subset.** `pin` selects the first ordered local target. `warm` chooses the first exact ready target, then the first activating target, else the first target. The virtual ID is rewritten before target alias filters. Public listing status follows pinned strategy semantics and carries optional name, description, and JSON-compatible metadata with router-owned keys protected. Selector IDs are not direct-upstream or unload IDs. `spillover` is **inapplicable** because its concurrent reservation distribution requires multi-resident or peer capacity, which conflicts with the one-child supervisor contract. | Deterministic parser, routing, concurrent activation, rewrite/filter order, event identity, direct-upstream rejection, hidden/listing status, and metadata tests pass. The private current-engine harness lists the selector and must prove a warm selector reuses resident A with zero activation delta; execution remains required. | +| Start, stop, switch, PID identity, re-adoption | Native manager is the sole process owner. Routed transitions, HTTP and OS/lifespan daemon exit, and legacy manual engine controls use the same coordinator; manual claims fail while routing owns or admits work. Explicit, dynamic, and omitted ports are matched to exact persisted targets, with omitted ports bound only to the configured default. | Deterministic tests prove exact explicit/dynamic/omitted-default-port re-adoption, ambiguity and argument mismatch rejection, recovered identity after failed readiness, matching-token release, routed-lease conflict rejection, stop preemption with stale-token protection, routed admission waiting behind a blocked or client-disconnected manual start, failed-readiness rollback completing after client cancellation, shutdown rejecting queued/new admission while draining active leases and all manual transaction tokens, and drain-before-detach with idempotent exit handling. The complete suite, including disposable actual-child/process-group tests, passed twice on hosted Ubuntu at `5ee1e26`; current-engine evidence remains required. | +| Readiness and diagnostic health | Native `/ready` atomically checks exact resident identity and an uncached profile-configured engine path behind the admission barrier; `/health` retains generation-aware status/maintenance semantics and diagnostic daemon `/health` remains liveness. Non-health readiness paths use HTTP-success semantics but cannot leave the owned loopback port. | Deterministic tests prove default health-state handling, custom-path dispatch, no cold-load, stale model/args/port rejection, maintenance-state rejection, active-target reload refusal, and that a conflicting swap cannot begin during a successful readiness probe. The private qualifier configures the real engine `/ready` path; current execution remains required. | +| Automatic OpenAI model-ID routing | Native single-engine coordinator with priority-aware admission and health-gated activation | Deterministic HTTP coverage plus hosted Linux disposable-child routing passed. GMKtek EVO-X2 real-engine evidence remains required. | +| OpenAI model list, completion and chat completion forwarding | Native catalog-key-protected `GET /v1/models` and pinned `GET /models` alias return identical visible canonical IDs and, by policy, alternate IDs; unlisted profiles and aliases are omitted. Public records carry standard ownership/timestamp fields, optional descriptions, and atomic loaded/unloaded status: launch intent alone remains unloaded, while an exact manager-owned child in readiness-gated activation is loaded; canonical and alternate IDs share status. Request-byte-preserving proxy includes SSE body forwarding. The backend's stateless response-resource lookup/cancel routes preserve its authenticated `invalid_request_error` 404 without arbitrary model activation. | Deterministic tests cover exact alias payload/CORS/key protection, unloaded, pre-ownership launch intent, exact activating, resident, stale-identity and activation-failure recovery status; canonical/alternate listing and routing without local model-path or argument disclosure; hidden routable profiles; every model-bearing supported text endpoint; stateless response-resource compatibility without admission; request bytes; SSE bytes; upstream error status/body/safe headers; and lease release. Direct, cold, warm, cancellation, and performance evidence remains required. | +| Model display and capability metadata | Native profiles accept display `name`, JSON-compatible nested `metadata`, and declarative text `capabilities` for `in`/`out`, `tools`, and nonnegative `context`. Canonical and listed alternate records share display values and render pinned `architecture`, `capabilities.function_calling`, `supported_parameters`, `context_length`, `context_window`, and `meta.n_ctx` fields. Operator metadata is nested under `meta.freetoken`; router-owned canonical/alias identity wins collisions, and capability-owned keys are filtered when capabilities are declared. Metadata does not change routing or enable inference features. | Deterministic catalog and HTTP tests prove nested/list/scalar JSON validation, display propagation, exact canonical/alias identity, collision precedence, capability rendering, empty-capability behavior, no model-path disclosure, malformed-type rejection, and fail-closed rejection of unsupported image/audio/video or reranker claims. The private control-plane gate verifies display and metadata consistency across both public-list aliases and router inventory; current GMKtek execution remains required. Operators remain responsible for advertising tools only when the selected model and template actually support them. | +| Browser CORS compatibility | Native global `OPTIONS` preflight returns the pinned 204 compatibility headers without entering routing or lifecycle work; requested header names are token-sanitized. Authenticated `/v1/models` and `/models` reflect `Origin`. | Deterministic HTTP tests prove unknown-path preflight, default and sanitized requested headers, zero manager calls, retained 401 on unauthenticated model listings, and origin reflection after bearer authentication. | +| Optional streaming cold-load feedback | **Implemented, applicable.** Native global configuration with a nullable per-profile override applies only to strictly streaming `/v1/chat/completions` when the exact target is not readiness-gated resident. An atomic post-concurrency reservation signal commits HTTP 200 only for admitted cold work, emits reasoning and queue-position SSE, then continues the real upstream stream; post-commit activation/connect failures are framed in-band with `[DONE]`. | Deterministic tests prove queued cold and warm behavior, global/override precedence, strict route/stream eligibility, unchanged disabled-path status/body/headers, preserved upstream SSE, pre-admission 429 JSON, activation/connect failure framing, explicit cancellation, client-disconnect cleanup, reservation/lease ownership and metrics. The hosted Linux disposable-process router test passed with loading feedback before the real child's terminal SSE. The private native harness requires loading frames on cold-B/A-B-A trials and their absence on warm-A; current GMKtek execution remains required. | +| OpenAI Responses endpoint | Native `POST /v1/responses` uses the same admission and proxy contract. FreeToken's stateless response lookup/cancel stubs are authenticated compatibility routes that preserve the engine's `invalid_request_error` 404 without model admission. | Deterministic tests cover the model-bearing routed endpoint and exact no-admission lookup/cancel errors. Add routed response-object and cancellation proof only if FreeToken gains a stateful backend. | +| Reference versionless and llama.cpp-native text aliases | The pinned reference routes `/v/chat/completions`, `/v/responses`, `/v/completions`, `/v/messages`, `/v/messages/count_tokens`, `/completion`, and `/infill`. FreeToken's engine registers none of these aliases; its text contract is the `/v1/*` surface above plus model-less legacy `/generate`. | Intentionally inapplicable while the backend lacks those routes; do not advertise fabricated compatibility. A custom or future backend route remains reachable only through explicit `/upstream/{profile}/...` selection until it becomes a FreeToken-supported model-bearing endpoint. | +| Anthropic Messages and token-count routing | Native routes use the same admission and proxy contract | Deterministic HTTP tests cover both Messages and token-count routing; add live failure proof. | +| FreeToken legacy `POST /generate` | The request schema has no model identifier, so an automatic route at the stable daemon URL is intentionally inapplicable: choosing a model would require an unsafe implicit default. Profile-qualified `POST /upstream/{profile}/generate` remains available through unified admission. | Deterministic HTTP proof rejects ambiguous top-level `/generate` and preserves the explicit passthrough method, body, SSE response, and lease. | +| Unknown-model status and direct upstream access | Native stable `unknown_model` error envelope and `/upstream/{model-id}/...` passthrough through the same lease. The longest configured canonical/alternate ID wins when IDs contain slashes; encoded model separators and the downstream escaped path/query are preserved. A safe bounded suffix policy defaults to pinned static extensions and returns 409 before reservation or activation when the exact model is unloaded. | Deterministic HTTP tests prove the identical 404 error type across all five routed text endpoints, namespaced longest-prefix and encoded-alias routing, exact escaped slash/query forwarding, bare-root and GET passthrough, cold static rejection with zero lifecycle/upstream work, warm static forwarding, policy validation, and rejection of unsafe direct `prepare-stop`. The private native harness requires a namespaced alias `/v1/stats` passthrough; GMKtek execution remains required. | +| FIFO, priority, concurrency, exclusive group routing | Native priority-aware FIFO queue, pinned default per-profile concurrency cap of 10, optional per-profile/global overrides, immediate 429 rejection with `Retry-After`, and one-engine exclusive admission. Reservations cover active, queued, and activating requests and alternate IDs share their canonical cap. The TOML parser rejects coexistence flags it cannot honor while admitting singleton persistent protected slots. | Deterministic tests cover default/override/global limits, alternate-ID sharing, immediate rejection before queue/upstream work, request-ID cleanup, released-slot reuse, duplicate-release protection, priority-before-earlier-low-priority queueing, accepted/rejected group policy, and capacity protection. The private native harness holds A, proves B queues without disturbing A, cancels A, and requires ordered B then A activation; current GMKtek execution remains required. | +| Matrix or equivalent capacity policy and eviction costs | **Native equivalent policy:** the sole `ServeManager` child is the one resident slot; status exposes its exact identity, group, availability, queue, and eviction decisions. | Deterministic tests and the private maintenance harness cover exclusive transitions and persistent-slot protection. Multi-resident matrix solving and memory-ranked victim selection are **inapplicable under one-engine ownership** because there is never a choice among co-resident victims; they become deferred requirements only if FreeToken adds multi-engine ownership. | +| Persistent resident models and startup preload | Native persistent group protects the sole resident slot until explicit unload. A validated singleton `preload_model` canonicalizes aliases and acquires through the same native lifecycle during app startup; optional `startup_routing_profile` activates a validated pin map before serving. Selectors, unknown targets, and multiple preloads are rejected under one-resident capacity. | Deterministic parser and lifespan tests prove canonicalization, unknown-target rejection, native readiness-gated preload, zero residual lease, and startup profile activation. The private harness enables startup only after protected maintenance begins and requires preload to reuse the exact re-adopted A PID/port with zero activation delta. Multi-resident preload is inapplicable with the current one-engine supervisor; GMKtek execution remains required. | +| TTL and unload timeout | Native timer schedules idle-only eviction; authenticated `POST /router/unload` uses the profile or global graceful-stop timeout and the existing accounting transaction | Deterministic lease/TTL and explicit-unload tests cover no eviction while leased, profile timeout selection, and durable manager cleanup; real-engine endurance remains separately bounded. | +| Load/unload management API and running-model list | Native router status, configured plus resident `/router/models`, authenticated lifecycle profiles at `/router/profiles`, `POST /router/load`, and `POST /router/unload` through the same lifecycle coordinator. The daemon CLI reads `/router/profiles`; `/models` is reserved for pinned public-list compatibility. A named body unloads that profile; no body unloads all residents (the current resident under one-engine capacity). | Deterministic HTTP tests prove CLI/control authentication, no profile-path disclosure through `/models`, named mismatch preservation, named unload, and no-body unload-all. Load-all and multi-resident management are inapplicable to the explicit one-engine capacity policy. | +| Runtime routing profiles | **Native:** validated `[profiles..pins]` atomically replaces a set of client model IDs before selectors, aliases, and target filters. Empty targets disable pins. Profile pins compose with selectors, rewrite longest direct-upstream prefixes, add non-shadowing virtual IDs to public listings, start cleared unless `startup_routing_profile` is configured, and reset on catalog reload. Authenticated `PUT /router/profiles/active` and CLI verbs activate or clear the map; concrete lifecycle load/unload ignores it. | Deterministic parser, startup, API, CLI, disabled-pin, shadow, profile→selector, alias-filter, escaped direct-upstream, listing, event, queued-snapshot, management-isolation, and reload-reset tests pass. The private harness must exercise both runtime and restart-time activation while reusing resident A with zero activation; current GMKtek execution remains required. | +| API keys | Native router keys accept case-insensitive Bearer, Basic-password, or `X-Api-Key` for inference-compatible routes (including both model-list paths) and, absent a separate daemon token, management; explicit Authorization wins over fallback. `X-FT-Token` remains the dedicated control-plane override, does not bypass catalog-key-protected inference listings, and all local credentials are terminated before proxying. | Deterministic authorization tests cover the separated listing/control domains, every key form, malformed-Basic fallback, anti-bypass precedence, Anthropic routing without credential forwarding, 401 challenge, atomic catalog-driven key rotation, and qualification credential isolation. The private live harness gates all three forms, requires unauthenticated inference and management to return 401, and never sends the key to the protected service or direct engine; GMKtek execution remains required. | +| Logs and bounded streaming logs | Native, separate bounded router event ring at authenticated `GET /router/logs?since=` with the same replay/resume/SSE contract as engine logs | Deterministic tests prove admission/completion events, privacy-safe payloads, bounded ring behavior, management authorization, and multi-frame bounded qualification capture. The live harness requires an authenticated `management_loaded` event; GMKtek execution remains required. | +| Prometheus and activity/performance metrics | Native `/metrics` exposes bounded router admission, active/reserved/queued requests, queue wait, active identity, activation time, failure, cancellation, eviction, normal-terminal-stream, last-TTFT, last-duration, response-byte, and proxy-byte-rate signals; router-cancelled streams are not credited as normal terminal completions. Authenticated `/api/performance` and `/router/performance` retain at most one hour of owned engine process-tree RAM/VRAM samples, support strict RFC3339 `after`, and preserve unavailable/source markers without fabricating adapter-wide sensors. | Deterministic tests cover sampler lifecycle generations, one-hour eviction, filtering, auth, disabled 503, failure isolation, and path/PID omission. `benchmarks/swap/qualify_native_router.py` requires an authenticated positive available periodic RAM/VRAM sample with source labels and no PID/model/path fields, plus router metrics, and collects private direct/warm/cold/alternating first-byte, duration, streamed-usage-derived completion-token-rate, process, and memory evidence. It still requires an approved Linux GMKtek EVO-X2 execution. | +| Inflight cancellation API | Native router issues or accepts opaque `X-FT-Request-ID` values, atomically reserves them before admission, lists IDs throughout queued/connecting/active ownership, removes disconnected waiters from the admission queue, and provides `POST /router/requests/{id}/cancel` | Deterministic tests prove duplicate IDs cannot create a second admission or upstream request; operator or disconnect cancellation removes queued work before a later swap; connecting cancellation closes eventual sockets and releases leases; failure paths release ownership; and active cancellation closes the socket and is not credited as normal completion. Cancellation telemetry is counted once per accepted cancellation. Same-instance real-engine terminal-abort proof remains required. | +| Parameter filters and configuration hooks | Native `use_model_name`, `drop_fields`, `set_fields`, and `set_fields_by_id` follow the pinned outbound-model/strip/global/by-ID order. The optional override changes the upstream JSON `model` without changing requested routing identity or by-ID selection. Hard values override clients; `?` values fill only absent paths; explicit null/zero/false remain present. By-ID tables automatically create collision-checked aliases. The top-level `model` field is otherwise protected. Policy comes from the exact admitted profile; JSON direct-upstream requests share it, while non-JSON and empty policies remain byte-exact. | Deterministic parser, transform, HTTP, cold-loading, alias-collision, protected-field, active-reload, direct-upstream, and qualification-canary tests cover the applicable data-only behavior. The private harness requires an alias response to report the configured upstream name with unchanged residency; GMKtek execution remains required. Lifecycle shell hooks are intentionally inapplicable because native `ServeManager` owns argument-vector launch, accounting, drain, rollback, and cleanup without a shell. | +| Configuration watch/reload | Native authenticated `POST /router/reload` and default cross-platform local catalog polling re-parse and atomically validate the catalog. Watch status and sanitized results are observable. | Deterministic tests cover manual valid replacement, invalid-file rejection, active-profile scheduling/effective-lifecycle redefinition refusal, watcher valid replacement and watcher rejection. Real-engine reload evidence remains required. | +| UI, hardware, captures, MCP, Tailcat | Native dependency-free `/ui/` management shell, authenticated `/router/hardware`, bounded performance history, restart-durable body-free activity/stat APIs, hashed session grouping, and opt-in memory-bounded redacted captures fetched by UI only on selection. Byte fields pair availability/source markers so unavailable probes cannot masquerade as zero. MCP and Tailcat are **Deferred** product expansions. | Deterministic tests prove API protection, performance bounds/filtering/privacy, fsynced restart recovery/compaction/failure health, hashed session identity, capture-disabled default, credential redaction, binary fidelity, overflow/cancellation refusal, eviction, aggregation, UI secret isolation, AMD SMI summing, and explicit unavailable memory. The private live gate requires positive measured RAM and VRAM; raw captures remain private. | +| Embedding, rerank, image, speech, transcription, ComfyUI, SDAPI routes | Inapplicable today where FreeToken has no matching server route | Document absent FreeToken backend capability and reject safely. Do not mimic endpoint success | +| Accounting, drain/abort barrier, rollback | Native automatic routing delegates every stop/switch to `ServeManager`; readiness and launch failures retain its recovery result, including through `POST /router/load` | Deterministic routing and management-API tests prove recovery evidence and restored exact identity. The private native harness now requires a failed disposable real-model switch, rollback launch, new durable outbox receipt, failure-counter increment, and restored completion; current-branch Linux and GMKtek execution remain required. | + +## Native real-process gate + +`tests/daemon/test_real_process_recovery.py` now includes a Linux-only native +router test that starts a disposable HTTP child through `ServeManager`, waits +for real `/health` readiness, routes an SSE request through the daemon, then +stops the child and verifies pidfile cleanup. A second Linux-only test persists +a live disposable child as prior-daemon state, re-adopts it into a new manager, +binds the exact catalog profile in a new routing coordinator, routes SSE without +calling the spawn function, and verifies cleanup by the new owner. It is skipped +on Windows. The complete 352-test daemon suite, including these tests, passed +with no skips in GitHub-hosted Ubuntu run `34941311939` for commit `39c3aaab`. +This closes the current-branch +disposable Linux process gate only; it does not qualify the current FreeToken +engine, GPU models, or the GMKtek maintenance matrix. + +Git also produced clean synthetic combined tree +`74a4f3b1649442d9d8c24576751d29f30e218d04` from swap head `39c3aaab` and +draft AMD compatibility head `c0534c6f`. In an isolated Windows export, 377 +daemon/privacy/benchmark-contract/reproducibility tests passed with 7 expected +Linux skips, followed by 21/21 grouped-output, SSM, and GGUF configuration +tests in a disposable CPU torch environment. This is source-tree compatibility +evidence only; it is not current-engine, GPU-model, or restoration proof. + +## Architecture gate + +The target is one FreeToken-owned router and lifecycle supervisor. It must not +delegate automatic routing to llama-swap while retaining safety only in the +manual daemon. The existing llama-swap integration remains a compatibility and +comparison reference until native request routing reaches the acceptance gates. + +## Remaining acceptance sequence + +1. In an approved GMKtek EVO-X2 maintenance window, run the private native + qualification harness through direct, warm, cold, alternating, cancellation, + same-model concurrency, conflicting-model drain, failed-switch recovery, + daemon re-adoption, reload-conflict, persistent-capacity, TTL, unauthenticated + rejection, and authenticated management/metrics/router-log gates. Supply the + exact operating-system hostname explicitly; the harness fails before artifacts + or service inspection if it does not match. +2. Restore and health-check the protected workload, retain raw evidence privately, + and publish only sanitized aggregate observations in the final audit. +3. Re-run deterministic, hosted Linux, and combined-tree compatibility suites at + the final PR head and keep the PR draft until all applicable evidence is linked. + +Every row moves to Native only after deterministic tests and relevant live +evidence are linked here. No endpoint name alone establishes parity. + +The privacy-safe native live acceptance matrix is maintained in +[native qualification runbook](freetoken-swap-native-qualification.md). diff --git a/docs/freetoken-swap-research.md b/docs/freetoken-swap-research.md new file mode 100644 index 000000000..946e1fa59 --- /dev/null +++ b/docs/freetoken-swap-research.md @@ -0,0 +1,137 @@ +# FreeToken swap compatibility and model repair + +## Findings + +Automatic model swapping is feasible without modifying llama.cpp or rewriting the llama-swap router. llama-swap already accepts OpenAI-compatible inference servers. FreeToken needs a compatible readiness contract, qualified model loaders, explicit resource limits, and a documented choice of process supervisor. The initial native daemon catalog was only a manual control plane. The current FreeToken branch adds a native model-ID router, lease-based admission, byte-preserving proxying, guarded upstream passthrough, cancellation, idle eviction, atomic catalog reload, and a deliberately explicit single-resident-model capacity policy. This is deterministic implementation evidence, not yet real-engine parity proof.[1][2] + +Two independent defect classes explain the unsuccessful initial attempts. First, the supervisor could report success incorrectly or time out before the model's readiness budget expired. Second, the AMD model loader and tokenizer did not support the exact resident GGUF layouts. A model catalog cannot repair a tensor format mismatch, and an HTTP listener cannot establish backend readiness. These defects need separate acceptance gates. + +The implementation target remains FreeToken GitHub. The reference source is mostlygeek/llama-swap, a separate MIT-licensed project, not a component in the llama.cpp repository. The inspected reference revision is `41ec321b6216d838488b2a7d936274ed227c0c5e`. No third-party source is copied into this implementation and no upstream llama.cpp change is proposed.[1] + +## How swapping should work + +The intended client contract is a stable proxy URL. A request names an allowlisted model alias in its JSON `model` field. The supervisor selects that configuration, starts the corresponding backend when necessary, waits for it to accept work, and forwards the request. Subsequent requests reuse the resident backend. A request for a different model causes the routing policy to decide which process must leave memory.[1] + +llama-swap's default routing is one model at a time. Its group router can explicitly make a group exclusive and require swapping among its members. Concurrent groups and the matrix router are additional capabilities, not evidence that a particular shared-memory machine has capacity to run multiple models safely. For initial GMKtek EVO-X2 qualification, an exclusive single-model policy is the appropriate starting point.[3] + +There are three distinct time budgets. The readiness timeout limits how long a new backend may take to become usable. The idle TTL determines when an unused backend may be evicted. The unload timeout limits graceful process termination after eviction has begun. Increasing one does not increase the others. A short TTL can cause expensive repeated cold loads, so the example retains one model indefinitely and shows a five-minute idle TTL for the other.[4] + +The assigned backend port must match the proxy target. The example passes `${PORT}` to FreeToken and explicitly proxies to `127.0.0.1:${PORT}`. It passes `${MODEL_ID}` as FreeToken's served model name. Where a backend expects a different name, the native profile's validated `use_model_name` rewrites only the outbound JSON model before strip/global/by-ID filters while preserving the requested routing identity. Catalog entries must point to already available, compatible model artifacts. The example does not download or qualify weights.[2] + +Streaming is part of the acceptance contract. A proxy that buffers all generated output before replying is not equivalent to an SSE-capable model router. Tests must also cover client cancellation, admission while a model changes, concurrent requests for one model, and conflicting requests for two models. An apparently healthy proxy can still have a broken inference path; liveness and successful routing are separate measurements. + +## Readiness incompatibility and repair + +llama-swap polls a configured endpoint and accepts HTTP 200 as readiness. FreeToken's existing `/health` deliberately returns a diagnostic JSON document even while loading or in an error state. Consequently, pointing llama-swap at FreeToken's default `/health` can release requests before the backend is usable.[2][5] + +The added `/ready` endpoint preserves `/health` compatibility. It returns HTTP 200 only when the health document reports `status=ok` and `maintenance=serving`. Loading, failure, and maintenance produce HTTP 503. The example sets `checkEndpoint: /ready`. `/v1/models` is not a substitute for this gate because listing a configured model does not prove that its weights and execution backend are ready. + +The manual daemon profile path also had a stale-cache hazard. Its general health probe caches by port, but successive engines can reuse a port. A readiness check now bypasses that cache and rechecks the managed PID after the HTTP request. It also uses the launch port captured for the transaction instead of resolving a potentially changed current port afterward. This reduces false success during replacement, but it is not a request lease or a complete proof against PID reuse and unrelated port ownership. + +Readiness failure returns HTTP 503 from profile operations, and the CLI returns nonzero for unsuccessful readiness responses, including responses from older servers that still use HTTP 200. The default profile transport budget is 1920 seconds, covering replacement and recovery readiness windows plus lifecycle overhead. A user-specified timeout still takes precedence. Native `switch-profile` now attempts previous-engine recovery after readiness failure, guarded by a one-use lifecycle epoch so newer operator actions win. Initial starts without a previous engine remain managed for inspection. Recovery retains the accounting safeguards and reports launch and readiness independently. + +The catalog validation also rejects the `--model-path` alias and abbreviations of supervisor-owned model and port options. FreeToken uses argparse, whose default abbreviation behavior makes checking only the exact strings `--model` and `--port` insufficient. Validation remains torch-free and accepts argument vectors rather than catalog-supplied shell commands.[6] + +## Model loading defects + +The GGUF label Q4_K_M describes a quantization recipe, not a guarantee that every tensor is Q4_K. GGUF tensor descriptors carry their individual types. Qwen hybrid attention has independent QKV, gate, and output projections. Their packed storage cannot be concatenated blindly when the quantization block formats differ.[7][8] + +The exact Qwen3.6 27B candidate contains Q6_K GDN QKV weights alongside Q4_K gate weights. The old loader attempted a packed concatenation and encountered incompatible row widths. The repair uses separate native GGUF linear operators, then combines their floating-point activations in the existing GDN computation. It does not expand the complete model to full precision. + +GDN output ordering requires another repair. Quantized output blocks can span more than one value head. Moving a fraction of a block as though it were an independent head also moves or misassociates shared quantization metadata. The repaired dense path keeps the packed output weights intact and applies the inverse head-group permutation to activations before the output projection. A permutation regression test is necessary in addition to shape checks. + +The first exact-file Qwen3.6 CPU/meta contract passed after applying the earlier candidate repair to an isolated AMD checkout. The same candidate did not pass Qwen3.8: its first GDN gate was Q8_0, while the candidate assumed Q4_K. This is direct evidence that a repair hardcoded for one quantization recipe should not be advertised as general Qwen support. The next iteration derives projection types from each tensor's descriptor and keeps the legacy MoE path separate. + +Dense `qwen35` also needs a tokenizer converter mapping. The candidate maps it to the compatible `qwen3` converter key rather than allowing a `qwen35` dictionary lookup to fail. A real tokenizer round-trip and chat-template test remain necessary because a successful architecture lookup alone does not establish special-token behavior. + +Dense checkpoints contain no routed experts. Their expert-only loading phase must be a no-op, while the separate MoE expert-cache contract must remain intact. Initial dense model qualification uses the fused/non-MoE execution selection. This should not be generalized to MoE checkpoints, which need their actual expert-residency configuration. + +## Architecture decision + +There are two valid operating modes, with different guarantees. In direct integration mode, a pinned llama-swap binary owns FreeToken processes and supplies automatic routing, streaming proxying, idle eviction, and its existing model-management interfaces. FreeToken supplies `/ready` and inference. The YAML example describes this mode. Do not simultaneously give those processes to `ft daemon`. + +In native daemon mode, FreeToken owns process groups, durable state, final accounting receipts, automatic inference routing, stream-aware admission, idle eviction, guarded passthrough, and explicit router cancellation. The router serializes unsafe replacements through the existing manager instead of double-supervising an engine. It presently supports one resident engine, so persistent groups reserve that slot and multi-resident matrix solving remains unimplemented. Calling this mode complete llama-swap parity would still overstate the evidence until real-engine, timing, and broader endpoint tests pass. + +The native mode now uses one FreeToken-owned routing and lifecycle layer with explicit leases, durable receipt semantics, rollback, and exact re-adoption. It is not a separate catalog layered over another supervisor. Direct integration with the pinned llama-swap binary remains a distinct comparison mode only: never run it against a process owned by `ft daemon`, and do not use its historical results as evidence for the current native implementation. The direct example does not promise the daemon's durable accounting outbox. + +Model support and runtime support must be pinned separately. This swap branch is based on the FreeToken fork's main branch, whereas the repaired Qwen loader targets its AMD branch. A model repair PR must target the AMD base rather than silently importing unrelated runtime and benchmark history into the control-plane PR. Combining branches for qualification is a local integration step, not proof that upstream FreeToken already supports the candidate. + +## Qualification and operating limits + +Validation proceeds from cheapest and safest checks to expensive serving tests. First parse the catalog and confirm the exact model artifact and architecture. Next validate all loader keys, shapes, and dtypes against a meta-device model. Then test tokenizer behavior and the relevant tensor-order transformations. Only after these gates should an isolated GPU process be started. + +The initial GPU profile should use an explicit small sequence and token budget, such as 4096 tokens, with a bounded prefill size. FreeToken's relevant flag is `--max-seq-len-override`, not llama.cpp's `--ctx-size`. A large automatically derived cache can turn a model compatibility check into an uncontrolled capacity experiment. Successful short-context qualification does not establish 64K support. + +At the current safety check, GMKtek EVO-X2 had an active llama.cpp process and approximately 23 GiB of available system memory. That process was left untouched. System `MemAvailable`, GPU-visible UMA, and current accelerator allocations are distinct measurements. A historical GPU-memory value cannot authorize a new load, and model file size alone cannot establish fit after runtime overhead, KV cache, staging, and other workloads are included. + +The next real-model gate is one deterministic completion, repeated after a cold reload, with model identity and raw output retained privately. After that, test A-to-B-to-A routing, ordinary and streamed responses, cancellation, same-model concurrency, conflicting-model admission, idle eviction, forced backend failure, and shutdown. Record peak memory, swap activity, load time, time to first token, and final process cleanup. Stop a failed quality or memory-safety trial without promoting it to production. + +CPU contract tests and mocked HTTP tests are valuable regression evidence, but they are not proof of GPU numerical correctness, backend graph readiness, or live swap throughput. Any release checklist must retain those distinctions. The subsequently approved maintenance-window results below supersede the initial restriction on stopping the protected service. No permanent production activation was performed. + +## Completed live iterations + +The repaired Qwen3.6 and Qwen3.8 files both passed their exact CPU/meta tensor contracts and tokenizer text round-trips. Twenty-one model tests passed, including a matrix of independently typed QKV/gate projections. The daemon suite passed 52 tests with two platform skips; the AMD benchmark/privacy suite passed 27 tests. + +The first live startup problem was a qualification-command error: `python -m freetoken` is the legacy direct-server entrypoint and rejects the `serve` subcommand. The corrected invocation is `python -m freetoken.cli serve`. Another attempt stalled behind an abandoned shared PyTorch extension-cache lock. The solution was a private `TORCH_EXTENSIONS_DIR` and native-kernel preflight before stopping the protected service. The shared cache was left untouched. + +Two complete A-to-B-to-A passes then succeeded through the pinned, unmodified llama-swap binary. The extended pass returned the deterministic answer `4` for Qwen3.6, Qwen3.8, then Qwen3.6 in 35.99, 44.17, and 33.17 seconds, including loading or switching. It also passed two concurrent requests for the same model, concurrent requests for different models, explicit streamed usage blocks, and five-second idle eviction. These are bounded functional controls, not broad quality benchmarks or isolated decode-throughput measurements. + +Both ordinary and SSE responses were checked, including `[DONE]`. Adding `stream_options: {"include_usage": true}` eliminated the missing-usage metrics issue without changing llama-swap. The original stream was valid JSON but lacked the usage block that its metrics parser requires. The final proxy/backend log contained no recorded traceback or streaming-metrics error. + +Every maintenance trial restored the protected service and verified a deterministic completion. After the final pass, the service manager reported it active and running, and the test listeners were closed. Raw artifacts remain private under the logical sets `freetoken-swap-live-20260910-d` and `freetoken-swap-live-20260910-e`. FreeToken PR #1 contains the control-plane integration and PR #2 contains the AMD model repair and anonymization. + +Remaining limits are explicit: no claim of long-context qualification, comprehensive tool-calling quality, direct-supervisor rollback, or long-duration reliability is made. The direct integration does not acquire the daemon's durable accounting guarantees. Semaphore-cleanup warnings remain a follow-up investigation even though the final worker-process and port cleanup checks passed. The additional bounded cancellation and native real-model recovery results below supersede those earlier unqualified gates. + +### Approved live cancellation and native recovery + +The subsequent approved window passed the cancellation harness using the same pinned llama-swap binary and repaired runtime. Qwen3.6, Qwen3.8, and Qwen3.6 returned `4` in 35.01, 36.95, and 33.29 seconds, including loading/switching. The cancellation request produced first content after 0.368 seconds. After disconnect, the same engine instance reached zero active requests in an observed 0.254 seconds, while completed requests stayed at one. No engine restart or natural completion was accepted as cancellation. Post-disconnect A-to-B-to-A streaming, same-model concurrency, conflicting-model concurrency, and idle eviction passed again. + +The native daemon was then tested against the real Qwen3.6 runtime through its profile API. A private invalid GGUF fixture caused the real loader to raise `GGUF magic invalid`; the switch returned HTTP 503. Automatic rollback restored the previous Qwen3.6 model, reached readiness, and returned `4` in a streamed completion. Accounting preserved the previous engine's sealed receipt (27 prompt tokens, 2 completion tokens, complete drain). The failed loader's separate crash receipt was marked degraded with unknown token totals, rather than inventing zero usage. + +Both phases restored the original llama.cpp service and verified generation. Final read-only checks found no test listeners or remaining FreeToken multiprocessing workers. Private logical artifact sets are `freetoken-swap-live-20260910-f` and `freetoken-native-recovery-20260910-a`. These results qualify the documented bounded workflows, not every model, failure mode, context size, or extended workload. + +### Native recovery regression suite + +The additional Linux real-process suite passes both normal SIGTERM and SIGTERM-resistant child cases on GMKtek EVO-X2, without loading models or interrupting the protected workload. It uses isolated loopback HTTP test children and verifies previous-engine readiness recovery, restored arguments and pidfile, two durable replacement receipts, process-group worker cleanup, and a closed listening port. This strengthens OS lifecycle evidence but is not GPU model-failure qualification. + +The current native-router Windows daemon suite passes 313 tests with 7 expected Linux-only skips. Coverage exercises replacement launch failure, recovery launch failure, readiness error and timeout, recovery readiness failure, accounting failure preservation, replacement exit and persisted-state cleanup, one-use recovery tickets, automatic canonical, alternate, pin/warm virtual, runtime/startup-profile-pinned model-ID routing, singleton alias-canonicalized startup preload through native lifecycle, profile-to-selector composition, disabled and shadowing pins, explicit upstream model-name rewrite with preserved by-ID routing identity, model display/JSON metadata propagation and collision precedence, global/per-profile upstream socket timeouts, profile and selector rewrite/filter ordering, strategy-specific listing status, safe configurable readiness paths and manager-owned loopback proxy prefixes, HTTP-success readiness without body retention, remote/explicit-port/query/fragment/traversal target rejection at both parser and connector boundaries, active-target reload refusal, direct-upstream longest-prefix profile rewrites, atomic profile selection/listing snapshots and reload clearing, hidden-profile list policy, exact `/models` public-list alias and separate profile-control authentication, atomic public pre-ownership/unloaded/activating/resident/stale model status without path disclosure, global/per-profile concurrency reservations and immediate rejection, concurrent cold dynamic-target sharing, global/per-profile cold-load feedback after admission with queue reasoning SSE, warm and disabled-path preservation, in-band activation errors, explicit cancellation and disconnect cleanup, sanitized side-effect-free browser preflight and authenticated model-list CORS, Bearer/Basic-password/`X-Api-Key` extraction and anti-bypass precedence with local credential termination, atomic readiness, disconnect-safe shared manual/routed lifecycle exclusion and rollback completion, coordinated HTTP and OS/lifespan daemon shutdown, drain-before-detach including preempted manual transactions, immediate shutdown admission closure under lifecycle-pool contention, queued/connecting/active cancellation ownership, guarded longest-prefix passthrough with escaped path/query preservation, authenticated stateless response-resource compatibility without model admission, race-safe atomic reload and dynamic-port binding, strict filters and namespaced-ID validation, exact explicit/dynamic/omitted-default-port re-adoption, capacity protection, invalidation by newer lifecycle operations, exact-origin qualification credentials, unauthenticated control/inference rejection, authenticated alias/selector/profile/readiness/upstream-model/upstream-timeout/model-metadata/metrics/router-log evidence gates, and privacy-safe exact-host maintenance gating before side effects. These are controlled CPU and loopback-HTTP tests, not new real-model measurements. + +The latest complete daemon suite passed on GitHub-hosted Ubuntu at commit +`a3fc0ddbf6c929164aa925d50c2270c1ca90c318`: run `34933392153` reported +320 passed with no failures, errors, or skips. It executes the actual disposable +Linux child, process-group escalation, readiness rollback, exact re-adoption, +dynamic-port reactivation, routed SSE, cleanup cases skipped on Windows, and +the deterministic pin/warm selector, runtime routing-profile, upstream model-name, +model display/metadata, and safe readiness/proxy-target gates. This run +establishes current-branch Linux process behavior only; current-engine and +GMKtek GPU-model qualification remain separate gates. + +The pinned optional cold-load feedback behavior is now implemented through an +atomic reservation callback after concurrency admission. Strictly streaming chat +requests for a nonresident target receive queue/load progress as reasoning SSE; +warm and disabled requests retain the ordinary proxy, immediate concurrency +rejection remains HTTP 429 JSON, and post-commit activation or connection failure +is framed in-band before `[DONE]`. Deterministic tests cover explicit cancellation, +client disconnect, reservation and lease cleanup, and byte-preserving bypasses. +The private native harness now requires loading frames during cold-B and +alternating-A trials and rejects them during warm-A, but that gate has not yet +been executed on the current branch during an approved GMKtek maintenance window. + +## Privacy and publication + +Public material identifies the primary test computer as GMKtek EVO-X2. Personal home paths use `/home/operator` or equivalent placeholders, and LAN addresses use documentation-only example addresses. Raw logs remain private because they may contain personal paths, hostnames, device identifiers, and request content. Redaction must not make an example address appear to be a working deployment address. + +Privacy review preserves license notices, upstream authorship, and repository URLs needed for provenance. Working-tree sanitation does not remove identifiers from historical Git objects, forks, cached PR revisions, or previously generated PDFs. History rewriting and regenerated publication artifacts require a separate, verified pass; they must not be reported as completed merely because current Markdown has been sanitized. + +## Sources + +Sources were inspected on 2026-09-10. Local implementation and test observations above refer to the candidate branches, not to claims made by upstream maintainers. + +1. mostlygeek/llama-swap contributors. [Repository and feature overview](https://github.com/mostlygeek/llama-swap/tree/41ec321b6216d838488b2a7d936274ed227c0c5e), pinned revision; MIT license in `LICENSE.md`. +2. mostlygeek/llama-swap contributors. [Writing the cmd for a model](https://github.com/mostlygeek/llama-swap/blob/41ec321b6216d838488b2a7d936274ed227c0c5e/docs/kb/guides/model-runtime/writing-cmd.md), updated 2026-08-25. Port assignment, readiness, and model-name rewriting. +3. mostlygeek/llama-swap contributors. [Running several models at once with groups and matrix](https://github.com/mostlygeek/llama-swap/blob/41ec321b6216d838488b2a7d936274ed227c0c5e/docs/kb/guides/routing/groups-and-matrix.md), updated 2026-08-25. +4. mostlygeek/llama-swap contributors. [Automatic model unloading with ttl](https://github.com/mostlygeek/llama-swap/blob/41ec321b6216d838488b2a7d936274ed227c0c5e/docs/kb/guides/model-runtime/ttl-and-unloading.md), updated 2026-08-25. +5. FreeToken contributors. [Control API](../python/freetoken/server/control_api.py), `build_health` and `register_control_routes` in the candidate checkout. +6. FreeToken contributors. [Server argument parser](../python/freetoken/server/args.py), model aliases and parser construction in the candidate checkout. +7. ggml-org/llama.cpp contributors. [Qwen35 model implementation](https://github.com/ggml-org/llama.cpp/blob/master/src/models/qwen35.cpp). Independent attention/gate model structure; moving upstream reference, not a candidate qualification result. +8. ggml-org/llama.cpp contributors. [Quantization recipes discussion](https://github.com/ggml-org/llama.cpp/discussions/20522). Primary maintainer discussion of per-tensor quantization choices; not evidence that every file using the same recipe has identical layouts. diff --git a/docs/freetoken-swap-source-inventory.md b/docs/freetoken-swap-source-inventory.md new file mode 100644 index 000000000..213511391 --- /dev/null +++ b/docs/freetoken-swap-source-inventory.md @@ -0,0 +1,163 @@ +# freetoken-swap pinned-source inventory + +This document decomposes the parity contract in +[`freetoken-swap-parity-matrix.md`](freetoken-swap-parity-matrix.md). The source +of truth is the read-only `mostlygeek/llama-swap` commit +`41ec321b6216d838488b2a7d936274ed227c0c5e`. Source was inspected with +`git show` and `git ls-tree`; no reference code is vendored. The pinned +`LICENSE.md` is the MIT License, copyright 2024 Benson Wong. + +Classifications describe behavior, not matching names: + +- **Native** — implemented in FreeToken and covered by deterministic tests. +- **Equivalent** — a different native contract provides the applicable + behavior and is covered by deterministic tests. +- **Missing** — applicable behavior that is not implemented yet. A missing + live-only proof is called out separately from missing implementation. +- **Deferred** — potentially applicable expansion that is not part of the + current one-engine product contract. It remains an open difference, not + parity. +- **Inapplicable** — impossible or misleading under a stated FreeToken backend + or one-engine architectural constraint. A future capability change reopens + the item. + +The current-engine and GMKtek EVO-X2 maintenance qualification remains pending; +therefore **Native** never implies that the live gate is complete. + +## Configuration schema + +### Global fields + +Pinned sources: `internal/config/config.go` (`Config`, `GroupConfig`, +`HookOnStartup`, `ProfileConfig`, `RoutingConfig`), +`internal/config/performance.go`, `internal/config/upstream.go`, +`internal/config/peer.go`, and `internal/config/tailcat.go`. + +| Pinned field or block | Classification | FreeToken behavior and evidence location | +| --- | --- | --- | +| `models`, `apiKeys`, `globalTTL`, `unloadTimeout`, `globalConcurrencyLimit`, `includeAliasesInList`, `sendLoadingState` | **Native** | Allowlisted TOML equivalents in `python/freetoken/daemon/catalog.py`; routing/auth/list/loading tests in `tests/daemon/test_catalog.py` and `test_router.py`. | +| `startPort` | **Equivalent** | Each profile accepts an explicit port; `port = 0` asks the kernel for a loopback port at activation. Allocation and reactivation are tested. | +| `routing.scheduler.use=fifo` and FIFO priorities | **Native** | One priority-aware FIFO coordinator in `python/freetoken/daemon/router.py`; unsupported schedulers fail validation. | +| group `members`, `swap`, `exclusive`, `persistent` | **Native applicable subset** | Membership and persistent protection are native. Configuration that requests coexistence outside the sole resident slot is rejected rather than weakened. | +| matrix `vars`, `sets`, `evict_costs` | **Inapplicable today** | `ServeManager` owns exactly one child, so there is no co-resident set or victim choice to solve. This reopens if FreeToken gains multi-engine ownership. | +| startup `hooks.on_startup.preload` and `profile` | **Native applicable subset** | `router.preload_model` allows one concrete model/alias and `router.startup_routing_profile` selects a validated pin map. Multiple preloads/selectors are rejected under one-resident capacity. | +| runtime `profiles` descriptions and pin maps, including disabled pins | **Native** | `[profiles..pins]` and authenticated activation API; parser, routing, reload, and lifespan tests. | +| `selectors` (`pin`, `warm`, `spillover`) | **Native applicable subset** | `pin` and `warm` are native. `spillover` is **inapplicable today** because it requires simultaneous reservations across local residents or peers. | +| global `macros` | **Inapplicable by safety contract** | FreeToken accepts argument vectors and explicit typed fields; arbitrary command/proxy/environment interpolation is rejected to prevent shell and target injection. | +| `peers` and peer credentials/filters/timeouts | **Deferred** | The current contract is one local FreeToken-owned engine. No distributed peer transport is claimed. | +| `upstream.ignorePaths` | **Native safe subset** | `router.upstream_no_activation_suffixes` defaults to the pinned static extensions, returns 409 before reservation/activation/upstream I/O while the exact local model is unloaded, and proxies normally when resident. A bounded validated suffix list replaces arbitrary regex to avoid a regex execution surface. | +| `healthCheckTimeout` | **Native** | Per-profile `ready_timeout_s` bounds readiness; the checked path is configurable. | +| request-log level/time/stdio fields | **Equivalent** | Native daemon logging and bounded rings have their own process-level controls; these are not hot catalog policy. Router events deliberately omit bodies, headers, query strings, and secrets. | +| `metricsMaxInMemory` | **Equivalent** | Native router logs and metric state are bounded; Prometheus counters are aggregate rather than a queryable in-memory activity table. | +| `captureBuffer` | **Native safe equivalent** | `router.capture_buffer_mb` is opt-in and defaults to zero. Captures are credential-redacted, serialized-byte-budgeted, per-response capped, binary-safe, memory-only, and retrieved by activity ID. | +| `store.path` | **Native safe equivalent** | The daemon-owned state directory contains lifecycle/accounting state and bounded body-free `activity.jsonl`. Rows are fsynced, streamed on recovery, strictly validated, and atomically compacted; persistence health is exposed without its path. Captures remain memory-only. | +| `ui.activity.session_id` | **Native privacy-preserving equivalent** | Validated non-credential header names select the first nonempty value, but only a stable truncated SHA-256 label is stored and shown. Matching is case-insensitive and raw identifiers/general headers are not persisted. | +| `performance.disabled`, `performance.every` | **Native privacy-preserving equivalent** | Validated `router.performance_disabled` and `performance_every_s` (5–3600 seconds) control an app-owned sampler retaining at most one hour in memory. It samples only the owned engine process-tree RAM/VRAM probe. | +| `tailcat` | **Deferred** | No Tailcat network dependency or remote-listener product contract exists. Local auth and route allowlisting do not claim Tailcat interoperability. | + +### Per-model fields + +Pinned source: `internal/config/model_config.go` (`ModelConfig`, +`TimeoutsConfig`, `CompatConfig`, `ModelCapConfig`) and +`internal/config/filters.go` (`Filters`). + +| Pinned field | Classification | FreeToken behavior and evidence location | +| --- | --- | --- | +| `cmd` | **Equivalent, safer** | `model` plus `args` constructs an allowlisted `ft serve` argument vector without a shell. Unknown daemon-owned options are rejected. | +| `cmdStop` | **Inapplicable by ownership contract** | `ServeManager` performs drain/abort, exact-identity signalling, process-group cleanup, accounting, and rollback; arbitrary stop commands would create a second lifecycle authority. | +| `env` | **Inapplicable by safety contract** | Per-profile environment injection is rejected. The daemon inherits its controlled service environment. | +| `proxy`, `checkEndpoint` | **Native applicable subset** | Exact plain-HTTP loopback `${PORT}` target with optional fixed path prefix and a safe readiness path. Remote targets, credentials, fragments, traversal, and ambiguous port templates fail closed. | +| `aliases`, `unlisted`, `useModelName` | **Native** | Collision-safe canonicalization/listing and outbound JSON model rewrite with client routing identity retained. | +| `ttl`, `unloadTimeout` | **Native** | Per-profile override plus global default, idle-only eviction, and manager-owned graceful stop. | +| `name`, `description`, `metadata` | **Native** | JSON-compatible metadata with router-owned identity/capability precedence; no local model paths or args leak through public listings. | +| `concurrencyLimit` | **Native** | Canonical and alternate IDs share one reservation cap; admission rejects before lifecycle/upstream work. | +| `filters.stripParams`, `setParams`, `setParamsByID` | **Native** | `drop_fields`, hard/soft `set_fields`, and `set_fields_by_id` preserve the pinned strip/global/by-ID order and protect `model`. Nested safe JSON paths extend the flat pinned behavior. | +| per-model `macros` | **Inapplicable by safety contract** | Explicit typed fields replace arbitrary interpolation. | +| `sendLoadingState` | **Native** | Nullable per-profile override of the global setting for admitted cold streaming chat requests. | +| timeout `connect`, `responseHeader` | **Equivalent** | One per-profile/global upstream socket deadline bounds connect and response reads for ordinary and SSE requests. It is deliberately simpler than independent phase timers. | +| timeout `keepalive`, `idleConn` | **Inapplicable today** | Native proxy calls use fresh manager-owned loopback HTTP connections, not a reusable idle pool. | +| timeout `tlsHandshake` | **Inapplicable today** | Valid proxy targets are plain HTTP loopback only. | +| timeout `expectContinue` | **Inapplicable today** | The supported small JSON text routes are forwarded over a fresh local connection without an Expect/Continue policy surface. | +| `compat.ignoreWebsockets` | **Inapplicable today** | Neither the current FreeToken engine nor the stable router registers a websocket inference route. | +| capabilities `in`, `out`, `tools`, `context` | **Native declarative subset** | Text/tool/context listing metadata is native and does not enable inference features. Unsupported image/audio/video declarations fail closed. | +| capability `reranker` | **Inapplicable today** | FreeToken has no rerank backend route, so advertising it is rejected. | +| copied `healthCheckTimeout` | **Native** | Resolved directly from each profile's `ready_timeout_s`. | + +## HTTP and management routes + +Pinned route source: `internal/server/server.go` (`modelPostJSONRoutes`, +`modelPostFormRoutes`, `modelGetRoutes`, `routes`, `ServeTailcatHTTP`). Native +registrations are in `python/freetoken/daemon/app.py`. + +| Pinned route family | Classification | Native behavior or boundary | +| --- | --- | --- | +| `POST /v1/chat/completions`, `/v1/completions`, `/v1/responses`, `/v1/messages`, `/v1/messages/count_tokens` | **Native** | Automatic model-ID inference, lifecycle acquisition, filtering, byte/SSE forwarding, cancellation and accounting share one coordinator. | +| `/v/*` versionless aliases, `/completion`, `/infill` | **Inapplicable today** | The current engine does not register these model-bearing aliases. Explicit profile-qualified upstream access remains available. | +| embeddings and rerank/reranking families | **Inapplicable today** | No matching FreeToken backend modality/route. | +| audio speech/voices/transcriptions and generic audio task route | **Inapplicable today** | No matching FreeToken backend modality/route. | +| image generations/edits, SDAPI, `/props`, ComfyUI | **Inapplicable today** | No matching FreeToken backend modality/route. | +| `GET /v1/models`, `/models` | **Native** | Authenticated canonical/optional-alias listing with atomic loaded state, display metadata and CORS. | +| `/logs` and `/logs/stream*` | **Equivalent** | Bounded engine and router streams use `/engine/logs` and `/router/logs?since=` with replay/resume behavior. | +| `/health`, `/wol-health` | **Native / inapplicable split** | `/health` is native liveness; `/ready` is stricter readiness. Wake-on-LAN health is not part of the local service contract. | +| root redirect, favicon, `/ui/` | **Equivalent** | Dependency-free local management UI is native; matching static asset names are not a parity requirement. | +| `/metrics` | **Native** | Authenticated Prometheus lifecycle, queue, cancellation, activation, timing, bytes and throughput signals. | +| `/unload`, `/running` | **Equivalent** | `/router/unload`, `/router/status`, and `/router/models`; one/all unload and configured/resident views are tested. | +| `/upstream/{model}/{path...}` | **Native** | Same admission/lifecycle lease, longest slash-namespaced ID, escaped suffix/query preservation, safe credential termination, and a pre-admission static-suffix guard that returns 409 rather than cold-loading. | +| `/api/models/unload*`, `/api/profiles`, `/api/profiles/active` | **Equivalent** | Native router management APIs implement the behavior under one-resident capacity. | +| `/api/inflight/{id}/cancel` | **Equivalent** | Opaque request reservation/list/cancel API covers queued, connecting, and active requests. | +| `/api/events` | **Equivalent** | Bounded resumable router event stream; route templates and lifecycle facts only. | +| `/api/metrics/activity`, `/api/metrics/stats` | **Native bounded equivalent** | Authenticated `/router/activity` supports newest-first bounded pagination and model filtering; `/router/activity/stats` reports counts, errors, cancellation, bytes, and average duration. Rows are body-free. | +| `/api/performance` | **Native privacy-preserving equivalent** | Authenticated pinned and native route aliases return a bounded one-hour `sys_stats` history with strict RFC3339 `after` filtering. Rows declare engine-process-tree scope and RAM/VRAM availability/source; `gpu_stats` stays empty rather than fabricating adapter-wide sensors. Disabled monitoring returns the pinned 503 `{enabled:false}` contract. | +| `/api/version` | **Equivalent** | `ft --version` and package version provide build identity; no duplicate router JSON endpoint is required for lifecycle behavior. | +| `/api/hardware` | **Native** | `/router/hardware` reports process-tree RAM and explicit available/source GPU memory. | +| `/api/captures/{id}` | **Native safe equivalent** | Authenticated opt-in retrieval by activity ID with pinned and custom credential-header redaction, Base64 bodies, one-MiB response cap, total serialized-byte budget, and no capture for cancellation/overflow. | +| `/api/mcp` | **Deferred** | The pinned endpoint exposes llama-swap's embedded docs/tools. FreeToken has no equivalent agent-tool product contract. | +| `/api/tailcat` and Tailcat listener restrictions | **Deferred** | No Tailcat listener or client protocol is claimed. | + +## Lifecycle and routing internals + +| Pinned source subsystem | Classification | FreeToken implementation | +| --- | --- | --- | +| `internal/router/{base,router,loading}.go` | **Native** | `RoutingCoordinator` owns admission, loading feedback, leases, readiness, eviction and routing snapshots. | +| `internal/router/group.go` and FIFO scheduler | **Native applicable subset** | Exclusive one-slot routing, persistent protection, priority and FIFO are deterministic-tested. | +| `internal/router/{matrix,matrix_solver}.go` | **Inapplicable today** | No multi-resident placement or victim set exists under one child. | +| `internal/router/peer.go` | **Deferred** | No remote peer transport in the local one-engine contract. | +| `internal/process/*` | **Native** | `ServeManager`, `osproc.py`, `pidfile.py`, and accounting state provide launch, exact identity, stop/reap/tree cleanup, rollback and re-adoption. | +| server profile/selector/filter middleware | **Native** | Order is routing profile, selector, alias, target filtering, admission and proxy; queued requests retain their admitted policy snapshot. | +| global and model concurrency middleware | **Native** | Reservations include queued/activating/active work and release once on every terminal path. | + +## Authentication, observability, UI, and persistence + +| Pinned behavior | Classification | FreeToken implementation or gap | +| --- | --- | --- | +| API-key middleware | **Native** | Case-insensitive Bearer, Basic password, and `X-Api-Key`; dedicated `X-FT-Token` control override; upstream credential stripping and rotation tests. | +| Inflight ownership/cancellation | **Native** | Opaque IDs reserve before admission and cancel queued, connecting, or active work. | +| Bounded logs and SSE resume | **Native** | Separate engine and privacy-safe router rings. | +| Prometheus metrics | **Native** | Lifecycle, queue, activation, cancellation, eviction, terminal stream, TTFT, duration and byte signals. | +| Bounded activity/performance stores | **Native** | Body-free inference activity survives restart in a bounded fsynced/compacted store. Performance history is intentionally memory-only and retains at most one hour, matching the pinned ring behavior. | +| Redacted bounded request/response captures | **Native** | Disabled by default; opt-in memory budget, sensitive-header redaction, binary-safe bodies, overflow/cancellation refusal, authenticated retrieval, and deterministic tests. Captures must never become public evidence artifacts. | +| Embedded management UI | **Native applicable subset** | Status/models/profiles/requests/logs/metrics/hardware/performance plus body-free activity and explicit-on-click capture views are local and dependency-free. The initial HTML embeds no operational data. | +| Hardware snapshot | **Native, extended** | Current process-tree RAM plus NVIDIA/AMD per-process VRAM, with unavailable distinct from zero. | +| Embedded documentation MCP | **Deferred** | No FreeToken MCP contract. This does not affect inference or lifecycle parity. | +| Tailcat remote access | **Deferred** | No FreeToken Tailcat contract. This does not imply generic remote access is safe. | + +## Tests and evidence classes + +Pinned source tests span `internal/**/*_test.go`, router/process/config/server +tests, and UI tests. Native deterministic coverage lives in `tests/daemon`: + +| Evidence class | Current state | +| --- | --- | +| Catalog validation, routing, HTTP/auth/SSE, filters, profiles/selectors, loading state, cancellation, TTL, reload, metrics/logs, process/accounting and startup hooks | **Native deterministic evidence present.** | +| Disposable actual-child process, process-group cleanup, re-adoption and routed SSE on Linux | **Native hosted-Linux evidence present** at the exact PR lineage recorded in the parity matrix. | +| Combined-tree/current engine compatibility | A clean synthetic tree at the recorded current swap/AMD heads passed 377 daemon/privacy/benchmark/reproducibility tests (7 Windows skips) and 21 model tests. Current-engine and protected restoration evidence remain required. | +| GMKtek EVO-X2 direct/warm/cold/A-B-A/concurrency/cancellation/failure/rollback/re-adoption/reload/TTL/auth/metrics/logs/restoration | **Live evidence missing; maintenance authorization required.** | +| Bounded activity/stat and opt-in capture APIs | **Native deterministic implementation and tests present.** Body-free rows survive app reconstruction; captures remain memory-only by policy. UI fetches captures only on explicit selection. | +| Periodic performance history | **Native deterministic implementation and tests present.** One-hour eviction, filtering, privacy, auth, disabled behavior, probe failure isolation, and sampler generation cleanup are covered. | + +## Open applicable implementation gaps + +No protocol-agnostic implementation gap is currently identified by this pinned +source inventory. Pending current-engine, GPU-model, combined-tree, and protected +restoration qualification remains an evidence gap and must not be conflated with +implementation parity. diff --git a/docs/freetoken-swap.md b/docs/freetoken-swap.md new file mode 100644 index 000000000..4c271e4fd --- /dev/null +++ b/docs/freetoken-swap.md @@ -0,0 +1,409 @@ +# freetoken-swap: native, safe model routing + +`freetoken-swap` is the native `ft daemon` routing mode. A client sends a +supported FreeToken OpenAI- or Anthropic-compatible request to the daemon's +stable URL with an allowlisted catalog alias in JSON `model`. The daemon alone +admits the request, starts or reuses one `ft serve` child, waits for its +generation-aware readiness, and proxies ordinary and SSE bytes unchanged. Its +lease stays active until the response closes, so another model cannot replace a +stream in flight. The same owner performs accounting, graceful drain/abort, +process-identity checks, cleanup, rollback, and re-adoption; **do not** put +llama-swap or another supervisor in front of the same FreeToken child. + +Legacy `/engine/start`, `/engine/stop`, `/engine/switch`, and profile variants +remain available only when the router does not own or admit work. They reserve +the same lifecycle barrier for their complete transaction, so a routed request +waits rather than racing a manual process operation. A manual stop may supersede +a manual operation blocked in readiness; its newer manager intent invalidates +stale rollback, and the older token cannot clear the stop's barrier. If the +manual HTTP client disconnects, the barrier remains held until the complete +executor-backed lifecycle transaction, including required rollback, terminates. +Daemon shutdown uses the same coordinator: it closes admission, wakes queued +requests with a stable shutdown error, drains active leases and lifecycle work, +then permanently stops the manager-owned child. A failed stop reopens admission; +a successful stop requests daemon exit even if the initiating client disconnects. +OS- and lifespan-triggered exit also quiesces this coordinator. The default +detach policy drains ownership and leaves the exact persisted child available +for re-adoption; `--stop-serve-on-exit` drains and permanently stops it instead. +Catalog reload binds profile lookup, priority ticketing, and dynamic-port +selection atomically. Reload is rejected while admission or lifecycle work is +pending, so an activating or queued request cannot change definitions mid-flight. + +The read-only, pinned llama-swap source remains a compatibility reference and +an optional separate deployment mode, not a runtime dependency. That direct +mode cannot gain this daemon's accounting guarantees. See the +[parity matrix](freetoken-swap-parity-matrix.md) for the source-backed +capability classification and [research](freetoken-swap-research.md) for +bounded qualification evidence and limits. + +The catalog is TOML and is optional. Start the daemon with `--catalog` or set `FREETOKEN_SWAP_CATALOG`: + +```toml +[router] +send_loading_state = true +preload_model = "qwen-coder-compatible" +startup_routing_profile = "coding" +# Matching direct-upstream assets return 409 instead of cold-loading. This +# suffix-only safe subset defaults to js/json/css/png/gif/jpg/jpeg/ico/txt. +upstream_no_activation_suffixes = [".js", ".json", ".css", ".png"] +# Activity metadata is always bounded and body-free. Captures are disabled by +# default; enabling them retains redacted bodies in memory only. +activity_max_entries = 1000 +capture_buffer_mb = 0 +activity_session_headers = ["X-Session-ID", "X-Litellm-Session-Id"] +performance_disabled = false +performance_every_s = 5 + +[models.qwen-coder] +model = "/models/Qwen3-Coder-30B-A3B-Q4_K_M.gguf" +port = 1922 +args = ["--max-seq-len-override", "4096", "--num-tokens", "4096"] +description = "GMKtek EVO-X2 candidate coding profile" +name = "Qwen coder" +aliases = ["qwen-coder-compatible"] +concurrency_limit = 2 +ready_timeout_s = 300 +check_endpoint = "/ready" +proxy = "http://127.0.0.1:${PORT}" +use_model_name = "qwen-coder" +upstream_timeout_s = 600 +send_loading_state = false + +[models.qwen-coder.metadata] +tier = "candidate" +family = "qwen" + +[models.qwen-coder.capabilities] +in = ["text"] +out = ["text"] +tools = true +context = 4096 + +[models.qwen-coder.set_fields] +"max_tokens?" = 4096 +"chat_template_kwargs.enable_thinking?" = true + +[models.qwen-coder.set_fields_by_id."qwen-coder:high"] +"chat_template_kwargs.reasoning_effort" = "high" + +[models.qwen-chat] +model = "/models/Qwen3.5-27B-Q4_K_M.gguf" +args = ["--max-seq-len-override", "4096", "--num-tokens", "4096"] + +[selectors.preferred-chat] +strategy = "warm" +targets = ["qwen-coder-compatible", "qwen-chat"] +name = "Preferred chat model" +description = "Reuse a ready target, otherwise start the first target" + +[selectors.preferred-chat.metadata] +tier = "stable" + +[profiles.coding] +description = "Coding-focused routing mode" + +[profiles.coding.pins] +llm-code = "preferred-chat" +llm-plan = "qwen-coder:high" +image-gen = "" +``` + +```bash +ft daemon --catalog /etc/freetoken/models.toml +ft daemon models +ft daemon routing-profiles +ft daemon activate-routing-profile coding +ft daemon clear-routing-profile +ft daemon start-profile qwen-coder +ft daemon switch-profile qwen-chat +ft daemon health +``` + +`GET /router/profiles`, `PUT /router/profiles/active`, +`POST /engine/start-profile`, and `POST /engine/switch-profile` expose explicit +control-plane operations. They require `X-FT-Token` whenever the daemon has a +token configured. The start/switch endpoints select a concrete model lifecycle +profile; the PUT endpoint activates or clears a runtime routing profile. +`GET /models` is instead the pinned public-model-list alias of `GET /v1/models` +and uses catalog API-key authentication. Use `switch-profile --force` only for +the same recovery case as `ft daemon switch --force`: the final accounting +receipt may be incomplete when a failed engine cannot be observed. + +Model lifecycle entries accept allowlisted `model`, `port`, `args`, `description`, `aliases`, +`unlisted`, readiness, TTL/unload, priority, group, and safe JSON request-filter +fields. Optional `use_model_name` gives the same profile a distinct model name +for outbound JSON requests without changing its configured or requested routing +identity. Optional `name` and JSON-compatible `metadata` provide display-only +model-list information; canonical and listed alternate IDs share those values. +Router-owned `type`, `aliases`, and `modelID` metadata wins over conflicting +operator keys, and declared capabilities own their rendered architecture, +capability, parameter, and context fields. A nested `capabilities` table may declare `in`/`out` +text modalities, `tools`, and a nonnegative `context` length for compatible +model-list clients. This metadata does not enable model behavior: operators +must advertise tools only when the model and chat template actually support +them. Unsupported image, audio, video, and reranker claims are rejected rather +than fabricated. Alternate IDs resolve to the same canonical profile and +resident process. Alias names must be unique and cannot collide with canonical +profile names. Canonical and alternate model IDs may use slash-separated safe +segments such as `organization/model` and colon variants such as `model:high`; +empty, traversal-like, and non-ASCII +segments are rejected, and the complete ID is limited to 128 characters. +Group names and each dot-delimited request-field segment retain the narrower +safe-name grammar. An unlisted profile and all its aliases remain routable and +manageable but are omitted from `GET /v1/models`. Set +`router.include_aliases_in_list = true` to list aliases for visible profiles; +canonical visible IDs are always listed. `args` +is passed as an argument vector to `ft serve`; it is never interpreted by a +shell. A profile cannot set `--model` or `--port` in `args`, because those +fields are owned by the supervisor and are part of its conflict and re-adoption +identity. The model files and catalog remain local operational configuration, +not repository content. + +Runtime routing profiles are named, atomically selected maps under +`[profiles..pins]`. A pin replaces a client model ID before aliases, +selectors, and target filters; an empty target disables that ID. Pins may +target a configured canonical ID, alternate ID, or selector, allowing one +profile switch to change several stable client names together. No routing +profile is active by default; `router.startup_routing_profile` selects one +validated profile before serving. Catalog reload still clears runtime pinning. +Active non-disabled pins +that do not shadow configured model/alias/selector IDs appear in the public +model listing with `meta.freetoken.type = "profile"`; disabled pins are omitted. +Profile pins also use longest-prefix replacement on `/upstream/` paths, but a +pin that targets a selector remains invalid there because selectors are not +direct-upstream IDs. Concrete load/unload management ignores active pin maps. + +`router.preload_model` accepts one concrete canonical or alternate ID, resolves +aliases during catalog validation, and acquires that model through the same +readiness/accounting/rollback path during daemon startup. The singleton limit +matches native one-resident capacity; selectors and unknown IDs are rejected. +Use a singleton persistent group when the preloaded model must remain resident +until explicit unload. + +Selectors are inference-only virtual model IDs. `pin` always resolves to its +first ordered target. `warm` resolves to the first readiness-gated resident +target, then the first target already activating, and otherwise falls back to +the first target. Resolution rewrites the request's top-level `model` to the +selected canonical or alternate target before that target's ordered request +filters run. Selector IDs appear in `/v1/models` unless `unlisted = true`; +their loaded status follows only the first target for `pin` and any target for +`warm`. Optional JSON-compatible selector `metadata` is nested under +`meta.freetoken`, while router-owned `type`, `strategy`, and `targets` keys +cannot be overridden. Targets must be configured profiles or aliases, selector +chaining is rejected, and `/upstream/{model-id}` plus named unload remain +concrete profile/alias controls. The `spillover` strategy requires concurrent +multi-resident or peer capacity and is therefore rejected under FreeToken's +explicit one-resident policy rather than emulated inaccurately. + +`drop_fields` removes configured dot-delimited object paths. `set_fields` +forces JSON-compatible values; a quoted key ending in `?` sets the value only +when that path is absent, so explicit `null`, zero, and false remain client +choices. `set_fields_by_id` runs last and can override global assignments for a +canonical or alternate requested ID. Its table names automatically become +aliases of the same resident model, subject to the normal collision checks. +When configured, `use_model_name` first rewrites the outbound top-level `model`; +filters then run in `drop_fields`, `set_fields`, and `set_fields_by_id` order and +cannot directly configure that protected field. The by-ID table still keys on +the client-facing selected/requested ID rather than the upstream override. For +a selector request without an explicit override, the router first replaces the +field with the resolved target. All filters apply to the exact acquired target +snapshot, including JSON direct-upstream requests; non-JSON direct bodies and +profiles with no rewrite or filters remain byte-exact. +An active profile cannot have its filter policy changed by catalog reload. +There is no expression evaluator or lifecycle shell-hook language. + +Set `port = 0` to request a kernel-selected loopback port on every cold native +activation. The daemon records the concrete assigned port and uses that same +target for child identity, readiness, proxying, accounting, and re-adoption; +an already resident dynamic profile keeps its port until it is unloaded. +Dynamic binding occurs only when a request reaches the head of admission, so +simultaneous cold requests for one profile share the single committed target. +`models..check_endpoint` selects a safe absolute readiness path and +defaults to `/health`; the private native qualifier uses `/ready`. A non-health +endpoint follows pinned HTTP-success semantics while the daemon still checks +the exact managed PID before and after every probe. `models..proxy` may +add a safe path prefix to `http://127.0.0.1:${PORT}`. The `${PORT}` placeholder +is mandatory, and other schemes, hosts, explicit ports, credentials, queries, +fragments, empty path segments, and traversal are rejected. This deliberately +keeps proxy traffic on the exact manager-owned child rather than creating an +arbitrary SSRF or split-ownership target. + +`models..upstream_timeout_s` overrides `router.upstream_timeout_s` for +the acquired profile's fresh loopback HTTP connection and response reads. The +exact admitted profile snapshot supplies the timeout for both ordinary and SSE +requests. TLS-handshake and pooled keepalive timeout knobs from the reference +are inapplicable because native targets are restricted to fresh manager-owned +plain-HTTP loopback connections. + +Each profile admits at most 10 reserved requests by default across its canonical +and alternate IDs. Set `models..concurrency_limit` to a positive override. +`router.global_concurrency_limit = 0` leaves the global cap disabled; a positive +value caps all active, queued, and activating routed requests. Capacity is +reserved before loading, so excess work is rejected immediately with HTTP 429, +`Retry-After: 1`, and `error.type=concurrency_limit` rather than consuming a +queue slot or launching an engine. Status and Prometheus expose reserved work. + +`router.send_loading_state = true` enables optional cold-load feedback for +strictly streaming `POST /v1/chat/completions` requests. A profile can override +the global setting with `models..send_loading_state = true` or `false`. +After concurrency admission, a cold request receives HTTP 200 SSE reasoning +deltas with loading and queue-position text until the readiness-gated engine is +available, followed by the real upstream stream. Admission rejection remains a +normal HTTP 429 JSON response. Once loading SSE has committed HTTP 200, a later +activation or connection failure is delivered as an in-band `error` event and +terminated with `data: [DONE]`. The default is disabled, and warm, non-chat, and +non-streaming requests retain the ordinary byte/status/header-preserving proxy. + +The native capacity policy is deliberately one resident child. Therefore a +nonpersistent group must use `swap = true, exclusive = true`; a persistent +protected slot must be a one-member group with `swap = false, exclusive = true`. +Catalog reload rejects llama-swap coexistence configurations instead of silently +pretending that multiple FreeToken engines are resident. + +When started with `--catalog`, the daemon polls it once per second by default. +`--catalog-watch-interval 0` disables that watcher. A changed catalog is parsed +and fully validated before atomic installation; malformed files and active +profile redefinitions are rejected without disturbing the running child. The +watcher's last result appears in `GET /router/status` and its sanitized events +appear in `/router/logs`. + +## Native router API + +The routed inference surface is `GET /v1/models` plus `POST /v1/chat/completions`, +`/v1/completions`, `/v1/responses`, `/v1/messages`, and +`/v1/messages/count_tokens`. Canonical and alternate IDs share one canonical +residency, capability metadata, and loaded/unloaded listing status while +preserving the client's request body. Readiness-gated activation is reported as +loaded, and stale child identity is reported as unloaded. The public listing includes descriptions but +never model paths or launch arguments. Declared text modalities, tool calling, +and context length use the pinned llama-swap listing fields. Unknown IDs return +a stable 404; unsupported FreeToken modalities are not fabricated. `GET /router/status`, `/router/models`, +`/router/profiles`, `/router/requests`, and `/metrics` expose configured and +resident state, capacity, queues, lifecycle timing, response bytes and proxy +byte rate, concurrency reservations, cancellation, and eviction signals. These transport measurements do +not substitute for live engine token-throughput qualification. +Browser clients receive the pinned compatibility contract: any `OPTIONS` +preflight is answered without lifecycle side effects, requested header names +are restricted to valid HTTP tokens, and authenticated `GET /v1/models` +reflects its `Origin`. Preflight never authorizes the corresponding request; +inference and management routes still enforce their configured keys. +`activeIdentityMatchesEngine` makes a stale or out-of-band child visible rather +than reporting its configured alias as resident. +`PUT /router/profiles/active`, `POST /router/unload`, `/router/reload`, and +`/router/requests/{id}/cancel` control idle eviction, atomic catalog reload, +and a queued, connecting, or active request. The request list exposes reserved +IDs from admission through stream completion, so an operator can cancel any +owned phase. An unload body containing `name` targets that profile; +an omitted body unloads all residents, which is exactly the current resident +under the explicit one-engine capacity policy. `POST /router/load` activates a named profile through +the same native lifecycle transaction without fabricating an inference request. +`GET /router/logs?since=` is a bounded SSE event stream; +it records only event type, alias, registered route template, status, +cancellation state, and response byte count—never prompts, request bodies, +headers, concrete URL paths, query strings, model paths, or API keys. +Router Bearer, Basic-password, and `X-Api-Key` credentials plus the daemon +`X-FT-Token` are terminated at the router and never forwarded to the engine; +ordinary non-hop-by-hop application headers are otherwise preserved. + +FreeToken's legacy `POST /generate` body has no model identifier, so exposing it +at the stable router URL would require an implicit default and violate explicit +model-ID ownership. It is therefore intentionally absent there. Clients that +need this legacy protocol must select a configured alias explicitly with +`POST /upstream/{profile}/generate`; that guarded route still acquires the same +router lease and preserves the request and SSE response bytes. + +`GET /ready` is an unauthenticated, side-effect-free readiness probe for the +stable router URL. It returns 200 only while a resident routed engine reports +FreeToken's `status=ok` and `maintenance=serving` **and** still exactly matches +the resident alias's model, port, and argument vector. Identity and fresh +health are checked behind the admission barrier, so a conflicting swap cannot +begin between the identity snapshot and a successful response; the probe never +cold-loads a profile. The stateless backend's `GET /v1/responses/{id}` and +response-specific cancel endpoints are authenticated at the stable daemon URL +and return the backend's documented `invalid_request_error` 404 without loading +a model. They are therefore compatibility endpoints, not routing or lifecycle +operations. + +`GET /ui/` serves a dependency-free local management shell. It embeds no +catalog values, paths, keys, or machine data; the operator enters a bearer key +for the current browser session and it calls the authenticated router APIs. +The UI presents configured/resident models, load/unload/reload controls, router +status, and the privacy-preserving `GET /router/hardware` memory view. Authenticated +`GET /router/activity`, `/router/activity/stats`, and `/router/captures/{id}` +provide bounded diagnostics. Activity rows never contain bodies or headers. +Real daemon runs persist them as bounded, fsynced JSONL under the daemon-owned +state directory; the file is atomically compacted and corrupt/truncated rows +fail closed. API responses expose persistence health without exposing its path. +Captures are disabled unless `router.capture_buffer_mb` is positive, live only +in memory, cap each response at 1 MiB, obey the total serialized-byte budget, +and redact Authorization, proxy authorization, cookies, `X-Api-Key`, +`X-FT-Token`, and custom token/secret/API-key header names. Bodies use Base64 +fields for binary fidelity and are never persisted. The UI lists body-free +activity and fetches a capture only after an explicit click. Configured session +headers are validated, may not name credentials, and are stored/displayed only +as stable 16-character SHA-256 labels; raw identifiers are never retained. MCP +and Tailcat remain deferred product expansions. + +`GET /api/performance` and `/router/performance` expose at most one hour of +periodic samples at `router.performance_every_s` (minimum five seconds). +The compatible envelope contains `sys_stats` and `gpu_stats`; native +`sys_stats` rows are explicitly scoped to the owned engine process tree and +contain only RAM/VRAM bytes, availability, and probe source. `gpu_stats` is +empty because FreeToken does not fabricate adapter-wide utilization, +temperature, power, or fan data from process memory. Strict RFC3339 `after` +filtering is supported. The history is memory-only, authenticated, bounded, +and disabled with `router.performance_disabled = true`. + +When `router.api_keys` is configured, authentication accepts an +`Authorization: Bearer` value, an HTTP Basic password, or `X-Api-Key` for +inference and, absent a daemon token, router management. Explicit Authorization +credentials take precedence over `X-Api-Key`; malformed Basic may fall back to +it. Invalid requests include a `WWW-Authenticate` challenge. An explicit daemon +`X-FT-Token` remains the dedicated control-plane override. The guarded +`/upstream/{model-id}/...` passthrough uses the same lease but refuses a direct +engine `prepare-stop`, which only the lifecycle owner may invoke. For +slash-namespaced IDs, the longest configured canonical or alternate ID wins; +encoded model separators and the remaining escaped path and query are +forwarded without decoding. By default, direct-upstream paths ending in +`.js`, `.json`, `.css`, `.png`, `.gif`, `.jpg`, `.jpeg`, `.ico`, or `.txt` +return HTTP 409 while the selected model is unloaded, rather than activating +an engine for a speculative asset request. They proxy normally when that exact +model is resident. Configure the bounded, dot-suffix-only +`router.upstream_no_activation_suffixes` list, or set it to `[]` to disable the +guard. Matching is case-sensitive and excludes the query string. + +These are illustrative paths, not a list of qualified models. In particular, dense Qwen GGUF support requires a compatible AMD/model-loader branch and cannot be inferred from this control-plane PR. + +After a profile launch, the daemon polls uncached engine health, verifies the process identity again after each probe, and waits for `status=ok` and `maintenance=serving`. Readiness failure returns HTTP 503. The client returns a nonzero exit code and defaults to a 1920-second transport budget, covering two maximum 900-second readiness windows plus lifecycle overhead. A user-specified client timeout still takes precedence. + +For `switch-profile`, readiness failure attempts to restore the exact previous engine and probes its readiness using a second window of the requested profile's `ready_timeout_s`. The response remains HTTP 503 because the requested replacement failed, with a separate `rollback.readiness` result. Recovery is single-use and invalidated by any newer start, stop, switch, or shutdown. HTTP probing releases the lifecycle lock and runs in the proxy pool, so an operator can stop a loading engine without waiting for the readiness timeout. Accounting failure during recovery preserves the failed engine instead of silently forcing cleanup. An initial `start-profile` or a switch with no previous engine leaves the failed process managed for diagnosis. These policies do not change the direct llama-swap supervisor. + +If a replacement launch raises before an owned child exists, the daemon attempts to relaunch the previous model with its exact port and arguments, under the same lifecycle transaction. Both switch endpoints return HTTP 503 with `code=switch_launch_failed`, the original accounting receipt, and a `rollback` result. `rollback.launched` means only that the recovery process launched, not that it is ready. A failed recovery is reported explicitly. Accounting failures before stop preserve the original engine; a post-spawn failure that leaves an owned child does not trigger a second launch. These safeguards apply to daemon switches, not the separate direct llama-swap supervisor. + +FreeToken's `/health` remains a backwards-compatible diagnostic endpoint and can return HTTP 200 while loading or failed. `/ready` returns HTTP 503 for loading, failure, or maintenance, and HTTP 200 only when accepting requests. Configure llama-swap with `checkEndpoint: /ready`, never `/health` or `/v1/models` as a substitute. + +Use `ft serve` or `python -m freetoken.cli serve` in a process command. The legacy `python -m freetoken` entrypoint does not accept the `serve` subcommand. Use a revision-specific `TORCH_EXTENSIONS_DIR` and prebuild native GGUF kernels before a maintenance window so an abandoned shared build lock cannot stall model initialization. For SSE token metrics, clients should request `stream_options: {"include_usage": true}`. + +The opt-in native maintenance harness `benchmarks/swap/qualify_native_router.py` +generates a private API key scoped only to its temporary daemon origin. Its +acceptance result requires 401 responses without that key and authenticated +Bearer, Basic-password, `X-Api-Key`, model/profile inventory, Prometheus metrics, +bounded router-log SSE, and authenticated periodic-performance history with a +positive available owned-process RAM/VRAM sample and no PID/model/path fields; +the key, catalog, headers, and raw captures are never publication artifacts. + +## Cancellation qualification + +The opt-in Linux harness `benchmarks/swap/qualify.py --cancellation` adds a live disconnect gate to its maintenance-window run. It reads SSE incrementally, verifies that generation is active, closes the response after the first content delta, and polls backend statistics through `/upstream/model-a/v1/stats`. Passing requires the same backend instance to become idle without increasing the normal-completion count. A backend restart, an already-finished response, or a missing terminal abort fails the gate. It then checks fresh A-to-B-to-A streaming completions. Prefix bytes, backend snapshots, first-content timing, abort latency, and recovery responses are private artifacts. + +The gate passed against the real Qwen3.6 GPU workload on GMKtek EVO-X2 in an approved maintenance window. The same backend changed from one active request to zero, with its normal-completion count unchanged. Observed first content was 0.368 seconds and terminal abort was observed 0.254 seconds after disconnect. Post-cancellation A-to-B-to-A streaming, concurrent routing, idle eviction, and protected-service restoration also passed. This is one bounded cancellation case, not a cancellation endurance benchmark. Use `--extended` as well to retain concurrent-request and TTL gates. Existing mandatory source, model, protected-service, and artifact arguments still apply. The current harness additionally requires the exact operating-system hostname in `--expected-hostname` before artifact creation or service inspection; `--allow-maintenance` is not a substitute for operator approval. + +## Native model-failure recovery qualification + +`benchmarks/swap/qualify_native_recovery.py` exercises the actual daemon profile endpoints with real FreeToken child processes. It starts the supplied model, verifies generation, switches to a deliberately invalid GGUF fixture in its private artifact directory, checks the HTTP 503 response and automatic recovery readiness, then verifies streamed generation from the restored model. The real Qwen3.6 run passed after the loader reported `GGUF magic invalid`. The original engine's sealed accounting receipt was complete; the failed loader's crash receipt was explicitly degraded with unknown token totals. Cleanup and restoration of the protected llama.cpp workload passed. + +The harness accepts separate `--source` and `--daemon-source` paths so the AMD runtime and the swap feature branch can be tested together without modifying a live checkout. `--extensions-dir` must identify a private cache prebuilt from the selected runtime source. Required arguments also include `--python`, `--model`, `--protected-service`, `--protected-url`, `--artifacts`, `--expected-hostname`, and `--allow-maintenance`. The exact hostname must match before artifact creation or service inspection; mismatch errors do not disclose it. The fixture never replaces an existing model. Raw logs and result files contain private deployment details and must not be published unreviewed. + +## Provenance and scope + +The design was informed by [mostlygeek/llama-swap](https://github.com/mostlygeek/llama-swap), checked out locally at `41ec321b6216d838488b2a7d936274ed227c0c5e` on 2026-09-10. llama-swap is MIT licensed (`LICENSE.md`). No llama-swap or llama.cpp code is vendored, modified, or submitted by this feature. FreeToken remains the sole change and pull-request target. diff --git a/examples/freetoken-swap.toml b/examples/freetoken-swap.toml new file mode 100644 index 000000000..e0bdb5130 --- /dev/null +++ b/examples/freetoken-swap.toml @@ -0,0 +1,103 @@ +# Native freetoken-swap catalog. This is not a llama-swap YAML file. +# +# Migration: map each llama-swap models..cmd to one native model path +# plus argv tokens. Map checkEndpoint /ready to the built-in readiness gate. +# Never paste shell fragments, ${PORT}, or command substitutions here. Set +# `port = 0` for a kernel-selected loopback port per native activation. + +# What: open the router TOML table; why: following settings belong to the router configuration namespace. +[router] +# Inference routes accept Bearer, a Basic-auth password, or `X-Api-Key` when +# this is nonempty. Router credentials are never forwarded to the engine. +# What: show an illustrative router API-key list; why: operators must replace the placeholder before clients authenticate to control and inference routes. +api_keys = ["replace-with-a-secret"] +# What: set an illustrative 300-second default idle lifetime; why: operators balance model residency against the latency and memory cost of later reloads. +default_ttl_s = 300 +# What: set an illustrative 30-second unload deadline; why: the router needs a finite bound for stopping an engine before declaring cleanup failure. +unload_timeout_s = 30 +# What: set an illustrative 900-second upstream request deadline; why: long generations may need time while the finite limit prevents requests from hanging forever. +upstream_timeout_s = 900 +# Safe static suffixes that return 409 rather than cold-loading through +# /upstream/{model-id}/...; set [] to disable. No regex is accepted. +# What: list static suffixes that must not cold-load a model; why: asset-like requests fail with 409 instead of spending memory and startup time on accidental activation. +upstream_no_activation_suffixes = [".js", ".json", ".css", ".png", ".gif", ".jpg", ".jpeg", ".ico", ".txt"] +# Body-free activity rows are always bounded. Captures are sensitive, in-memory, +# credential-redacted, and opt-in; zero disables request/response retention. +# What: retain at most 1000 body-free activity rows; why: the bounded history supports diagnosis without unbounded memory growth. +activity_max_entries = 1000 +# What: disable sensitive body capture by setting its budget to zero; why: operators must opt in explicitly before request or response bodies are retained in memory. +capture_buffer_mb = 0 +# Session grouping stores only a stable SHA-256 label, never the raw value. +# What: name headers used to derive a hashed session label; why: activity grouping works without storing the raw potentially sensitive header values. +activity_session_headers = ["X-Session-ID", "X-Litellm-Session-Id"] +# Retain at most one hour of owned-process RAM/VRAM samples in memory. +# What: leave owned-process performance sampling enabled; why: operators can observe RAM and VRAM while retaining the option to disable sampler overhead. +performance_disabled = false +# What: sample performance every five seconds; why: the interval balances trend visibility against monitoring overhead. +performance_every_s = 5 +# What: select first-in-first-out request scheduling; why: the example favors predictable arrival order over alternate prioritization policies. +scheduler = "fifo" +# Zero disables the global cap. Every profile still has a default cap of 10. +# What: cap concurrent routed requests at 32; why: the finite limit protects host capacity while operators tune it for their workload. +global_concurrency_limit = 32 +# Include alternate IDs in /v1/models. They remain routable when this is false. +# What: include aliases in the public model listing; why: clients can discover alternate IDs, with the tradeoff of a larger advertised catalog. +include_aliases_in_list = true + +# What: open the router.groups.interactive TOML table; why: following settings belong to the router.groups.interactive configuration namespace. +[router.groups.interactive] +# What: assign coding and chat profiles to the interactive group; why: the group policy coordinates residency and exclusivity across both profiles. +members = ["coding", "chat"] +# What: enable swaps within the interactive group; why: activating one member may replace another instead of requiring simultaneous residency. +swap = true +# What: make the interactive group mutually exclusive; why: only one member occupies the constrained group at a time. +exclusive = true +# What: disable persistent residency for this group; why: idle members may unload so memory can be reclaimed. +persistent = false + +# What: open the models.coding TOML table; why: following settings belong to the models.coding configuration namespace. +[models.coding] +# What: show an illustrative local GGUF model path; why: operators replace the placeholder with the model file available on their host. +model = "/models/coding.gguf" +# Slash-namespaced IDs are valid; every segment uses letters, digits, `.`, `_`, or `-`. +# What: show alternate routed identifiers for the coding profile; why: compatible clients can select the same model through either approved alias. +aliases = ["coding-compatible", "local/coding-compatible"] +# What: show an illustrative engine port; why: operators must avoid collisions or use the supported dynamic-port policy for their deployment. +port = 1919 +# What: show illustrative ft serve arguments; why: the served name and context limit must match client identity and available memory. +args = ["--served-model-name", "coding", "--max-seq-len-override", "32768"] +# What: allow up to 300 seconds for model readiness; why: large models may need startup time while the finite limit keeps activation bounded. +ready_timeout_s = 300 +# What: set this profile idle lifetime to zero; why: the example unloads immediately after leases end rather than retaining model memory. +ttl_s = 0 +# What: set the profile scheduling priority; why: operators tune this value to control which queued activation wins contention. +priority = 10 +# What: set the profile request concurrency cap; why: the cap protects that model from more simultaneous work than the host can sustain. +concurrency_limit = 2 +# What: assign the profile to the interactive routing group; why: members share the group swap and exclusivity policy. +group = "interactive" +# Optional narrow compatibility filter. It removes only named top-level JSON +# fields from requests for this profile. `model` can never be removed. +# What: drop the optional metadata request field; why: the narrow compatibility filter removes only the named unsupported field. +drop_fields = ["metadata"] + +# What: open the models.chat TOML table; why: following settings belong to the models.chat configuration namespace. +[models.chat] +# What: show an illustrative local GGUF model path; why: operators replace the placeholder with the model file available on their host. +model = "/models/chat.gguf" +# Hidden profiles remain routable and manageable but are omitted from /v1/models, +# along with all of their aliases. +# What: hide the chat profile from model listings; why: the profile remains directly routable while discovery omits it and its aliases. +unlisted = true +# What: show an illustrative engine port; why: operators must avoid collisions or use the supported dynamic-port policy for their deployment. +port = 1919 +# What: show illustrative ft serve arguments; why: the served name and context limit must match client identity and available memory. +args = ["--served-model-name", "chat", "--max-seq-len-override", "16384"] +# What: allow up to 300 seconds for model readiness; why: large models may need startup time while the finite limit keeps activation bounded. +ready_timeout_s = 300 +# What: set the profile scheduling priority; why: operators tune this value to control which queued activation wins contention. +priority = 0 +# What: set the profile request concurrency cap; why: the cap protects that model from more simultaneous work than the host can sustain. +concurrency_limit = 4 +# What: assign the profile to the interactive routing group; why: members share the group swap and exclusivity policy. +group = "interactive" diff --git a/examples/freetoken-swap.yaml b/examples/freetoken-swap.yaml new file mode 100644 index 000000000..db47ee5e0 --- /dev/null +++ b/examples/freetoken-swap.yaml @@ -0,0 +1,43 @@ +# Integration template, not a claim that these placeholder models are qualified. +# Use a pinned llama-swap build and a FreeToken build containing GET /ready. +# Do not also manage these processes with ft daemon. +# What: set health check timeout to 300; why: the example permits slow model startup while still bounding a failed readiness probe. +healthCheckTimeout: 300 +# What: set global ttl to 0; why: the example disables an implicit global eviction policy so per-model TTL choices remain visible. +globalTTL: 0 +# What: set unload timeout to 30; why: the example allows bounded graceful shutdown before an operator chooses a stricter limit. +unloadTimeout: 30 +# What: show the illustrative models value; why: operators adapt this models choice to their model, memory budget, port policy, and startup latency rather than treating it as universal. +models: + # What: show the illustrative model a value; why: operators adapt this model a choice to their model, memory budget, port policy, and startup latency rather than treating it as universal. + model-a: + # What: show the illustrative cmd value >-; why: operators adapt this cmd choice to their model, memory budget, port policy, and startup latency rather than treating it as universal. + # What: provide the illustrative ft serve model and loopback launch fragment; why: llama-swap starts this model path on the allocated local port; operators replace the placeholder path. + # What: forward llama-swap's model identifier to ft serve; why: the launched engine advertises the same routed model identity selected by llama-swap. + # What: show illustrative context and token-cache limits; why: operators tune these memory-throughput tradeoffs for their hardware instead of treating 4096 as universal. + cmd: >- + ft serve --model /models/model-a --host 127.0.0.1 --port ${PORT} + --served-model-name ${MODEL_ID} + --max-seq-len-override 4096 --num-tokens 4096 + # What: set check endpoint to /ready; why: llama-swap probes the daemon readiness contract rather than treating a listening socket as ready. + checkEndpoint: /ready + # What: set proxy to http://127.0.0.1:${PORT}; why: llama-swap forwards model traffic to the loopback port allocated for this model entry. + proxy: http://127.0.0.1:${PORT} + # What: set ttl to 0; why: the example demonstrates the unload-latency versus residency tradeoff for this model. + ttl: 0 + # What: show the illustrative model b value; why: operators adapt this model b choice to their model, memory budget, port policy, and startup latency rather than treating it as universal. + model-b: + # What: show the illustrative cmd value >-; why: operators adapt this cmd choice to their model, memory budget, port policy, and startup latency rather than treating it as universal. + # What: provide the illustrative ft serve model and loopback launch fragment; why: llama-swap starts this model path on the allocated local port; operators replace the placeholder path. + # What: forward llama-swap's model identifier to ft serve; why: the launched engine advertises the same routed model identity selected by llama-swap. + # What: show illustrative context and token-cache limits; why: operators tune these memory-throughput tradeoffs for their hardware instead of treating 4096 as universal. + cmd: >- + ft serve --model /models/model-b --host 127.0.0.1 --port ${PORT} + --served-model-name ${MODEL_ID} + --max-seq-len-override 4096 --num-tokens 4096 + # What: set check endpoint to /ready; why: llama-swap probes the daemon readiness contract rather than treating a listening socket as ready. + checkEndpoint: /ready + # What: set proxy to http://127.0.0.1:${PORT}; why: llama-swap forwards model traffic to the loopback port allocated for this model entry. + proxy: http://127.0.0.1:${PORT} + # What: set ttl to 300; why: the example demonstrates the unload-latency versus residency tradeoff for this model. + ttl: 300 diff --git a/pyproject.toml b/pyproject.toml index 8bd653f87..1521c6f3f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,6 +56,8 @@ dependencies = [ # correctly from PyPI alone; uv additionally pins the index below. "torch>=2.11,<2.12", "tqdm>=4.66,<5", + # What: add tomli for Python versions earlier than 3.11; why: those interpreters lack tomllib, so the daemon needs this fallback to parse TOML model catalogs. + "tomli>=2.0,<3; python_version < '3.11'", "transformers>=5.5,<6", "triton==3.6.0; platform_system == 'Linux'", "uvicorn>=0.30,<1", diff --git a/python/freetoken/daemon/README.md b/python/freetoken/daemon/README.md index ea3b016ea..e775b9409 100644 --- a/python/freetoken/daemon/README.md +++ b/python/freetoken/daemon/README.md @@ -45,9 +45,15 @@ ft daemon start MODEL --port 1919 -- --moe-cache-auto # args after -- go to ft ft daemon status ft daemon logs # stream engine logs (SSE) ft daemon health # proxied serve /health (camelCased) -ft daemon metrics # engine-only RAM(PSS)+VRAM footprint +ft daemon metrics # engine-only RAM(PSS)+process GPU-memory footprint ft daemon switch OTHER_MODEL # stop old + start new +ft daemon models # list freetoken-swap named profiles +ft daemon routing-profiles # list runtime model-ID pin profiles +ft daemon activate-routing-profile coding # atomically activate a pin map +ft daemon clear-routing-profile # return to direct model IDs +ft daemon switch-profile coding # atomic switch via the local TOML catalog ft daemon stop +ft daemon shutdown # stop the serve and then the control plane # Recovery only: permit a degraded receipt if the failed engine cannot seal final totals. ft daemon stop --force ``` @@ -55,17 +61,31 @@ ft daemon stop --force Target a non-default daemon with `--url http://host:1900` (or `$FREETOKEN_DAEMON_URL`) and `--token`/`$FREETOKEN_DAEMON_TOKEN`. +For named model catalogs and the `start-profile` / `switch-profile` controls, see +[`docs/freetoken-swap.md`](../../../docs/freetoken-swap.md). Catalog profiles are argument +vectors for `ft serve`, never shell commands. Optional readiness paths and proxy +path prefixes remain restricted to the exact manager-owned loopback `${PORT}` target. + ## HTTP API (camelCase JSON, loopback by default) | Method / path | Notes | | --- | --- | | `GET /health` | Daemon self-health; always answers, never gated by `--token`. | +| `GET /v1/models`, `GET /models` | Identical catalog-key-protected public canonical/optional alternate IDs with atomic loaded/unloaded status and no model paths or launch arguments. | | `POST /engine/start` `{model,port,args[]}` | Idempotent on the full `(model,port,args)`; a differing config on the same port → `409`. | | `POST /engine/stop` `{force?:false}` | Close admission, drain/abort, durably enqueue the final-accounting receipt, then `SIGTERM`→grace→`SIGKILL`. A prepare/outbox failure preserves the engine. | | `POST /engine/switch` `{model,port,args[],force?:false}` | One serialized stop-accounting-start transaction. | +| `GET /router/profiles` | Lists local freetoken-swap named lifecycle profiles for authenticated control clients. | +| `PUT /router/profiles/active` `{name:string\|null}` | Atomically activates or clears a runtime model-ID pin profile. | +| `POST /engine/start-profile\|switch-profile` `{name,force?:false}` | Starts or atomically replaces the engine using a validated local profile. | | `GET /engine/status` | `{running,pid,model,port,uptimeS,lastExitCode,…}`; outlives any single serve. | | `GET /engine/logs?since=` | SSE, ANSI-stripped, tqdm-`\r` collapsed, ring replay, `id:`, `Last-Event-ID` resume. | -| `GET /engine/metrics` | `{ramBytes,vramBytes}` — the serve tree's own footprint only. | +| `GET /router/logs?since=` | SSE, bounded native router admission/proxy/cancellation events. It is separate from engine stdout and records route templates only—never concrete paths, request bodies, headers, query strings, model paths, or keys. | +| `GET /router/activity`, `/router/activity/stats` | Authenticated bounded body-free inference history and aggregates. Real daemon runs fsync and compact rows under `--state-dir`; persistence health is explicit. | +| `GET /api/performance`, `/router/performance` | Authenticated, memory-only one-hour history of owned engine process-tree RAM/VRAM; strict RFC3339 `after` filtering. | +| `GET /router/captures/{id}` | Authenticated opt-in, memory-bounded request/response capture. Credential headers are redacted and binary bodies are Base64. | +| `/upstream/{model-id}/...` | Guarded direct passthrough with longest-prefix slash-namespaced ID resolution and escaped suffix preservation. Safe configured static suffixes return 409 instead of cold-loading and proxy normally when the exact model is resident. | +| `GET /engine/metrics` | The serve tree's own `{ramBytes,vramBytes,pids}` footprint only. `ramAvailable`/`vramAvailable` and source fields distinguish a measured zero from an unavailable probe; Linux PSS, NVIDIA NVML/SMI, and AMD SMI process memory are supported. | | `GET /engine/health` | Proxied serve `/health` + daemon reachability. | | `GET /engine/stats` | Proxied serve `/v1/stats`. | | `GET /accounting/pending` | Unacknowledged durable final-accounting receipts, replayable after a Desktop/client crash. | diff --git a/python/freetoken/daemon/activity.py b/python/freetoken/daemon/activity.py new file mode 100644 index 000000000..2e7ee8307 --- /dev/null +++ b/python/freetoken/daemon/activity.py @@ -0,0 +1,662 @@ +"""Bounded inference activity and opt-in redacted request/response captures.""" +# What: document bounded inference activity and opt in redacted in the activity docstring; why: introspection and maintainers read this exact docstring fragment to understand activity behavior without executing it. + +# What: enable postponed evaluation of annotations; why: type hints in activity can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import base64 for record using base64; why: record uses base64 b64encode, making that imported dependency available to its named operation. +import base64 +# What: import ordered dict and deque for init using collections and ordered dict and deque; why: __init__ uses ordered dict and deque, making that imported dependency available to its named operation. +from collections import OrderedDict, deque +# What: import dataclass for module initialization using dataclasses and dataclass; why: module initialization uses dataclass, making that imported dependency available to its named operation. +from dataclasses import dataclass +# What: import hashlib for record using hashlib; why: record uses hashlib sha256, making that imported dependency available to its named operation. +import hashlib +# What: import json for load using json; why: _load uses json jsondecode error, making that imported dependency available to its named operation. +import json +# What: import math for from public using math; why: from_public uses math isfinite, making that imported dependency available to its named operation. +import math +# What: import os for compact locked using os; why: _compact_locked uses os replace, making that imported dependency available to its named operation. +import os +# What: import threading for init using threading; why: __init__ uses threading lock, making that imported dependency available to its named operation. +import threading +# What: import time for record using time; why: record uses time time, making that imported dependency available to its named operation. +import time +# What: import mapping for headers using typing and mapping; why: _headers uses the mapping annotation in headers, making that imported dependency available to its named operation. +from typing import Mapping + + +# What: compute sensitive headers from authorization and proxy authorization and cookie and set cookie and x api key; why: normalized in sensitive headers later reads sensitive headers, so activity must retain the computed value under that name. +_SENSITIVE_HEADERS = { + # What: apply the authorization proxy authorization cookie set cookie x api key x ft token portion of sensitive headers; why: activity uses this clause to evaluate sensitive headers as one grouped value. + "authorization", "proxy-authorization", "cookie", "set-cookie", "x-api-key", "x-ft-token", +# What: complete the _SENSITIVE_HEADERS collection with authorization and proxy authorization and cookie and set cookie; why: activity groups the supplied clauses as one _SENSITIVE_HEADERS collection before its value is consumed. +} +# What: compute max persisted row chars from 8192; why: while line source readline max persisted row chars later reads max persisted row chars, so activity must retain the computed value under that name. +_MAX_PERSISTED_ROW_CHARS = 8192 + + +# What: define _sensitive_header around name; why: its direct callers call _sensitive_header for sensitive header and rely on this exact input and result contract. +def _sensitive_header(name: str) -> bool: + # What: compute normalized from replace and lower and name and value and value; why: parts normalized split later reads normalized, so _sensitive_header must retain the computed value under that name. + normalized = name.lower().replace("_", "-") + # What: compute parts from split and normalized and value; why: or token in parts later reads parts, so _sensitive_header must retain the computed value under that name. + parts = normalized.split("-") + # What: return normalized and sensitive headers and parts and token and secret from _sensitive_header; why: _sensitive_header exposes normalized and sensitive headers and parts and token and secret so its caller can continue with the function\'s computed outcome. + return ( + # What: apply the normalized in sensitive headers portion of the enclosing predicate; why: this clause remains in _sensitive_header\'s enclosing expression so its grouping and evaluation order stay intact. + normalized in _SENSITIVE_HEADERS + # What: apply the or token in parts portion of the enclosing predicate; why: this clause remains in _sensitive_header\'s enclosing expression so its grouping and evaluation order stay intact. + or "token" in parts + # What: apply the or secret in parts portion of the enclosing predicate; why: this clause remains in _sensitive_header\'s enclosing expression so its grouping and evaluation order stay intact. + or "secret" in parts + # What: apply the or api in parts and key portion of the enclosing predicate; why: this clause remains in _sensitive_header\'s enclosing expression so its grouping and evaluation order stay intact. + or ("api" in parts and "key" in parts) + # What: complete the _sensitive_header signature with name; why: _sensitive_header groups the supplied clauses as one _sensitive_header signature before its value is consumed. + ) + + +# What: define _headers around values; why: its direct callers call _headers for headers and rely on this exact input and result contract. +def _headers(values: Mapping[str, str]) -> dict[str, str]: + # What: initialize result as an empty runtime accumulator; why: _headers appends or maps entries into it during result key redacted if sensitive header key else before consuming the aggregate. + result: dict[str, str] = {} + # What: iterate across list and items and values to perform result and key and sensitive header and str and value; why: _headers repeats the body only while or for the loop header admits an iteration. + for key, value in list(values.items())[:64]: + # What: compute result entry from sensitive header and key and str and value and redacted; why: return result later reads result entry, so _headers must retain the computed value under that name. + result[key] = "[REDACTED]" if _sensitive_header(key) else str(value)[:1024] + # What: return result from _headers; why: _headers exposes result so its caller can continue with the function\'s computed outcome. + return result + + +# What: generate dataclass initialization and value semantics for ActivityRecord; why: ActivityRecord acts as a typed state record with consistent construction, comparison, and representation. +@dataclass(frozen=True) +# What: define ActivityRecord as the owner of public and from_public; why: daemon callers use this class boundary so those methods share one activity record state invariant. +class ActivityRecord: + # What: compute id from the named fixture input; why: id self id later reads id, so activity must retain the computed value under that name. + id: int + # What: compute timestamp from the named fixture input; why: timestamp self timestamp later reads timestamp, so activity must retain the computed value under that name. + timestamp: float + # What: compute model from the named fixture input; why: model self model later reads model, so activity must retain the computed value under that name. + model: str + # What: compute route from the named fixture input; why: route self route later reads route, so activity must retain the computed value under that name. + route: str + # What: compute method from the named fixture input; why: method self method later reads method, so activity must retain the computed value under that name. + method: str + # What: compute status from the named fixture input; why: status self status later reads status, so activity must retain the computed value under that name. + status: int + # What: compute duration s from the named fixture input; why: duration s self duration s later reads duration s, so activity must retain the computed value under that name. + duration_s: float + # What: compute ttft s from the named fixture input; why: ttft s self ttft s later reads ttft s, so activity must retain the computed value under that name. + ttft_s: float | None + # What: compute response bytes from the named fixture input; why: response bytes self response bytes later reads response bytes, so activity must retain the computed value under that name. + response_bytes: int + # What: compute cancelled from the named fixture input; why: cancelled self cancelled later reads cancelled, so activity must retain the computed value under that name. + cancelled: bool + # What: compute session id from the named fixture input; why: session id self session id later reads session id, so activity must retain the computed value under that name. + session_id: str | None + # What: compute has capture from the named fixture input; why: has capture self has capture later reads has capture, so activity must retain the computed value under that name. + has_capture: bool + + # What: define public around the current object state; why: its direct callers call public for public and rely on this exact input and result contract. + def public(self) -> dict: + # What: return id and timestamp and model and route from public; why: public exposes id and timestamp and model and route so its caller can continue with the function\'s computed outcome. + return { + # What: map the id field as id; why: ActivityRecord.public carries id into "id": self.id. + "id": self.id, + # What: map the timestamp field as timestamp; why: ActivityRecord.public carries timestamp into "timestamp": self.timestamp. + "timestamp": self.timestamp, + # What: map the model field as model; why: ActivityRecord.public sends this field through "model": self.model so the router selects the canonical model or alias for upstream dispatch. + "model": self.model, + # What: map the route field as route; why: ActivityRecord.public carries route into "route": self.route. + "route": self.route, + # What: map the method field as method; why: ActivityRecord.public carries method into "method": self.method. + "method": self.method, + # What: map the status field as status; why: ActivityRecord.public carries status into "status": self.status. + "status": self.status, + # What: map the duration s field as duration s; why: ActivityRecord.public carries duration s into "durationS": self.duration_s. + "durationS": self.duration_s, + # What: map the ttft s field as ttft s; why: ActivityRecord.public carries ttft s into "ttftS": self.ttft_s. + "ttftS": self.ttft_s, + # What: map the response bytes field as response bytes; why: ActivityRecord.public carries response bytes into "responseBytes": self.response_bytes. + "responseBytes": self.response_bytes, + # What: map the cancelled field as cancelled; why: ActivityRecord.public carries cancelled into "cancelled": self.cancelled. + "cancelled": self.cancelled, + # What: map the session id field as session id; why: ActivityRecord.public carries session id into "sessionId": self.session_id. + "sessionId": self.session_id, + # What: map the has capture field as has capture; why: ActivityRecord.public carries has capture into "hasCapture": self.has_capture. + "hasCapture": self.has_capture, + # What: complete the enclosing predicate mapping with id and timestamp and model and route and method; why: ActivityRecord.public groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + # What: bind from_public to the class rather than an instance; why: factory and parser callers construct from_public from class-level state without requiring an existing object. + @classmethod + # What: define from_public around item; why: the registered API client call from_public for from public and rely on this exact input and result contract. + def from_public(cls, item: dict) -> "ActivityRecord": + # What: gate on isinstance and item and dict before value error; why: from_public admits value error only for this predicate and excludes the opposite state. + if not isinstance(item, dict): + # What: raise ValueError for the caller; why: ActivityRecord.from_public stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("activity row must be an object") + # What: compute integer fields from id and status and response bytes; why: if any type item get key is later reads integer fields, so from_public must retain the computed value under that name. + integer_fields = ("id", "status", "responseBytes") + # What: gate on any and int and key and integer fields and type before value error; why: from_public admits value error only for this predicate and excludes the opposite state. + if any(type(item.get(key)) is not int for key in integer_fields): + # What: raise ValueError for the caller; why: ActivityRecord.from_public stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("activity integer field is invalid") + # What: gate on bool and type and get and item before value error; why: from_public admits value error only for this predicate and excludes the opposite state. + if type(item.get("cancelled")) is not bool: + # What: raise ValueError for the caller; why: ActivityRecord.from_public stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("activity cancellation field is invalid") + # What: iterate across the computed value to perform value and get and key and item; why: from_public repeats the body only while or for the loop header admits an iteration. + for key, maximum in (("model", 128), ("route", 256), ("method", 16)): + # What: compute value from get and key and item; why: if not isinstance value str or later reads value, so from_public must retain the computed value under that name. + value = item.get(key) + # What: gate on value and maximum and isinstance and str and len before value error; why: from_public admits value error only for this predicate and excludes the opposite state. + if not isinstance(value, str) or not value or len(value) > maximum: + # What: raise ValueError for the caller; why: ActivityRecord.from_public stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("activity string field is invalid") + # What: compute numeric from get and item and timestamp and duration s; why: if any type value not in later reads numeric, so from_public must retain the computed value under that name. + numeric = (item.get("timestamp"), item.get("durationS")) + # What: gate on any and value and numeric and type and int before value error; why: from_public admits value error only for this predicate and excludes the opposite state. + if any(type(value) not in (int, float) or not math.isfinite(value) for value in numeric): + # What: raise ValueError for the caller; why: ActivityRecord.from_public stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("activity timing field is invalid") + # What: compute ttft from get and item and ttft s; why: if ttft is not and later reads ttft, so from_public must retain the computed value under that name. + ttft = item.get("ttftS") + # What: gate on ttft and type and int and float and isfinite before value error; why: from_public admits value error only for this predicate and excludes the opposite state. + if ttft is not None and ( + # What: call type with ttft; why: from_public consumes the type return value while evaluating type(ttft) not in (int, float) or not math.isfinite(ttft) or ttft < 0. + type(ttft) not in (int, float) or not math.isfinite(ttft) or ttft < 0 + # What: complete the enclosing predicate with ttft is not and type ttft not in int; why: ActivityRecord.from_public groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise ValueError for the caller; why: ActivityRecord.from_public stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("activity TTFT field is invalid") + # What: compute session id from get and item and session id; why: if session id is not and later reads session id, so from_public must retain the computed value under that name. + session_id = item.get("sessionId") + # What: gate on session id and any and isinstance and str and len before value error; why: from_public admits value error only for this predicate and excludes the opposite state. + if session_id is not None and ( + # What: call isinstance with session id and str; why: from_public invokes isinstance while performing or len session id; the call advances that operation through its result or side effect. + not isinstance(session_id, str) + # What: call len with session id; why: from_public invokes len while performing or any char not in abcdef; the call advances that operation through its result or side effect. + or len(session_id) != 16 + # What: call any with char and session id and abcdef; why: from_public consumes the any return value while evaluating or any(char not in "0123456789abcdef" for char in session_id). + or any(char not in "0123456789abcdef" for char in session_id) + # What: complete the enclosing predicate with session id is not and not isinstance session id str or; why: ActivityRecord.from_public groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise ValueError for the caller; why: ActivityRecord.from_public stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("activity session field is invalid") + # What: gate on item before value error; why: from_public admits value error only for this predicate and excludes the opposite state. + if ( + # What: apply the item id or item response bytes portion of the enclosing predicate; why: this clause remains in from_public\'s enclosing expression so its grouping and evaluation order stay intact. + item["id"] < 1 or item["responseBytes"] < 0 + # What: apply the or not item status portion of the enclosing predicate; why: this clause remains in from_public\'s enclosing expression so its grouping and evaluation order stay intact. + or not 100 <= item["status"] <= 599 + # What: apply the or item timestamp or item duration s portion of the enclosing predicate; why: this clause remains in from_public\'s enclosing expression so its grouping and evaluation order stay intact. + or item["timestamp"] < 0 or item["durationS"] < 0 + # What: complete the enclosing predicate with if item id 1 or item response bytes 0 or; why: ActivityRecord.from_public groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise ValueError for the caller; why: ActivityRecord.from_public stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("activity row is out of range") + # What: return session id and int and float and str from from_public; why: from_public exposes session id and int and float and str so its caller can continue with the function\'s computed outcome. + return cls( + # What: supply id to int; why: from_public binds this int and item and id value to int's id input. + id=int(item["id"]), + # What: supply timestamp to float; why: from_public binds this float and item and timestamp value to float's timestamp input. + timestamp=float(item["timestamp"]), + # What: supply model to str; why: from_public binds this str and item and model value to str's model input. + model=str(item["model"]), + # What: supply route to str; why: from_public binds this str and item and route value to str's route input. + route=str(item["route"]), + # What: supply method to str; why: from_public binds this str and item and method value to str's method input. + method=str(item["method"]), + # What: supply status to int; why: from_public binds this int and item and status value to int's status input. + status=int(item["status"]), + # What: supply duration s to float; why: from_public binds this float and item and duration s value to float's duration s input. + duration_s=float(item["durationS"]), + # What: supply ttft s to float; why: from_public binds this float and get and item and ttft s and ttft s value to float's ttft s input. + ttft_s=float(item["ttftS"]) if item.get("ttftS") is not None else None, + # What: supply response bytes to int; why: from_public binds this int and item and response bytes value to int's response bytes input. + response_bytes=int(item["responseBytes"]), + # What: supply cancelled to bool; why: from_public binds this bool and item and cancelled value to bool's cancelled input. + cancelled=bool(item["cancelled"]), + # What: supply session id to cls; why: from_public binds this session id value to cls's session id input. + session_id=session_id, + # What: supply has capture to cls; why: from_public binds this false value to cls's has capture input. + has_capture=False, + # What: complete the cls call with id and timestamp and model and route and method; why: ActivityRecord.from_public groups the supplied clauses as one cls call before its value is consumed. + ) + + +# What: define ActivityStore as the owner of __init__ and reconfigure and capture_item_limit and record and list; why: daemon callers use this class boundary so those methods share one activity store state invariant. +class ActivityStore: + """Thread-safe bounded rows plus a byte-budgeted capture LRU.""" +# What: document thread safe bounded rows plus a byte budgeted in the ActivityStore docstring; why: introspection and maintainers read this exact docstring fragment to understand activity store behavior without executing it. + + # What: define __init__ around max entries and capture budget bytes and persistence path and session headers; why: its direct callers call __init__ for init and rely on this exact input and result contract. + def __init__( + # What: declare the self input for __init__; why: __init__ consumes self during self lock threading lock, so callers must bind it with the other signature inputs. + self, max_entries: int, capture_budget_bytes: int, persistence_path: str | None = None, + # What: declare the session headers input for __init__; why: __init__ consumes session headers during self session headers session headers, so callers must bind it with the other signature inputs. + session_headers: tuple[str, ...] = (), + # What: complete the enclosing predicate with group delimiter; why: ActivityStore.__init__ groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> None: + # What: compute lock from lock and threading; why: the enclosing return or state update later reads lock, so __init__ must retain the computed value under that name. + self._lock = threading.Lock() + # What: compute next id from 1; why: the enclosing return or state update later reads next id, so __init__ must retain the computed value under that name. + self._next_id = 1 + # What: compute records from deque; why: the enclosing return or state update later reads records, so __init__ must retain the computed value under that name. + self._records: deque[ActivityRecord] = deque() + # What: compute captures from ordered dict; why: the enclosing return or state update later reads captures, so __init__ must retain the computed value under that name. + self._captures: OrderedDict[int, tuple[int, dict]] = OrderedDict() + # What: compute capture bytes from 0; why: the enclosing return or state update later reads capture bytes, so __init__ must retain the computed value under that name. + self._capture_bytes = 0 + # What: compute max entries from max entries; why: the enclosing return or state update later reads max entries, so __init__ must retain the computed value under that name. + self._max_entries = max_entries + # What: compute capture budget from capture budget bytes; why: the enclosing return or state update later reads capture budget, so __init__ must retain the computed value under that name. + self._capture_budget = capture_budget_bytes + # What: compute persistence path from persistence path; why: the enclosing return or state update later reads persistence path, so __init__ must retain the computed value under that name. + self._persistence_path = persistence_path + # What: compute persisted rows from 0; why: the enclosing return or state update later reads persisted rows, so __init__ must retain the computed value under that name. + self._persisted_rows = 0 + # What: compute persistence error from the named fixture input; why: the enclosing return or state update later reads persistence error, so __init__ must retain the computed value under that name. + self._persistence_error: str | None = None + # What: compute rewrite required from false; why: the enclosing return or state update later reads rewrite required, so __init__ must retain the computed value under that name. + self._rewrite_required = False + # What: compute session headers from session headers; why: the enclosing return or state update later reads session headers, so __init__ must retain the computed value under that name. + self._session_headers = session_headers + # What: call self._load with the declared inputs; why: __init__ invokes self._load while performing the enclosing return; the call advances that operation through its result or side effect. + self._load() + + # What: define reconfigure around max entries and capture budget bytes and session headers; why: its direct callers call reconfigure for reconfigure and rely on this exact input and result contract. + def reconfigure( + # What: declare the self input for reconfigure; why: reconfigure consumes self during with self lock, so callers must bind it with the other signature inputs. + self, max_entries: int, capture_budget_bytes: int, + # What: declare the session headers input for reconfigure; why: reconfigure consumes session headers during if session headers is not, so callers must bind it with the other signature inputs. + session_headers: tuple[str, ...] | None = None, + # What: complete the enclosing predicate with group delimiter; why: ActivityStore.reconfigure groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> None: + # What: enter the lock managed context before self max entries max entries; why: reconfigure releases this resource or lock after self max entries max entries on both success and failure paths. + with self._lock: + # What: compute max entries from max entries; why: the enclosing return or state update later reads max entries, so reconfigure must retain the computed value under that name. + self._max_entries = max_entries + # What: compute capture budget from capture budget bytes; why: the enclosing return or state update later reads capture budget, so reconfigure must retain the computed value under that name. + self._capture_budget = capture_budget_bytes + # What: gate on session headers before session headers and session headers; why: reconfigure admits session headers and session headers only for this predicate and excludes the opposite state. + if session_headers is not None: + # What: compute session headers from session headers; why: the enclosing return or state update later reads session headers, so reconfigure must retain the computed value under that name. + self._session_headers = session_headers + # What: iterate across max entries and len and records to perform removed and popleft and records; why: reconfigure repeats the body only while or for the loop header admits an iteration. + while len(self._records) > max_entries: + # What: compute removed from popleft and records; why: self drop capture locked removed id later reads removed, so reconfigure must retain the computed value under that name. + removed = self._records.popleft() + # What: call self._drop_capture_locked with id and removed; why: reconfigure invokes self._drop_capture_locked while performing while self capture bytes capture budget bytes and self captures; the call advances that operation through its result or side effect. + self._drop_capture_locked(removed.id) + # What: iterate across captures and capture bytes and capture budget bytes to perform value and popitem and size and captures; why: reconfigure repeats the body only while or for the loop header admits an iteration. + while self._capture_bytes > capture_budget_bytes and self._captures: + # What: compute and size and from popitem and captures and false; why: the enclosing return or state update later reads and size and, so reconfigure must retain the computed value under that name. + _, (size, _) = self._captures.popitem(last=False) + # What: compute capture bytes from size; why: the enclosing return or state update later reads capture bytes, so reconfigure must retain the computed value under that name. + self._capture_bytes -= size + # What: gate on persistence path before compact locked; why: reconfigure admits compact locked only for this predicate and excludes the opposite state. + if self._persistence_path is not None: + # What: call self._compact_locked with the declared inputs; why: reconfigure invokes self._compact_locked while performing the enclosing return; the call advances that operation through its result or side effect. + self._compact_locked() + + # What: expose capture_item_limit as a read-only computed property; why: callers read capture_item_limit through attribute access while its getter retains control of the derived value. + @property + # What: define capture_item_limit around the current object state; why: the registered API client call capture_item_limit for capture item limit and rely on this exact input and result contract. + def capture_item_limit(self) -> int: + # What: enter the lock managed context before return min self capture budget; why: capture_item_limit releases this resource or lock after return min self capture budget on both success and failure paths. + with self._lock: + # What: return min and capture budget and 1024 and 1024 from capture_item_limit; why: capture_item_limit exposes min and capture budget and 1024 and 1024 so its caller can continue with the function\'s computed outcome. + return min(self._capture_budget, 1024 * 1024) + + # What: define record around model and route and method and status and started and ttft s and response bytes and cancelled and request headers and request body and response headers and response body; why: its direct callers call record for record and rely on this exact input and result contract. + def record( + # What: declare the self input for record; why: record consumes self during with self lock, so callers must bind it with the other signature inputs. + self, *, model: str, route: str, method: str, status: int, + # What: declare the started input for record; why: record consumes started during duration max time monotonic started, so callers must bind it with the other signature inputs. + started: float, ttft_s: float | None, response_bytes: int, cancelled: bool, + # What: declare the request headers input for record; why: record consumes request headers during lowered headers key lower value for key value, so callers must bind it with the other signature inputs. + request_headers: Mapping[str, str], request_body: bytes, + # What: declare the response headers input for record; why: record consumes response headers during response headers headers response headers, so callers must bind it with the other signature inputs. + response_headers: Mapping[str, str], response_body: bytes | None, + # What: complete the enclosing predicate with dict; why: ActivityStore.record groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> dict: + # What: compute ended from time; why: id row id timestamp ended model model later reads ended, so record must retain the computed value under that name. + ended = time.time() + # What: compute duration from max and started and monotonic and time and 0 0; why: status status duration s round duration later reads duration, so record must retain the computed value under that name. + duration = max(0.0, time.monotonic() - started) + # What: compute lowered headers from value and lower and key and items; why: hashlib sha256 str lowered headers header encode utf 8 later reads lowered headers, so record must retain the computed value under that name. + lowered_headers = {key.lower(): value for key, value in request_headers.items()} + # What: compute session id from next and header and session headers and hexdigest; why: session id session id has capture has capture later reads session id, so record must retain the computed value under that name. + session_id = next( + # What: complete the next call with header; why: ActivityStore.record groups the supplied clauses as one next call before its value is consumed. + ( + # What: call operation.hexdigest with the declared inputs; why: record invokes operation.hexdigest while performing for header in self session headers if lowered headers get; the call advances that operation through its result or side effect. + hashlib.sha256(str(lowered_headers[header]).encode("utf-8")).hexdigest()[:16] + # What: call lowered_headers.get with header; why: record consumes the lowered_headers.get return value while evaluating for header in self._session_headers if lowered_headers.get(header). + for header in self._session_headers if lowered_headers.get(header) + # What: complete the next call with header; why: ActivityStore.record groups the supplied clauses as one next call before its value is consumed. + ), + # What: apply the grouped expression portion of session id; why: record uses this clause to evaluate session id as one grouped value. + None, + # What: complete the next call with header; why: ActivityStore.record groups the supplied clauses as one next call before its value is consumed. + ) + # What: enter the lock managed context before row id self next id; why: record releases this resource or lock after row id self next id on both success and failure paths. + with self._lock: + # What: compute row id from next id; why: id row id later reads row id, so record must retain the computed value under that name. + row_id = self._next_id + # What: compute next id from 1; why: the enclosing return or state update later reads next id, so record must retain the computed value under that name. + self._next_id += 1 + # What: compute has capture from false; why: has capture later reads has capture, so record must retain the computed value under that name. + has_capture = False + # What: gate on capture budget and cancelled and response body and len and request body before capture and row id and route and method and headers; why: record admits capture and row id and route and method and headers only for this predicate and excludes the opposite state. + if ( + # What: apply the self capture budget portion of the enclosing predicate; why: this clause remains in record\'s enclosing expression so its grouping and evaluation order stay intact. + self._capture_budget > 0 + # What: apply the and not cancelled portion of the enclosing predicate; why: this clause remains in record\'s enclosing expression so its grouping and evaluation order stay intact. + and not cancelled + # What: apply the and response body is not portion of the enclosing predicate; why: this clause remains in record\'s enclosing expression so its grouping and evaluation order stay intact. + and response_body is not None + # What: call len with request body; why: record consumes the len return value while evaluating and len(request_body) + len(response_body) <= self._capture_budget. + and len(request_body) + len(response_body) <= self._capture_budget + # What: complete the enclosing predicate with if self capture budget 0 and not cancelled and response body is; why: ActivityStore.record groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: compute capture from row id and route and method and headers; why: size len json dumps capture separators encode later reads capture, so record must retain the computed value under that name. + capture = { + # What: map the id field as row id; why: ActivityStore.record carries id through capture into size len json dumps capture separators encode utf 8. + "id": row_id, + # What: map the route field as route; why: ActivityStore.record carries route through capture into size len json dumps capture separators encode utf 8. + "route": route, + # What: map the method field as method; why: ActivityStore.record carries method through capture into size len json dumps capture separators encode utf 8. + "method": method, + # What: map the request headers field as headers and request headers; why: ActivityStore.record carries request headers through capture into size len json dumps capture separators encode utf 8. + "requestHeaders": _headers(request_headers), + # What: map the request body base64 field as decode and b64encode and request body and base64 and ascii; why: ActivityStore.record carries request body base64 through capture into size len json dumps capture separators encode utf 8. + "requestBodyBase64": base64.b64encode(request_body).decode("ascii"), + # What: map the response headers field as headers and response headers; why: ActivityStore.record carries response headers through capture into size len json dumps capture separators encode utf 8. + "responseHeaders": _headers(response_headers), + # What: map the response body base64 field as decode and b64encode and response body and base64 and ascii; why: ActivityStore.record carries response body base64 through capture into size len json dumps capture separators encode utf 8. + "responseBodyBase64": base64.b64encode(response_body).decode("ascii"), + # What: complete the capture mapping with id and route and method and request headers and request body base64; why: ActivityStore.record groups the supplied clauses as one capture mapping before its value is consumed. + } + # What: compute size from len and encode and dumps and capture; why: if size self capture budget later reads size, so record must retain the computed value under that name. + size = len(json.dumps(capture, separators=(",", ":")).encode("utf-8")) + # What: gate on size and capture budget before captures and capture bytes and old size and capture budget and value; why: record admits captures and capture bytes and old size and capture budget and value only for this predicate and excludes the opposite state. + if size <= self._capture_budget: + # What: iterate across captures and capture budget and capture bytes and size to perform value and popitem and old size and captures; why: record repeats the body only while or for the loop header admits an iteration. + while self._capture_bytes + size > self._capture_budget and self._captures: + # What: compute and old size and from popitem and captures and false; why: the enclosing return or state update later reads and old size and, so record must retain the computed value under that name. + _, (old_size, _) = self._captures.popitem(last=False) + # What: compute capture bytes from old size; why: self capture bytes size later reads capture bytes, so record must retain the computed value under that name. + self._capture_bytes -= old_size + # What: compute captures entry from size and capture; why: the enclosing return or state update later reads captures entry, so record must retain the computed value under that name. + self._captures[row_id] = (size, capture) + # What: compute capture bytes from size; why: the enclosing return or state update later reads capture bytes, so record must retain the computed value under that name. + self._capture_bytes += size + # What: compute has capture from true; why: session id session id has capture has capture later reads has capture, so record must retain the computed value under that name. + has_capture = True + # What: compute record from activity record and row id and ended and model; why: self records append record later reads record, so record must retain the computed value under that name. + record = ActivityRecord( + # What: supply id to ActivityRecord; why: record binds this row id value to ActivityRecord's id input. + id=row_id, timestamp=ended, model=model, route=route, method=method, + # What: supply status to round; why: record binds this status value to round's status input. + status=status, duration_s=round(duration, 6), + # What: supply ttft s to round; why: record binds this ttft s and round and 6 value to round's ttft s input. + ttft_s=round(ttft_s, 6) if ttft_s is not None else None, + # What: supply response bytes to ActivityRecord; why: record binds this response bytes value to ActivityRecord's response bytes input. + response_bytes=response_bytes, cancelled=cancelled, + # What: supply session id to ActivityRecord; why: record binds this session id value to ActivityRecord's session id input. + session_id=session_id, has_capture=has_capture, + # What: complete the ActivityRecord call with id and timestamp and model and route and method; why: ActivityStore.record groups the supplied clauses as one ActivityRecord call before its value is consumed. + ) + # What: call self._records.append with record; why: record invokes self._records.append while performing while len self records self max entries; the call advances that operation through its result or side effect. + self._records.append(record) + # What: iterate across max entries and len and records to perform removed and popleft and records; why: record repeats the body only while or for the loop header admits an iteration. + while len(self._records) > self._max_entries: + # What: compute removed from popleft and records; why: self drop capture locked removed id later reads removed, so record must retain the computed value under that name. + removed = self._records.popleft() + # What: call self._drop_capture_locked with id and removed; why: record invokes self._drop_capture_locked while performing self append locked record; the call advances that operation through its result or side effect. + self._drop_capture_locked(removed.id) + # What: call self._append_locked with record; why: record invokes self._append_locked while performing return record public; the call advances that operation through its result or side effect. + self._append_locked(record) + # What: return public and record from record; why: record exposes public and record so its caller can continue with the function\'s computed outcome. + return record.public() + + # What: define list around limit and before id and model; why: its direct callers call list for list and rely on this exact input and result contract. + def list(self, *, limit: int = 100, before_id: int | None = None, model: str | None = None) -> dict: + # What: enter the lock managed context before rows row for row in reversed; why: list releases this resource or lock after rows row for row in reversed on both success and failure paths. + with self._lock: + # What: compute rows from row and reversed and records and before id; why: for row in rows limit later reads rows, so list must retain the computed value under that name. + rows = [row for row in reversed(self._records) + # What: apply the if before id is or row id before id portion of rows; why: list uses this clause to evaluate rows as one grouped value. + if (before_id is None or row.id < before_id) and (model is None or row.model == model)] + # What: initialize data as an empty runtime accumulator; why: ActivityStore.list appends or maps entries into it during data append item before consuming the aggregate. + data = [] + # What: iterate across rows and limit to perform item and public and row; why: list repeats the body only while or for the loop header admits an iteration. + for row in rows[:limit]: + # What: compute item from public and row; why: item has capture row id in self captures later reads item, so list must retain the computed value under that name. + item = row.public() + # What: compute item entry from id and captures and row; why: data append item later reads item entry, so list must retain the computed value under that name. + item["hasCapture"] = row.id in self._captures + # What: call data.append with item; why: list invokes data.append while performing return; the call advances that operation through its result or side effect. + data.append(item) + # What: return data and len and persistence locked and limit from list; why: list exposes data and len and persistence locked and limit so its caller can continue with the function\'s computed outcome. + return { + # What: map the data field as data; why: ActivityStore.list carries data into "data": data. + "data": data, + # What: map the count field as len and data; why: ActivityStore.list carries count into "count": len(data). + "count": len(data), + # What: map the next before id field as limit and len and rows and data and id; why: ActivityStore.list carries next before id into "nextBeforeId": data[-1]["id"] if len(rows) > limit else None. + "nextBeforeId": data[-1]["id"] if len(rows) > limit else None, + # What: map the persistence field as persistence locked; why: ActivityStore.list carries persistence into "persistence": self._persistence_locked(). + "persistence": self._persistence_locked(), + # What: complete the enclosing predicate mapping with data and count and next before id and persistence; why: ActivityStore.list groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + # What: define stats around model; why: its direct callers call stats for stats and rely on this exact input and result contract. + def stats(self, *, model: str | None = None) -> dict: + # What: enter the lock managed context before rows row for row in self records; why: stats releases this resource or lock after rows row for row in self records on both success and failure paths. + with self._lock: + # What: compute rows from row and records and model; why: count len rows later reads rows, so stats must retain the computed value under that name. + rows = [row for row in self._records if model is None or row.model == model] + # What: compute count from len and rows; why: count count later reads count, so stats must retain the computed value under that name. + count = len(rows) + # What: return count and sum and persistence locked and cancelled from stats; why: stats exposes count and sum and persistence locked and cancelled so its caller can continue with the function\'s computed outcome. + return { + # What: map the count field as count; why: ActivityStore.stats carries count into "count": count. + "count": count, + # What: map the cancelled field as sum and cancelled and row and rows; why: ActivityStore.stats carries cancelled into "cancelled": sum(row.cancelled for row in rows). + "cancelled": sum(row.cancelled for row in rows), + # What: map the errors field as sum and status and row and rows and 400; why: ActivityStore.stats carries errors into "errors": sum(row.status >= 400 for row in rows). + "errors": sum(row.status >= 400 for row in rows), + # What: map the response bytes field as sum and response bytes and row and rows; why: ActivityStore.stats carries response bytes into "responseBytes": sum(row.response_bytes for row in rows). + "responseBytes": sum(row.response_bytes for row in rows), + # What: map the average duration s field as count and round and sum and duration s; why: ActivityStore.stats carries average duration s into "averageDurationS": round(sum(row.duration_s for row in rows) / count, 6. + "averageDurationS": round(sum(row.duration_s for row in rows) / count, 6) if count else None, + # What: map the persistence field as persistence locked; why: ActivityStore.stats carries persistence into "persistence": self._persistence_locked(). + "persistence": self._persistence_locked(), + # What: complete the enclosing predicate mapping with count and cancelled and errors and response bytes and average duration s; why: ActivityStore.stats groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + # What: define capture around row id; why: its direct callers call capture for capture and rely on this exact input and result contract. + def capture(self, row_id: int) -> dict | None: + # What: enter the lock managed context before item self captures get row id; why: capture releases this resource or lock after item self captures get row id on both success and failure paths. + with self._lock: + # What: compute item from get and row id and captures; why: if item is later reads item, so capture must retain the computed value under that name. + item = self._captures.get(row_id) + # What: gate on item before the computed value; why: capture admits the computed value only for this predicate and excludes the opposite state. + if item is None: + # What: return no value from capture; why: capture returns no value to callers that depend on its completed result. + return None + # What: call self._captures.move_to_end with row id; why: capture invokes self._captures.move_to_end while performing return dict item; the call advances that operation through its result or side effect. + self._captures.move_to_end(row_id) + # What: return dict and item and 1 from capture; why: capture exposes dict and item and 1 so its caller can continue with the function\'s computed outcome. + return dict(item[1]) + + # What: define _drop_capture_locked around row id; why: its direct callers call _drop_capture_locked for drop capture locked and rely on this exact input and result contract. + def _drop_capture_locked(self, row_id: int) -> None: + # What: compute item from pop and row id and captures; why: if item is not later reads item, so _drop_capture_locked must retain the computed value under that name. + item = self._captures.pop(row_id, None) + # What: gate on item before capture bytes and item; why: _drop_capture_locked admits capture bytes and item only for this predicate and excludes the opposite state. + if item is not None: + # What: compute capture bytes from item and 0; why: the enclosing return or state update later reads capture bytes, so _drop_capture_locked must retain the computed value under that name. + self._capture_bytes -= item[0] + + # What: define _persistence_locked around the current object state; why: its direct callers call _persistence_locked for persistence locked and rely on this exact input and result contract. + def _persistence_locked(self) -> dict: + # What: return persistence error and persistence path and enabled and healthy and error from _persistence_locked; why: _persistence_locked exposes persistence error and persistence path and enabled and healthy and error so its caller can continue with the function\'s computed outcome. + return { + # What: map the enabled field as persistence path; why: ActivityStore._persistence_locked carries enabled into "enabled": self._persistence_path is not None. + "enabled": self._persistence_path is not None, + # What: map the healthy field as persistence path and persistence error; why: ActivityStore._persistence_locked carries healthy into "healthy": self._persistence_path is None or self._persistence_error is. + "healthy": self._persistence_path is None or self._persistence_error is None, + # What: map the error field as persistence error; why: ActivityStore._persistence_locked carries error into "error": self._persistence_error. + "error": self._persistence_error, + # What: complete the enclosing predicate mapping with enabled and healthy and error; why: ActivityStore._persistence_locked groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + # What: define _load around the current object state; why: its direct callers call _load for load and rely on this exact input and result contract. + def _load(self) -> None: + # What: gate on persistence path before the computed value; why: _load admits the computed value only for this predicate and excludes the opposite state. + if self._persistence_path is None: + # What: return no value from _load; why: _load returns no value to callers that depend on its completed result. + return + # What: compute invalid from false; why: invalid later reads invalid, so _load must retain the computed value under that name. + invalid = False + # What: compute last id from 0; why: if row id last id later reads last id, so _load must retain the computed value under that name. + last_id = 0 + # What: establish the handler boundary for the protected operation; why: ActivityStore._load routes failures to file not found error and oserror while preserving cleanup and success flow. + try: + # What: enter the open managed context before while line source readline max persisted row chars; why: _load releases this resource or lock after while line source readline max persisted row chars on both success and failure paths. + with open(self._persistence_path, encoding="utf-8") as source: + # What: iterate across line and readline and source and max persisted row chars to perform max persisted row chars and invalid and len and line and readline; why: _load repeats the body only while or for the loop header admits an iteration. + while line := source.readline(_MAX_PERSISTED_ROW_CHARS + 1): + # What: gate on max persisted row chars and len and line before invalid; why: _load admits invalid only for this predicate and excludes the opposite state. + if len(line) > _MAX_PERSISTED_ROW_CHARS: + # What: compute invalid from true; why: invalid later reads invalid, so _load must retain the computed value under that name. + invalid = True + # What: iterate across line and endswith to perform line and readline and source and max persisted row chars; why: _load repeats the body only while or for the loop header admits an iteration. + while line and not line.endswith("\n"): + # What: compute line from readline and source and max persisted row chars and 1; why: row activity record from public json loads line later reads line, so _load must retain the computed value under that name. + line = source.readline(_MAX_PERSISTED_ROW_CHARS + 1) + # What: apply the continue portion of the enclosing predicate; why: this clause remains in _load\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: establish the handler boundary for the protected operation; why: ActivityStore._load routes failures to key error and type error and value error and jsondecode error and json while preserving cleanup and success flow. + try: + # What: compute row from from public and activity record and loads and line; why: if row id last id later reads row, so _load must retain the computed value under that name. + row = ActivityRecord.from_public(json.loads(line)) + # What: gate on id and last id and row before value error; why: _load admits value error only for this predicate and excludes the opposite state. + if row.id <= last_id: + # What: raise ValueError for the caller; why: ActivityStore._load stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("activity IDs must increase") + # What: handle key error and type error and value error and jsondecode error and json by invalid true; why: ActivityStore._load converts that failure into this concrete recovery, response, or cleanup behavior. + except (KeyError, TypeError, ValueError, json.JSONDecodeError): + # What: compute invalid from true; why: if invalid or self persisted rows self max entries later reads invalid, so _load must retain the computed value under that name. + invalid = True + # What: apply the continue portion of the enclosing predicate; why: this clause remains in _load\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: call self._records.append with row; why: _load invokes self._records.append while performing last id row id; the call advances that operation through its result or side effect. + self._records.append(row) + # What: compute last id from id and row; why: the enclosing return or state update later reads last id, so _load must retain the computed value under that name. + last_id = row.id + # What: compute persisted rows from 1; why: if invalid or self persisted rows self max entries later reads persisted rows, so _load must retain the computed value under that name. + self._persisted_rows += 1 + # What: compute next id from max and next id and id and row and 1; why: the enclosing return or state update later reads next id, so _load must retain the computed value under that name. + self._next_id = max(self._next_id, row.id + 1) + # What: iterate across max entries and len and records to perform popleft and records; why: _load repeats the body only while or for the loop header admits an iteration. + while len(self._records) > self._max_entries: + # What: call self._records.popleft with the declared inputs; why: _load invokes self._records.popleft while performing except file not found error; the call advances that operation through its result or side effect. + self._records.popleft() + # What: handle file not found error by return; why: ActivityStore._load converts that failure into this concrete recovery, response, or cleanup behavior. + except FileNotFoundError: + # What: return no value from _load; why: _load returns no value to callers that depend on its completed result. + return + # What: handle oserror by self persistence error load failed; why: ActivityStore._load converts that failure into this concrete recovery, response, or cleanup behavior. + except OSError: + # What: compute persistence error from load failed; why: the enclosing return or state update later reads persistence error, so _load must retain the computed value under that name. + self._persistence_error = "load_failed" + # What: compute rewrite required from true; why: the enclosing return or state update later reads rewrite required, so _load must retain the computed value under that name. + self._rewrite_required = True + # What: return no value from _load; why: _load returns no value to callers that depend on its completed result. + return + # What: gate on invalid and persisted rows and max entries before compact locked; why: _load admits compact locked only for this predicate and excludes the opposite state. + if invalid or self._persisted_rows > self._max_entries: + # What: call self._compact_locked with the declared inputs; why: _load invokes self._compact_locked while performing the enclosing return; the call advances that operation through its result or side effect. + self._compact_locked() + + # What: define _append_locked around record; why: its direct callers call _append_locked for append locked and rely on this exact input and result contract. + def _append_locked(self, record: ActivityRecord) -> None: + # What: gate on persistence path before the computed value; why: _append_locked admits the computed value only for this predicate and excludes the opposite state. + if self._persistence_path is None: + # What: return no value from _append_locked; why: _append_locked returns no value to callers that depend on its completed result. + return + # What: gate on rewrite required before compact locked; why: _append_locked admits compact locked only for this predicate and excludes the opposite state. + if self._rewrite_required: + # What: call self._compact_locked with the declared inputs; why: _append_locked invokes self._compact_locked while performing return; the call advances that operation through its result or side effect. + self._compact_locked() + # What: return no value from _append_locked; why: _append_locked returns no value to callers that depend on its completed result. + return + # What: establish the handler boundary for the protected operation; why: ActivityStore._append_locked routes failures to oserror while preserving cleanup and success flow. + try: + # What: enter the open managed context before target write json dumps record public separators n; why: _append_locked releases this resource or lock after target write json dumps record public separators n on both success and failure paths. + with open(self._persistence_path, "a", encoding="utf-8", newline="\n") as target: + # What: preserve the exact target write json dumps record public separators n literal fragment; why: _append_locked passes this fragment verbatim through target.write(json.dumps(record.public(), separators=(",", ":")) + "\n"), because changing it would alter a protocol payload, serialized fixture, or p. + target.write(json.dumps(record.public(), separators=(",", ":")) + "\n") + # What: call target.flush with the declared inputs; why: _append_locked invokes target.flush while performing os fsync target fileno; the call advances that operation through its result or side effect. + target.flush() + # What: call os.fsync with fileno and target; why: _append_locked invokes os.fsync while performing self persisted rows; the call advances that operation through its result or side effect. + os.fsync(target.fileno()) + # What: compute persisted rows from 1; why: if self persisted rows max self max entries later reads persisted rows, so _append_locked must retain the computed value under that name. + self._persisted_rows += 1 + # What: compute persistence error from the named fixture input; why: self persistence error write failed later reads persistence error, so _append_locked must retain the computed value under that name. + self._persistence_error = None + # What: gate on persisted rows and max and max entries before compact locked; why: _append_locked admits compact locked only for this predicate and excludes the opposite state. + if self._persisted_rows > max(2, self._max_entries * 2): + # What: call self._compact_locked with the declared inputs; why: _append_locked invokes self._compact_locked while performing except oserror; the call advances that operation through its result or side effect. + self._compact_locked() + # What: handle oserror by self persistence error write failed; why: ActivityStore._append_locked converts that failure into this concrete recovery, response, or cleanup behavior. + except OSError: + # What: compute persistence error from write failed; why: the enclosing return or state update later reads persistence error, so _append_locked must retain the computed value under that name. + self._persistence_error = "write_failed" + + # What: define _compact_locked around the current object state; why: its direct callers call _compact_locked for compact locked and rely on this exact input and result contract. + def _compact_locked(self) -> None: + # What: gate on persistence path before the computed value; why: _compact_locked admits the computed value only for this predicate and excludes the opposite state. + if self._persistence_path is None: + # What: return no value from _compact_locked; why: _compact_locked returns no value to callers that depend on its completed result. + return + # What: compute temporary from persistence path and tmp; why: with open temporary w encoding utf 8 later reads temporary, so _compact_locked must retain the computed value under that name. + temporary = self._persistence_path + ".tmp" + # What: establish the handler boundary for the protected operation; why: ActivityStore._compact_locked routes failures to oserror while preserving cleanup and success flow. + try: + # What: enter the open managed context before for record in self records; why: _compact_locked releases this resource or lock after for record in self records on both success and failure paths. + with open(temporary, "w", encoding="utf-8", newline="\n") as target: + # What: iterate across records to perform write and target and dumps and json and public; why: _compact_locked repeats the body only while or for the loop header admits an iteration. + for record in self._records: + # What: preserve the exact target write json dumps record public separators n literal fragment; why: _compact_locked passes this fragment verbatim through target.write(json.dumps(record.public(), separators=(",", ":")) + "\n"), because changing it would alter a protocol payload, serialized fixture. + target.write(json.dumps(record.public(), separators=(",", ":")) + "\n") + # What: call target.flush with the declared inputs; why: _compact_locked invokes target.flush while performing os fsync target fileno; the call advances that operation through its result or side effect. + target.flush() + # What: call os.fsync with fileno and target; why: _compact_locked invokes os.fsync while performing os replace temporary self persistence path; the call advances that operation through its result or side effect. + os.fsync(target.fileno()) + # What: call os.replace with temporary and persistence path; why: _compact_locked invokes os.replace while performing self persisted rows len self records; the call advances that operation through its result or side effect. + os.replace(temporary, self._persistence_path) + # What: compute persisted rows from len and records; why: the enclosing return or state update later reads persisted rows, so _compact_locked must retain the computed value under that name. + self._persisted_rows = len(self._records) + # What: compute persistence error from the named fixture input; why: self persistence error compact failed later reads persistence error, so _compact_locked must retain the computed value under that name. + self._persistence_error = None + # What: compute rewrite required from false; why: the enclosing return or state update later reads rewrite required, so _compact_locked must retain the computed value under that name. + self._rewrite_required = False + # What: handle oserror by self persistence error compact failed; why: ActivityStore._compact_locked converts that failure into this concrete recovery, response, or cleanup behavior. + except OSError: + # What: compute persistence error from compact failed; why: the enclosing return or state update later reads persistence error, so _compact_locked must retain the computed value under that name. + self._persistence_error = "compact_failed" + # What: establish the handler boundary for the protected operation; why: ActivityStore._compact_locked routes failures to oserror while preserving cleanup and success flow. + try: + # What: call os.unlink with temporary; why: _compact_locked invokes os.unlink while performing except oserror; the call advances that operation through its result or side effect. + os.unlink(temporary) + # What: handle oserror by pass; why: ActivityStore._compact_locked converts that failure into this concrete recovery, response, or cleanup behavior. + except OSError: + # What: ignore the anticipated exception handled by this branch; why: _compact_locked continues its retry or cleanup path instead of re-raising that transient failure. + pass diff --git a/python/freetoken/daemon/app.py b/python/freetoken/daemon/app.py index d7a0a53c6..e23233298 100644 --- a/python/freetoken/daemon/app.py +++ b/python/freetoken/daemon/app.py @@ -9,23 +9,186 @@ from __future__ import annotations import asyncio +# What: import base64 for extract api key using base64; why: _extract_api_key uses base64 b64decode, making that imported dependency available to its named operation. +import base64 +# What: import binascii for extract api key using binascii; why: _extract_api_key uses binascii error, making that imported dependency available to its named operation. +import binascii import collections +# What: import datetime for router performance using datetime and datetime; why: router_performance uses datetime fromisoformat, making that imported dependency available to its named operation. +from datetime import datetime import functools import json import os +# What: import re for module initialization using re; why: module initialization uses re compile, making that imported dependency available to its named operation. +import re import sys +# What: import threading for build app using threading; why: build_app uses threading lock, making that imported dependency available to its named operation. +import threading +# What: import time for forward routed using time; why: forward_routed uses time monotonic, making that imported dependency available to its named operation. +import time +# What: import uuid for forward routed using uuid; why: forward_routed uses uuid uuid4, making that imported dependency available to its named operation. +import uuid from concurrent.futures import ThreadPoolExecutor from typing import Any, Callable +# What: import quote from bytes for escaped path suffix using urllib and parse and quote from bytes; why: _escaped_path_suffix uses quote from bytes, making that imported dependency available to its named operation. +from urllib.parse import quote_from_bytes -from fastapi import Depends, FastAPI, Header, HTTPException, Request -from fastapi.responses import JSONResponse, StreamingResponse +# What: import from fastapi import Depends FastAPI Header HTTPException Query Request; why: this module calls or annotates these symbols in the branch-created operations below. +from fastapi import Depends, FastAPI, Header, HTTPException, Query, Request +# What: import from fastapi responses import HTMLResponse JSONResponse PlainTextResponse Response StreamingResponse; why: this module calls or annotates these symbols in the branch-created operations below. +from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse, Response, StreamingResponse from pydantic import BaseModel from .accounting import AccountingOutboxError, AccountingPrepareError -from .serve_manager import Conflict +# What: import activity store for build app using activity and activity store; why: build_app uses activity store, making that imported dependency available to its named operation. +from .activity import ActivityStore +# What: import from catalog import CatalogError ModelCatalog; why: this module calls or annotates these symbols in the branch-created operations below. +from .catalog import CatalogError, ModelCatalog +# What: import from inference proxy import; why: this module calls or annotates these symbols in the branch-created operations below. +from .inference_proxy import ( + # What: execute RequestModelError; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + RequestModelError, + # What: execute filter request body; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + filter_request_body, + # What: execute open upstream; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + open_upstream, + # What: execute request model; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + request_model, + # What: execute response headers; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + response_headers, +# What: complete the enclosing predicate with from inference proxy import request model error filter request body open upstream request model response headers; why: app groups the supplied clauses as one enclosing predicate expression before its value is consumed. +) +# What: import log ring for build app using logring and log ring; why: build_app uses the log ring annotation in build app, making that imported dependency available to its named operation. +from .logring import LogRing +# What: import performance monitor for build app using performance and performance monitor; why: build_app uses performance monitor, making that imported dependency available to its named operation. +from .performance import PerformanceMonitor +# What: import wait for ready for profile result using readiness and wait for ready; why: profile_result uses wait for ready, making that imported dependency available to its named operation. +from .readiness import wait_for_ready +# What: import from router import RoutingCoordinator RoutingError allocate loopback port; why: this module calls or annotates these symbols in the branch-created operations below. +from .router import RoutingCoordinator, RoutingError, allocate_loopback_port +# What: import from serve manager import Conflict SwitchLaunchError; why: this module calls or annotates these symbols in the branch-created operations below. +from .serve_manager import Conflict, SwitchLaunchError from .version import DAEMON_VERSION +# What: compute http token from compile and re and value and a za z; why: if part raw strip and http token fullmatch part later reads http token, so app must retain the computed value under that name. +_HTTP_TOKEN = re.compile(r"^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$") +# What: compute default cors headers from content type and authorization and accept and x requested with; why: return default cors headers later reads default cors headers, so app must retain the computed value under that name. +_DEFAULT_CORS_HEADERS = "Content-Type, Authorization, Accept, X-Requested-With" + + +# What: define _cors_request_headers around value; why: its direct callers call _cors_request_headers for cors request headers and rely on this exact input and result contract. +def _cors_request_headers(value: str | None) -> str: + """Echo only syntactically valid HTTP header names in a CORS preflight.""" + # What: document echo only syntactically valid http header in the _cors_request_headers docstring; why: introspection and maintainers read this exact docstring fragment to understand cors request headers behavior without executing it. + # What: gate on value before default cors headers; why: _cors_request_headers admits default cors headers only for this predicate and excludes the opposite state. + if value is None: + # What: return default cors headers from _cors_request_headers; why: _cors_request_headers exposes default cors headers so its caller can continue with the function\'s computed outcome. + return _DEFAULT_CORS_HEADERS + # What: return join and part and raw and split from _cors_request_headers; why: _cors_request_headers exposes join and part and raw and split so its caller can continue with the function\'s computed outcome. + return ", ".join( + # What: call value.split with value; why: _cors_request_headers invokes value.split while performing if part raw strip and http token fullmatch part; the call advances that operation through its result or side effect. + part for raw in value.split(",") + # What: call _HTTP_TOKEN.fullmatch with part; why: _cors_request_headers consumes the _HTTP_TOKEN.fullmatch return value while evaluating if (part := raw.strip()) and _HTTP_TOKEN.fullmatch(part). + if (part := raw.strip()) and _HTTP_TOKEN.fullmatch(part) + # What: complete the operation.join call with part; why: _cors_request_headers groups the supplied clauses as one operation.join call before its value is consumed. + ) + + +# What: define _escaped_path_suffix around raw path and decoded prefix; why: its direct callers call _escaped_path_suffix for escaped path suffix and rely on this exact input and result contract. +def _escaped_path_suffix(raw_path: bytes, decoded_prefix: str) -> str | None: + """Remove a decoded prefix while retaining the suffix's original escaping.""" + # What: document remove a decoded prefix while retaining in the _escaped_path_suffix docstring; why: introspection and maintainers read this exact docstring fragment to understand escaped path suffix behavior without executing it. + # What: compute prefix from encode and decoded prefix and utf 8; why: while raw index len raw path and prefix index later reads prefix, so _escaped_path_suffix must retain the computed value under that name. + prefix = decoded_prefix.encode("utf-8") + # What: compute raw index from 0; why: while raw index len raw path and prefix index later reads raw index, so _escaped_path_suffix must retain the computed value under that name. + raw_index = prefix_index = 0 + # What: iterate across raw index and prefix index and len and raw path and prefix to perform end and raw index; why: _escaped_path_suffix repeats the body only while or for the loop header admits an iteration. + while raw_index < len(raw_path) and prefix_index < len(prefix): + # What: compute end from raw index and 1; why: end raw index later reads end, so _escaped_path_suffix must retain the computed value under that name. + end = raw_index + 1 + # What: compute value from raw path and raw index; why: if value ord later reads value, so _escaped_path_suffix must retain the computed value under that name. + value = raw_path[raw_index] + # What: gate on value and ord before raw index and len and raw path; why: _escaped_path_suffix admits raw index and len and raw path only for this predicate and excludes the opposite state. + if value == ord("%"): + # What: gate on raw index and len and raw path before the computed value; why: _escaped_path_suffix admits the computed value only for this predicate and excludes the opposite state. + if raw_index + 3 > len(raw_path): + # What: reject the malformed or mismatched escaped path; why: the upstream proxy returns no suffix so its caller emits HTTP 400 instead of forwarding ambiguous path bytes. + return None + # What: establish the handler boundary for the protected operation; why: _escaped_path_suffix routes failures to value error while preserving cleanup and success flow. + try: + # What: compute value from int and raw path and raw index and 16 and 1; why: if value prefix prefix index later reads value, so _escaped_path_suffix must retain the computed value under that name. + value = int(raw_path[raw_index + 1:raw_index + 3], 16) + # What: handle value error by return; why: _escaped_path_suffix converts that failure into this concrete recovery, response, or cleanup behavior. + except ValueError: + # What: reject the malformed or mismatched escaped path; why: the upstream proxy returns no suffix so its caller emits HTTP 400 instead of forwarding ambiguous path bytes. + return None + # What: compute end from raw index and 3; why: raw index end later reads end, so _escaped_path_suffix must retain the computed value under that name. + end = raw_index + 3 + # What: gate on value and prefix and prefix index before the computed value; why: _escaped_path_suffix admits the computed value only for this predicate and excludes the opposite state. + if value != prefix[prefix_index]: + # What: reject the malformed or mismatched escaped path; why: the upstream proxy returns no suffix so its caller emits HTTP 400 instead of forwarding ambiguous path bytes. + return None + # What: compute raw index from end; why: suffix raw path raw index later reads raw index, so _escaped_path_suffix must retain the computed value under that name. + raw_index = end + # What: compute prefix index from 1; why: if prefix index len prefix later reads prefix index, so _escaped_path_suffix must retain the computed value under that name. + prefix_index += 1 + # What: gate on prefix index and len and prefix before the computed value; why: _escaped_path_suffix admits the computed value only for this predicate and excludes the opposite state. + if prefix_index != len(prefix): + # What: reject the malformed or mismatched escaped path; why: the upstream proxy returns no suffix so its caller emits HTTP 400 instead of forwarding ambiguous path bytes. + return None + # What: compute suffix from raw path and raw index; why: return suffix decode ascii later reads suffix, so _escaped_path_suffix must retain the computed value under that name. + suffix = raw_path[raw_index:] + # What: establish the handler boundary for the protected operation; why: _escaped_path_suffix routes failures to unicode decode error while preserving cleanup and success flow. + try: + # What: return decode and suffix and ascii from _escaped_path_suffix; why: _escaped_path_suffix exposes decode and suffix and ascii so its caller can continue with the function\'s computed outcome. + return suffix.decode("ascii") + # What: handle unicode decode error by return quote from bytes suffix safe value; why: _escaped_path_suffix converts that failure into this concrete recovery, response, or cleanup behavior. + except UnicodeDecodeError: + # What: return quote from bytes and suffix and value from _escaped_path_suffix; why: _escaped_path_suffix exposes quote from bytes and suffix and value so its caller can continue with the function\'s computed outcome. + return quote_from_bytes(suffix, safe="/%:@!$&'()*+,;=-._~") + + +# What: define _extract_api_key around authorization and x api key; why: its direct callers call _extract_api_key for extract api key and rely on this exact input and result contract. +def _extract_api_key(authorization: str | None, x_api_key: str | None) -> str | None: + """Apply the pinned Basic-password, Bearer, then x-api-key contract.""" + # What: document apply the pinned basic password bearer then in the _extract_api_key docstring; why: introspection and maintainers read this exact docstring fragment to understand extract api key behavior without executing it. + # What: compute bearer key from the named fixture input; why: bearer key credentials or later reads bearer key, so _extract_api_key must retain the computed value under that name. + bearer_key = None + # What: compute basic key from the named fixture input; why: basic key decoded split or later reads basic key, so _extract_api_key must retain the computed value under that name. + basic_key = None + # What: gate on authorization before scheme and separator and credentials and partition and authorization; why: _extract_api_key admits scheme and separator and credentials and partition and authorization only for this predicate and excludes the opposite state. + if authorization: + # What: compute scheme and separator and credentials from partition and authorization and value; why: if separator and scheme lower bearer later reads scheme and separator and credentials, so _extract_api_key must retain the computed value under that name. + scheme, separator, credentials = authorization.partition(" ") + # What: gate on separator and lower and scheme before bearer key and credentials; why: _extract_api_key admits bearer key and credentials only for this predicate and excludes the opposite state. + if separator and scheme.lower() == "bearer": + # What: compute bearer key from credentials; why: return basic key or bearer key or x api key later reads bearer key, so _extract_api_key must retain the computed value under that name. + bearer_key = credentials or None + # What: gate on separator and lower and scheme before decoded and decode and error and value error and basic key; why: _extract_api_key admits decoded and decode and error and value error and basic key only for this predicate and excludes the opposite state. + elif separator and scheme.lower() == "basic": + # What: establish the handler boundary for the protected operation; why: _extract_api_key routes failures to error and value error and binascii while preserving cleanup and success flow. + try: + # What: compute decoded from decode and b64decode and credentials and base64 and utf 8; why: if in decoded later reads decoded, so _extract_api_key must retain the computed value under that name. + decoded = base64.b64decode(credentials, validate=True).decode( + # What: supply errors to operation.decode; why: _extract_api_key binds this surrogateescape value to operation.decode's errors input. + "utf-8", errors="surrogateescape" + # What: complete the operation.decode call with errors; why: _extract_api_key groups the supplied clauses as one operation.decode call before its value is consumed. + ) + # What: handle error and value error and binascii by pass; why: _extract_api_key converts that failure into this concrete recovery, response, or cleanup behavior. + except (binascii.Error, ValueError): + # What: ignore the anticipated exception handled by this branch; why: _extract_api_key continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: select the remaining branch that performs if in decoded; why: _extract_api_key covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: gate on decoded before basic key and split and decoded; why: _extract_api_key admits basic key and split and decoded only for this predicate and excludes the opposite state. + if ":" in decoded: + # What: compute basic key from split and decoded and 1 and value and 1; why: return basic key or bearer key or x api key later reads basic key, so _extract_api_key must retain the computed value under that name. + basic_key = decoded.split(":", 1)[1] or None + # What: return basic key and bearer key and x api key from _extract_api_key; why: _extract_api_key exposes basic key and bearer key and x api key so its caller can continue with the function\'s computed outcome. + return basic_key or bearer_key or x_api_key + + class StartBody(BaseModel): model: str port: int | None = None @@ -40,6 +203,32 @@ class SwitchBody(StartBody): force: bool = False +# What: define ProfileBody as the owner of its declared state; why: daemon callers use this class boundary so those methods share one profile body state invariant. +class ProfileBody(BaseModel): + # What: compute name from the named fixture input; why: name str later reads name, so app must retain the computed value under that name. + name: str + # What: compute force from false; why: return await run lifecycle pool manager stop bool later reads force, so app must retain the computed value under that name. + force: bool = False + + +# What: define RoutingProfileSelectionBody as the owner of its declared state; why: daemon callers use this class boundary so those methods share one routing profile selection body state invariant. +class RoutingProfileSelectionBody(BaseModel): + # What: compute name from the named fixture input; why: name str later reads name, so app must retain the computed value under that name. + name: str | None + + +# What: define RouterUnloadBody as the owner of its declared state; why: daemon callers use this class boundary so those methods share one router unload body state invariant. +class RouterUnloadBody(BaseModel): + # What: compute name from the named fixture input; why: name str later reads name, so app must retain the computed value under that name. + name: str | None = None + + +# What: define RouterLoadBody as the owner of its declared state; why: daemon callers use this class boundary so those methods share one router load body state invariant. +class RouterLoadBody(BaseModel): + # What: compute name from the named fixture input; why: html lang en head meta charset later reads name, so app must retain the computed value under that name. + name: str + + class AccountingAckBody(BaseModel): receiptId: str @@ -58,6 +247,42 @@ class BenchBody(BaseModel): args: list[str] = [] +# Deliberately dependency-free management view. It never embeds catalog data, +# local paths, tokens, or machine identifiers in the initial HTML response; +# authenticated JSON API calls populate the view only after the operator enters +# a bearer token for this browser session. +# What: embed the exact router ui doctype html router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact html lang en head meta charset router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact title free token swap title style router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact body font px system ui sans serif max width router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact style head body h1 free token swap router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact div class row label bearer key router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact h2 status h2 pre id status router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact h2 activity h2 p rows are router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact script router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact const id document get element by id id headers authorization router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact async function api path opt let router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact function show id value id text content router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact async function refresh try let s router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact refresh onclick refresh reload onclick async router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +# What: embed the exact script body html router-interface fragment; why: the router UI consumer receives this fragment verbatim through router ui, preserving browser markup, style, or script behavior. +_ROUTER_UI = """ + +FreeToken swap

FreeToken swap

Enter a router bearer key to inspect or control this local daemon. The key is kept only in this page's memory.

+
+

Status

Not loaded.

Models

Hardware

Not loaded.

Performance history

Not loaded.
+

Activity

Rows are body-free. Captures may contain prompts and are fetched only when selected.

No capture selected.
+""" + + def _bench_profile_path(gpu_uuid: str | None) -> str | None: # per-GPU profiles and no torch here: the serve's own card when its --gpu names one, else the newest file from freetoken.moe.bench_profile import default_profile_path, latest_profile_path # torch-free @@ -127,12 +352,140 @@ def build_app( started_wall: float = 0.0, wall_now: Callable[[], float] | None = None, shutdown_hook: Callable[[], None] | None = None, + # What: declare the catalog input for build_app; why: build_app consumes catalog during catalog catalog or model catalog empty, so callers must bind it with the other signature inputs. + catalog: ModelCatalog | None = None, + # What: declare the router input for build_app; why: build_app consumes router during router router or routing coordinator, so callers must bind it with the other signature inputs. + router: RoutingCoordinator | None = None, + # What: declare the catalog path input for build_app; why: build_app consumes catalog path during if not catalog path, so callers must bind it with the other signature inputs. + catalog_path: str | None = None, + # What: declare the router ring input for build_app; why: build_app consumes router ring during router ring router ring or log ring capacity, so callers must bind it with the other signature inputs. + router_ring: LogRing | None = None, + # What: declare the catalog watch interval s input for build_app; why: build_app consumes catalog watch interval s during interval s catalog watch interval s if catalog watch interval s else, so callers must bind it with the other signature inputs. + catalog_watch_interval_s: float = 0.0, + # What: declare the activity path input for build_app; why: build_app consumes activity path during activity path, so callers must bind it with the other signature inputs. + activity_path: str | None = None, ) -> FastAPI: import time as _time wall_now = wall_now or _time.time app = FastAPI(title="FreeToken daemon", version=DAEMON_VERSION) + # What: register cors_preflight as HTTP middleware; why: every matching request passes through cors_preflight before the route handler so authentication or accounting wraps the request. + @app.middleware("http") + # What: define cors_preflight around request and call next; why: the registered API client call cors_preflight for cors preflight and rely on this exact input and result contract. + async def cors_preflight(request: Request, call_next): + # Match the pinned compatibility server's side-effect-free global + # preflight contract. Actual requests still pass through normal route + # authentication and lifecycle ownership. + # What: gate on method and request before call next and request; why: cors_preflight admits call next and request only for this predicate and excludes the opposite state. + if request.method != "OPTIONS": + # What: return call next and request from cors_preflight; why: cors_preflight exposes call next and request so its caller can continue with the function\'s computed outcome. + return await call_next(request) + # What: return response and cors request headers and get and headers from cors_preflight; why: cors_preflight exposes response and cors request headers and get and headers so its caller can continue with the function\'s computed outcome. + return Response( + # What: supply status code to Response; why: cors_preflight binds this 204 value to Response's status code input. + status_code=204, + # What: supply headers to Response; why: cors_preflight binds this cors request headers and get and headers and request and access control allow origin value to Response's headers input. + headers={ + # What: map the access control allow origin field as value; why: cors_preflight carries access control allow origin into "Access-Control-Allow-Origin": "*". + "Access-Control-Allow-Origin": "*", + # What: map the access control allow methods field as get and post and put and patch; why: cors_preflight carries access control allow methods into "Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS". + "Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS", + # What: map the access control allow headers field as cors request headers and get and headers and request and access control request headers; why: cors_preflight carries access control allow headers into "Access-Control-Allow-Headers": _cors_request_headers(. + "Access-Control-Allow-Headers": _cors_request_headers( + # What: call request.headers.get with access control request headers; why: cors_preflight consumes the request.headers.get return value while evaluating request.headers.get("access-control-request-headers"). + request.headers.get("access-control-request-headers") + # What: complete the _cors_request_headers call with get; why: cors_preflight groups the supplied clauses as one _cors_request_headers call before its value is consumed. + ), + # What: map the access control max age field as 86400; why: cors_preflight carries access control max age into "Access-Control-Max-Age": "86400". + "Access-Control-Max-Age": "86400", + # What: complete the enclosing predicate mapping with access control allow origin and access control allow methods and access control allow headers and access control max age; why: cors_preflight groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + }, + # What: complete the Response call with status code and headers; why: cors_preflight groups the supplied clauses as one Response call before its value is consumed. + ) + # What: compute catalog from catalog and empty and model catalog; why: manager catalog probe default port default serve port later reads catalog, so build_app must retain the computed value under that name. + catalog = catalog or ModelCatalog.empty() + # What: compute router from router and routing coordinator and manager and catalog; why: app router add event handler startup start performance monitor later reads router, so build_app must retain the computed value under that name. + router = router or RoutingCoordinator( + # What: supply default port to RoutingCoordinator; why: build_app binds this default serve port value to RoutingCoordinator's default port input. + manager, catalog, probe, default_port=default_serve_port + # What: complete the RoutingCoordinator call with default port; why: build_app groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + # Keep router events separate from captured engine stdout. Apart from + # making an operator's engine-log view useful, this prevents a noisy child + # from evicting the bounded lifecycle/proxy audit trail. The event payload + # deliberately contains no headers, query strings, request body, or model + # path: those may carry credentials or prompts. + # What: compute router ring from router ring and log ring and 1000; why: app state router ring router ring later reads router ring, so build_app must retain the computed value under that name. + router_ring = router_ring or LogRing(capacity=1000) + # What: compute router ring from router ring; why: router ring append later reads router ring, so build_app must retain the computed value under that name. + app.state.router_ring = router_ring + # What: compute activity store from activity store and activity max entries and activity path and activity session headers; why: app state activity store activity store later reads activity store, so build_app must retain the computed value under that name. + activity_store = ActivityStore( + # What: apply the catalog settings activity max entries portion of activity store; why: build_app uses this clause to evaluate activity store as one grouped value. + catalog.settings.activity_max_entries, + # What: apply the catalog settings capture buffer mb portion of activity store; why: build_app uses this clause to evaluate activity store as one grouped value. + catalog.settings.capture_buffer_mb * 1024 * 1024, + # What: apply the activity path portion of activity store; why: build_app uses this clause to evaluate activity store as one grouped value. + activity_path, + # What: apply the catalog settings activity session headers portion of activity store; why: build_app uses this clause to evaluate activity store as one grouped value. + catalog.settings.activity_session_headers, + # What: complete the ActivityStore call with activity max entries and capture buffer mb and activity path and activity session headers; why: build_app groups the supplied clauses as one ActivityStore call before its value is consumed. + ) + # What: compute activity store from activity store; why: activity store reconfigure later reads activity store, so build_app must retain the computed value under that name. + app.state.activity_store = activity_store + # What: compute performance monitor from performance monitor and performance every s and performance disabled and wall now; why: app state performance monitor performance monitor later reads performance monitor, so build_app must retain the computed value under that name. + performance_monitor = PerformanceMonitor( + # What: call footprint_fn with get and status and manager and pid; why: build_app invokes footprint_fn while performing every s catalog settings performance every s; the call advances that operation through its result or side effect. + lambda: footprint_fn(manager.status().get("pid")), + # What: supply every s to PerformanceMonitor; why: build_app binds this performance every s and settings and catalog value to PerformanceMonitor's every s input. + every_s=catalog.settings.performance_every_s, + # What: supply disabled to PerformanceMonitor; why: build_app binds this performance disabled and settings and catalog value to PerformanceMonitor's disabled input. + disabled=catalog.settings.performance_disabled, + # What: supply wall now to PerformanceMonitor; why: build_app binds this wall now value to PerformanceMonitor's wall now input. + wall_now=wall_now, + # What: complete the PerformanceMonitor call with every s and disabled and wall now; why: build_app groups the supplied clauses as one PerformanceMonitor call before its value is consumed. + ) + # What: compute performance monitor from performance monitor; why: performance monitor start later reads performance monitor, so build_app must retain the computed value under that name. + app.state.performance_monitor = performance_monitor + + # What: define _start_performance_monitor around the current object state; why: its direct callers call _start_performance_monitor for start performance monitor and rely on this exact input and result contract. + async def _start_performance_monitor() -> None: + # What: call performance_monitor.start with the declared inputs; why: _start_performance_monitor invokes performance_monitor.start while performing the enclosing return; the call advances that operation through its result or side effect. + performance_monitor.start() + + # What: define _stop_performance_monitor around the current object state; why: its direct callers call _stop_performance_monitor for stop performance monitor and rely on this exact input and result contract. + async def _stop_performance_monitor() -> None: + # What: call performance_monitor.stop with the declared inputs; why: _stop_performance_monitor invokes performance_monitor.stop while performing the enclosing return; the call advances that operation through its result or side effect. + performance_monitor.stop() + + # What: preserve the exact app router add event handler startup start performance monitor literal fragment; why: build_app passes this fragment verbatim through app.router.add_event_handler("startup", _start_performance_monitor), because changing it would alter a protocol payload, serialized fixture, or public mes. + app.router.add_event_handler("startup", _start_performance_monitor) + # What: preserve the exact app router add event handler shutdown stop performance monitor literal fragment; why: build_app passes this fragment verbatim through app.router.add_event_handler("shutdown", _stop_performance_monitor), because changing it would alter a protocol payload, serialized fixture, or public mes. + app.router.add_event_handler("shutdown", _stop_performance_monitor) + # What: compute inflight lock from lock and threading; why: with inflight lock later reads inflight lock, so build_app must retain the computed value under that name. + inflight_lock = threading.Lock() + # What: initialize inflight as an empty runtime accumulator; why: build_app appends or maps entries into it during inflight request id before consuming the aggregate. + inflight: dict[str, dict] = {} + # What: initialize request reservations as an empty runtime accumulator; why: build_app appends or maps entries into it during if request id in request reservations before consuming the aggregate. + request_reservations: dict[str, dict] = {} + # What: compute watch stop from event and threading; why: app state catalog watch stop watch stop later reads watch stop, so build_app must retain the computed value under that name. + watch_stop = threading.Event() + # What: compute watch lock from lock and threading; why: with watch lock later reads watch lock, so build_app must retain the computed value under that name. + watch_lock = threading.Lock() + # What: compute watch state from bool and catalog watch interval s and catalog path and enabled and interval s; why: watch state last result result later reads watch state, so build_app must retain the computed value under that name. + watch_state = { + # What: map the enabled field as bool and catalog path and catalog watch interval s and 0; why: build_app carries enabled through watch state into watch state last result result. + "enabled": bool(catalog_path and catalog_watch_interval_s > 0), + # What: map the interval s field as catalog watch interval s and 0; why: build_app carries interval s through watch state into watch state last result result. + "intervalS": catalog_watch_interval_s if catalog_watch_interval_s > 0 else None, + # What: map the last result field as the fixture input; why: build_app carries last result through watch state into watch state last result result. + "lastResult": None, + # What: complete the watch_state mapping with enabled and interval s and last result; why: build_app groups the supplied clauses as one watch_state mapping before its value is consumed. + } + # What: compute catalog watch stop from watch stop; why: the enclosing return or state update later reads catalog watch stop, so build_app must retain the computed value under that name. + app.state.catalog_watch_stop = watch_stop + if shutdown_hook is not None: @app.on_event("shutdown") @@ -145,22 +498,255 @@ async def _on_shutdown() -> None: except Exception: # noqa: BLE001 pass - def require_token(x_ft_token: str | None = Header(default=None)) -> None: - if token is not None and x_ft_token != token: - raise HTTPException(status_code=401, detail="invalid or missing X-FT-Token") + # What: define require_token around x ft token and authorization and x api key; why: its direct callers call require_token for require token and rely on this exact input and result contract. + def require_token( + # What: declare the x ft token input for require_token; why: require_token consumes x ft token during if x ft token token, so callers must bind it with the other signature inputs. + x_ft_token: str | None = Header(default=None), + # What: declare the authorization input for require_token; why: require_token consumes authorization during supplied extract api key authorization x api key, so callers must bind it with the other signature inputs. + authorization: str | None = Header(default=None), + # What: declare the x api key input for require_token; why: require_token consumes x api key during supplied extract api key authorization x api key, so callers must bind it with the other signature inputs. + x_api_key: str | None = Header(default=None), + # What: complete the enclosing predicate with group delimiter; why: require_token groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> None: + # What: gate on token before x ft token and token and httpexception; why: require_token admits x ft token and token and httpexception only for this predicate and excludes the opposite state. + if token is not None: + # What: gate on x ft token and token before httpexception; why: require_token admits httpexception only for this predicate and excludes the opposite state. + if x_ft_token != token: + # What: raise HTTPException for the caller; why: require_token stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=401, detail="invalid or missing X-FT-Token") + # What: return no value from require_token; why: require_token returns no value to callers that depend on its completed result. + return + # What: compute keys from api keys and settings and catalog and router; why: if keys later reads keys, so require_token must retain the computed value under that name. + keys = router.catalog.settings.api_keys + # What: gate on keys before supplied and extract api key and authorization and x api key; why: require_token admits supplied and extract api key and authorization and x api key only for this predicate and excludes the opposite state. + if keys: + # What: compute supplied from extract api key and authorization and x api key; why: if supplied not in keys later reads supplied, so require_token must retain the computed value under that name. + supplied = _extract_api_key(authorization, x_api_key) + # What: gate on supplied and keys before httpexception; why: require_token admits httpexception only for this predicate and excludes the opposite state. + if supplied not in keys: + # What: raise HTTPException for the caller; why: require_token stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException( + # What: supply status code to HTTPException; why: require_token binds this 401 value to HTTPException's status code input. + status_code=401, + # What: supply detail to HTTPException; why: require_token binds this invalid and or and missing and api value to HTTPException's detail input. + detail="invalid or missing API key", + # What: map the www authenticate field as basic and realm and freetoken swap; why: require_token carries www authenticate into headers={"WWW-Authenticate": 'Basic realm="freetoken-swap"'}. + headers={"WWW-Authenticate": 'Basic realm="freetoken-swap"'}, + # What: complete the HTTPException call with status code and detail and headers; why: require_token groups the supplied clauses as one HTTPException call before its value is consumed. + ) auth = [Depends(require_token)] - async def run(pool: ThreadPoolExecutor, fn, *args): + # What: define require_router_key around authorization and x api key; why: its direct callers call require_router_key for require router key and rely on this exact input and result contract. + def require_router_key( + # What: declare the authorization input for require_router_key; why: require_router_key consumes authorization during supplied extract api key authorization x api key, so callers must bind it with the other signature inputs. + authorization: str | None = Header(default=None), + # What: declare the x api key input for require_router_key; why: require_router_key consumes x api key during supplied extract api key authorization x api key, so callers must bind it with the other signature inputs. + x_api_key: str | None = Header(default=None), + # What: complete the enclosing predicate with group delimiter; why: require_router_key groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> None: + # What: compute keys from api keys and settings and catalog and router; why: if not keys later reads keys, so require_router_key must retain the computed value under that name. + keys = router.catalog.settings.api_keys + # What: gate on keys before the computed value; why: require_router_key admits the computed value only for this predicate and excludes the opposite state. + if not keys: + # What: return no value from require_router_key; why: require_router_key returns no value to callers that depend on its completed result. + return + # What: compute supplied from extract api key and authorization and x api key; why: if supplied not in keys later reads supplied, so require_router_key must retain the computed value under that name. + supplied = _extract_api_key(authorization, x_api_key) + # What: gate on supplied and keys before httpexception; why: require_router_key admits httpexception only for this predicate and excludes the opposite state. + if supplied not in keys: + # What: raise HTTPException for the caller; why: require_router_key stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException( + # What: supply status code to HTTPException; why: require_router_key binds this 401 value to HTTPException's status code input. + status_code=401, + # What: supply detail to HTTPException; why: require_router_key binds this invalid and or and missing and api value to HTTPException's detail input. + detail="invalid or missing API key", + # What: map the www authenticate field as basic and realm and freetoken swap; why: require_router_key carries www authenticate into headers={"WWW-Authenticate": 'Basic realm="freetoken-swap"'}. + headers={"WWW-Authenticate": 'Basic realm="freetoken-swap"'}, + # What: complete the HTTPException call with status code and detail and headers; why: require_router_key groups the supplied clauses as one HTTPException call before its value is consumed. + ) + + # What: define run around pool and fn; why: its direct callers call run for run and rely on this exact input and result contract. + async def run(pool: ThreadPoolExecutor, fn, *args, **kwargs): loop = asyncio.get_running_loop() - return await loop.run_in_executor(pool, functools.partial(fn, *args)) + # What: return run in executor and pool and loop and partial from run; why: run exposes run in executor and pool and loop and partial so its caller can continue with the function\'s computed outcome. + return await loop.run_in_executor(pool, functools.partial(fn, *args, **kwargs)) + + # What: define run_to_completion around operation; why: its direct callers call run_to_completion for run to completion and rely on this exact input and result contract. + async def run_to_completion(operation): + """Defer caller cancellation until an ownership transaction is terminal.""" + # What: document defer caller cancellation until an ownership in the run_to_completion docstring; why: introspection and maintainers read this exact docstring fragment to understand run to completion behavior without executing it. + # What: compute task from create task and asyncio and operation; why: return await asyncio shield task later reads task, so run_to_completion must retain the computed value under that name. + task = asyncio.create_task(operation()) + # What: establish the handler boundary for the protected operation; why: run_to_completion routes failures to cancelled error and asyncio while preserving cleanup and success flow. + try: + # What: return shield and task and asyncio from run_to_completion; why: run_to_completion exposes shield and task and asyncio so its caller can continue with the function\'s computed outcome. + return await asyncio.shield(task) + # What: handle cancelled error and asyncio by while true; why: run_to_completion converts that failure into this concrete recovery, response, or cleanup behavior. + except asyncio.CancelledError: + # What: iterate across the computed value to perform cancelled error and base exception and asyncio and shield and task; why: run_to_completion repeats the body only while or for the loop header admits an iteration. + while True: + # What: establish the handler boundary for the protected operation; why: run_to_completion routes failures to cancelled error and asyncio and base exception while preserving cleanup and success flow. + try: + # What: call asyncio.shield with task; why: run_to_completion invokes asyncio.shield while performing except asyncio cancelled error; the call advances that operation through its result or side effect. + await asyncio.shield(task) + # What: handle cancelled error and asyncio by continue; why: run_to_completion converts that failure into this concrete recovery, response, or cleanup behavior. + except asyncio.CancelledError: + # What: apply the continue portion of the enclosing predicate; why: this clause remains in run_to_completion\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: handle base exception by break; why: run_to_completion converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: apply the break portion of the enclosing predicate; why: this clause remains in run_to_completion\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: select the remaining branch that performs break; why: run_to_completion covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: apply the break portion of the enclosing predicate; why: this clause remains in run_to_completion\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: re-propagate the active failure to the caller; why: run_to_completion stops this rejected path before it can mutate state, dispatch work, or report success. + raise + + # What: define run_manual_transaction around operation and preempt manual; why: its direct callers call run_manual_transaction for run manual transaction and rely on this exact input and result contract. + async def run_manual_transaction(operation, *, preempt_manual: bool = False): + """Keep manual ownership until the complete transaction reaches a terminal state.""" + # What: document keep manual ownership until the complete in the run_manual_transaction docstring; why: introspection and maintainers read this exact docstring fragment to understand run manual transaction behavior without executing it. + # What: compute owner from begin manual lifecycle and preempt manual; why: router end manual lifecycle owner later reads owner, so run_manual_transaction must retain the computed value under that name. + owner = begin_manual_lifecycle(preempt_manual=preempt_manual) + # What: establish the handler boundary for the protected operation; why: run_manual_transaction routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: return run to completion and operation from run_manual_transaction; why: run_manual_transaction exposes run to completion and operation so its caller can continue with the function\'s computed outcome. + return await run_to_completion(operation) + # What: run router end manual lifecycle owner on every exit path; why: run_manual_transaction performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: call router.end_manual_lifecycle with owner; why: run_manual_transaction invokes router.end_manual_lifecycle while performing the enclosing return; the call advances that operation through its result or side effect. + router.end_manual_lifecycle(owner) + + # What: define acquire_route around name and cancellation and on reserved and apply loading policy and apply routing profile; why: its direct callers call acquire_route for acquire route and rely on this exact input and result contract. + async def acquire_route( + # What: declare the name input for acquire_route; why: acquire_route consumes name during name, so callers must bind it with the other signature inputs. + name: str, + # What: declare the cancellation input for acquire_route; why: acquire_route consumes cancellation during cancellation cancellation or threading event, so callers must bind it with the other signature inputs. + cancellation: threading.Event | None = None, + # What: declare the on reserved input for acquire_route; why: acquire_route consumes on reserved during on reserved, so callers must bind it with the other signature inputs. + on_reserved: Callable[[bool, int], None] | None = None, + # What: mark the remaining parameters as keyword-only; why: acquire_route prevents callers from confusing adjacent lifecycle and timing arguments. + *, + # What: declare the apply loading policy input for acquire_route; why: acquire_route consumes apply loading policy during apply loading policy apply loading policy, so callers must bind it with the other signature inputs. + apply_loading_policy: bool = False, + # What: declare the apply routing profile input for acquire_route; why: acquire_route consumes apply routing profile during apply routing profile apply routing profile, so callers must bind it with the other signature inputs. + apply_routing_profile: bool = True, + # What: complete the enclosing predicate with async def acquire route name str cancellation threading event on reserved callable; why: acquire_route groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + """Keep executor-side admission owned if its HTTP task is cancelled.""" + # What: document keep executor side admission owned if its in the acquire_route docstring; why: introspection and maintainers read this exact docstring fragment to understand acquire route behavior without executing it. + # What: compute loop from get running loop and asyncio; why: future loop run in executor later reads loop, so acquire_route must retain the computed value under that name. + loop = asyncio.get_running_loop() + # What: compute cancellation from cancellation and event and threading; why: cancellation later reads cancellation, so acquire_route must retain the computed value under that name. + cancellation = cancellation or threading.Event() + # What: compute future from run in executor and lifecycle pool and loop and partial; why: shielded asyncio shield future later reads future, so acquire_route must retain the computed value under that name. + future = loop.run_in_executor( + # What: apply the lifecycle pool portion of future; why: acquire_route uses this clause to evaluate future as one grouped value. + lifecycle_pool, + # What: call functools.partial with acquire and router and name and cancellation; why: acquire_route invokes functools.partial while performing router acquire; the call advances that operation through its result or side effect. + functools.partial( + # What: apply the router acquire portion of future; why: acquire_route uses this clause to evaluate future as one grouped value. + router.acquire, + # What: apply the name portion of future; why: acquire_route uses this clause to evaluate future as one grouped value. + name, + # What: apply the cancellation portion of future; why: acquire_route uses this clause to evaluate future as one grouped value. + cancellation, + # What: apply the on reserved portion of future; why: acquire_route uses this clause to evaluate future as one grouped value. + on_reserved, + # What: supply apply loading policy to functools.partial; why: acquire_route binds this apply loading policy value to functools.partial's apply loading policy input. + apply_loading_policy=apply_loading_policy, + # What: supply apply routing profile to functools.partial; why: acquire_route binds this apply routing profile value to functools.partial's apply routing profile input. + apply_routing_profile=apply_routing_profile, + # What: complete the functools.partial call with apply loading policy and apply routing profile; why: acquire_route groups the supplied clauses as one functools.partial call before its value is consumed. + ), + # What: complete the loop.run_in_executor call with lifecycle pool and partial; why: acquire_route groups the supplied clauses as one loop.run_in_executor call before its value is consumed. + ) + # What: compute shielded from shield and future and asyncio; why: return await shielded later reads shielded, so acquire_route must retain the computed value under that name. + shielded = asyncio.shield(future) + # What: establish the handler boundary for the protected operation; why: acquire_route routes failures to cancelled error and asyncio while preserving cleanup and success flow. + try: + # What: return shielded from acquire_route; why: acquire_route exposes shielded so its caller can continue with the function\'s computed outcome. + return await shielded + # What: handle cancelled error and asyncio by router cancel acquire cancellation; why: acquire_route converts that failure into this concrete recovery, response, or cleanup behavior. + except asyncio.CancelledError: + # What: call router.cancel_acquire with cancellation; why: acquire_route invokes router.cancel_acquire while performing try; the call advances that operation through its result or side effect. + router.cancel_acquire(cancellation) + # Retain ownership until the executor-side admission is terminal. + # This retrieves its expected RoutingError before the event loop + # can close and releases a lease if admission won the race. + # What: establish the handler boundary for the protected operation; why: acquire_route routes failures to base exception while preserving cleanup and success flow. + try: + # What: compute orphaned from shield and future and asyncio; why: orphaned release later reads orphaned, so acquire_route must retain the computed value under that name. + orphaned = await asyncio.shield(future) + # What: handle base exception by pass; why: acquire_route converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: ignore the anticipated exception handled by this branch; why: acquire_route continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: select the remaining branch that performs orphaned release; why: acquire_route covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: call orphaned.release with the declared inputs; why: acquire_route invokes orphaned.release while performing raise; the call advances that operation through its result or side effect. + orphaned.release() + # What: re-propagate the active failure to the caller; why: acquire_route stops this rejected path before it can mutate state, dispatch work, or report success. + raise + + # What: define connect_upstream around the current object state; why: its direct callers call connect_upstream for connect upstream and rely on this exact input and result contract. + async def connect_upstream(**kwargs): + """Close a connector result that arrives after its HTTP task disconnects.""" + # What: document close a connector result that arrives in the connect_upstream docstring; why: introspection and maintainers read this exact docstring fragment to understand connect upstream behavior without executing it. + # What: compute loop from get running loop and asyncio; why: future loop run in executor proxy pool functools partial open upstream kwargs later reads loop, so connect_upstream must retain the computed value under that name. + loop = asyncio.get_running_loop() + # What: compute future from run in executor and proxy pool and loop and partial; why: return await asyncio shield future later reads future, so connect_upstream must retain the computed value under that name. + future = loop.run_in_executor(proxy_pool, functools.partial(open_upstream, **kwargs)) + # What: establish the handler boundary for the protected operation; why: connect_upstream routes failures to cancelled error and asyncio while preserving cleanup and success flow. + try: + # What: return shield and future and asyncio from connect_upstream; why: connect_upstream exposes shield and future and asyncio so its caller can continue with the function\'s computed outcome. + return await asyncio.shield(future) + # What: handle cancelled error and asyncio by def close orphaned upstream done; why: connect_upstream converts that failure into this concrete recovery, response, or cleanup behavior. + except asyncio.CancelledError: + # What: define close_orphaned_upstream around done; why: its direct callers call close_orphaned_upstream for close orphaned upstream and rely on this exact input and result contract. + def close_orphaned_upstream(done) -> None: + # What: establish the handler boundary for the protected operation; why: close_orphaned_upstream routes failures to base exception while preserving cleanup and success flow. + try: + # What: compute orphaned from result and done; why: orphaned close later reads orphaned, so close_orphaned_upstream must retain the computed value under that name. + orphaned = done.result() + # What: handle base exception by return; why: close_orphaned_upstream converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: return no value from close_orphaned_upstream; why: close_orphaned_upstream returns no value to callers that depend on its completed result. + return + # What: call orphaned.close with the declared inputs; why: close_orphaned_upstream invokes orphaned.close while performing the enclosing return; the call advances that operation through its result or side effect. + orphaned.close() + + # What: call future.add_done_callback with close orphaned upstream; why: connect_upstream invokes future.add_done_callback while performing raise; the call advances that operation through its result or side effect. + future.add_done_callback(close_orphaned_upstream) + # What: re-propagate the active failure to the caller; why: connect_upstream stops this rejected path before it can mutate state, dispatch work, or report success. + raise def resolve_port(explicit: int | None) -> int: + # What: gate on explicit before allocate loopback port; why: resolve_port admits allocate loopback port only for this predicate and excludes the opposite state. + if explicit == 0: + # What: return allocate loopback port from resolve_port; why: resolve_port exposes allocate loopback port so its caller can continue with the function\'s computed outcome. + return allocate_loopback_port() if explicit is not None: return explicit st = manager.status() return st.get("port") or default_serve_port + # What: define begin_manual_lifecycle around preempt manual; why: its direct callers call begin_manual_lifecycle for begin manual lifecycle and rely on this exact input and result contract. + def begin_manual_lifecycle(*, preempt_manual: bool = False) -> object: + """Atomically keep legacy engine controls outside routed ownership.""" + # What: document atomically keep legacy engine controls outside in the begin_manual_lifecycle docstring; why: introspection and maintainers read this exact docstring fragment to understand begin manual lifecycle behavior without executing it. + # What: establish the handler boundary for the protected operation; why: begin_manual_lifecycle routes failures to routing error while preserving cleanup and success flow. + try: + # What: return begin manual lifecycle and router and preempt manual from begin_manual_lifecycle; why: begin_manual_lifecycle exposes begin manual lifecycle and router and preempt manual so its caller can continue with the function\'s computed outcome. + return router.begin_manual_lifecycle(preempt_manual=preempt_manual) + # What: handle routing error by raise httpexception status code exc status code detail str exc; why: begin_manual_lifecycle converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: raise HTTPException for the caller; why: begin_manual_lifecycle stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=exc.status_code, detail=str(exc)) from exc + def accounting_error(exc: Exception) -> JSONResponse: code = ( "accounting_outbox_failed" @@ -188,33 +774,2169 @@ async def health(): "engineRunning": bool(st.get("running")), } - # ---- engine lifecycle ---- + # What: register GET /ready on the application router; why: clients reach ready's handler only through this method-and-path binding. + @app.get("/ready") + # What: define the /ready control-plane handler; why: supervisors call this handler to learn whether the daemon control plane can accept requests, independent of engine stop receipts or token accounting. + async def ready(): + """Stable router readiness; it never starts a model as a probe side effect.""" + # What: document stable router readiness it never starts in the ready docstring; why: introspection and maintainers read this exact docstring fragment to understand ready behavior without executing it. + # What: compute accepting from run and proxy pool and is ready and probe; why: return jsonresponse status code if accepting else later reads accepting, so ready must retain the computed value under that name. + accepting = await run(proxy_pool, router.is_ready, probe) + # What: return HTTP 200 when accepting and 503 otherwise; why: supervisors use this status and ready boolean to decide whether the daemon control plane may receive traffic. + return JSONResponse(status_code=200 if accepting else 503, content={"ready": accepting}) + + # What: register GET /ui/ on the application router; why: clients reach router_ui's handler only through this method-and-path binding. + @app.get("/ui/") + # What: define router_ui around the current object state; why: the registered API client call router_ui for router ui and rely on this exact input and result contract. + async def router_ui(): + """A static shell; authenticated APIs supply all operational data.""" + # What: document a static shell authenticated apis supply in the router_ui docstring; why: introspection and maintainers read this exact docstring fragment to understand router ui behavior without executing it. + # What: return htmlresponse and router ui from router_ui; why: router_ui exposes htmlresponse and router ui so its caller can continue with the function\'s computed outcome. + return HTMLResponse(_ROUTER_UI) + + # What: define router_event around event; why: its direct callers call router_event for router event and rely on this exact input and result contract. + def router_event(event: str, **fields: Any) -> None: + # What: call router_ring.append with dumps and json and event and fields and event; why: router_event invokes router_ring.append while performing json dumps event event fields separators sort keys; the call advances that operation through its result or side effect. + router_ring.append( + # What: map the event field as event; why: router_event carries event into json.dumps({"event": event, **fields}, separators=(",", ":"), sort_keys=. + json.dumps({"event": event, **fields}, separators=(",", ":"), sort_keys=True), + # What: preserve the exact kind event literal fragment; why: router_event passes this fragment verbatim through kind="event", because changing it would alter a protocol payload, serialized fixture, or public message. + kind="event", + # What: supply ts to wall_now; why: router_event binds this wall now value to wall_now's ts input. + ts=wall_now(), + # What: complete the router_ring.append call with kind and ts; why: router_event groups the supplied clauses as one router_ring.append call before its value is consumed. + ) - @app.post("/engine/start", dependencies=auth) - async def engine_start(body: StartBody): - port = resolve_port(body.port) + # What: execute if catalog settings startup routing profile is not None; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + if catalog.settings.startup_routing_profile is not None: + # What: call router.set_active_routing_profile with startup routing profile and settings and catalog; why: build_app invokes router.set_active_routing_profile while performing if catalog settings preload model is not; the call advances that operation through its result or side effect. + router.set_active_routing_profile(catalog.settings.startup_routing_profile) + + # What: gate on preload model and settings and catalog before name and preload model and on event and settings and lease; why: build_app admits name and preload model and on event and settings and lease only for this predicate and excludes the opposite state. + if catalog.settings.preload_model is not None: + + # What: apply app.on_event behavior to _preload_model; why: Python attaches this named decorator's registration or descriptor semantics to _preload_model. + @app.on_event("startup") + # What: define _preload_model around the current object state; why: the registered API client call _preload_model for preload model and rely on this exact input and result contract. + async def _preload_model() -> None: + # What: compute name from preload model and settings and catalog; why: lease await acquire route name apply routing profile later reads name, so _preload_model must retain the computed value under that name. + name = catalog.settings.preload_model + # What: establish the handler boundary for the protected operation; why: _preload_model routes failures to base exception while preserving cleanup and success flow. + try: + # What: compute lease from acquire route and name and false; why: lease release later reads lease, so _preload_model must retain the computed value under that name. + lease = await acquire_route(name, apply_routing_profile=False) + # What: handle base exception by router event startup preload failed profile name code type exc; why: _preload_model converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException as exc: + # What: preserve the exact router event startup preload failed profile name code type literal fragment; why: _preload_model passes this fragment verbatim through router_event("startup_preload_failed", profile=name, code=type(exc).__na, because changing it would alter a protocol payload, serialized fixt. + router_event("startup_preload_failed", profile=name, code=type(exc).__name__) + # What: return no value from _preload_model; why: _preload_model returns no value to callers that depend on its completed result. + return + # What: call lease.release with the declared inputs; why: _preload_model invokes lease.release while performing router event startup preloaded profile lease profile name; the call advances that operation through its result or side effect. + lease.release() + # What: preserve the exact router event startup preloaded profile lease profile name literal fragment; why: _preload_model passes this fragment verbatim through router_event("startup_preloaded", profile=lease.profile.name), because changing it would alter a protocol payload, serialized fixture, or public m. + router_event("startup_preloaded", profile=lease.profile.name) + + # What: define record_watch around result; why: its direct callers call record_watch for record watch and rely on this exact input and result contract. + def record_watch(result: str) -> None: + # What: enter the watch lock managed context before watch state last result result; why: record_watch releases this resource or lock after watch state last result result on both success and failure paths. + with watch_lock: + # What: compute watch state entry from result; why: watch state last changed at wall now later reads watch state entry, so record_watch must retain the computed value under that name. + watch_state["lastResult"] = result + # What: compute watch state entry from wall now; why: the enclosing return or state update later reads watch state entry, so record_watch must retain the computed value under that name. + watch_state["lastChangedAt"] = wall_now() + + # What: define catalog_watch_snapshot around the current object state; why: its direct callers call catalog_watch_snapshot for catalog watch snapshot and rely on this exact input and result contract. + def catalog_watch_snapshot() -> dict: + # What: enter the watch lock managed context before return dict watch state; why: catalog_watch_snapshot releases this resource or lock after return dict watch state on both success and failure paths. + with watch_lock: + # What: return dict and watch state from catalog_watch_snapshot; why: catalog_watch_snapshot exposes dict and watch state so its caller can continue with the function\'s computed outcome. + return dict(watch_state) + + # What: define catalog_stamp around the current object state; why: its direct callers call catalog_stamp for catalog stamp and rely on this exact input and result contract. + def catalog_stamp() -> tuple[int, int] | None: + # What: gate on catalog path before the computed value; why: catalog_stamp admits the computed value only for this predicate and excludes the opposite state. + if not catalog_path: + # What: return no value from catalog_stamp; why: catalog_stamp returns no value to callers that depend on its completed result. + return None + # What: establish the handler boundary for the protected operation; why: catalog_stamp routes failures to oserror while preserving cleanup and success flow. try: - return await run(lifecycle_pool, manager.start, body.model, port, list(body.args)) - except Conflict as exc: - st = manager.status() + # What: compute stat from stat and catalog path and os; why: return stat st mtime ns stat st size later reads stat, so catalog_stamp must retain the computed value under that name. + stat = os.stat(catalog_path) + # What: handle oserror by return; why: catalog_stamp converts that failure into this concrete recovery, response, or cleanup behavior. + except OSError: + # What: return no value from catalog_stamp; why: catalog_stamp returns no value to callers that depend on its completed result. + return None + # What: return st mtime ns and st size and stat from catalog_stamp; why: catalog_stamp exposes st mtime ns and st size and stat so its caller can continue with the function\'s computed outcome. + return stat.st_mtime_ns, stat.st_size + + # What: define start_catalog_watcher around the current object state; why: its direct callers call start_catalog_watcher for start catalog watcher and rely on this exact input and result contract. + def start_catalog_watcher() -> None: + """Poll a local catalog safely; only a fully validated tree is installed. + + Polling keeps the daemon stdlib-only and cross-platform. A changed + malformed file is remembered until it changes again, avoiding a log + storm while an editor writes it. Active-profile redefinition is still + refused by the coordinator, so a watcher cannot steal a live child. + """ + # What: document poll a local catalog safely only in the start_catalog_watcher docstring; why: introspection and maintainers read this exact docstring fragment to understand start catalog watcher behavior without executing it. + # What: document polling keeps the daemon stdlib only and in the start_catalog_watcher docstring; why: introspection and maintainers read this exact docstring fragment to understand start catalog watcher behavior without executing it. + # What: document malformed file is remembered until it in the start_catalog_watcher docstring; why: introspection and maintainers read this exact docstring fragment to understand start catalog watcher behavior without executing it. + # What: document storm while an editor writes it in the start_catalog_watcher docstring; why: introspection and maintainers read this exact docstring fragment to understand start catalog watcher behavior without executing it. + # What: document refused by the coordinator so a in the start_catalog_watcher docstring; why: introspection and maintainers read this exact docstring fragment to understand start catalog watcher behavior without executing it. + # What: preserve the paragraph boundary in the the start_catalog_watcher docstring; why: introspection and maintainers read this paragraph break to understand start catalog watcher behavior without executing it. + # What: gate on watch state before the computed value; why: start_catalog_watcher admits the computed value only for this predicate and excludes the opposite state. + if not watch_state["enabled"]: + # What: return no value from start_catalog_watcher; why: start_catalog_watcher returns no value to callers that depend on its completed result. + return + # What: compute interval from float and catalog watch interval s; why: while not watch stop wait interval later reads interval, so start_catalog_watcher must retain the computed value under that name. + interval = float(catalog_watch_interval_s) + + # What: define watch around the current object state; why: its direct callers call watch for watch and rely on this exact input and result contract. + def watch() -> None: + # What: compute previous from catalog stamp; why: if changed previous later reads previous, so watch must retain the computed value under that name. + previous = catalog_stamp() + # What: iterate across wait and interval and watch stop to perform changed and catalog stamp; why: watch repeats the body only while or for the loop header admits an iteration. + while not watch_stop.wait(interval): + # What: compute changed from catalog stamp; why: if changed previous later reads changed, so watch must retain the computed value under that name. + changed = catalog_stamp() + # What: gate on changed and previous before the computed value; why: watch admits the computed value only for this predicate and excludes the opposite state. + if changed == previous: + # What: apply the continue portion of the enclosing predicate; why: this clause remains in watch\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: compute previous from changed; why: the enclosing return or state update later reads previous, so watch must retain the computed value under that name. + previous = changed + # What: establish the handler boundary for the protected operation; why: watch routes failures to catalog error and routing error while preserving cleanup and success flow. + try: + # What: compute replacement from load and catalog path and model catalog; why: router replace catalog replacement later reads replacement, so watch must retain the computed value under that name. + replacement = ModelCatalog.load(catalog_path) + # What: call router.replace_catalog with replacement; why: watch invokes router.replace_catalog while performing activity store reconfigure; the call advances that operation through its result or side effect. + router.replace_catalog(replacement) + # What: execute activity store reconfigure; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + activity_store.reconfigure( + # What: apply the replacement settings activity max entries portion of the enclosing predicate; why: this clause remains in watch\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.activity_max_entries, + # What: apply the replacement settings capture buffer mb portion of the enclosing predicate; why: this clause remains in watch\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.capture_buffer_mb * 1024 * 1024, + # What: apply the replacement settings activity session headers portion of the enclosing predicate; why: this clause remains in watch\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.activity_session_headers, + # What: complete the activity_store.reconfigure call with activity max entries and capture buffer mb and activity session headers; why: watch groups the supplied clauses as one activity_store.reconfigure call before its value is consumed. + ) + # What: call performance_monitor.reconfigure; why: app needs this line to preserve the surrounding expression or collection structure. + performance_monitor.reconfigure( + # What: apply the replacement settings performance every s portion of the enclosing predicate; why: this clause remains in watch\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.performance_every_s, + # What: apply the replacement settings performance disabled portion of the enclosing predicate; why: this clause remains in watch\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.performance_disabled, + # What: complete the performance_monitor.reconfigure call with performance every s and performance disabled; why: watch groups the supplied clauses as one performance_monitor.reconfigure call before its value is consumed. + ) + # What: handle catalog error by record watch invalid catalog; why: watch converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError: + # What: preserve the exact record watch invalid catalog literal fragment; why: watch passes this fragment verbatim through record_watch("invalid_catalog"), because changing it would alter a protocol payload, serialized fixture, or public message. + record_watch("invalid_catalog") + # What: preserve the exact router event catalog watch rejected code invalid catalog literal fragment; why: watch passes this fragment verbatim through router_event("catalog_watch_rejected", code="invalid_catalog"), because changing it would alter a protocol payload, serialized fixture, or public me. + router_event("catalog_watch_rejected", code="invalid_catalog") + # What: handle routing error by record watch exc code; why: watch converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: call record_watch with code and exc; why: watch invokes record_watch while performing router event catalog watch rejected code exc code; the call advances that operation through its result or side effect. + record_watch(exc.code) + # What: preserve the exact router event catalog watch rejected code exc code literal fragment; why: watch passes this fragment verbatim through router_event("catalog_watch_rejected", code=exc.code), because changing it would alter a protocol payload, serialized fixture, or public message. + router_event("catalog_watch_rejected", code=exc.code) + # What: select the remaining branch that performs record watch reloaded; why: watch covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: preserve the exact record watch reloaded literal fragment; why: watch passes this fragment verbatim through record_watch("reloaded"), because changing it would alter a protocol payload, serialized fixture, or public message. + record_watch("reloaded") + # What: preserve the exact router event catalog watch reloaded literal fragment; why: watch passes this fragment verbatim through router_event("catalog_watch_reloaded"), because changing it would alter a protocol payload, serialized fixture, or public message. + router_event("catalog_watch_reloaded") + + # What: compute thread from thread and threading and watch and ft daemon catalog watch and true; why: app state catalog watch thread thread later reads thread, so start_catalog_watcher must retain the computed value under that name. + thread = threading.Thread(target=watch, name="ft-daemon-catalog-watch", daemon=True) + # What: compute catalog watch thread from thread; why: the enclosing return or state update later reads catalog watch thread, so start_catalog_watcher must retain the computed value under that name. + app.state.catalog_watch_thread = thread + # What: call thread.start with the declared inputs; why: start_catalog_watcher invokes thread.start while performing the enclosing return; the call advances that operation through its result or side effect. + thread.start() + + # What: call start_catalog_watcher with the declared inputs; why: build_app invokes start_catalog_watcher while performing if watch state enabled; the call advances that operation through its result or side effect. + start_catalog_watcher() + + # What: gate on watch state before on event and set and app and watch stop; why: build_app admits on event and set and app and watch stop only for this predicate and excludes the opposite state. + if watch_state["enabled"]: + + # What: apply app.on_event behavior to _stop_catalog_watcher; why: Python attaches this named decorator's registration or descriptor semantics to _stop_catalog_watcher. + @app.on_event("shutdown") + # What: define _stop_catalog_watcher around the current object state; why: the registered API client call _stop_catalog_watcher for stop catalog watcher and rely on this exact input and result contract. + async def _stop_catalog_watcher() -> None: + # What: call watch_stop.set with the declared inputs; why: _stop_catalog_watcher invokes watch_stop.set while performing the enclosing return; the call advances that operation through its result or side effect. + watch_stop.set() + + # What: define forward_routed around request and model and path and query and body and apply request filters; why: its direct callers call forward_routed for forward routed and rely on this exact input and result contract. + async def forward_routed( + # What: declare the request input for forward_routed; why: forward_routed consumes request during safe route getattr request scope get route path request method, so callers must bind it with the other signature inputs. + request: Request, + # What: declare the model input for forward_routed; why: forward_routed consumes model during profile model, so callers must bind it with the other signature inputs. + model: str, + # What: mark the remaining parameters as keyword-only; why: forward_routed prevents callers from confusing adjacent lifecycle and timing arguments. + *, + # What: declare the path and query input for forward_routed; why: forward_routed consumes path and query during path and query path and query, so callers must bind it with the other signature inputs. + path_and_query: str, + # What: declare the body input for forward_routed; why: forward_routed consumes body during outbound body body, so callers must bind it with the other signature inputs. + body: bytes, + # What: declare the apply request filters input for forward_routed; why: forward_routed consumes apply request filters during if not apply request filters, so callers must bind it with the other signature inputs. + apply_request_filters: bool = False, + # What: complete the enclosing predicate with async def forward routed request request model str path and query str; why: forward_routed groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + """Select a configured model, then stream the engine response unchanged. + + The lease spans the full downstream iterator. If a client disconnects, + Starlette closes that iterator, which closes the upstream socket and + releases admission for the next model swap. + """ + # What: document select a configured model then stream in the forward_routed docstring; why: introspection and maintainers read this exact docstring fragment to understand forward routed behavior without executing it. + # What: document the lease spans the full downstream in the forward_routed docstring; why: introspection and maintainers read this exact docstring fragment to understand forward routed behavior without executing it. + # What: document starlette closes that iterator which closes in the forward_routed docstring; why: introspection and maintainers read this exact docstring fragment to understand forward routed behavior without executing it. + # What: document releases admission for the next model in the forward_routed docstring; why: introspection and maintainers read this exact docstring fragment to understand forward routed behavior without executing it. + # What: preserve the paragraph boundary in the the forward_routed docstring; why: introspection and maintainers read this paragraph break to understand forward routed behavior without executing it. + # What: compute started from monotonic and time; why: yield observed loading frame f done time monotonic later reads started, so forward_routed must retain the computed value under that name. + started = time.monotonic() + # What: compute request id from hex and get and headers and uuid4; why: if not request id isascii or not request id later reads request id, so forward_routed must retain the computed value under that name. + request_id = request.headers.get("x-ft-request-id") or uuid.uuid4().hex + # What: gate on request id and isascii and len before httpexception; why: forward_routed admits httpexception only for this predicate and excludes the opposite state. + if not request_id.isascii() or not request_id or len(request_id) > 128: + # What: raise HTTPException for the caller; why: forward_routed stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=400, detail="X-FT-Request-ID must be 1 to 128 ASCII characters") + # Use FastAPI's registered route template, not the concrete path or + # query. An upstream passthrough tail can itself contain a signed URL, + # opaque bearer-like value, or tenant identifier. + # What: compute safe route from getattr and method and get and request; why: router event request conflict profile model route safe route later reads safe route, so forward_routed must retain the computed value under that name. + safe_route = getattr(request.scope.get("route"), "path", request.method) + # What: compute admission cancellation from event and threading; why: cancellation admission cancellation later reads admission cancellation, so forward_routed must retain the computed value under that name. + admission_cancellation = threading.Event() + # What: compute capture limit from capture item limit and activity store; why: if capture limit and not capture overflow later reads capture limit, so forward_routed must retain the computed value under that name. + capture_limit = activity_store.capture_item_limit + # What: enter the inflight lock managed context before if request id in request reservations; why: forward_routed releases this resource or lock after if request id in request reservations on both success and failure paths. + with inflight_lock: + # What: gate on request id and request reservations before router event and model and safe route; why: forward_routed admits router event and model and safe route only for this predicate and excludes the opposite state. + if request_id in request_reservations: + # What: preserve the exact router event request conflict profile model route safe route literal fragment; why: forward_routed passes this fragment verbatim through router_event("request_conflict", profile=model, route=safe_route), because changing it would alter a protocol payload, serialized fixture. + router_event("request_conflict", profile=model, route=safe_route) + # What: return jsonresponse and 409 and error and message and type from forward_routed; why: forward_routed exposes jsonresponse and 409 and error and message and type so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: forward_routed binds this 409 value to JSONResponse's status code input. + status_code=409, + # What: map the error field as message and type and request and id and is; why: forward_routed carries error through content={"error": { into except exception as exc the lease must. + content={"error": { + # What: apply the message request id is already active portion of the enclosing predicate; why: this clause remains in forward_routed\'s enclosing expression so its grouping and evaluation order stay intact. + "message": "request id is already active", "type": "request_conflict", + # What: complete the enclosing predicate mapping with error; why: forward_routed groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + }}, + # What: complete the JSONResponse call with status code and content; why: forward_routed groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: compute request reservations entry from model and admission cancellation and profile and cancellation and; why: forward_routed consumes request reservations entry during cancelled before connect request reservations request id cancelled, so request reservations entry value receives the computed val. + request_reservations[request_id] = { + # What: map the profile field as model; why: forward_routed carries profile through request reservations entry into cancelled before connect request reservations request id cancelled. + "profile": model, + # What: map the cancellation field as admission cancellation; why: forward_routed carries cancellation through request reservations entry into cancelled before connect request reservations request id cancelled. + "cancellation": admission_cancellation, + # What: map the cancelled field as false; why: forward_routed carries cancelled through request reservations entry into cancelled before connect request reservations request id cancelled. + "cancelled": False, + # What: complete the request_reservations entry mapping with profile and cancellation and cancelled; why: forward_routed groups the supplied clauses as one request_reservations entry mapping before its value is consumed. + } + + # What: define filtered_body around route lease; why: its direct callers call filtered_body for filtered body and rely on this exact input and result contract. + def filtered_body(route_lease) -> bytes: + # What: gate on apply request filters before body; why: filtered_body admits body only for this predicate and excludes the opposite state. + if not apply_request_filters: + # What: return body from filtered_body; why: filtered_body exposes body so its caller can continue with the function\'s computed outcome. + return body + # What: compute profile from profile and route lease; why: profile drop fields later reads profile, so filtered_body must retain the computed value under that name. + profile = route_lease.profile + # What: compute target model from model id and model and route lease; why: requested model target model later reads target model, so filtered_body must retain the computed value under that name. + target_model = route_lease.model_id or model + # What: return filter request body and body and drop fields and set fields from filtered_body; why: filtered_body exposes filter request body and body and drop fields and set fields so its caller can continue with the function\'s computed outcome. + return filter_request_body( + # What: apply the body portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + body, + # What: apply the profile drop fields portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + profile.drop_fields, + # What: apply the profile set fields portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + profile.set_fields, + # What: apply the profile set fields by id portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + profile.set_fields_by_id, + # What: supply requested model to filter_request_body; why: filtered_body binds this target model value to filter_request_body's requested model input. + requested_model=target_model, + # What: supply rewrite model to filter_request_body; why: filtered_body binds this use model name and profile and target model and selector id value to filter_request_body's rewrite model input. + rewrite_model=( + # What: apply the profile use model name portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + profile.use_model_name + # What: apply the or portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + or ( + # What: apply the target model portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + target_model + # What: apply the if route lease selector id is not portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + if route_lease.selector_id is not None + # What: apply the or route lease routing profile id is not portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + or route_lease.routing_profile_id is not None + # What: apply the else portion of the enclosing predicate; why: this clause remains in filtered_body\'s enclosing expression so its grouping and evaluation order stay intact. + else None + # What: complete the filter_request_body call with requested model and rewrite model; why: filtered_body groups the supplied clauses as one filter_request_body call before its value is consumed. + ) + # What: complete the filter_request_body call with requested model and rewrite model; why: filtered_body groups the supplied clauses as one filter_request_body call before its value is consumed. + ), + # What: complete the filter_request_body call with requested model and rewrite model; why: filtered_body groups the supplied clauses as one filter_request_body call before its value is consumed. + ) + + # What: define lease_event_identity around route lease; why: its direct callers call lease_event_identity for lease event identity and rely on this exact input and result contract. + def lease_event_identity(route_lease) -> dict[str, str]: + # What: map the profile field as name and profile and route lease; why: lease_event_identity carries profile through identity into identity selector route lease selector id. + identity = {"profile": route_lease.profile.name} + # What: gate on selector id and route lease before selector id and identity and route lease; why: lease_event_identity admits selector id and identity and route lease only for this predicate and excludes the opposite state. + if route_lease.selector_id is not None: + # What: compute identity entry from selector id and route lease; why: identity target route lease model id later reads identity entry, so lease_event_identity must retain the computed value under that name. + identity["selector"] = route_lease.selector_id + # What: compute identity entry from model id and route lease; why: identity routing profile route lease routing profile id later reads identity entry, so lease_event_identity must retain the computed value under that name. + identity["target"] = route_lease.model_id + # What: gate on routing profile id and route lease before routing profile id and identity and route lease; why: lease_event_identity admits routing profile id and identity and route lease only for this predicate and excludes the opposite state. + if route_lease.routing_profile_id is not None: + # What: compute identity entry from routing profile id and route lease; why: identity pin route lease pin id later reads identity entry, so lease_event_identity must retain the computed value under that name. + identity["routingProfile"] = route_lease.routing_profile_id + # What: compute identity entry from pin id and route lease; why: identity setdefault target route lease model id later reads identity entry, so lease_event_identity must retain the computed value under that name. + identity["pin"] = route_lease.pin_id + # What: preserve the exact identity setdefault target route lease model id literal fragment; why: lease_event_identity passes this fragment verbatim through identity.setdefault("target", route_lease.model_id), because changing it would alter a protocol payload, serialized fixture, or public message. + identity.setdefault("target", route_lease.model_id) + # What: return identity from lease_event_identity; why: lease_event_identity exposes identity so its caller can continue with the function\'s computed outcome. + return identity + + # What: define loading_frame around text; why: its direct callers call loading_frame for loading frame and rely on this exact input and result contract. + def loading_frame(text: str) -> bytes: + # What: map the choices field as text and delta and reasoning content; why: loading_frame carries choices through payload into payload separators ensure ascii false. + payload = {"choices": [{"delta": {"reasoning_content": text}}]} + # What: return encode and dumps and payload and json and utf 8 from loading_frame; why: loading_frame exposes encode and dumps and payload and json and utf 8 so its caller can continue with the function\'s computed outcome. + return b"data: " + json.dumps( + # What: supply separators to operation.encode; why: loading_frame binds this value and value value to operation.encode's separators input. + payload, separators=(",", ":"), ensure_ascii=False + # What: apply the encode utf 8 b n n portion of the enclosing predicate; why: this clause remains in loading_frame\'s enclosing expression so its grouping and evaluation order stay intact. + ).encode("utf-8") + b"\n\n" + + # What: define loading_error around exc; why: its direct callers call loading_error for loading error and rely on this exact input and result contract. + def loading_error(exc: BaseException) -> bytes: + # What: compute error type from code and isinstance and exc and routing error and upstream unavailable; why: payload dict str any error message later reads error type, so loading_error must retain the computed value under that name. + error_type = exc.code if isinstance(exc, RoutingError) else "upstream_unavailable" + # What: map the error field as error type and str and exc and message and type; why: loading_error carries error through payload into payload recovery exc recovery. + payload: dict[str, Any] = {"error": {"message": str(exc), "type": error_type}} + # What: gate on isinstance and exc and routing error and recovery before recovery and payload and exc; why: loading_error admits recovery and payload and exc only for this predicate and excludes the opposite state. + if isinstance(exc, RoutingError) and exc.recovery is not None: + # What: compute payload entry from recovery and exc; why: json dumps payload separators encode utf 8 later reads payload entry, so loading_error must retain the computed value under that name. + payload["recovery"] = exc.recovery + # What: return encode and dumps and payload and json and utf 8 from loading_error; why: loading_error exposes encode and dumps and payload and json and utf 8 so its caller can continue with the function\'s computed outcome. + return ( + # What: apply the b data portion of the enclosing predicate; why: this clause remains in loading_error\'s enclosing expression so its grouping and evaluation order stay intact. + b"data: " + # What: supply separators to operation.encode; why: loading_error binds this value and value value to operation.encode's separators input. + + json.dumps(payload, separators=(",", ":")).encode("utf-8") + # What: apply the b n ndata done n n portion of the enclosing predicate; why: this clause remains in loading_error\'s enclosing expression so its grouping and evaluation order stay intact. + + b"\n\ndata: [DONE]\n\n" + # What: complete the loading_error signature with exc; why: loading_error groups the supplied clauses as one loading_error signature before its value is consumed. + ) + + # What: map the done field as false; why: forward_routed carries done through abandon state into if abandon state done. + abandon_state = {"done": False} + + # What: define abandon_loading_acquisition around acquisition and record cancellation; why: its direct callers call abandon_loading_acquisition for abandon loading acquisition and rely on this exact input and result contract. + def abandon_loading_acquisition( + # What: declare the acquisition input for abandon_loading_acquisition; why: abandon_loading_acquisition consumes acquisition during acquisition add done callback release if admitted, so callers must bind it with the other signature inputs. + acquisition: asyncio.Task, *, record_cancellation: bool = True + # What: complete the enclosing predicate with group delimiter; why: abandon_loading_acquisition groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> None: + """Wake an abandoned admission and release any lease it later returns.""" + # What: document wake an abandoned admission and release in the abandon_loading_acquisition docstring; why: introspection and maintainers read this exact docstring fragment to understand abandon loading acquisition behavior without executing it. + # What: gate on abandon state before the computed value; why: abandon_loading_acquisition admits the computed value only for this predicate and excludes the opposite state. + if abandon_state["done"]: + # What: return no value from abandon_loading_acquisition; why: abandon_loading_acquisition returns no value to callers that depend on its completed result. + return + # What: compute abandon state entry from true; why: the enclosing return or state update later reads abandon state entry, so abandon_loading_acquisition must retain the computed value under that name. + abandon_state["done"] = True + # What: call router.cancel_acquire with admission cancellation; why: abandon_loading_acquisition invokes router.cancel_acquire while performing if record cancellation; the call advances that operation through its result or side effect. + router.cancel_acquire(admission_cancellation) + # What: gate on record cancellation before record cancellation and router; why: abandon_loading_acquisition admits record cancellation and router only for this predicate and excludes the opposite state. + if record_cancellation: + # What: call router.record_cancellation with the declared inputs; why: abandon_loading_acquisition invokes router.record_cancellation while performing def release if admitted done asyncio task; the call advances that operation through its result or side effect. + router.record_cancellation() + + # What: define release_if_admitted around done; why: its direct callers call release_if_admitted for release if admitted and rely on this exact input and result contract. + def release_if_admitted(done: asyncio.Task) -> None: + # What: establish the handler boundary for the protected operation; why: release_if_admitted routes failures to base exception while preserving cleanup and success flow. + try: + # What: compute admitted from result and done; why: admitted release later reads admitted, so release_if_admitted must retain the computed value under that name. + admitted = done.result() + # What: handle base exception by return; why: release_if_admitted converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: return no value from release_if_admitted; why: release_if_admitted returns no value to callers that depend on its completed result. + return + # What: call admitted.release with the declared inputs; why: release_if_admitted invokes admitted.release while performing the enclosing return; the call advances that operation through its result or side effect. + admitted.release() + + # What: call acquisition.add_done_callback with release if admitted; why: abandon_loading_acquisition invokes acquisition.add_done_callback while performing the enclosing return; the call advances that operation through its result or side effect. + acquisition.add_done_callback(release_if_admitted) + + # What: define loading_stream around acquisition; why: its direct callers call loading_stream for loading stream and rely on this exact input and result contract. + async def loading_stream(acquisition: asyncio.Task): + """Bridge one admitted cold request into loading SSE, then its real response.""" + # What: document bridge one admitted cold request into in the loading_stream docstring; why: introspection and maintainers read this exact docstring fragment to understand loading stream behavior without executing it. + # What: compute lease from the named fixture input; why: lease acquisition result later reads lease, so loading_stream must retain the computed value under that name. + lease = None + # What: compute upstream from the named fixture input; why: request cancelled before upstream connection later reads upstream, so loading_stream must retain the computed value under that name. + upstream = None + # What: compute first byte at from the named fixture input; why: if first byte at is later reads first byte at, so loading_stream must retain the computed value under that name. + first_byte_at = None + # What: compute byte count from 0; why: byte count len chunk later reads byte count, so loading_stream must retain the computed value under that name. + byte_count = 0 + # What: compute cancelled from false; why: cancelled before connect request reservations request id cancelled later reads cancelled, so loading_stream must retain the computed value under that name. + cancelled = False + # What: compute cancellation recorded from false; why: cancellation recorded later reads cancellation recorded, so loading_stream must retain the computed value under that name. + cancellation_recorded = False + # What: compute last position from the named fixture input; why: last position initial position later reads last position, so loading_stream must retain the computed value under that name. + last_position = None + # What: compute outbound body from body; why: outbound body filtered body lease later reads outbound body, so loading_stream must retain the computed value under that name. + outbound_body = body + # What: compute captured response from bytearray; why: if len captured response len chunk capture limit later reads captured response, so loading_stream must retain the computed value under that name. + captured_response = bytearray() + # What: compute capture overflow from false; why: nonlocal capture overflow later reads capture overflow, so loading_stream must retain the computed value under that name. + capture_overflow = False + + # What: define observed around chunk; why: its direct callers call observed for observed and rely on this exact input and result contract. + def observed(chunk: bytes) -> bytes: + # What: apply the nonlocal capture overflow portion of the enclosing predicate; why: this clause remains in observed\'s enclosing expression so its grouping and evaluation order stay intact. + nonlocal capture_overflow + # What: gate on capture limit and capture overflow before capture limit and capture overflow and extend and chunk and clear; why: observed admits capture limit and capture overflow and extend and chunk and clear only for this predicate and excludes the opposite state. + if capture_limit and not capture_overflow: + # What: gate on capture limit and len and captured response and chunk before extend and chunk and captured response; why: observed admits extend and chunk and captured response only for this predicate and excludes the opposite state. + if len(captured_response) + len(chunk) <= capture_limit: + # What: call captured_response.extend with chunk; why: observed invokes captured_response.extend while performing else; the call advances that operation through its result or side effect. + captured_response.extend(chunk) + # What: select the remaining branch that performs capture overflow; why: observed covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute capture overflow from true; why: the enclosing return or state update later reads capture overflow, so observed must retain the computed value under that name. + capture_overflow = True + # What: call captured_response.clear with the declared inputs; why: observed invokes captured_response.clear while performing return chunk; the call advances that operation through its result or side effect. + captured_response.clear() + # What: return chunk from observed; why: observed exposes chunk so its caller can continue with the function\'s computed outcome. + return chunk + # What: establish the handler boundary for the protected operation; why: loading_stream routes failures to cancelled error and asyncio and exception while preserving cleanup and success flow. + try: + # What: preserve the exact yield observed loading frame n literal fragment; why: loading_stream passes this fragment verbatim through yield observed(loading_frame("━━━━━\n")), because changing it would alter a protocol payload, serialized fixture, or public message. + yield observed(loading_frame("━━━━━\n")) + # What: embed the exact yield observed loading frame f freetoken swap loading router-interface fragment; why: the router UI consumer receives this fragment verbatim through yield observed(loading_frame(f"freetoken-swap loading model: {model}\n"), preserving browser markup, style, or script behavior. + yield observed(loading_frame(f"freetoken-swap loading model: {model}\n")) + # What: compute initial position from get and reservation state and queue position; why: if isinstance initial position int later reads initial position, so loading_stream must retain the computed value under that name. + initial_position = reservation_state.get("queuePosition") + # What: gate on isinstance and initial position and int before last position and initial position; why: loading_stream admits last position and initial position only for this predicate and excludes the opposite state. + if isinstance(initial_position, int): + # What: compute last position from initial position; why: if position is not and position later reads last position, so loading_stream must retain the computed value under that name. + last_position = initial_position + # What: execute yield observed loading frame f nQueue position initial position; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + yield observed(loading_frame(f"\nQueue position: #{initial_position} ")) + # What: iterate across done and acquisition to perform position and queue position and admission cancellation and router; why: loading_stream repeats the body only while or for the loop header admits an iteration. + while not acquisition.done(): + # What: compute position from queue position and admission cancellation and router; why: if position is not and position later reads position, so loading_stream must retain the computed value under that name. + position = router.queue_position(admission_cancellation) + # What: gate on position and last position before last position and position; why: loading_stream admits last position and position only for this predicate and excludes the opposite state. + if position is not None and position != last_position: + # What: compute last position from position; why: the enclosing return or state update later reads last position, so loading_stream must retain the computed value under that name. + last_position = position + # What: execute yield observed loading frame f nQueue position position; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + yield observed(loading_frame(f"\nQueue position: #{position} ")) + # What: compute done and from wait and asyncio and acquisition and 0 75; why: if acquisition in done later reads done and, so loading_stream must retain the computed value under that name. + done, _ = await asyncio.wait({acquisition}, timeout=0.75) + # What: gate on acquisition and done before lease and result and acquisition; why: loading_stream admits lease and result and acquisition only for this predicate and excludes the opposite state. + if acquisition in done: + # What: compute lease from result and acquisition; why: if lease is later reads lease, so loading_stream must retain the computed value under that name. + lease = acquisition.result() + # What: select the remaining branch that performs yield observed loading frame; why: loading_stream covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: preserve the exact yield observed loading frame literal fragment; why: loading_stream passes this fragment verbatim through yield observed(loading_frame(".")), because changing it would alter a protocol payload, serialized fixture, or public message. + yield observed(loading_frame(".")) + # What: gate on lease before lease and result and acquisition; why: loading_stream admits lease and result and acquisition only for this predicate and excludes the opposite state. + if lease is None: + # What: compute lease from result and acquisition; why: outbound body filtered body lease later reads lease, so loading_stream must retain the computed value under that name. + lease = acquisition.result() + + # What: preserve the exact yield observed loading frame n literal fragment; why: loading_stream passes this fragment verbatim through yield observed(loading_frame("\n")), because changing it would alter a protocol payload, serialized fixture, or public message. + yield observed(loading_frame("\n")) + # What: embed the exact yield observed loading frame f done time monotonic router-interface fragment; why: the router UI consumer receives this fragment verbatim through yield observed(loading_frame(f"Done! ({time.monotonic() - started:.2f}s), preserving browser markup, style, or script behavior. + yield observed(loading_frame(f"Done! ({time.monotonic() - started:.2f}s)\n")) + # What: preserve the exact yield observed loading frame n literal fragment; why: loading_stream passes this fragment verbatim through yield observed(loading_frame("━━━━━\n")), because changing it would alter a protocol payload, serialized fixture, or public message. + yield observed(loading_frame("━━━━━\n")) + # What: preserve the exact yield observed loading frame n literal fragment; why: loading_stream passes this fragment verbatim through yield observed(loading_frame(" \n")), because changing it would alter a protocol payload, serialized fixture, or public message. + yield observed(loading_frame(" \n")) + + # What: enter the inflight lock managed context before cancelled before connect request reservations request id cancelled; why: loading_stream releases this resource or lock after cancelled before connect request reservations request id cancelled on both success and failure paths. + with inflight_lock: + # What: compute cancelled before connect from request reservations and request id and cancelled; why: if cancelled before connect later reads cancelled before connect, so loading_stream must retain the computed value under that name. + cancelled_before_connect = request_reservations[request_id]["cancelled"] + # What: gate on cancelled before connect before routing error; why: loading_stream admits routing error only for this predicate and excludes the opposite state. + if cancelled_before_connect: + # What: raise RoutingError for the caller; why: loading_stream stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: apply the request cancelled portion of the enclosing predicate; why: this clause remains in loading_stream\'s enclosing expression so its grouping and evaluation order stay intact. + "request_cancelled", + # What: apply the request cancelled before upstream connection portion of the enclosing predicate; why: this clause remains in loading_stream\'s enclosing expression so its grouping and evaluation order stay intact. + "request cancelled before upstream connection", + # What: supply status code to RoutingError; why: loading_stream binds this 409 value to RoutingError's status code input. + status_code=409, + # What: complete the RoutingError call with status code; why: loading_stream groups the supplied clauses as one RoutingError call before its value is consumed. + ) + + # What: compute outbound body from filtered body and lease; why: body outbound body later reads outbound body, so loading_stream must retain the computed value under that name. + outbound_body = filtered_body(lease) + + # What: compute upstream from connect upstream and port and proxy base url and path and query; why: upstream upstream later reads upstream, so loading_stream must retain the computed value under that name. + upstream = await connect_upstream( + # What: supply port to connect_upstream; why: loading_stream binds this port and lease value to connect_upstream's port input. + port=lease.port, + # What: supply base url to connect_upstream; why: loading_stream binds this proxy base url and lease value to connect_upstream's base url input. + base_url=lease.proxy_base_url, + # What: supply path and query to connect_upstream; why: loading_stream binds this path and query value to connect_upstream's path and query input. + path_and_query=path_and_query, + # What: supply headers to dict; why: loading_stream binds this dict and headers and request value to dict's headers input. + headers=dict(request.headers), + # What: supply body to connect_upstream; why: loading_stream binds this outbound body value to connect_upstream's body input. + body=outbound_body, + # What: supply method to connect_upstream; why: loading_stream binds this method and request value to connect_upstream's method input. + method=request.method, + # What: supply timeout s to connect_upstream; why: loading_stream binds this upstream timeout s and profile and router and lease value to connect_upstream's timeout s input. + timeout_s=lease.profile.upstream_timeout_s or router.upstream_timeout_s, + # What: complete the connect_upstream call with port and base url and path and query and headers and body; why: loading_stream groups the supplied clauses as one connect_upstream call before its value is consumed. + ) + # What: enter the inflight lock managed context before cancelled while connecting request reservations request id cancelled; why: loading_stream releases this resource or lock after cancelled while connecting request reservations request id cancelled on both success and failure paths. + with inflight_lock: + # What: compute cancelled while connecting from request reservations and request id and cancelled; why: if not cancelled while connecting later reads cancelled while connecting, so loading_stream must retain the computed value under that name. + cancelled_while_connecting = request_reservations[request_id]["cancelled"] + # What: gate on cancelled while connecting before inflight and request id and name and upstream and profile; why: loading_stream admits inflight and request id and name and upstream and profile only for this predicate and excludes the opposite state. + if not cancelled_while_connecting: + # What: compute inflight entry from name and upstream and profile and lease and profile; why: item inflight get request id later reads inflight entry, so loading_stream must retain the computed value under that name. + inflight[request_id] = { + # What: map the profile field as name and profile and lease; why: loading_stream carries profile through inflight entry into item inflight get request id. + "profile": lease.profile.name, + # What: map the upstream field as upstream; why: loading_stream carries upstream through inflight entry into item inflight get request id. + "upstream": upstream, + # What: map the cancelled field as false; why: loading_stream carries cancelled through inflight entry into item inflight get request id. + "cancelled": False, + # What: complete the inflight entry mapping with profile and upstream and cancelled; why: loading_stream groups the supplied clauses as one inflight entry mapping before its value is consumed. + } + # What: gate on cancelled while connecting before routing error; why: loading_stream admits routing error only for this predicate and excludes the opposite state. + if cancelled_while_connecting: + # What: raise RoutingError for the caller; why: loading_stream stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: apply the request cancelled portion of the enclosing predicate; why: this clause remains in loading_stream\'s enclosing expression so its grouping and evaluation order stay intact. + "request_cancelled", + # What: apply the request cancelled while opening upstream connection portion of the enclosing predicate; why: this clause remains in loading_stream\'s enclosing expression so its grouping and evaluation order stay intact. + "request cancelled while opening upstream connection", + # What: supply status code to RoutingError; why: loading_stream binds this 409 value to RoutingError's status code input. + status_code=409, + # What: complete the RoutingError call with status code; why: loading_stream groups the supplied clauses as one RoutingError call before its value is consumed. + ) + + # What: call router_event with admitted; why: loading_stream invokes router_event while performing admitted lease event identity lease route safe route; the call advances that operation through its result or side effect. + router_event( + # What: preserve the exact admitted lease event identity lease route safe route literal fragment; why: loading_stream passes this fragment verbatim through "admitted", **lease_event_identity(lease), route=safe_route, because changing it would alter a protocol payload, serialized fixture, or public. + "admitted", **lease_event_identity(lease), route=safe_route + # What: complete the router_event call with route; why: loading_stream groups the supplied clauses as one router_event call before its value is consumed. + ) + # What: compute iterator from iter and chunks and upstream; why: return next iterator later reads iterator, so loading_stream must retain the computed value under that name. + iterator = iter(upstream.chunks()) + + # What: define next_chunk around the current object state; why: its direct callers call next_chunk for next chunk and rely on this exact input and result contract. + def next_chunk(): + # What: establish the handler boundary for the protected operation; why: next_chunk routes failures to stop iteration while preserving cleanup and success flow. + try: + # What: return next and iterator and true from next_chunk; why: next_chunk exposes next and iterator and true so its caller can continue with the function\'s computed outcome. + return True, next(iterator) + # What: handle stop iteration by return false b; why: next_chunk converts that failure into this concrete recovery, response, or cleanup behavior. + except StopIteration: + # What: return false from next_chunk; why: next_chunk exposes false so its caller can continue with the function\'s computed outcome. + return False, b"" + + # What: compute loop from get running loop and asyncio; why: has chunk chunk await loop run in executor proxy pool next chunk later reads loop, so loading_stream must retain the computed value under that name. + loop = asyncio.get_running_loop() + # What: iterate across the computed value to perform has chunk and chunk and run in executor and proxy pool and next chunk; why: loading_stream repeats the body only while or for the loop header admits an iteration. + while True: + # What: compute has chunk and chunk from run in executor and proxy pool and next chunk and loop; why: if not has chunk later reads has chunk and chunk, so loading_stream must retain the computed value under that name. + has_chunk, chunk = await loop.run_in_executor(proxy_pool, next_chunk) + # What: gate on has chunk before the computed value; why: loading_stream admits the computed value only for this predicate and excludes the opposite state. + if not has_chunk: + # What: apply the break portion of the enclosing predicate; why: this clause remains in loading_stream\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: gate on first byte at before first byte at and monotonic and time; why: loading_stream admits first byte at and monotonic and time only for this predicate and excludes the opposite state. + if first_byte_at is None: + # What: compute first byte at from monotonic and time; why: ttft s first byte at started if first byte at is later reads first byte at, so loading_stream must retain the computed value under that name. + first_byte_at = time.monotonic() + # What: compute byte count from len and chunk; why: response bytes byte count later reads byte count, so loading_stream must retain the computed value under that name. + byte_count += len(chunk) + # What: call observed with chunk; why: loading_stream invokes observed while performing except asyncio cancelled error; the call advances that operation through its result or side effect. + yield observed(chunk) + # What: handle cancelled error and asyncio by cancelled true; why: loading_stream converts that failure into this concrete recovery, response, or cleanup behavior. + except asyncio.CancelledError: + # What: compute cancelled from true; why: cancelled later reads cancelled, so loading_stream must retain the computed value under that name. + cancelled = True + # What: call abandon_loading_acquisition with acquisition; why: loading_stream invokes abandon_loading_acquisition while performing cancellation recorded; the call advances that operation through its result or side effect. + abandon_loading_acquisition(acquisition) + # What: compute cancellation recorded from true; why: acquisition record cancellation not cancellation recorded later reads cancellation recorded, so loading_stream must retain the computed value under that name. + cancellation_recorded = True + # What: preserve the exact router event request cancelled profile model route safe route literal fragment; why: loading_stream passes this fragment verbatim through router_event("request_cancelled", profile=model, route=safe_route), because changing it would alter a protocol payload, serialized fixture. + router_event("request_cancelled", profile=model, route=safe_route) + # What: re-propagate the active failure to the caller; why: loading_stream stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: handle exception by if isinstance exc routing error; why: loading_stream converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: + # What: gate on isinstance and exc and routing error before router event and model and safe route and code and exc; why: loading_stream admits router event and model and safe route and code and exc only for this predicate and excludes the opposite state. + if isinstance(exc, RoutingError): + # What: preserve the exact router event admission failed profile model route safe route literal fragment; why: loading_stream passes this fragment verbatim through router_event("admission_failed", profile=model, route=safe_route, code=e, because changing it would alter a protocol payload, serialize. + router_event("admission_failed", profile=model, route=safe_route, code=exc.code) + # What: select the remaining branch that performs router event upstream connect failed profile model route safe route; why: loading_stream covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: preserve the exact router event upstream connect failed profile model route safe route literal fragment; why: loading_stream passes this fragment verbatim through router_event("upstream_connect_failed", profile=model, route=safe_route), because changing it would alter a protocol payload, se. + router_event("upstream_connect_failed", profile=model, route=safe_route) + # What: call observed with loading error and exc; why: loading_stream invokes observed while performing finally; the call advances that operation through its result or side effect. + yield observed(loading_error(exc)) + # What: run ended time monotonic on every exit path; why: loading_stream performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: compute ended from monotonic and time; why: duration s ended started later reads ended, so loading_stream must retain the computed value under that name. + ended = time.monotonic() + # Starlette may finalize an async response iterator with + # ``GeneratorExit`` rather than injecting ``CancelledError``. + # A downstream that disappears must still synchronously wake + # and cancel any queued ownership. + # What: gate on done and acquisition before cancelled; why: loading_stream admits cancelled only for this predicate and excludes the opposite state. + if not acquisition.done(): + # What: compute cancelled from true; why: cancelled later reads cancelled, so loading_stream must retain the computed value under that name. + cancelled = True + # What: call abandon_loading_acquisition with acquisition; why: loading_stream invokes abandon_loading_acquisition while performing acquisition record cancellation not cancellation recorded; the call advances that operation through its result or side effect. + abandon_loading_acquisition( + # What: supply record cancellation to abandon_loading_acquisition; why: loading_stream binds this cancellation recorded value to abandon_loading_acquisition's record cancellation input. + acquisition, record_cancellation=not cancellation_recorded + # What: complete the abandon_loading_acquisition call with record cancellation; why: loading_stream groups the supplied clauses as one abandon_loading_acquisition call before its value is consumed. + ) + # What: gate on lease before lease and base exception and cancelled and result and cancellation recorded; why: loading_stream admits lease and base exception and cancelled and result and cancellation recorded only for this predicate and excludes the opposite state. + elif lease is None: + # What: establish the handler boundary for the protected operation; why: loading_stream routes failures to base exception while preserving cleanup and success flow. + try: + # What: compute lease from result and acquisition; why: if lease is not later reads lease, so loading_stream must retain the computed value under that name. + lease = acquisition.result() + # What: handle base exception by pass; why: loading_stream converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: ignore the anticipated exception handled by this branch; why: next_chunk continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: select the remaining branch that performs cancelled; why: loading_stream covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute cancelled from true; why: cancelled later reads cancelled, so loading_stream must retain the computed value under that name. + cancelled = True + # What: gate on cancellation recorded before record cancellation and router; why: loading_stream admits record cancellation and router only for this predicate and excludes the opposite state. + if not cancellation_recorded: + # What: call router.record_cancellation with the declared inputs; why: loading_stream invokes router.record_cancellation while performing if upstream is not; the call advances that operation through its result or side effect. + router.record_cancellation() + # What: gate on upstream before close and upstream; why: loading_stream admits close and upstream only for this predicate and excludes the opposite state. + if upstream is not None: + # What: call upstream.close with the declared inputs; why: loading_stream invokes upstream.close while performing with inflight lock; the call advances that operation through its result or side effect. + upstream.close() + # What: enter the inflight lock managed context before reservation request reservations get request id; why: loading_stream releases this resource or lock after reservation request reservations get request id on both success and failure paths. + with inflight_lock: + # What: compute reservation from get and request id and request reservations; why: or bool reservation get cancelled later reads reservation, so loading_stream must retain the computed value under that name. + reservation = request_reservations.get(request_id, {}) + # What: compute item from get and request id and inflight; why: or bool item get cancelled later reads item, so loading_stream must retain the computed value under that name. + item = inflight.get(request_id, {}) + # What: compute cancelled from cancelled and bool and get and reservation; why: cancelled later reads cancelled, so loading_stream must retain the computed value under that name. + cancelled = ( + # What: apply the cancelled portion of cancelled; why: loading_stream uses this clause to evaluate cancelled as one grouped value. + cancelled + # What: call bool with get and reservation and cancelled; why: loading_stream invokes bool while performing or bool item get cancelled; the call advances that operation through its result or side effect. + or bool(reservation.get("cancelled")) + # What: call bool with get and item and cancelled; why: loading_stream consumes the bool return value while evaluating or bool(item.get("cancelled")). + or bool(item.get("cancelled")) + # What: complete the cancelled expression with cancelled cancelled or bool reservation get cancelled or bool item get; why: loading_stream groups the supplied clauses as one cancelled expression before its value is consumed. + ) + # What: gate on upstream and get and item before pop and request id and inflight; why: loading_stream admits pop and request id and inflight only for this predicate and excludes the opposite state. + if upstream is not None and item.get("upstream") is upstream: + # What: call inflight.pop with request id and the named fixture input; why: loading_stream invokes inflight.pop while performing request reservations pop request id; the call advances that operation through its result or side effect. + inflight.pop(request_id, None) + # What: call request_reservations.pop with request id and the named fixture input; why: loading_stream invokes request_reservations.pop while performing if lease is not; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: gate on lease before ttft s and first byte at and started; why: loading_stream admits ttft s and first byte at and started only for this predicate and excludes the opposite state. + if lease is not None: + # What: compute ttft s from first byte at and started; why: ttft s ttft s later reads ttft s, so loading_stream must retain the computed value under that name. + ttft_s = (first_byte_at - started) if first_byte_at is not None else None + # What: call router.record_stream with the declared inputs; why: loading_stream invokes router.record_stream while performing ttft s ttft s; the call advances that operation through its result or side effect. + router.record_stream( + # What: supply ttft s to router.record_stream; why: loading_stream binds this ttft s value to router.record_stream's ttft s input. + ttft_s=ttft_s, + # What: supply duration s to router.record_stream; why: loading_stream binds this ended and started value to router.record_stream's duration s input. + duration_s=ended - started, + # What: supply response bytes to router.record_stream; why: loading_stream binds this byte count value to router.record_stream's response bytes input. + response_bytes=byte_count, + # What: supply completed to router.record_stream; why: loading_stream binds this cancelled value to router.record_stream's completed input. + completed=not cancelled, + # What: complete the router.record_stream call with ttft s and duration s and response bytes and completed; why: loading_stream groups the supplied clauses as one router.record_stream call before its value is consumed. + ) + # What: call lease.release with the declared inputs; why: loading_stream invokes lease.release while performing router event; the call advances that operation through its result or side effect. + lease.release() + # What: call router_event with request finished; why: loading_stream invokes router_event while performing request finished; the call advances that operation through its result or side effect. + router_event( + # What: preserve the exact request finished literal fragment; why: loading_stream passes this fragment verbatim through "request_finished", because changing it would alter a protocol payload, serialized fixture, or public message. + "request_finished", + # What: supply expanded arguments to lease_event_identity; why: loading_stream binds this lease event identity and lease value to lease_event_identity's expanded input. + **lease_event_identity(lease), + # What: supply route to router_event; why: loading_stream binds this safe route value to router_event's route input. + route=safe_route, + # What: supply status to router_event; why: loading_stream binds this status and upstream and 200 value to router_event's status input. + status=upstream.status if upstream is not None else 200, + # What: supply cancelled to router_event; why: loading_stream binds this cancelled value to router_event's cancelled input. + cancelled=cancelled, + # What: supply response bytes to router_event; why: loading_stream binds this byte count value to router_event's response bytes input. + responseBytes=byte_count, + # What: complete the router_event call with route and status and cancelled and response bytes; why: loading_stream groups the supplied clauses as one router_event call before its value is consumed. + ) + # What: call asyncio.get_running_loop with the declared inputs; why: loading_stream invokes asyncio.get_running_loop while performing proxy pool; the call advances that operation through its result or side effect. + await asyncio.get_running_loop().run_in_executor( + # What: apply the proxy pool portion of the enclosing predicate; why: this clause remains in loading_stream\'s enclosing expression so its grouping and evaluation order stay intact. + proxy_pool, + # What: call functools.partial with record and activity store; why: loading_stream invokes functools.partial while performing activity store record; the call advances that operation through its result or side effect. + functools.partial( + # What: apply the activity store record portion of the enclosing predicate; why: this clause remains in loading_stream\'s enclosing expression so its grouping and evaluation order stay intact. + activity_store.record, + # What: supply model to functools.partial; why: loading_stream binds this name and profile and lease value to functools.partial's model input. + model=lease.profile.name, + # What: supply route to functools.partial; why: loading_stream binds this safe route value to functools.partial's route input. + route=safe_route, + # What: supply method to functools.partial; why: loading_stream binds this method and request value to functools.partial's method input. + method=request.method, + # What: supply status to functools.partial; why: loading_stream binds this status and upstream and 200 value to functools.partial's status input. + status=upstream.status if upstream is not None else 200, + # What: supply started to functools.partial; why: loading_stream binds this started value to functools.partial's started input. + started=started, + # What: supply ttft s to functools.partial; why: loading_stream binds this ttft s value to functools.partial's ttft s input. + ttft_s=ttft_s, + # What: supply response bytes to functools.partial; why: loading_stream binds this byte count value to functools.partial's response bytes input. + response_bytes=byte_count, + # What: supply cancelled to functools.partial; why: loading_stream binds this cancelled value to functools.partial's cancelled input. + cancelled=cancelled, + # What: supply request headers to dict; why: loading_stream binds this dict and headers and request value to dict's request headers input. + request_headers=dict(request.headers), + # What: supply request body to functools.partial; why: loading_stream binds this outbound body value to functools.partial's request body input. + request_body=outbound_body, + # What: supply response headers to functools.partial; why: loading_stream binds this headers and upstream value to functools.partial's response headers input. + response_headers=upstream.headers if upstream is not None else {}, + # What: supply response body to functools.partial; why: loading_stream binds this capture overflow and bytes and captured response and upstream value to functools.partial's response body input. + response_body=( + # What: apply the if upstream is or capture overflow portion of the enclosing predicate; why: this clause remains in loading_stream\'s enclosing expression so its grouping and evaluation order stay intact. + None if upstream is None or capture_overflow + # What: call bytes with captured response; why: loading_stream consumes the bytes return value while evaluating else bytes(captured_response). + else bytes(captured_response) + # What: complete the functools.partial call with model and route and method and status and started; why: loading_stream groups the supplied clauses as one functools.partial call before its value is consumed. + ), + # What: complete the functools.partial call with model and route and method and status and started; why: loading_stream groups the supplied clauses as one functools.partial call before its value is consumed. + ), + # What: complete the operation.run_in_executor call with proxy pool and partial; why: loading_stream groups the supplied clauses as one operation.run_in_executor call before its value is consumed. + ) + + # What: compute loading eligible from false; why: loading eligible isinstance request doc dict and request doc get later reads loading eligible, so forward_routed must retain the computed value under that name. + loading_eligible = False + # What: gate on path and url and request before request doc and loads and body and unicode decode error and jsondecode error; why: forward_routed admits request doc and loads and body and unicode decode error and jsondecode error only for this predicate and excludes the opposite state. + if request.url.path == "/v1/chat/completions": + # What: establish the handler boundary for the protected operation; why: forward_routed routes failures to unicode decode error and jsondecode error and json while preserving cleanup and success flow. + try: + # What: compute request doc from loads and body and json; why: request doc later reads request doc, so forward_routed must retain the computed value under that name. + request_doc = json.loads(body) + # What: handle unicode decode error and jsondecode error and json by request doc; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except (UnicodeDecodeError, json.JSONDecodeError): + # What: compute request doc from the named fixture input; why: loading eligible isinstance request doc dict and request doc get later reads request doc, so forward_routed must retain the computed value under that name. + request_doc = None + # What: compute loading eligible from isinstance and request doc and dict and get and true; why: if loading eligible later reads loading eligible, so forward_routed must retain the computed value under that name. + loading_eligible = isinstance(request_doc, dict) and request_doc.get("stream") is True + + # What: gate on loading eligible before loop and get running loop and asyncio; why: forward_routed admits loop and get running loop and asyncio only for this predicate and excludes the opposite state. + if loading_eligible: + # What: compute loop from get running loop and asyncio; why: loop call soon threadsafe reserved set later reads loop, so forward_routed must retain the computed value under that name. + loop = asyncio.get_running_loop() + # What: compute reserved from event and asyncio; why: loop call soon threadsafe reserved set later reads reserved, so forward_routed must retain the computed value under that name. + reserved = asyncio.Event() + # What: initialize reservation state as an empty runtime accumulator; why: forward_routed appends or maps entries into it during reservation state loading required loading required before consuming the aggregate. + reservation_state: dict[str, Any] = {} + + # What: define on_reserved around loading required and queue position; why: its direct callers call on_reserved for on reserved and rely on this exact input and result contract. + def on_reserved(loading_required: bool, queue_position: int) -> None: + # What: compute reservation state entry from loading required; why: reservation state queue position queue position later reads reservation state entry, so on_reserved must retain the computed value under that name. + reservation_state["loadingRequired"] = loading_required + # What: compute reservation state entry from queue position; why: the enclosing return or state update later reads reservation state entry, so on_reserved must retain the computed value under that name. + reservation_state["queuePosition"] = queue_position + # What: call loop.call_soon_threadsafe with set and reserved; why: on_reserved invokes loop.call_soon_threadsafe while performing the enclosing return; the call advances that operation through its result or side effect. + loop.call_soon_threadsafe(reserved.set) + + # What: compute acquisition from create task and asyncio and acquire route and model; why: acquisition reservation wait return when asyncio first completed later reads acquisition, so forward_routed must retain the computed value under that name. + acquisition = asyncio.create_task( + # What: call acquire_route with model and admission cancellation and on reserved; why: forward_routed invokes acquire_route while performing model; the call advances that operation through its result or side effect. + acquire_route( + # What: apply the model portion of acquisition; why: forward_routed uses this clause to evaluate acquisition as one grouped value. + model, + # What: apply the admission cancellation portion of acquisition; why: forward_routed uses this clause to evaluate acquisition as one grouped value. + admission_cancellation, + # What: apply the on reserved portion of acquisition; why: forward_routed uses this clause to evaluate acquisition as one grouped value. + on_reserved, + # What: supply apply loading policy to acquire_route; why: forward_routed binds this true value to acquire_route's apply loading policy input. + apply_loading_policy=True, + # What: complete the acquire_route call with apply loading policy; why: forward_routed groups the supplied clauses as one acquire_route call before its value is consumed. + ) + # What: complete the asyncio.create_task call with acquire route; why: forward_routed groups the supplied clauses as one asyncio.create_task call before its value is consumed. + ) + # What: compute reservation wait from create task and asyncio and wait and reserved; why: acquisition reservation wait return when asyncio first completed later reads reservation wait, so forward_routed must retain the computed value under that name. + reservation_wait = asyncio.create_task(reserved.wait()) + # What: establish the handler boundary for the protected operation; why: forward_routed routes failures to routing error and cancelled error and asyncio and base exception while preserving cleanup and success flow. + try: + # What: compute done and from wait and asyncio and acquisition and reservation wait; why: if acquisition in done later reads done and, so forward_routed must retain the computed value under that name. + done, _ = await asyncio.wait( + # What: supply return when to asyncio.wait; why: forward_routed binds this first completed and asyncio value to asyncio.wait's return when input. + {acquisition, reservation_wait}, return_when=asyncio.FIRST_COMPLETED + # What: complete the asyncio.wait call with return when; why: forward_routed groups the supplied clauses as one asyncio.wait call before its value is consumed. + ) + # What: gate on acquisition and done before cancel and reservation wait; why: forward_routed admits cancel and reservation wait only for this predicate and excludes the opposite state. + if acquisition in done: + # What: call reservation_wait.cancel with the declared inputs; why: forward_routed invokes reservation_wait.cancel while performing lease acquisition result; the call advances that operation through its result or side effect. + reservation_wait.cancel() + # What: compute lease from result and acquisition; why: lease await acquisition later reads lease, so forward_routed must retain the computed value under that name. + lease = acquisition.result() + # What: select the remaining branch that performs if reservation state loading required; why: forward_routed covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: gate on reservation state before streaming response and owned and inflight lock and call and scope; why: forward_routed admits streaming response and owned and inflight lock and call and scope only for this predicate and excludes the opposite state. + if reservation_state["loadingRequired"]: + # What: define AdmissionOwnedStreamingResponse as the owner of __call__; why: daemon callers use this class boundary so those methods share one admission owned streaming response state invariant. + class AdmissionOwnedStreamingResponse(StreamingResponse): + # What: define __call__ around scope and receive and send; why: its direct callers call __call__ for call and rely on this exact input and result contract. + async def __call__(self, scope, receive, send) -> None: + # What: establish the handler boundary for the protected operation; why: AdmissionOwnedStreamingResponse.__call__ routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: call operation.__call__ with scope and receive and send; why: __call__ invokes operation.__call__ while performing finally; the call advances that operation through its result or side effect. + await super().__call__(scope, receive, send) + # What: run async generator finalization can be deferred on every exit path; why: __call__ performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # Async-generator finalization can be deferred + # beyond response termination. The response is + # the ownership barrier for both unstarted and + # suspended loading iterators. + # What: enter the inflight lock managed context before owned request id in request reservations; why: __call__ releases this resource or lock after owned request id in request reservations on both success and failure paths. + with inflight_lock: + # What: compute owned from request id and request reservations; why: if owned later reads owned, so __call__ must retain the computed value under that name. + owned = request_id in request_reservations + # What: call request_reservations.pop with request id and the named fixture input; why: __call__ invokes request_reservations.pop while performing if owned; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: gate on owned before abandon loading acquisition and acquisition; why: __call__ admits abandon loading acquisition and acquisition only for this predicate and excludes the opposite state. + if owned: + # What: call abandon_loading_acquisition with acquisition; why: __call__ invokes abandon_loading_acquisition while performing the enclosing return; the call advances that operation through its result or side effect. + abandon_loading_acquisition(acquisition) + + # What: return admission owned streaming response and loading stream and acquisition and request id and 200 from; why: forward_routed exposes admission owned streaming response and loading stream and acquisition and request id and 200 so its caller can continue with the function\'s computed outcome. + return AdmissionOwnedStreamingResponse( + # What: call loading_stream with acquisition; why: forward_routed invokes loading_stream while performing status code; the call advances that operation through its result or side effect. + loading_stream(acquisition), + # What: supply status code to AdmissionOwnedStreamingResponse; why: forward_routed binds this 200 value to AdmissionOwnedStreamingResponse's status code input. + status_code=200, + # What: supply headers to AdmissionOwnedStreamingResponse; why: forward_routed binds this request id and cache control and connection and x ft request id and no cache value to AdmissionOwnedStreamingResponse's headers input. + headers={ + # What: map the cache control field as no cache; why: forward_routed carries cache control through "Cache-Control": "no-cache" into except exception as exc the lease must. + "Cache-Control": "no-cache", + # What: map the connection field as keep alive; why: forward_routed carries connection through "Connection": "keep-alive" into except exception as exc the lease must. + "Connection": "keep-alive", + # What: map the x ft request id field as request id; why: forward_routed carries x ft request id through "X-FT-Request-ID": request_id into except exception as exc the lease must. + "X-FT-Request-ID": request_id, + # What: complete the enclosing predicate mapping with cache control and connection and x ft request id; why: forward_routed groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + }, + # What: supply media type to AdmissionOwnedStreamingResponse; why: forward_routed binds this text and event stream value to AdmissionOwnedStreamingResponse's media type input. + media_type="text/event-stream", + # What: complete the AdmissionOwnedStreamingResponse call with status code and headers and media type; why: forward_routed groups the supplied clauses as one AdmissionOwnedStreamingResponse call before its value is consumed. + ) + # What: compute lease from acquisition; why: lease later reads lease, so forward_routed must retain the computed value under that name. + lease = await acquisition + # What: handle routing error by reservation wait cancel; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: call reservation_wait.cancel with the declared inputs; why: forward_routed invokes reservation_wait.cancel while performing with inflight lock; the call advances that operation through its result or side effect. + reservation_wait.cancel() + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing router event admission failed profile model route safe route; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: preserve the exact router event admission failed profile model route safe route literal fragment; why: forward_routed passes this fragment verbatim through router_event("admission_failed", profile=model, route=safe_route, code=e, because changing it would alter a protocol payload, serialized fi. + router_event("admission_failed", profile=model, route=safe_route, code=exc.code) + # What: map the error field as code and str and exc and message and type; why: forward_routed carries error through content into content recovery exc recovery. + content = {"error": {"message": str(exc), "type": exc.code}} + # What: gate on recovery and exc before recovery and content and exc; why: forward_routed admits recovery and content and exc only for this predicate and excludes the opposite state. + if exc.recovery is not None: + # What: compute content entry from recovery and exc; why: content content later reads content entry, so forward_routed must retain the computed value under that name. + content["recovery"] = exc.recovery + # What: return jsonresponse and status code and content and exc and 429 from forward_routed; why: forward_routed exposes jsonresponse and status code and content and exc and 429 so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: forward_routed binds this status code and exc value to JSONResponse's status code input. + status_code=exc.status_code, + # What: supply content to JSONResponse; why: forward_routed binds this content value to JSONResponse's content input. + content=content, + # What: map the retry after field as 1; why: forward_routed carries retry after through headers={"Retry-After": "1"} if exc.status_code == 429 else None into except exception as exc the lease must. + headers={"Retry-After": "1"} if exc.status_code == 429 else None, + # What: complete the JSONResponse call with status code and content and headers; why: forward_routed groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: handle cancelled error and asyncio by reservation wait cancel; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except asyncio.CancelledError: + # What: call reservation_wait.cancel with the declared inputs; why: forward_routed invokes reservation_wait.cancel while performing with inflight lock; the call advances that operation through its result or side effect. + reservation_wait.cancel() + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing abandon loading acquisition acquisition; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: call abandon_loading_acquisition with acquisition; why: forward_routed invokes abandon_loading_acquisition while performing raise; the call advances that operation through its result or side effect. + abandon_loading_acquisition(acquisition) + # What: re-propagate the active failure to the caller; why: forward_routed stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: handle base exception by reservation wait cancel; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: call reservation_wait.cancel with the declared inputs; why: forward_routed invokes reservation_wait.cancel while performing with inflight lock; the call advances that operation through its result or side effect. + reservation_wait.cancel() + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing if not acquisition done; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: gate on done and acquisition before abandon loading acquisition and acquisition; why: forward_routed admits abandon loading acquisition and acquisition only for this predicate and excludes the opposite state. + if not acquisition.done(): + # What: supply record cancellation to abandon_loading_acquisition; why: forward_routed binds this false value to abandon_loading_acquisition's record cancellation input. + abandon_loading_acquisition(acquisition, record_cancellation=False) + # What: re-propagate the active failure to the caller; why: forward_routed stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: run if not reservation wait done on every exit path; why: forward_routed performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: gate on done and reservation wait before cancel and reservation wait; why: forward_routed admits cancel and reservation wait only for this predicate and excludes the opposite state. + if not reservation_wait.done(): + # What: call reservation_wait.cancel with the declared inputs; why: forward_routed invokes reservation_wait.cancel while performing else; the call advances that operation through its result or side effect. + reservation_wait.cancel() + # What: select the remaining branch that performs lease; why: forward_routed covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute lease from the named fixture input; why: if lease is later reads lease, so forward_routed must retain the computed value under that name. + lease = None + # What: establish the handler boundary for the protected operation; why: forward_routed routes failures to routing error and cancelled error and asyncio and base exception while preserving cleanup and success flow. + try: + # What: gate on lease before lease and acquire route and model and admission cancellation; why: forward_routed admits lease and acquire route and model and admission cancellation only for this predicate and excludes the opposite state. + if lease is None: + # What: compute lease from acquire route and model and admission cancellation; why: lease release later reads lease, so forward_routed must retain the computed value under that name. + lease = await acquire_route(model, admission_cancellation) + # What: handle routing error by with inflight lock; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing router event admission failed profile model route safe route; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: preserve the exact router event admission failed profile model route safe route literal fragment; why: forward_routed passes this fragment verbatim through router_event("admission_failed", profile=model, route=safe_route, code=e, because changing it would alter a protocol payload, serialized fixtur. + router_event("admission_failed", profile=model, route=safe_route, code=exc.code) + # What: map the error field as code and str and exc and message and type; why: forward_routed carries error through content into content recovery exc recovery. + content = {"error": {"message": str(exc), "type": exc.code}} + # What: gate on recovery and exc before recovery and content and exc; why: forward_routed admits recovery and content and exc only for this predicate and excludes the opposite state. + if exc.recovery is not None: + # What: compute content entry from recovery and exc; why: content content later reads content entry, so forward_routed must retain the computed value under that name. + content["recovery"] = exc.recovery + # What: return jsonresponse and status code and content and exc and 429 from forward_routed; why: forward_routed exposes jsonresponse and status code and content and exc and 429 so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: forward_routed binds this status code and exc value to JSONResponse's status code input. + status_code=exc.status_code, + # What: supply content to JSONResponse; why: forward_routed binds this content value to JSONResponse's content input. + content=content, + # What: map the retry after field as 1; why: forward_routed carries retry after through headers={"Retry-After": "1"} if exc.status_code == 429 else None into except exception as exc the lease must. + headers={"Retry-After": "1"} if exc.status_code == 429 else None, + # What: complete the JSONResponse call with status code and content and headers; why: forward_routed groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: handle cancelled error and asyncio by with inflight lock; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except asyncio.CancelledError: + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing router cancel acquire admission cancellation; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: call router.cancel_acquire with admission cancellation; why: forward_routed invokes router.cancel_acquire while performing router record cancellation; the call advances that operation through its result or side effect. + router.cancel_acquire(admission_cancellation) + # What: call router.record_cancellation with the declared inputs; why: forward_routed invokes router.record_cancellation while performing router event request cancelled profile model route safe route; the call advances that operation through its result or side effect. + router.record_cancellation() + # What: preserve the exact router event request cancelled profile model route safe route literal fragment; why: forward_routed passes this fragment verbatim through router_event("request_cancelled", profile=model, route=safe_route), because changing it would alter a protocol payload, serialized fixture, or. + router_event("request_cancelled", profile=model, route=safe_route) + # What: re-propagate the active failure to the caller; why: forward_routed stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: handle base exception by with inflight lock; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing raise; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: re-propagate the active failure to the caller; why: forward_routed stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: enter the inflight lock managed context before cancelled before connect request reservations request id cancelled; why: forward_routed releases this resource or lock after cancelled before connect request reservations request id cancelled on both success and failure paths. + with inflight_lock: + # What: compute cancelled before connect from request reservations and request id and cancelled; why: if cancelled before connect later reads cancelled before connect, so forward_routed must retain the computed value under that name. + cancelled_before_connect = request_reservations[request_id]["cancelled"] + # What: gate on cancelled before connect before pop and request id and request reservations; why: forward_routed admits pop and request id and request reservations only for this predicate and excludes the opposite state. + if cancelled_before_connect: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing if cancelled before connect; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: gate on cancelled before connect before release and lease; why: forward_routed admits release and lease only for this predicate and excludes the opposite state. + if cancelled_before_connect: + # What: call lease.release with the declared inputs; why: forward_routed invokes lease.release while performing return jsonresponse; the call advances that operation through its result or side effect. + lease.release() + # What: return jsonresponse and 409 and error and message and type from forward_routed; why: forward_routed exposes jsonresponse and 409 and error and message and type so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: forward_routed binds this 409 value to JSONResponse's status code input. + status_code=409, + # What: map the error field as message and type and request and cancelled and before; why: forward_routed carries error through content={"error": { into except exception as exc the lease must. + content={"error": { + # What: apply the message request cancelled before upstream connection portion of the enclosing predicate; why: this clause remains in forward_routed\'s enclosing expression so its grouping and evaluation order stay intact. + "message": "request cancelled before upstream connection", + # What: apply the type request cancelled portion of the enclosing predicate; why: this clause remains in forward_routed\'s enclosing expression so its grouping and evaluation order stay intact. + "type": "request_cancelled", + # What: complete the enclosing predicate mapping with error; why: forward_routed groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + }}, + # What: complete the JSONResponse call with status code and content; why: forward_routed groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: establish the handler boundary for the protected operation; why: forward_routed routes failures to request model error while preserving cleanup and success flow. + try: + # What: compute outbound body from filtered body and lease; why: body outbound body later reads outbound body, so forward_routed must retain the computed value under that name. + outbound_body = filtered_body(lease) + # What: handle request model error by lease release; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except RequestModelError as exc: + # What: call lease.release with the declared inputs; why: forward_routed invokes lease.release while performing with inflight lock; the call advances that operation through its result or side effect. + lease.release() + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing return jsonresponse; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: return jsonresponse and str and exc and 400 and error from forward_routed; why: forward_routed exposes jsonresponse and str and exc and 400 and error so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: forward_routed binds this 400 value to JSONResponse's status code input. + status_code=400, + # What: map the error field as str and exc and message and type and invalid request; why: forward_routed carries error through content={"error": {"message": str(exc), "type": "invalid_request"}} into except exception as exc the lease must. + content={"error": {"message": str(exc), "type": "invalid_request"}}, + # What: complete the JSONResponse call with status code and content; why: forward_routed groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + try: + # What: compute upstream from connect upstream and port and proxy base url and path and query; why: upstream upstream later reads upstream, so forward_routed must retain the computed value under that name. + upstream = await connect_upstream( + # What: supply port to connect_upstream; why: forward_routed binds this port and lease value to connect_upstream's port input. + port=lease.port, + # What: supply base url to connect_upstream; why: forward_routed binds this proxy base url and lease value to connect_upstream's base url input. + base_url=lease.proxy_base_url, + # What: supply path and query to connect_upstream; why: forward_routed binds this path and query value to connect_upstream's path and query input. + path_and_query=path_and_query, + # What: supply headers to dict; why: forward_routed binds this dict and headers and request value to dict's headers input. + headers=dict(request.headers), + # What: supply body to connect_upstream; why: forward_routed binds this outbound body value to connect_upstream's body input. + body=outbound_body, + # What: supply method to connect_upstream; why: forward_routed binds this method and request value to connect_upstream's method input. + method=request.method, + # What: supply timeout s to connect_upstream; why: forward_routed binds this upstream timeout s and profile and router and lease value to connect_upstream's timeout s input. + timeout_s=lease.profile.upstream_timeout_s or router.upstream_timeout_s, + # What: complete the connect_upstream call with port and base url and path and query and headers and body; why: forward_routed groups the supplied clauses as one connect_upstream call before its value is consumed. + ) + # What: handle cancelled error and asyncio by lease release; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except asyncio.CancelledError: + # What: call lease.release with the declared inputs; why: forward_routed invokes lease.release while performing with inflight lock; the call advances that operation through its result or side effect. + lease.release() + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing router record cancellation; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: call router.record_cancellation with the declared inputs; why: forward_routed invokes router.record_cancellation while performing router event request cancelled profile model route safe route; the call advances that operation through its result or side effect. + router.record_cancellation() + # What: preserve the exact router event request cancelled profile model route safe route literal fragment; why: forward_routed passes this fragment verbatim through router_event("request_cancelled", profile=model, route=safe_route), because changing it would alter a protocol payload, serialized fixture, or. + router_event("request_cancelled", profile=model, route=safe_route) + # What: re-propagate the active failure to the caller; why: forward_routed stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: handle exception by lease release; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: # the lease must not strand a pending swap on connect failure + # What: call lease.release with the declared inputs; why: forward_routed invokes lease.release while performing with inflight lock; the call advances that operation through its result or side effect. + lease.release() + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing router event upstream connect failed profile model route safe route; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: preserve the exact router event upstream connect failed profile model route safe route literal fragment; why: forward_routed passes this fragment verbatim through router_event("upstream_connect_failed", profile=model, route=safe_route), because changing it would alter a protocol payload, serialized. + router_event("upstream_connect_failed", profile=model, route=safe_route) + # What: map the error field as str and exc and message and type and upstream unavailable; why: forward_routed carries error into return JSONResponse(status_code=502, content={"error": {"message": str(e. + return JSONResponse(status_code=502, content={"error": {"message": str(exc), "type": "upstream_unavailable"}}) + # What: handle base exception by lease release; why: forward_routed converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: call lease.release with the declared inputs; why: forward_routed invokes lease.release while performing with inflight lock; the call advances that operation through its result or side effect. + lease.release() + # What: enter the inflight lock managed context before request reservations pop request id; why: forward_routed releases this resource or lock after request reservations pop request id on both success and failure paths. + with inflight_lock: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing raise; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: re-propagate the active failure to the caller; why: forward_routed stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: enter the inflight lock managed context before cancelled while connecting request reservations request id cancelled; why: forward_routed releases this resource or lock after cancelled while connecting request reservations request id cancelled on both success and failure paths. + with inflight_lock: + # What: compute cancelled while connecting from request reservations and request id and cancelled; why: if cancelled while connecting later reads cancelled while connecting, so forward_routed must retain the computed value under that name. + cancelled_while_connecting = request_reservations[request_id]["cancelled"] + # What: gate on cancelled while connecting before pop and request id and request reservations; why: forward_routed admits pop and request id and request reservations only for this predicate and excludes the opposite state. + if cancelled_while_connecting: + # What: call request_reservations.pop with request id and the named fixture input; why: forward_routed invokes request_reservations.pop while performing else; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: select the remaining branch that performs inflight request id; why: forward_routed covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute inflight entry from name and upstream and profile and lease and profile; why: item inflight get request id later reads inflight entry, so forward_routed must retain the computed value under that name. + inflight[request_id] = { + # What: map the profile field as name and profile and lease; why: forward_routed carries profile through inflight entry into item inflight get request id. + "profile": lease.profile.name, + # What: map the upstream field as upstream; why: forward_routed carries upstream through inflight entry into item inflight get request id. + "upstream": upstream, + # What: map the cancelled field as false; why: forward_routed carries cancelled through inflight entry into item inflight get request id. + "cancelled": False, + # What: complete the inflight entry mapping with profile and upstream and cancelled; why: forward_routed groups the supplied clauses as one inflight entry mapping before its value is consumed. + } + # What: gate on cancelled while connecting before close and upstream; why: forward_routed admits close and upstream only for this predicate and excludes the opposite state. + if cancelled_while_connecting: + # What: call upstream.close with the declared inputs; why: forward_routed invokes upstream.close while performing lease release; the call advances that operation through its result or side effect. + upstream.close() + # What: call lease.release with the declared inputs; why: forward_routed invokes lease.release while performing return jsonresponse; the call advances that operation through its result or side effect. + lease.release() + # What: return jsonresponse and 409 and error and message and type from forward_routed; why: forward_routed exposes jsonresponse and 409 and error and message and type so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: forward_routed binds this 409 value to JSONResponse's status code input. + status_code=409, + # What: map the error field as message and type and request and cancelled and while; why: forward_routed carries error into content={"error": {. + content={"error": { + # What: apply the message request cancelled while opening upstream portion of the enclosing predicate; why: this clause remains in forward_routed\'s enclosing expression so its grouping and evaluation order stay intact. + "message": "request cancelled while opening upstream connection", + # What: apply the type request cancelled portion of the enclosing predicate; why: this clause remains in forward_routed\'s enclosing expression so its grouping and evaluation order stay intact. + "type": "request_cancelled", + # What: complete the enclosing predicate mapping with error; why: forward_routed groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + }}, + # What: complete the JSONResponse call with status code and content; why: forward_routed groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: preserve the exact router event admitted lease event identity lease route safe route literal fragment; why: forward_routed passes this fragment verbatim through router_event("admitted", **lease_event_identity(lease), route=safe_route, because changing it would alter a protocol payload, serialized fixtu. + router_event("admitted", **lease_event_identity(lease), route=safe_route) + + # What: define stream_response around the current object state; why: its direct callers call stream_response for stream response and rely on this exact input and result contract. + def stream_response(): + # What: compute first byte at from the named fixture input; why: if first byte at is later reads first byte at, so stream_response must retain the computed value under that name. + first_byte_at = None + # What: compute byte count from 0; why: byte count len chunk later reads byte count, so stream_response must retain the computed value under that name. + byte_count = 0 + # What: compute captured response from bytearray; why: if len captured response len chunk capture limit later reads captured response, so stream_response must retain the computed value under that name. + captured_response = bytearray() + # What: compute capture overflow from false; why: if capture limit and not capture overflow later reads capture overflow, so stream_response must retain the computed value under that name. + capture_overflow = False + # What: establish the handler boundary for the protected operation; why: stream_response routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: iterate across chunks and upstream to perform first byte at and monotonic and time; why: stream_response repeats the body only while or for the loop header admits an iteration. + for chunk in upstream.chunks(): + # What: gate on first byte at before first byte at and monotonic and time; why: stream_response admits first byte at and monotonic and time only for this predicate and excludes the opposite state. + if first_byte_at is None: + # What: compute first byte at from monotonic and time; why: ttft s first byte at started if first byte at is later reads first byte at, so stream_response must retain the computed value under that name. + first_byte_at = time.monotonic() + # What: compute byte count from len and chunk; why: response bytes byte count later reads byte count, so stream_response must retain the computed value under that name. + byte_count += len(chunk) + # What: gate on capture limit and capture overflow before capture limit and capture overflow and extend and chunk and clear; why: stream_response admits capture limit and capture overflow and extend and chunk and clear only for this predicate and excludes the opposite state. + if capture_limit and not capture_overflow: + # What: gate on capture limit and len and captured response and chunk before extend and chunk and captured response; why: stream_response admits extend and chunk and captured response only for this predicate and excludes the opposite state. + if len(captured_response) + len(chunk) <= capture_limit: + # What: call captured_response.extend with chunk; why: stream_response invokes captured_response.extend while performing else; the call advances that operation through its result or side effect. + captured_response.extend(chunk) + # What: select the remaining branch that performs capture overflow; why: stream_response covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute capture overflow from true; why: response body if capture overflow else bytes captured response later reads capture overflow, so stream_response must retain the computed value under that name. + capture_overflow = True + # What: call captured_response.clear with the declared inputs; why: stream_response invokes captured_response.clear while performing yield chunk; the call advances that operation through its result or side effect. + captured_response.clear() + # What: apply the yield chunk portion of the enclosing predicate; why: this clause remains in stream_response\'s enclosing expression so its grouping and evaluation order stay intact. + yield chunk + # What: run ended time monotonic on every exit path; why: stream_response performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: compute ended from monotonic and time; why: duration s ended started later reads ended, so stream_response must retain the computed value under that name. + ended = time.monotonic() + # What: enter the inflight lock managed context before item inflight get request id; why: stream_response releases this resource or lock after item inflight get request id on both success and failure paths. + with inflight_lock: + # What: compute item from get and request id and inflight; why: cancelled bool item get cancelled later reads item, so stream_response must retain the computed value under that name. + item = inflight.get(request_id, {}) + # What: compute cancelled from bool and get and item and cancelled; why: completed not cancelled later reads cancelled, so stream_response must retain the computed value under that name. + cancelled = bool(item.get("cancelled")) + # What: gate on upstream and get and item before pop and request id and inflight; why: stream_response admits pop and request id and inflight only for this predicate and excludes the opposite state. + if item.get("upstream") is upstream: + # What: call inflight.pop with request id and the named fixture input; why: stream_response invokes inflight.pop while performing request reservations pop request id; the call advances that operation through its result or side effect. + inflight.pop(request_id, None) + # What: call request_reservations.pop with request id and the named fixture input; why: stream_response invokes request_reservations.pop while performing ttft s first byte at started if first byte at is; the call advances that operation through its result or side effect. + request_reservations.pop(request_id, None) + # What: compute ttft s from first byte at and started; why: ttft s ttft s later reads ttft s, so stream_response must retain the computed value under that name. + ttft_s = (first_byte_at - started) if first_byte_at is not None else None + # What: call router.record_stream with the declared inputs; why: stream_response invokes router.record_stream while performing ttft s ttft s; the call advances that operation through its result or side effect. + router.record_stream( + # What: supply ttft s to router.record_stream; why: stream_response binds this ttft s value to router.record_stream's ttft s input. + ttft_s=ttft_s, + # What: supply duration s to router.record_stream; why: stream_response binds this ended and started value to router.record_stream's duration s input. + duration_s=ended - started, + # What: supply response bytes to router.record_stream; why: stream_response binds this byte count value to router.record_stream's response bytes input. + response_bytes=byte_count, + # What: supply completed to router.record_stream; why: stream_response binds this cancelled value to router.record_stream's completed input. + completed=not cancelled, + # What: complete the router.record_stream call with ttft s and duration s and response bytes and completed; why: stream_response groups the supplied clauses as one router.record_stream call before its value is consumed. + ) + # What: call lease.release with the declared inputs; why: stream_response invokes lease.release while performing router event; the call advances that operation through its result or side effect. + lease.release() + # What: call router_event with request finished; why: stream_response invokes router_event while performing request finished; the call advances that operation through its result or side effect. + router_event( + # What: preserve the exact request finished literal fragment; why: stream_response passes this fragment verbatim through "request_finished", because changing it would alter a protocol payload, serialized fixture, or public message. + "request_finished", + # What: supply expanded arguments to lease_event_identity; why: stream_response binds this lease event identity and lease value to lease_event_identity's expanded input. + **lease_event_identity(lease), + # What: supply route to router_event; why: stream_response binds this safe route value to router_event's route input. + route=safe_route, + # What: supply status to router_event; why: stream_response binds this status and upstream value to router_event's status input. + status=upstream.status, + # What: supply cancelled to router_event; why: stream_response binds this cancelled value to router_event's cancelled input. + cancelled=cancelled, + # What: supply response bytes to router_event; why: stream_response binds this byte count value to router_event's response bytes input. + responseBytes=byte_count, + # What: complete the router_event call with route and status and cancelled and response bytes; why: stream_response groups the supplied clauses as one router_event call before its value is consumed. + ) + # What: call activity_store.record with the declared inputs; why: stream_response invokes activity_store.record while performing model lease profile name; the call advances that operation through its result or side effect. + activity_store.record( + # What: supply model to activity_store.record; why: stream_response binds this name and profile and lease value to activity_store.record's model input. + model=lease.profile.name, + # What: supply route to activity_store.record; why: stream_response binds this safe route value to activity_store.record's route input. + route=safe_route, + # What: supply method to activity_store.record; why: stream_response binds this method and request value to activity_store.record's method input. + method=request.method, + # What: supply status to activity_store.record; why: stream_response binds this status and upstream value to activity_store.record's status input. + status=upstream.status, + # What: supply started to activity_store.record; why: stream_response binds this started value to activity_store.record's started input. + started=started, + # What: supply ttft s to activity_store.record; why: stream_response binds this ttft s value to activity_store.record's ttft s input. + ttft_s=ttft_s, + # What: supply response bytes to activity_store.record; why: stream_response binds this byte count value to activity_store.record's response bytes input. + response_bytes=byte_count, + # What: supply cancelled to activity_store.record; why: stream_response binds this cancelled value to activity_store.record's cancelled input. + cancelled=cancelled, + # What: supply request headers to activity_store.record; why: stream_response binds this headers and request value to activity_store.record's request headers input. + request_headers=request.headers, + # What: supply request body to activity_store.record; why: stream_response binds this outbound body value to activity_store.record's request body input. + request_body=outbound_body, + # What: supply response headers to activity_store.record; why: stream_response binds this headers and upstream value to activity_store.record's response headers input. + response_headers=upstream.headers, + # What: supply response body to bytes; why: stream_response binds this capture overflow and bytes and captured response value to bytes's response body input. + response_body=None if capture_overflow else bytes(captured_response), + # What: complete the activity_store.record call with model and route and method and status and started; why: stream_response groups the supplied clauses as one activity_store.record call before its value is consumed. + ) + + # What: compute headers from response headers and headers and upstream; why: headers x ft request id request id later reads headers, so forward_routed must retain the computed value under that name. + headers = response_headers(upstream.headers) + # What: compute headers entry from request id; why: headers headers later reads headers entry, so forward_routed must retain the computed value under that name. + headers["X-FT-Request-ID"] = request_id + # What: return streaming response and stream response and status and headers from forward_routed; why: forward_routed exposes streaming response and stream response and status and headers so its caller can continue with the function\'s computed outcome. + return StreamingResponse( + # What: call stream_response with the declared inputs; why: forward_routed invokes stream_response while performing status code upstream status; the call advances that operation through its result or side effect. + stream_response(), + # What: supply status code to StreamingResponse; why: forward_routed binds this status and upstream value to StreamingResponse's status code input. + status_code=upstream.status, + # What: supply headers to StreamingResponse; why: forward_routed binds this headers value to StreamingResponse's headers input. + headers=headers, + # What: supply media type to upstream.headers.get; why: forward_routed binds this get and headers and upstream and content type value to upstream.headers.get's media type input. + media_type=upstream.headers.get("Content-Type"), + # What: complete the StreamingResponse call with status code and headers and media type; why: forward_routed groups the supplied clauses as one StreamingResponse call before its value is consumed. + ) + + # What: define route_inference around request; why: its direct callers call route_inference for route inference and rely on this exact input and result contract. + async def route_inference(request: Request): + # What: compute body from body and request; why: model request model body later reads body, so route_inference must retain the computed value under that name. + body = await request.body() + # What: establish the handler boundary for the protected operation; why: route_inference routes failures to request model error and catalog error while preserving cleanup and success flow. + try: + # What: compute model from request model and body; why: if not router has routable id model later reads model, so route_inference must retain the computed value under that name. + model = request_model(body) + # What: gate on has routable id and model and router before catalog error and model; why: route_inference admits catalog error and model only for this predicate and excludes the opposite state. + if not router.has_routable_id(model): + # What: raise CatalogError for the caller; why: route_inference stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"unknown model profile {model!r}") + # What: handle request model error by raise httpexception status code 400 detail str exc; why: route_inference converts that failure into this concrete recovery, response, or cleanup behavior. + except RequestModelError as exc: + # What: raise HTTPException for the caller; why: route_inference stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=400, detail=str(exc)) from exc + # What: handle catalog error by return jsonresponse; why: route_inference converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError as exc: + # What: return jsonresponse and str and exc and 404 and error from route_inference; why: route_inference exposes jsonresponse and str and exc and 404 and error so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: route_inference binds this 404 value to JSONResponse's status code input. + status_code=404, + # What: map the error field as str and exc and message and type and unknown model; why: route_inference carries error into content={"error": {"message": str(exc), "type": "unknown_model"}}. + content={"error": {"message": str(exc), "type": "unknown_model"}}, + # What: complete the JSONResponse call with status code and content; why: route_inference groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: compute suffix from query and url and request and value and value; why: path and query request url path suffix later reads suffix, so route_inference must retain the computed value under that name. + suffix = f"?{request.url.query}" if request.url.query else "" + # What: return forward routed and request and model and body from route_inference; why: route_inference exposes forward routed and request and model and body so its caller can continue with the function\'s computed outcome. + return await forward_routed( + # What: apply the request portion of the enclosing predicate; why: this clause remains in route_inference\'s enclosing expression so its grouping and evaluation order stay intact. + request, + # What: apply the model portion of the enclosing predicate; why: this clause remains in route_inference\'s enclosing expression so its grouping and evaluation order stay intact. + model, + # What: supply path and query to forward_routed; why: route_inference binds this path and suffix and url and request value to forward_routed's path and query input. + path_and_query=request.url.path + suffix, + # What: supply body to forward_routed; why: route_inference binds this body value to forward_routed's body input. + body=body, + # What: supply apply request filters to forward_routed; why: route_inference binds this true value to forward_routed's apply request filters input. + apply_request_filters=True, + # What: complete the forward_routed call with path and query and body and apply request filters; why: route_inference groups the supplied clauses as one forward_routed call before its value is consumed. + ) + + # FreeToken's supported inference surface. All routes use the same native + # admission and proxy path so an OpenAI or Anthropic client cannot bypass + # lifecycle, accounting, readiness, or cancellation ownership. + # What: register POST /v1/chat/completions on the application router; why: clients reach inference_proxy's handler only through this method-and-path binding. + @app.post("/v1/chat/completions", dependencies=[Depends(require_router_key)]) + # What: register POST /v1/completions on the application router; why: clients reach inference_proxy's handler only through this method-and-path binding. + @app.post("/v1/completions", dependencies=[Depends(require_router_key)]) + # What: register POST /v1/responses on the application router; why: clients reach inference_proxy's handler only through this method-and-path binding. + @app.post("/v1/responses", dependencies=[Depends(require_router_key)]) + # What: register POST /v1/messages on the application router; why: clients reach inference_proxy's handler only through this method-and-path binding. + @app.post("/v1/messages", dependencies=[Depends(require_router_key)]) + # What: register POST /v1/messages/count_tokens on the application router; why: clients reach inference_proxy's handler only through this method-and-path binding. + @app.post("/v1/messages/count_tokens", dependencies=[Depends(require_router_key)]) + # What: define inference_proxy around request; why: the registered API client call inference_proxy for inference proxy and rely on this exact input and result contract. + async def inference_proxy(request: Request): + # What: return route inference and request from inference_proxy; why: inference_proxy exposes route inference and request so its caller can continue with the function\'s computed outcome. + return await route_inference(request) + + # FreeToken's Responses implementation is deliberately stateless. Keep its + # registered resource routes available at the stable daemon URL, but do not + # activate an arbitrary model for a request that carries no model identity. + # The envelope matches the engine contract and remains behind inference auth. + # What: register GET /v1/responses/{response_id} on the application router; why: clients reach stateless_response_not_found's handler only through this method-and-path binding. + @app.get("/v1/responses/{response_id}", dependencies=[Depends(require_router_key)]) + # What: register POST the configured path on the application router; why: clients reach stateless_response_not_found's handler only through this method-and-path binding. + @app.post( + # What: apply the v1 responses response id cancel portion of the enclosing predicate; why: this clause remains in stateless_response_not_found\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/responses/{response_id}/cancel", + # What: supply dependencies to Depends; why: stateless_response_not_found binds this depends and require router key value to Depends's dependencies input. + dependencies=[Depends(require_router_key)], + # What: complete the app.post call with dependencies; why: stateless_response_not_found groups the supplied clauses as one app.post call before its value is consumed. + ) + # What: define stateless_response_not_found around response id; why: the registered API client call stateless_response_not_found for stateless response not found and rely on this exact input and result contract. + async def stateless_response_not_found(response_id: str): + # What: return jsonresponse and response id and 404 and error and message from stateless_response_not_found; why: stateless_response_not_found exposes jsonresponse and response id and 404 and error and message so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: stateless_response_not_found binds this 404 value to JSONResponse's status code input. + status_code=404, + # What: supply content to JSONResponse; why: stateless_response_not_found binds this response id and error and message and type and code value to JSONResponse's content input. + content={ + # What: apply the error portion of the enclosing predicate; why: this clause remains in stateless_response_not_found\'s enclosing expression so its grouping and evaluation order stay intact. + "error": { + # What: map the message field as response id and response and not and found and stateless; why: stateless_response_not_found carries message into "message": f"response {response_id!r} not found (stateless server)". + "message": f"response {response_id!r} not found (stateless server)", + # What: map the type field as invalid request error; why: stateless_response_not_found carries type into "type": "invalid_request_error". + "type": "invalid_request_error", + # What: map the code field as the fixture input; why: stateless_response_not_found carries code into "code": None. + "code": None, + # What: complete the enclosing predicate mapping with message and type and code; why: stateless_response_not_found groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + # What: complete the enclosing predicate mapping with error; why: stateless_response_not_found groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + }, + # What: complete the JSONResponse call with status code and content; why: stateless_response_not_found groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + + # What: register GET /models on the application router; why: clients reach openai_model_list's handler only through this method-and-path binding. + @app.get("/models", dependencies=[Depends(require_router_key)]) + # What: register GET /v1/models on the application router; why: clients reach openai_model_list's handler only through this method-and-path binding. + @app.get("/v1/models", dependencies=[Depends(require_router_key)]) + # What: define openai_model_list around request; why: the registered API client call openai_model_list for openai model list and rely on this exact input and result contract. + async def openai_model_list(request: Request): + """OpenAI-compatible public metadata without exposing local model paths.""" + # What: document open ai compatible public metadata without exposing local in the openai_model_list docstring; why: introspection and maintainers read this exact docstring fragment to understand openai model list behavior without executing it. + # What: evaluate and capture catalog snapshot loaded profiles active routing profile; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + catalog_snapshot, loaded_profiles, active_routing_profile = ( + # What: call router.public_model_listing_snapshot with the declared inputs; why: openai_model_list consumes the router.public_model_listing_snapshot return value while evaluating router.public_model_listing_snapshot(). + router.public_model_listing_snapshot() + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + ) + # What: compute created from int and time; why: created created later reads created, so openai_model_list must retain the computed value under that name. + created = int(time.time()) + # What: initialize data as an empty runtime accumulator; why: openai_model_list appends or maps entries into it during data append record before consuming the aggregate. + data = [] + # What: iterate across listed model ids and catalog snapshot to perform selector and model id and catalog snapshot; why: openai_model_list repeats the body only while or for the loop header admits an iteration. + for model_id in catalog_snapshot.listed_model_ids(): + # What: compute selector from selector and model id and catalog snapshot; why: profile if selector is not else later reads selector, so openai_model_list must retain the computed value under that name. + selector = catalog_snapshot.selector(model_id) + # What: compute profile from selector and get and model id and catalog snapshot; why: loaded profile name in loaded profiles later reads profile, so openai_model_list must retain the computed value under that name. + profile = None if selector is not None else catalog_snapshot.get(model_id) + # What: gate on selector before loaded and name and loaded profiles and profile; why: openai_model_list admits loaded and name and loaded profiles and profile only for this predicate and excludes the opposite state. + if selector is None: + # What: compute loaded from name and loaded profiles and profile; why: loaded any later reads loaded, so openai_model_list must retain the computed value under that name. + loaded = profile.name in loaded_profiles + # What: select the remaining branch that performs targets selector targets if selector strategy pin else; why: openai_model_list covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute targets from targets and strategy and selector and pin and 1; why: catalog snapshot get target name in loaded profiles for later reads targets, so openai_model_list must retain the computed value under that name. + targets = selector.targets[:1] if selector.strategy == "pin" else selector.targets + # What: compute loaded from any and name and loaded profiles and target; why: value loaded if loaded else unloaded later reads loaded, so openai_model_list must retain the computed value under that name. + loaded = any( + # What: call catalog_snapshot.get with target; why: openai_model_list consumes the catalog_snapshot.get return value while evaluating catalog_snapshot.get(target).name in loaded_profiles for target in targe. + catalog_snapshot.get(target).name in loaded_profiles for target in targets + # What: complete the any call with name; why: openai_model_list groups the supplied clauses as one any call before its value is consumed. + ) + # What: compute record from model id and created and loaded and id and object; why: record name selector display name later reads record, so openai_model_list must retain the computed value under that name. + record = { + # What: map the id field as model id; why: openai_model_list carries id through record into record name selector display name. + "id": model_id, + # What: map the object field as model; why: openai_model_list carries object through record into record name selector display name. + "object": "model", + # What: map the created field as created; why: openai_model_list carries created through record into record name selector display name. + "created": created, + # What: map the owned by field as freetoken; why: openai_model_list carries owned by through record into record name selector display name. + "owned_by": "freetoken", + # What: apply the status portion of record; why: openai_model_list uses this clause to evaluate record as one grouped value. + "status": { + # What: map the value field as loaded and loaded and unloaded; why: openai_model_list carries value through record into record name selector display name. + "value": "loaded" if loaded else "unloaded" + # What: complete the record mapping with value; why: openai_model_list groups the supplied clauses as one record mapping before its value is consumed. + }, + # What: complete the record mapping with id and object and created and owned by and status; why: openai_model_list groups the supplied clauses as one record mapping before its value is consumed. + } + # What: gate on selector before display name and selector and record; why: openai_model_list admits display name and selector and record only for this predicate and excludes the opposite state. + if selector is not None: + # What: gate on display name and selector before display name and record and selector; why: openai_model_list admits display name and record and selector only for this predicate and excludes the opposite state. + if selector.display_name: + # What: compute record entry from display name and selector; why: record description selector description later reads record entry, so openai_model_list must retain the computed value under that name. + record["name"] = selector.display_name + # What: gate on description and selector before description and record and selector; why: openai_model_list admits description and record and selector only for this predicate and excludes the opposite state. + if selector.description: + # What: compute record entry from description and selector; why: record meta freetoken selector metadata later reads record entry, so openai_model_list must retain the computed value under that name. + record["description"] = selector.description + # What: compute selector metadata from metadata and selector; why: selector metadata update later reads selector metadata, so openai_model_list must retain the computed value under that name. + selector_metadata = selector.metadata() + # What: call selector_metadata.update with strategy and selector and list and targets and type; why: openai_model_list invokes selector_metadata.update while performing type selector; the call advances that operation through its result or side effect. + selector_metadata.update({ + # What: map the type field as selector; why: openai_model_list carries type into "type": "selector". + "type": "selector", + # What: map the strategy field as strategy and selector; why: openai_model_list carries strategy into "strategy": selector.strategy. + "strategy": selector.strategy, + # What: map the targets field as list and targets and selector; why: openai_model_list carries targets into "targets": list(selector.targets). + "targets": list(selector.targets), + # What: complete the selector_metadata.update call with strategy; why: openai_model_list groups the supplied clauses as one selector_metadata.update call before its value is consumed. + }) + # What: map the freetoken field as selector metadata; why: openai_model_list carries freetoken through record entry into record name profile display name strip. + record["meta"] = {"freetoken": selector_metadata} + # What: gate on profile before display name and profile and record and strip; why: openai_model_list admits display name and profile and record and strip only for this predicate and excludes the opposite state. + if profile is not None: + # What: gate on display name and profile before record and strip and display name and profile; why: openai_model_list admits record and strip and display name and profile only for this predicate and excludes the opposite state. + if profile.display_name: + # What: compute record entry from strip and display name and profile; why: record description profile description strip later reads record entry, so openai_model_list must retain the computed value under that name. + record["name"] = profile.display_name.strip() + # What: gate on description and profile before record and strip and description and profile; why: openai_model_list admits record and strip and description and profile only for this predicate and excludes the opposite state. + if profile.description: + # What: compute record entry from strip and description and profile; why: record update capability fields later reads record entry, so openai_model_list must retain the computed value under that name. + record["description"] = profile.description.strip() + # What: compute capability fields from model listing fields and capabilities and profile; why: record update capability fields later reads capability fields, so openai_model_list must retain the computed value under that name. + capability_fields = profile.capabilities.model_listing_fields() + # What: call record.update with capability fields; why: openai_model_list invokes record.update while performing metadata profile metadata; the call advances that operation through its result or side effect. + record.update(capability_fields) + # What: compute metadata from metadata and profile; why: metadata pop key later reads metadata, so openai_model_list must retain the computed value under that name. + metadata = profile.metadata() + # What: gate on empty and capabilities and profile before key and pop and metadata; why: openai_model_list admits key and pop and metadata only for this predicate and excludes the opposite state. + if not profile.capabilities.empty(): + # What: iterate across the computed value to perform pop and key and metadata; why: openai_model_list repeats the body only while or for the loop header admits an iteration. + for key in ( + # What: apply the architecture capabilities supported parameters portion of the enclosing predicate; why: this clause remains in openai_model_list\'s enclosing expression so its grouping and evaluation order stay intact. + "architecture", "capabilities", "supported_parameters", + # What: apply the context length context window portion of the enclosing predicate; why: this clause remains in openai_model_list\'s enclosing expression so its grouping and evaluation order stay intact. + "context_length", "context_window", + # What: complete the enclosing predicate collection with architecture and capabilities and supported parameters and context length; why: openai_model_list groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + ): + # What: call metadata.pop with key and the named fixture input; why: openai_model_list invokes metadata.pop while performing if model id profile name; the call advances that operation through its result or side effect. + metadata.pop(key, None) + # What: gate on model id and name and profile before internal metadata; why: openai_model_list admits internal metadata only for this predicate and excludes the opposite state. + if model_id == profile.name: + # What: map the type field as model; why: openai_model_list carries type through internal metadata into internal metadata aliases list profile aliases. + internal_metadata = {"type": "model"} + # What: gate on aliases and profile before internal metadata and list and aliases and profile; why: openai_model_list admits internal metadata and list and aliases and profile only for this predicate and excludes the opposite state. + if profile.aliases: + # What: compute internal metadata entry from list and aliases and profile; why: internal metadata type alias model id profile name later reads internal metadata entry, so openai_model_list must retain the computed value under that name. + internal_metadata["aliases"] = list(profile.aliases) + # What: select the remaining branch that performs internal metadata type alias model id profile name; why: openai_model_list covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: map the type field as alias; why: openai_model_list carries type through internal metadata into metadata update internal metadata. + internal_metadata = {"type": "alias", "modelID": profile.name} + # What: call metadata.update with internal metadata; why: openai_model_list invokes metadata.update while performing record setdefault meta freetoken metadata; the call advances that operation through its result or side effect. + metadata.update(internal_metadata) + # What: compute result entry from metadata; why: the enclosing return or state update later reads result entry, so openai_model_list must retain the computed value under that name. + record.setdefault("meta", {})["freetoken"] = metadata + # What: call data.append with record; why: openai_model_list invokes data.append while performing routing profile; the call advances that operation through its result or side effect. + data.append(record) + # What: compute routing profile from active routing profile and routing profile and catalog snapshot; why: catalog snapshot routing profile active routing profile later reads routing profile, so openai_model_list must retain the computed value under that name. + routing_profile = ( + # What: call catalog_snapshot.routing_profile with active routing profile; why: openai_model_list invokes catalog_snapshot.routing_profile while performing if active routing profile is not else; the call advances that operation through its result or side effect. + catalog_snapshot.routing_profile(active_routing_profile) + # What: apply the if active routing profile is not else portion of routing profile; why: openai_model_list uses this clause to evaluate routing profile as one grouped value. + if active_routing_profile is not None else None + # What: complete the routing_profile expression with routing profile catalog snapshot routing profile active routing profile if active routing profile is not else; why: openai_model_list groups the supplied clauses as one routing_profile expression before its value is consumed. + ) + # What: gate on routing profile before pins and pin and target and routing profile and append; why: openai_model_list admits pins and pin and target and routing profile and append only for this predicate and excludes the opposite state. + if routing_profile is not None: + # What: iterate across pins and routing profile to perform target and has routable id and pin and catalog snapshot; why: openai_model_list repeats the body only while or for the loop header admits an iteration. + for pin, target in routing_profile.pins: + # What: gate on target and has routable id and pin and catalog snapshot before the computed value; why: openai_model_list admits the computed value only for this predicate and excludes the opposite state. + if target is None or catalog_snapshot.has_routable_id(pin): + # What: apply the continue portion of the enclosing predicate; why: this clause remains in openai_model_list\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: call data.append with pin and created and id and object and created; why: openai_model_list invokes data.append while performing id pin; the call advances that operation through its result or side effect. + data.append({ + # What: map the id field as pin; why: openai_model_list carries id into "id": pin. + "id": pin, + # What: map the object field as model; why: openai_model_list carries object into "object": "model". + "object": "model", + # What: map the created field as created; why: openai_model_list carries created into "created": created. + "created": created, + # What: map the owned by field as freetoken; why: openai_model_list carries owned by into "owned_by": "freetoken". + "owned_by": "freetoken", + # What: map the value field as unloaded; why: openai_model_list carries value into "status": {"value": "unloaded"}. + "status": {"value": "unloaded"}, + # What: map the freetoken field as type and profile; why: openai_model_list carries freetoken into "meta": {"freetoken": {"type": "profile"}}. + "meta": {"freetoken": {"type": "profile"}}, + # What: complete the data.append call with pin; why: openai_model_list groups the supplied clauses as one data.append call before its value is consumed. + }) + # What: compute response from jsonresponse and data and object and data and list; why: response headers access control allow origin origin later reads response, so openai_model_list must retain the computed value under that name. + response = JSONResponse(content={ + # What: map the object field as list; why: openai_model_list carries object through response into response headers access control allow origin origin. + "object": "list", + # What: map the data field as data; why: openai_model_list carries data through response into response headers access control allow origin origin. + "data": data, + # What: complete the JSONResponse call with content; why: openai_model_list groups the supplied clauses as one JSONResponse call before its value is consumed. + }) + # What: gate on origin and get and headers and request before origin and headers and response; why: openai_model_list admits origin and headers and response only for this predicate and excludes the opposite state. + if origin := request.headers.get("origin"): + # What: compute headers entry from origin; why: the enclosing return or state update later reads headers entry, so openai_model_list must retain the computed value under that name. + response.headers["Access-Control-Allow-Origin"] = origin + # What: return response from openai_model_list; why: openai_model_list exposes response so its caller can continue with the function\'s computed outcome. + return response + + # What: apply app.api_route behavior to upstream_proxy; why: Python attaches this named decorator's registration or descriptor semantics to upstream_proxy. + @app.api_route( + # What: apply the upstream upstream path path portion of the enclosing predicate; why: this clause remains in upstream_proxy\'s enclosing expression so its grouping and evaluation order stay intact. + "/upstream/{upstream_path:path}", + # What: supply methods to app.api_route; why: upstream_proxy binds this get and post and put and patch and delete value to app.api_route's methods input. + methods=["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"], + # What: supply dependencies to Depends; why: upstream_proxy binds this depends and require router key value to Depends's dependencies input. + dependencies=[Depends(require_router_key)], + # What: complete the app.api_route call with methods and dependencies; why: upstream_proxy groups the supplied clauses as one app.api_route call before its value is consumed. + ) + # What: define upstream_proxy around request and upstream path; why: the registered API client call upstream_proxy for upstream proxy and rely on this exact input and result contract. + async def upstream_proxy(request: Request, upstream_path: str): + # What: establish the handler boundary for the protected operation; why: upstream_proxy routes failures to catalog error while preserving cleanup and success flow. + try: + # What: compute source model and model and and remaining path from resolve upstream path and upstream path and ro; why: upstream_proxy consumes source model and model and and remaining path during escaped path suffix raw path f upstream source model, so source model and model and and remaining path value r. + source_model, model, _, remaining_path = router.resolve_upstream_path( + # What: apply the upstream path portion of source model and model and and remaining path; why: upstream_proxy uses this clause to evaluate source model and model and and remaining path as one grouped value. + upstream_path + # What: complete the router.resolve_upstream_path call with upstream path; why: upstream_proxy groups the supplied clauses as one router.resolve_upstream_path call before its value is consumed. + ) + # What: handle catalog error by return jsonresponse; why: upstream_proxy converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError as exc: + # What: return jsonresponse and str and exc and 404 and error from upstream_proxy; why: upstream_proxy exposes jsonresponse and str and exc and 404 and error so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: upstream_proxy binds this 404 value to JSONResponse's status code input. + status_code=404, + # What: map the error field as str and exc and message and type and unknown model; why: upstream_proxy carries error into content={"error": {"message": str(exc), "type": "unknown_model"}}. + content={"error": {"message": str(exc), "type": "unknown_model"}}, + # What: complete the JSONResponse call with status code and content; why: upstream_proxy groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # The daemon alone may call prepare-stop. Exposing it through an + # arbitrary passthrough would bypass durable accounting and leave a + # misleading routing lease behind. + # What: compute normalized from lstrip and remaining path and value; why: if normalized v1 admin prepare stop later reads normalized, so upstream_proxy must retain the computed value under that name. + normalized = remaining_path.lstrip("/") + # What: gate on normalized before httpexception; why: upstream_proxy admits httpexception only for this predicate and excludes the opposite state. + if normalized == "v1/admin/prepare-stop": + # What: raise HTTPException for the caller; why: upstream_proxy stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=403, detail="upstream prepare-stop is daemon-managed") + # What: gate on any and profile is resident and model and endswith and suffix before jsonresponse and model; why: upstream_proxy admits jsonresponse and model only for this predicate and excludes the opposite state. + if ( + # What: call any with endswith and suffix and upstream no activation suffixes and remaining path; why: upstream_proxy invokes any while performing remaining path endswith suffix; the call advances that operation through its result or side effect. + any( + # What: call remaining_path.endswith with suffix; why: upstream_proxy invokes remaining_path.endswith while performing for suffix in router catalog settings upstream no activation suffixes; the call advances that operation through its result or side effect. + remaining_path.endswith(suffix) + # What: apply the for suffix in router catalog settings upstream no activation suffixes portion of the enclosing predicate; why: this clause remains in upstream_proxy\'s enclosing expression so its grouping and evaluation order stay intact. + for suffix in router.catalog.settings.upstream_no_activation_suffixes + # What: complete the any call with endswith; why: upstream_proxy groups the supplied clauses as one any call before its value is consumed. + ) + # What: call router.profile_is_resident with model; why: upstream_proxy consumes the router.profile_is_resident return value while evaluating and not router.profile_is_resident(model). + and not router.profile_is_resident(model) + # What: complete the enclosing predicate with if any remaining path endswith suffix for suffix in router catalog settings upstream no activation suffixes and; why: upstream_proxy groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): return JSONResponse( status_code=409, content={ - "error": str(exc), - "code": "serve_conflict", - "currentModel": st.get("model"), - "currentPort": st.get("port"), + # What: apply the error portion of the enclosing predicate; why: this clause remains in upstream_proxy\'s enclosing expression so its grouping and evaluation order stay intact. + "error": { + # What: map the message field as model and model and is and not and loaded; why: upstream_proxy carries message into "message": (. + "message": ( + # What: embed the exact f model model r is not router-interface fragment; why: the router UI consumer receives this fragment verbatim through f"model {model!r} is not loaded; path matches ", preserving browser markup, style, or script behavior. + # What: preserve the exact router upstream no activation suffixes literal fragment; why: upstream_proxy passes this fragment verbatim through f"model {model!r} is not loaded; path matches ", because changing it would alter a protocol payload, serialized fixture, or public message. + f"model {model!r} is not loaded; path matches " + "router.upstream_no_activation_suffixes" + # What: complete the enclosing predicate mapping with message and type; why: upstream_proxy groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + ), + # What: map the type field as model not loaded; why: upstream_proxy carries type into "type": "model_not_loaded". + "type": "model_not_loaded", + # What: complete the enclosing predicate mapping with message and type; why: upstream_proxy groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } }, ) - except Exception as exc: # noqa: BLE001 — never propagate a 500-as-crash - raise HTTPException(status_code=500, detail=f"start failed: {exc}") + # What: compute raw path from get and scope and request and raw path; why: escaped path suffix raw path f upstream source model later reads raw path, so upstream_proxy must retain the computed value under that name. + raw_path = request.scope.get("raw_path") + # What: compute escaped path from isinstance and raw path and bytes and escaped path suffix; why: if escaped path is later reads escaped path, so upstream_proxy must retain the computed value under that name. + escaped_path = ( + # What: call _escaped_path_suffix with raw path and source model and upstream; why: upstream_proxy invokes _escaped_path_suffix while performing if isinstance raw path bytes else; the call advances that operation through its result or side effect. + _escaped_path_suffix(raw_path, f"/upstream/{source_model}") + # What: call isinstance with raw path and bytes; why: upstream_proxy consumes the isinstance return value while evaluating if isinstance(raw_path, bytes) else None. + if isinstance(raw_path, bytes) else None + # What: complete the escaped_path expression with escaped path escaped path suffix raw path f upstream source model if isinstance raw path; why: upstream_proxy groups the supplied clauses as one escaped_path expression before its value is consumed. + ) + # What: gate on escaped path before httpexception; why: upstream_proxy admits httpexception only for this predicate and excludes the opposite state. + if escaped_path is None: + # What: raise HTTPException for the caller; why: upstream_proxy stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=400, detail="invalid escaped upstream path") + # What: gate on escaped path before escaped path; why: upstream_proxy admits escaped path only for this predicate and excludes the opposite state. + if not escaped_path: + # What: compute escaped path from value; why: path and query escaped path suffix later reads escaped path, so upstream_proxy must retain the computed value under that name. + escaped_path = "/" + # What: compute raw query from get and scope and request and query string; why: suffix f raw query decode ascii if raw query later reads raw query, so upstream_proxy must retain the computed value under that name. + raw_query = request.scope.get("query_string", b"") + # What: compute suffix from raw query and decode and value and value and ascii; why: path and query escaped path suffix later reads suffix, so upstream_proxy must retain the computed value under that name. + suffix = f"?{raw_query.decode('ascii')}" if raw_query else "" + # What: return forward routed and request and model and escaped path from upstream_proxy; why: upstream_proxy exposes forward routed and request and model and escaped path so its caller can continue with the function\'s computed outcome. + return await forward_routed( + # What: apply the request portion of the enclosing predicate; why: this clause remains in upstream_proxy\'s enclosing expression so its grouping and evaluation order stay intact. + request, + # What: apply the model portion of the enclosing predicate; why: this clause remains in upstream_proxy\'s enclosing expression so its grouping and evaluation order stay intact. + model, + # What: supply path and query to forward_routed; why: upstream_proxy binds this escaped path and suffix value to forward_routed's path and query input. + path_and_query=escaped_path + suffix, + # What: supply body to request.body; why: upstream_proxy binds this body and request value to request.body's body input. + body=await request.body(), + # What: supply apply request filters to operation.lower; why: upstream_proxy binds this lower and get and headers and request and application value to operation.lower's apply request filters input. + apply_request_filters="application/json" in request.headers.get( + # What: apply the content type portion of the enclosing predicate; why: this clause remains in upstream_proxy\'s enclosing expression so its grouping and evaluation order stay intact. + "content-type", "" + # What: apply the lower portion of the enclosing predicate; why: this clause remains in upstream_proxy\'s enclosing expression so its grouping and evaluation order stay intact. + ).lower(), + # What: complete the forward_routed call with path and query and body and apply request filters; why: upstream_proxy groups the supplied clauses as one forward_routed call before its value is consumed. + ) - @app.post("/engine/stop", dependencies=auth) - async def engine_stop(body: StopBody | None = None): + # What: register GET /router/status on the application router; why: clients reach router_status's handler only through this method-and-path binding. + @app.get("/router/status", dependencies=auth) + # What: define router_status around the current object state; why: the registered API client call router_status for router status and rely on this exact input and result contract. + async def router_status(): + # What: map the catalog watch field as catalog watch snapshot; why: router_status carries catalog watch into return {**router.status(), "catalogWatch": catalog_watch_snapshot()}. + return {**router.status(), "catalogWatch": catalog_watch_snapshot()} + + # What: register GET /router/models on the application router; why: clients reach router_models's handler only through this method-and-path binding. + @app.get("/router/models", dependencies=auth) + # What: define router_models around the current object state; why: the registered API client call router_models for router models and rely on this exact input and result contract. + async def router_models(): + """Configured profiles annotated with the sole engine's live residency.""" + # What: document configured profiles annotated with the sole in the router_models docstring; why: introspection and maintainers read this exact docstring fragment to understand router models behavior without executing it. + # What: compute catalog snapshot and route state from control plane snapshot and router; why: for profile in catalog snapshot public later reads catalog snapshot and route state, so router_models must retain the computed value under that name. + catalog_snapshot, route_state = router.control_plane_snapshot() + # What: compute engine from status and manager; why: profile name active and bool engine get later reads engine, so router_models must retain the computed value under that name. + engine = manager.status() + # What: compute active from route state and active profile; why: profile name active and bool engine get later reads active, so router_models must retain the computed value under that name. + active = route_state["activeProfile"] + # What: compute active identity matches from route state and active identity matches engine; why: profile name active and bool engine get later reads active identity matches, so router_models must retain the computed value under that name. + active_identity_matches = route_state["activeIdentityMatchesEngine"] + # What: initialize data as an empty runtime accumulator; why: router_models appends or maps entries into it during data append profile before consuming the aggregate. + data = [] + # What: iterate across public and catalog snapshot to perform profile and dict; why: router_models repeats the body only while or for the loop header admits an iteration. + for profile in catalog_snapshot.public(): + # What: compute profile from dict and profile; why: profile configured later reads profile, so router_models must retain the computed value under that name. + profile = dict(profile) + # What: compute profile entry from true; why: profile resident later reads profile entry, so router_models must retain the computed value under that name. + profile["configured"] = True + # What: compute profile entry from active identity matches and active and bool and profile; why: profile name active and bool engine get later reads profile entry, so router_models must retain the computed value under that name. + profile["resident"] = ( + # What: call bool with get and engine and running; why: router_models consumes the bool return value while evaluating profile["name"] == active and bool(engine.get("running")) and active_ide. + profile["name"] == active and bool(engine.get("running")) and active_identity_matches + # What: complete the profile entry expression with profile resident profile name equals active and bool engine get; why: router_models groups the supplied clauses as one profile entry expression before its value is consumed. + ) + # What: compute profile entry from profile and route state and 0 and resident and active requests; why: data append profile later reads profile entry, so router_models must retain the computed value under that name. + profile["activeRequests"] = route_state["activeRequests"] if profile["resident"] else 0 + # What: call data.append with profile; why: router_models invokes data.append while performing return; the call advances that operation through its result or side effect. + data.append(profile) + # What: return data and public selectors and public routing profiles and route state from router_models; why: router_models exposes data and public selectors and public routing profiles and route state so its caller can continue with the function\'s computed outcome. + return { + # What: map the data field as data; why: router_models carries data into "data": data. + "data": data, + # What: map the selectors field as public selectors and catalog snapshot; why: router_models carries selectors into "selectors": catalog_snapshot.public_selectors(). + "selectors": catalog_snapshot.public_selectors(), + # What: map the routing profiles field as public routing profiles and catalog snapshot; why: router_models carries routing profiles into "routingProfiles": catalog_snapshot.public_routing_profiles(). + "routingProfiles": catalog_snapshot.public_routing_profiles(), + # What: map the active routing profile field as route state and active routing profile; why: router_models carries active routing profile into "activeRoutingProfile": route_state["activeRoutingProfile"]. + "activeRoutingProfile": route_state["activeRoutingProfile"], + # What: map the capacity field as route state and capacity; why: router_models carries capacity into "capacity": route_state["capacity"]. + "capacity": route_state["capacity"], + # What: complete the enclosing predicate mapping with data and selectors and routing profiles and active routing profile and capacity; why: router_models groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + # What: register GET /router/profiles on the application router; why: clients reach router_profiles's handler only through this method-and-path binding. + @app.get("/router/profiles", dependencies=auth) + # What: define router_profiles around the current object state; why: the registered API client call router_profiles for router profiles and rely on this exact input and result contract. + async def router_profiles(): + # What: compute catalog snapshot and route state from control plane snapshot and router; why: data catalog snapshot public later reads catalog snapshot and route state, so router_profiles must retain the computed value under that name. + catalog_snapshot, route_state = router.control_plane_snapshot() + # What: return public and public selectors and public routing profiles and route state from router_profiles; why: router_profiles exposes public and public selectors and public routing profiles and route state so its caller can continue with the function\'s computed outcome. + return { + # What: map the data field as public and catalog snapshot; why: router_profiles carries data into "data": catalog_snapshot.public(). + "data": catalog_snapshot.public(), + # What: map the selectors field as public selectors and catalog snapshot; why: router_profiles carries selectors into "selectors": catalog_snapshot.public_selectors(). + "selectors": catalog_snapshot.public_selectors(), + # What: map the routing profiles field as public routing profiles and catalog snapshot; why: router_profiles carries routing profiles into "routingProfiles": catalog_snapshot.public_routing_profiles(). + "routingProfiles": catalog_snapshot.public_routing_profiles(), + # What: map the active routing profile field as route state and active routing profile; why: router_profiles carries active routing profile into "activeRoutingProfile": route_state["activeRoutingProfile"]. + "activeRoutingProfile": route_state["activeRoutingProfile"], + # What: map the active profile field as route state and active profile; why: router_profiles carries active profile into "activeProfile": route_state["activeProfile"]. + "activeProfile": route_state["activeProfile"], + # What: complete the enclosing predicate mapping with data and selectors and routing profiles and active routing profile and active profile; why: router_profiles groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + # What: register PUT /router/profiles/active on the application router; why: clients reach set_active_routing_profile's handler only through this method-and-path binding. + @app.put("/router/profiles/active", dependencies=auth) + # What: define set_active_routing_profile around body; why: the registered API client call set_active_routing_profile for set active routing profile and rely on this exact input and result contract. + async def set_active_routing_profile(body: RoutingProfileSelectionBody): + # What: establish the handler boundary for the protected operation; why: set_active_routing_profile routes failures to routing error while preserving cleanup and success flow. + try: + # What: compute active from set active routing profile and name and router and body; why: router event routing profile changed routing profile active later reads active, so set_active_routing_profile must retain the computed value under that name. + active = router.set_active_routing_profile(body.name) + # What: handle routing error by return jsonresponse; why: set_active_routing_profile converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: return jsonresponse and status code and exc and code from set_active_routing_profile; why: set_active_routing_profile exposes jsonresponse and status code and exc and code so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: set_active_routing_profile binds this status code and exc value to JSONResponse's status code input. + status_code=exc.status_code, + # What: map the error field as code and str and exc and message and type; why: set_active_routing_profile carries error into content={"error": {"message": str(exc), "type": exc.code}}. + content={"error": {"message": str(exc), "type": exc.code}}, + # What: complete the JSONResponse call with status code and content; why: set_active_routing_profile groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: preserve the exact router event routing profile changed routing profile active literal fragment; why: set_active_routing_profile passes this fragment verbatim through router_event("routing_profile_changed", routingProfile=active), because changing it would alter a protocol payload, serialized fixture. + router_event("routing_profile_changed", routingProfile=active) + # What: map the active field as active; why: set_active_routing_profile carries active into return {"active": active}. + return {"active": active} + + # What: register GET /router/hardware on the application router; why: clients reach router_hardware's handler only through this method-and-path binding. + @app.get("/router/hardware", dependencies=auth) + # What: define router_hardware around the current object state; why: the registered API client call router_hardware for router hardware and rely on this exact input and result contract. + async def router_hardware(): + """Small, privacy-preserving local memory view for the management UI.""" + # What: document small privacy preserving local memory view for in the router_hardware docstring; why: introspection and maintainers read this exact docstring fragment to understand router hardware behavior without executing it. + # What: compute engine from status and manager; why: footprint await run proxy pool footprint fn engine get later reads engine, so router_hardware must retain the computed value under that name. + engine = manager.status() + # What: compute footprint from run and proxy pool and footprint fn and get; why: memory footprint later reads footprint, so router_hardware must retain the computed value under that name. + footprint = await run(proxy_pool, footprint_fn, engine.get("pid")) + # What: return footprint and bool and get and engine and engine from router_hardware; why: router_hardware exposes footprint and bool and get and engine and engine so its caller can continue with the function\'s computed outcome. + return { + # What: apply the engine portion of the enclosing predicate; why: this clause remains in router_hardware\'s enclosing expression so its grouping and evaluation order stay intact. + "engine": { + # What: map the running field as bool and get and engine and running; why: router_hardware carries running into "running": bool(engine.get("running")). + "running": bool(engine.get("running")), + # What: map the pid field as get and engine and pid; why: router_hardware carries pid into "pid": engine.get("pid"). + "pid": engine.get("pid"), + # What: map the port field as get and engine and port; why: router_hardware carries port into "port": engine.get("port"). + "port": engine.get("port"), + # What: complete the enclosing predicate mapping with running and pid and port; why: router_hardware groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + }, + # What: map the memory field as footprint; why: router_hardware carries memory into "memory": footprint. + "memory": footprint, + # What: complete the enclosing predicate mapping with engine and memory; why: router_hardware groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + # What: register GET /api/performance on the application router; why: clients reach router_performance's handler only through this method-and-path binding. + @app.get("/api/performance", dependencies=auth) + # What: register GET /router/performance on the application router; why: clients reach router_performance's handler only through this method-and-path binding. + @app.get("/router/performance", dependencies=auth) + # What: define router_performance around after; why: the registered API client call router_performance for router performance and rely on this exact input and result contract. + async def router_performance(after: str | None = Query(default=None)): + # What: compute parsed after from the named fixture input; why: parsed after datetime fromisoformat after replace z later reads parsed after, so router_performance must retain the computed value under that name. + parsed_after = None + # What: gate on after before fullmatch and after and httpexception and re; why: router_performance admits fullmatch and after and httpexception and re only for this predicate and excludes the opposite state. + if after is not None: + # What: gate on fullmatch and after and re before httpexception; why: router_performance admits httpexception only for this predicate and excludes the opposite state. + if not re.fullmatch( + # What: apply the r d d d t d portion of the enclosing predicate; why: this clause remains in router_performance\'s enclosing expression so its grouping and evaluation order stay intact. + r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})", + # What: apply the after portion of the enclosing predicate; why: this clause remains in router_performance\'s enclosing expression so its grouping and evaluation order stay intact. + after, + # What: complete the re.fullmatch call with after; why: router_performance groups the supplied clauses as one re.fullmatch call before its value is consumed. + ): + # What: raise HTTPException for the caller; why: router_performance stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException( + # What: supply status code to HTTPException; why: router_performance binds this 400 value to HTTPException's status code input. + status_code=400, detail="invalid 'after' timestamp, use RFC3339 format" + # What: complete the HTTPException call with status code and detail; why: router_performance groups the supplied clauses as one HTTPException call before its value is consumed. + ) + # What: establish the handler boundary for the protected operation; why: router_performance routes failures to value error while preserving cleanup and success flow. + try: + # What: compute parsed after from fromisoformat and datetime and replace and after and z; why: result performance monitor current after parsed after later reads parsed after, so router_performance must retain the computed value under that name. + parsed_after = datetime.fromisoformat(after.replace("Z", "+00:00")) + # What: handle value error by raise httpexception; why: router_performance converts that failure into this concrete recovery, response, or cleanup behavior. + except ValueError as exc: + # What: raise HTTPException for the caller; why: router_performance stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException( + # What: supply status code to HTTPException; why: router_performance binds this 400 value to HTTPException's status code input. + status_code=400, detail="invalid 'after' timestamp, use RFC3339 format" + # What: apply the from exc portion of the enclosing predicate; why: this clause remains in router_performance\'s enclosing expression so its grouping and evaluation order stay intact. + ) from exc + # What: compute result from current and performance monitor and parsed after; why: if not result enabled later reads result, so router_performance must retain the computed value under that name. + result = performance_monitor.current(after=parsed_after) + # What: gate on result before jsonresponse; why: router_performance admits jsonresponse only for this predicate and excludes the opposite state. + if not result["enabled"]: + # What: map the enabled field as false; why: router_performance carries enabled into return JSONResponse(status_code=503, content={"enabled": False}). + return JSONResponse(status_code=503, content={"enabled": False}) + # What: return result from router_performance; why: router_performance exposes result so its caller can continue with the function\'s computed outcome. + return result + + # What: register GET /router/requests on the application router; why: clients reach router_requests's handler only through this method-and-path binding. + @app.get("/router/requests", dependencies=auth) + # What: define router_requests around the current object state; why: the registered API client call router_requests for router requests and rely on this exact input and result contract. + async def router_requests(): + # What: enter the inflight lock managed context before data id request id profile item profile; why: router_requests releases this resource or lock after data id request id profile item profile on both success and failure paths. + with inflight_lock: + # What: map the id field as request id; why: router_requests carries id through data into return data data. + data = [{"id": request_id, "profile": item["profile"]} + # What: call request_reservations.items with the declared inputs; why: router_requests invokes request_reservations.items while performing return data data; the call advances that operation through its result or side effect. + for request_id, item in request_reservations.items()] + # What: map the data field as data; why: router_requests carries data into return {"data": data}. + return {"data": data} + + # What: register POST /router/requests/{request_id}/cancel on the application router; why: clients reach router_cancel's handler only through this method-and-path binding. + @app.post("/router/requests/{request_id}/cancel", dependencies=auth) + # What: define router_cancel around request id; why: the registered API client call router_cancel for router cancel and rely on this exact input and result contract. + async def router_cancel(request_id: str): + # What: enter the inflight lock managed context before item inflight get request id; why: router_cancel releases this resource or lock after item inflight get request id on both success and failure paths. + with inflight_lock: + # What: compute item from get and request id and inflight; why: if item is not and cancellation later reads item, so router_cancel must retain the computed value under that name. + item = inflight.get(request_id) + # What: compute reservation from get and request id and request reservations; why: if reservation is not and not later reads reservation, so router_cancel must retain the computed value under that name. + reservation = request_reservations.get(request_id) + # What: gate on reservation before reservation; why: router_cancel admits reservation only for this predicate and excludes the opposite state. + if reservation is not None and not reservation["cancelled"]: + # What: compute reservation entry from true; why: cancellation reservation cancellation later reads reservation entry, so router_cancel must retain the computed value under that name. + reservation["cancelled"] = True + # What: compute cancellation from reservation and cancellation; why: cancellation later reads cancellation, so router_cancel must retain the computed value under that name. + cancellation = reservation["cancellation"] + # What: select the remaining branch that performs cancellation; why: router_cancel covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute cancellation from the named fixture input; why: if item is not and cancellation later reads cancellation, so router_cancel must retain the computed value under that name. + cancellation = None + # What: gate on item and cancellation before item; why: router_cancel admits item only for this predicate and excludes the opposite state. + if item is not None and cancellation is not None: + # What: compute item entry from true; why: if item is later reads item entry, so router_cancel must retain the computed value under that name. + item["cancelled"] = True + # What: gate on cancellation before the computed value; why: router_cancel admits the computed value only for this predicate and excludes the opposite state. + if cancellation is None: + # What: map the cancelled field as false; why: router_cancel carries cancelled into return {"cancelled": False, "reason": "not_found"}. + return {"cancelled": False, "reason": "not_found"} + # What: gate on item before cancel acquire and cancellation and router; why: router_cancel admits cancel acquire and cancellation and router only for this predicate and excludes the opposite state. + if item is None: + # What: call router.cancel_acquire with cancellation; why: router_cancel invokes router.cancel_acquire while performing profile reservation profile; the call advances that operation through its result or side effect. + router.cancel_acquire(cancellation) + # What: compute profile from reservation and profile; why: profile item profile later reads profile, so router_cancel must retain the computed value under that name. + profile = reservation["profile"] + # What: select the remaining branch that performs item upstream close; why: router_cancel covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: preserve the exact item upstream close literal fragment; why: router_cancel passes this fragment verbatim through item["upstream"].close(), because changing it would alter a protocol payload, serialized fixture, or public message. + item["upstream"].close() + # What: compute profile from item and profile; why: router event request cancelled profile profile later reads profile, so router_cancel must retain the computed value under that name. + profile = item["profile"] + # What: call router.record_cancellation with the declared inputs; why: router_cancel invokes router.record_cancellation while performing router event request cancelled profile profile; the call advances that operation through its result or side effect. + router.record_cancellation() + # What: preserve the exact router event request cancelled profile profile literal fragment; why: router_cancel passes this fragment verbatim through router_event("request_cancelled", profile=profile), because changing it would alter a protocol payload, serialized fixture, or public message. + router_event("request_cancelled", profile=profile) + # What: map the cancelled field as true; why: router_cancel carries cancelled into return {"cancelled": True, "id": request_id}. + return {"cancelled": True, "id": request_id} + + # What: register GET /router/logs on the application router; why: clients reach router_logs's handler only through this method-and-path binding. + @app.get("/router/logs", dependencies=auth) + # What: define router_logs around request and since; why: the registered API client call router_logs for router logs and rely on this exact input and result contract. + async def router_logs(request: Request, since: int = 0): + """Bounded lifecycle/proxy event stream, separate from engine stdout.""" + # What: document bounded lifecycle proxy event stream separate in the router_logs docstring; why: introspection and maintainers read this exact docstring fragment to understand router logs behavior without executing it. + # What: return log stream and request and router ring and since from router_logs; why: router_logs exposes log stream and request and router ring and since so its caller can continue with the function\'s computed outcome. + return _log_stream(request, router_ring, since) + + # What: register GET /router/activity on the application router; why: clients reach router_activity's handler only through this method-and-path binding. + @app.get("/router/activity", dependencies=auth) + # What: define router_activity around limit and before id and model; why: the registered API client call router_activity for router activity and rely on this exact input and result contract. + async def router_activity( + # What: declare the limit input for router_activity; why: router_activity consumes limit during return activity store list limit limit before id before id, so callers must bind it with the other signature inputs. + limit: int = Query(default=100, ge=1, le=999), + # What: declare the before id input for router_activity; why: router_activity consumes before id during return activity store list limit limit before id before id, so callers must bind it with the other signature inputs. + before_id: int | None = Query(default=None, ge=1, alias="beforeId"), + # What: declare the model input for router_activity; why: router_activity consumes model during return activity store list limit limit before id before id, so callers must bind it with the other signature inputs. + model: str | None = None, + # What: complete the enclosing predicate with app get router activity dependencies auth async def router activity limit; why: router_activity groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: return list and activity store and limit and before id from router_activity; why: router_activity exposes list and activity store and limit and before id so its caller can continue with the function\'s computed outcome. + return activity_store.list(limit=limit, before_id=before_id, model=model) + + # What: register GET /router/activity/stats on the application router; why: clients reach router_activity_stats's handler only through this method-and-path binding. + @app.get("/router/activity/stats", dependencies=auth) + # What: define router_activity_stats around model; why: the registered API client call router_activity_stats for router activity stats and rely on this exact input and result contract. + async def router_activity_stats(model: str | None = None): + # What: return stats and activity store and model from router_activity_stats; why: router_activity_stats exposes stats and activity store and model so its caller can continue with the function\'s computed outcome. + return activity_store.stats(model=model) + + # What: register GET /router/captures/{capture_id} on the application router; why: clients reach router_capture's handler only through this method-and-path binding. + @app.get("/router/captures/{capture_id}", dependencies=auth) + # What: define router_capture around capture id; why: the registered API client call router_capture for router capture and rely on this exact input and result contract. + async def router_capture(capture_id: int): + # What: compute capture from capture and capture id and activity store; why: if capture is later reads capture, so router_capture must retain the computed value under that name. + capture = activity_store.capture(capture_id) + # What: gate on capture before httpexception; why: router_capture admits httpexception only for this predicate and excludes the opposite state. + if capture is None: + # What: raise HTTPException for the caller; why: router_capture stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=404, detail="capture not found") + # What: return capture from router_capture; why: router_capture exposes capture so its caller can continue with the function\'s computed outcome. + return capture + + # What: register GET /metrics on the application router; why: clients reach router_metrics's handler only through this method-and-path binding. + @app.get("/metrics", dependencies=auth) + # What: define router_metrics around the current object state; why: the registered API client call router_metrics for router metrics and rely on this exact input and result contract. + async def router_metrics(): + # What: return plain text response and prometheus and router and text and plain from router_metrics; why: router_metrics exposes plain text response and prometheus and router and text and plain so its caller can continue with the function\'s computed outcome. + return PlainTextResponse(router.prometheus(), media_type="text/plain; version=0.0.4") + + # What: register POST /router/unload on the application router; why: clients reach router_unload's handler only through this method-and-path binding. + @app.post("/router/unload", dependencies=auth) + # What: define router_unload around body; why: the registered API client call router_unload for router unload and rely on this exact input and result contract. + async def router_unload(body: RouterUnloadBody | None = None): try: - return await run(lifecycle_pool, manager.stop, None, bool(body and body.force)) + # What: compute unloaded from run and lifecycle pool and evict idle and router; why: return unloaded unloaded router router status later reads unloaded, so router_unload must retain the computed value under that name. + unloaded = await run(lifecycle_pool, router.evict_idle, body.name if body else None) except (AccountingPrepareError, AccountingOutboxError) as exc: return accounting_error(exc) + # What: map the unloaded field as unloaded; why: router_unload carries unloaded into return {"unloaded": unloaded, "router": router.status()}. + return {"unloaded": unloaded, "router": router.status()} + + # What: register POST /router/load on the application router; why: clients reach router_load's handler only through this method-and-path binding. + @app.post("/router/load", dependencies=auth) + # What: define router_load around body; why: the registered API client call router_load for router load and rely on this exact input and result contract. + async def router_load(body: RouterLoadBody): + """Activate one profile without inventing a synthetic inference request. + + The short lease still uses the identical admission, readiness, switch, + accounting, and rollback transaction as automatic routing. Releasing it + afterwards permits the configured idle-TTL policy to apply normally. + """ + # What: document activate one profile without inventing a in the router_load docstring; why: introspection and maintainers read this exact docstring fragment to understand router load behavior without executing it. + # What: document the short lease still uses the in the router_load docstring; why: introspection and maintainers read this exact docstring fragment to understand router load behavior without executing it. + # What: document accounting and rollback transaction as automatic in the router_load docstring; why: introspection and maintainers read this exact docstring fragment to understand router load behavior without executing it. + # What: document afterwards permits the configured idle ttl policy in the router_load docstring; why: introspection and maintainers read this exact docstring fragment to understand router load behavior without executing it. + # What: preserve the paragraph boundary in the the router_load docstring; why: introspection and maintainers read this paragraph break to understand router load behavior without executing it. + # What: establish the handler boundary for the protected operation; why: router_load routes failures to routing error while preserving cleanup and success flow. + try: + # What: compute lease from acquire route and name and body and false; why: result profile lease profile name port lease port pid later reads lease, so router_load must retain the computed value under that name. + lease = await acquire_route(body.name, apply_routing_profile=False) + # What: handle routing error by router event management load failed profile body name code exc code; why: router_load converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: preserve the exact router event management load failed profile body name code exc code literal fragment; why: router_load passes this fragment verbatim through router_event("management_load_failed", profile=body.name, code=exc.code), because changing it would alter a protocol payload, serialized fi. + router_event("management_load_failed", profile=body.name, code=exc.code) + # What: map the error field as code and str and exc and message and type; why: router_load carries error through content into content recovery exc recovery. + content = {"error": {"message": str(exc), "type": exc.code}} + # What: gate on recovery and exc before recovery and content and exc; why: router_load admits recovery and content and exc only for this predicate and excludes the opposite state. + if exc.recovery is not None: + # What: compute content entry from recovery and exc; why: content content later reads content entry, so router_load must retain the computed value under that name. + content["recovery"] = exc.recovery + # What: return jsonresponse and status code and content and exc and 429 from router_load; why: router_load exposes jsonresponse and status code and content and exc and 429 so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: router_load binds this status code and exc value to JSONResponse's status code input. + status_code=exc.status_code, + # What: supply content to JSONResponse; why: router_load binds this content value to JSONResponse's content input. + content=content, + # What: map the retry after field as 1; why: router_load carries retry after into headers={"Retry-After": "1"} if exc.status_code == 429 else None. + headers={"Retry-After": "1"} if exc.status_code == 429 else None, + # What: complete the JSONResponse call with status code and content and headers; why: router_load groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: establish the handler boundary for the protected operation; why: router_load routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: map the profile field as name and profile and lease; why: router_load carries profile through result into return result router router status. + result = {"profile": lease.profile.name, "port": lease.port, "pid": lease.pid} + # What: run lease release on every exit path; why: router_load performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: call lease.release with the declared inputs; why: router_load invokes lease.release while performing router event management loaded profile lease profile name; the call advances that operation through its result or side effect. + lease.release() + # What: preserve the exact router event management loaded profile lease profile name literal fragment; why: router_load passes this fragment verbatim through router_event("management_loaded", profile=lease.profile.name), because changing it would alter a protocol payload, serialized fixture, or public message. + router_event("management_loaded", profile=lease.profile.name) + # What: map the router field as status and router; why: router_load carries router into return {**result, "router": router.status()}. + return {**result, "router": router.status()} + + # What: register POST /router/reload on the application router; why: clients reach router_reload's handler only through this method-and-path binding. + @app.post("/router/reload", dependencies=auth) + # What: define router_reload around the current object state; why: the registered API client call router_reload for router reload and rely on this exact input and result contract. + async def router_reload(): + # What: gate on catalog path before httpexception; why: router_reload admits httpexception only for this predicate and excludes the opposite state. + if not catalog_path: + # What: raise HTTPException for the caller; why: router_reload stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=409, detail="catalog reload requires --catalog") + # What: establish the handler boundary for the protected operation; why: router_reload routes failures to catalog error and routing error while preserving cleanup and success flow. + try: + # What: compute replacement from run and proxy pool and load and catalog path; why: await run lifecycle pool router replace catalog replacement later reads replacement, so router_reload must retain the computed value under that name. + replacement = await run(proxy_pool, ModelCatalog.load, catalog_path) + # What: call run with lifecycle pool and replace catalog and router and replacement; why: router_reload invokes run while performing await run; the call advances that operation through its result or side effect. + await run(lifecycle_pool, router.replace_catalog, replacement) + # What: call run with proxy pool and reconfigure and activity store and activity max entries and settings and replacement; why: router_reload invokes run while performing proxy pool; the call advances that operation through its result or side effect. + await run( + # What: apply the proxy pool portion of the enclosing predicate; why: this clause remains in router_reload\'s enclosing expression so its grouping and evaluation order stay intact. + proxy_pool, + # What: apply the activity store reconfigure portion of the enclosing predicate; why: this clause remains in router_reload\'s enclosing expression so its grouping and evaluation order stay intact. + activity_store.reconfigure, + # What: apply the replacement settings activity max entries portion of the enclosing predicate; why: this clause remains in router_reload\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.activity_max_entries, + # What: apply the replacement settings capture buffer mb portion of the enclosing predicate; why: this clause remains in router_reload\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.capture_buffer_mb * 1024 * 1024, + # What: apply the replacement settings activity session headers portion of the enclosing predicate; why: this clause remains in router_reload\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.activity_session_headers, + # What: complete the run call with proxy pool and reconfigure and activity max entries and capture buffer mb and activity session headers; why: router_reload groups the supplied clauses as one run call before its value is consumed. + ) + # What: call run with proxy pool and reconfigure and performance monitor and performance every s and settings and replacement; why: router_reload invokes run while performing proxy pool; the call advances that operation through its result or side effect. + await run( + # What: apply the proxy pool portion of the enclosing predicate; why: this clause remains in router_reload\'s enclosing expression so its grouping and evaluation order stay intact. + proxy_pool, + # What: apply the performance monitor reconfigure portion of the enclosing predicate; why: this clause remains in router_reload\'s enclosing expression so its grouping and evaluation order stay intact. + performance_monitor.reconfigure, + # What: apply the replacement settings performance every s portion of the enclosing predicate; why: this clause remains in router_reload\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.performance_every_s, + # What: apply the replacement settings performance disabled portion of the enclosing predicate; why: this clause remains in router_reload\'s enclosing expression so its grouping and evaluation order stay intact. + replacement.settings.performance_disabled, + # What: complete the run call with proxy pool and reconfigure and performance every s and performance disabled; why: router_reload groups the supplied clauses as one run call before its value is consumed. + ) + # What: handle catalog error by raise httpexception status code 400 detail str exc; why: router_reload converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError as exc: + # What: raise HTTPException for the caller; why: router_reload stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=400, detail=str(exc)) from exc + # What: handle routing error by return jsonresponse; why: router_reload converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: return jsonresponse and status code and exc and code from router_reload; why: router_reload exposes jsonresponse and status code and exc and code so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: router_reload binds this status code and exc value to JSONResponse's status code input. + status_code=exc.status_code, + # What: map the error field as code and str and exc and message and type; why: router_reload carries error into content={"error": {"message": str(exc), "type": exc.code}}. + content={"error": {"message": str(exc), "type": exc.code}}, + # What: complete the JSONResponse call with status code and content; why: router_reload groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: preserve the exact record watch reloaded literal fragment; why: router_reload passes this fragment verbatim through record_watch("reloaded"), because changing it would alter a protocol payload, serialized fixture, or public message. + record_watch("reloaded") + # What: map the reloaded field as true; why: router_reload carries reloaded into return {"reloaded": True, "models": router.catalog.public()}. + return {"reloaded": True, "models": router.catalog.public()} + + # ---- engine lifecycle ---- + + # What: define profile_request around name; why: its direct callers call profile_request for profile request and rely on this exact input and result contract. + def profile_request(name: str) -> tuple[str, int, list[str]]: + # What: compute profile from get and name and catalog and router; why: return profile model resolve port profile port list profile args later reads profile, so profile_request must retain the computed value under that name. + profile = router.catalog.get(name) + # What: return model and profile and resolve port and port from profile_request; why: profile_request exposes model and profile and resolve port and port so its caller can continue with the function\'s computed outcome. + return profile.model, resolve_port(profile.port), list(profile.args) + + # What: define profile_result around name and result and port; why: its direct callers call profile_result for profile result and rely on this exact input and result contract. + def profile_result(name: str, result: dict, port: int): + # What: compute profile from get and name and catalog and router; why: timeout s profile ready timeout s later reads profile, so profile_result must retain the computed value under that name. + profile = router.catalog.get(name) + # What: compute readiness from wait for ready and manager and probe and port; why: content result profile name readiness readiness later reads readiness, so profile_result must retain the computed value under that name. + readiness = wait_for_ready( + # What: apply the manager portion of readiness; why: profile_result uses this clause to evaluate readiness as one grouped value. + manager, + # What: apply the probe portion of readiness; why: profile_result uses this clause to evaluate readiness as one grouped value. + probe, + # What: supply pid to result.get; why: profile_result binds this get and result and pid value to result.get's pid input. + pid=result.get("pid"), + # What: supply port to wait_for_ready; why: profile_result binds this port value to wait_for_ready's port input. + port=port, + # What: supply timeout s to wait_for_ready; why: profile_result binds this ready timeout s and profile value to wait_for_ready's timeout s input. + timeout_s=profile.ready_timeout_s, + # What: supply path to wait_for_ready; why: profile_result binds this check endpoint and profile value to wait_for_ready's path input. + path=profile.check_endpoint, + # What: complete the wait_for_ready call with pid and port and timeout s and path; why: profile_result groups the supplied clauses as one wait_for_ready call before its value is consumed. + ) + + # What: map the profile field as name; why: profile_result carries profile through content into return jsonresponse status code 503 content content. + content = {**result, "profile": name, "readiness": readiness} + # What: gate on readiness before jsonresponse and content; why: profile_result admits jsonresponse and content only for this predicate and excludes the opposite state. + if not readiness["ready"]: + # What: return jsonresponse and content and 503 from profile_result; why: profile_result exposes jsonresponse and content and 503 so its caller can continue with the function\'s computed outcome. + return JSONResponse(status_code=503, content=content) + # What: return content from profile_result; why: profile_result exposes content so its caller can continue with the function\'s computed outcome. + return content + + # What: apply app.exception_handler behavior to switch_launch_error; why: Python attaches this named decorator's registration or descriptor semantics to switch_launch_error. + @app.exception_handler(SwitchLaunchError) + # What: define switch_launch_error around request and exc; why: the registered API client call switch_launch_error for switch launch error and rely on this exact input and result contract. + async def switch_launch_error(request: Request, exc: SwitchLaunchError): + # What: return jsonresponse and rollback and accounting and str from switch_launch_error; why: switch_launch_error exposes jsonresponse and rollback and accounting and str so its caller can continue with the function\'s computed outcome. + return JSONResponse(status_code=503, content={ + # What: map the code field as switch launch failed; why: switch_launch_error carries code into "code": "switch_launch_failed", "error": str(exc). + "code": "switch_launch_failed", "error": str(exc), + # What: map the rollback field as rollback and exc; why: switch_launch_error carries rollback into "rollback": exc.rollback, "accounting": exc.accounting. + "rollback": exc.rollback, "accounting": exc.accounting, + # What: complete the JSONResponse call with status code and content; why: switch_launch_error groups the supplied clauses as one JSONResponse call before its value is consumed. + }) + + # What: register POST /engine/start on the application router; why: clients reach engine_start's handler only through this method-and-path binding. + @app.post("/engine/start", dependencies=auth) + # What: define engine_start around body; why: the registered API client call engine_start for engine start and rely on this exact input and result contract. + async def engine_start(body: StartBody): + # What: define operation around the current object state; why: its direct callers call operation for operation and rely on this exact input and result contract. + async def operation(): + # What: establish the handler boundary for the protected operation; why: operation routes failures to conflict and exception while preserving cleanup and success flow. + try: + # What: compute port from resolve port and port and body; why: return await run lifecycle pool manager start body model later reads port, so operation must retain the computed value under that name. + port = resolve_port(body.port) + # What: return run and lifecycle pool and start and model from operation; why: operation exposes run and lifecycle pool and start and model so its caller can continue with the function\'s computed outcome. + return await run(lifecycle_pool, manager.start, body.model, port, list(body.args)) + # What: handle conflict by st manager status; why: operation converts that failure into this concrete recovery, response, or cleanup behavior. + except Conflict as exc: + # What: compute st from status and manager; why: current model st get model later reads st, so operation must retain the computed value under that name. + st = manager.status() + # What: return jsonresponse and str and exc and get from operation; why: operation exposes jsonresponse and str and exc and get so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: operation binds this 409 value to JSONResponse's status code input. + status_code=409, + # What: supply content to JSONResponse; why: operation binds this str and exc and get and st and error value to JSONResponse's content input. + content={ + # What: map the error field as str and exc; why: operation carries error into "error": str(exc). + "error": str(exc), + # What: map the code field as serve conflict; why: operation carries code into "code": "serve_conflict". + "code": "serve_conflict", + # What: map the current model field as get and st and model; why: operation carries current model into "currentModel": st.get("model"). + "currentModel": st.get("model"), + # What: map the current port field as get and st and port; why: operation carries current port into "currentPort": st.get("port"). + "currentPort": st.get("port"), + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + }, + # What: complete the JSONResponse call with status code and content; why: operation groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: handle exception by raise httpexception status code 500 detail f start; why: operation converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: # noqa: BLE001 — never propagate a 500-as-crash + # What: raise HTTPException for the caller; why: operation stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=500, detail=f"start failed: {exc}") + + # What: return run manual transaction and operation from engine_start; why: engine_start exposes run manual transaction and operation so its caller can continue with the function\'s computed outcome. + return await run_manual_transaction(operation) + + # What: register POST /engine/stop on the application router; why: clients reach engine_stop's handler only through this method-and-path binding. + @app.post("/engine/stop", dependencies=auth) + # What: define engine_stop around body; why: the registered API client call engine_stop for engine stop and rely on this exact input and result contract. + async def engine_stop(body: StopBody | None = None): + # What: define operation around the current object state; why: its direct callers call operation for operation and rely on this exact input and result contract. + async def operation(): + # What: establish the handler boundary for the protected operation; why: operation routes failures to accounting prepare error and accounting outbox error while preserving cleanup and success flow. + try: + # What: return run and lifecycle pool and stop and manager from operation; why: operation exposes run and lifecycle pool and stop and manager so its caller can continue with the function\'s computed outcome. + return await run(lifecycle_pool, manager.stop, None, bool(body and body.force)) + # What: execute except AccountingPrepareError AccountingOutboxError as exc; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + except (AccountingPrepareError, AccountingOutboxError) as exc: + # What: return accounting error and exc from operation; why: operation exposes accounting error and exc so its caller can continue with the function\'s computed outcome. + return accounting_error(exc) + + # What: return run manual transaction and operation and true from engine_stop; why: engine_stop exposes run manual transaction and operation and true so its caller can continue with the function\'s computed outcome. + return await run_manual_transaction(operation, preempt_manual=True) @app.post("/shutdown", dependencies=auth) async def shutdown_daemon(request: Request, body: StopBody | None = None): @@ -222,34 +2944,210 @@ async def shutdown_daemon(request: Request, body: StopBody | None = None): # leave the ~18GB serve orphaned, THEN bring the daemon down. We reply before uvicorn # actually stops (it notices should_exit within ~0.1s) so the client still gets a clean 200. try: - stopped = await run(lifecycle_pool, manager.shutdown, None, bool(body and body.force)) - except (AccountingPrepareError, AccountingOutboxError) as exc: - return accounting_error(exc) - req = getattr(request.app.state, "request_shutdown", None) - if req is not None: - req() - return { - "stopping": True, - "already": stopped.get("already", False), - "accounting": stopped.get("accounting"), - } + # What: compute owner from begin shutdown and router; why: owner later reads owner, so shutdown_daemon must retain the computed value under that name. + owner = router.begin_shutdown() + # What: handle routing error by return jsonresponse; why: shutdown_daemon converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: return jsonresponse and status code and exc and code from shutdown_daemon; why: shutdown_daemon exposes jsonresponse and status code and exc and code so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: shutdown_daemon binds this status code and exc value to JSONResponse's status code input. + status_code=exc.status_code, + # What: map the error field as code and str and exc and message and type; why: shutdown_daemon carries error into content={"error": {"message": str(exc), "type": exc.code}}. + content={"error": {"message": str(exc), "type": exc.code}}, + # What: complete the JSONResponse call with status code and content; why: shutdown_daemon groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + + # What: define operation around the current object state; why: its direct callers call operation for operation and rely on this exact input and result contract. + async def operation(): + # What: establish the handler boundary for the protected operation; why: operation routes failures to accounting prepare error and accounting outbox error while preserving cleanup and success flow. + try: + # What: compute stopped from run and lifecycle pool and finish shutdown and owner; why: already stopped get already later reads stopped, so operation must retain the computed value under that name. + stopped = await run( + # What: apply the lifecycle pool portion of stopped; why: operation uses this clause to evaluate stopped as one grouped value. + lifecycle_pool, + # What: apply the router finish shutdown portion of stopped; why: operation uses this clause to evaluate stopped as one grouped value. + router.finish_shutdown, + # What: apply the owner portion of stopped; why: operation uses this clause to evaluate stopped as one grouped value. + owner, + # What: apply the grouped expression portion of stopped; why: operation uses this clause to evaluate stopped as one grouped value. + None, + # What: call bool with body and force; why: operation consumes the bool return value while evaluating bool(body and body.force). + bool(body and body.force), + # What: complete the run call with lifecycle pool and finish shutdown and owner and bool; why: operation groups the supplied clauses as one run call before its value is consumed. + ) + # What: execute except AccountingPrepareError AccountingOutboxError as exc; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + except (AccountingPrepareError, AccountingOutboxError) as exc: + # What: return accounting error and exc from operation; why: operation exposes accounting error and exc so its caller can continue with the function\'s computed outcome. + return accounting_error(exc) + # What: compute req from getattr and state and app and request and request shutdown; why: if req is not later reads req, so operation must retain the computed value under that name. + req = getattr(request.app.state, "request_shutdown", None) + # What: gate on req before req; why: operation admits req only for this predicate and excludes the opposite state. + if req is not None: + # What: call req with the declared inputs; why: operation invokes req while performing return; the call advances that operation through its result or side effect. + req() + # What: return get and stopped and stopping and already and accounting from operation; why: operation exposes get and stopped and stopping and already and accounting so its caller can continue with the function\'s computed outcome. + return { + # What: map the stopping field as true; why: operation carries stopping into "stopping": True. + "stopping": True, + # What: map the already field as get and stopped and already and false; why: operation carries already into "already": stopped.get("already", False). + "already": stopped.get("already", False), + # What: map the accounting field as get and stopped and accounting; why: operation carries accounting into "accounting": stopped.get("accounting"). + "accounting": stopped.get("accounting"), + # What: complete the enclosing predicate mapping with stopping and already and accounting; why: operation groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + # What: return run to completion and operation from shutdown_daemon; why: shutdown_daemon exposes run to completion and operation so its caller can continue with the function\'s computed outcome. + return await run_to_completion(operation) @app.post("/engine/switch", dependencies=auth) async def engine_switch(body: SwitchBody): - port = resolve_port(body.port) - try: - return await run( - lifecycle_pool, - manager.switch, - body.model, - port, - list(body.args), - body.force, - ) - except (AccountingPrepareError, AccountingOutboxError) as exc: - return accounting_error(exc) - except Exception as exc: # noqa: BLE001 - raise HTTPException(status_code=500, detail=f"switch failed: {exc}") + # What: define operation around the current object state; why: its direct callers call operation for operation and rely on this exact input and result contract. + async def operation(): + # What: establish the handler boundary for the protected operation; why: operation routes failures to accounting prepare error and accounting outbox error and exception while preserving cleanup and success flow. + try: + # What: compute port from resolve port and port and body; why: port later reads port, so operation must retain the computed value under that name. + port = resolve_port(body.port) + # What: return run and lifecycle pool and switch and model from operation; why: operation exposes run and lifecycle pool and switch and model so its caller can continue with the function\'s computed outcome. + return await run( + # What: apply the lifecycle pool portion of the enclosing predicate; why: this clause remains in operation\'s enclosing expression so its grouping and evaluation order stay intact. + lifecycle_pool, + # What: apply the manager switch portion of the enclosing predicate; why: this clause remains in operation\'s enclosing expression so its grouping and evaluation order stay intact. + manager.switch, + # What: apply the body model portion of the enclosing predicate; why: this clause remains in operation\'s enclosing expression so its grouping and evaluation order stay intact. + body.model, + # What: apply the port portion of the enclosing predicate; why: this clause remains in operation\'s enclosing expression so its grouping and evaluation order stay intact. + port, + # What: call list with args and body; why: operation invokes list while performing body force; the call advances that operation through its result or side effect. + list(body.args), + # What: apply the body force portion of the enclosing predicate; why: this clause remains in operation\'s enclosing expression so its grouping and evaluation order stay intact. + body.force, + # What: complete the run call with lifecycle pool and switch and model and port and list; why: operation groups the supplied clauses as one run call before its value is consumed. + ) + # What: execute except AccountingPrepareError AccountingOutboxError as exc; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + except (AccountingPrepareError, AccountingOutboxError) as exc: + # What: return accounting error and exc from operation; why: operation exposes accounting error and exc so its caller can continue with the function\'s computed outcome. + return accounting_error(exc) + # What: handle exception by if isinstance exc switch launch error; why: operation converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: # noqa: BLE001 + # What: gate on isinstance and exc and switch launch error before the computed value; why: operation admits the computed value only for this predicate and excludes the opposite state. + if isinstance(exc, SwitchLaunchError): + # What: re-propagate the active failure to the caller; why: operation stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: raise HTTPException for the caller; why: operation stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=500, detail=f"switch failed: {exc}") + + # What: return run manual transaction and operation from engine_switch; why: engine_switch exposes run manual transaction and operation so its caller can continue with the function\'s computed outcome. + return await run_manual_transaction(operation) + + # What: register POST /engine/start-profile on the application router; why: clients reach engine_start_profile's handler only through this method-and-path binding. + @app.post("/engine/start-profile", dependencies=auth) + # What: define engine_start_profile around body; why: the registered API client call engine_start_profile for engine start profile and rely on this exact input and result contract. + async def engine_start_profile(body: ProfileBody): + # What: define operation around the current object state; why: its direct callers call operation for operation and rely on this exact input and result contract. + async def operation(): + # What: establish the handler boundary for the protected operation; why: operation routes failures to catalog error and conflict and exception while preserving cleanup and success flow. + try: + # What: compute model and port and args from profile request and name and body; why: result await run lifecycle pool manager start model later reads model and port and args, so operation must retain the computed value under that name. + model, port, args = profile_request(body.name) + # What: compute result from run and lifecycle pool and start and model; why: return await run proxy pool profile result body name later reads result, so operation must retain the computed value under that name. + result = await run(lifecycle_pool, manager.start, model, port, args) + # What: return run and proxy pool and profile result and name from operation; why: operation exposes run and proxy pool and profile result and name so its caller can continue with the function\'s computed outcome. + return await run(proxy_pool, profile_result, body.name, result, port) + # What: handle catalog error by raise httpexception status code 404 detail str exc; why: operation converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError as exc: + # What: raise HTTPException for the caller; why: operation stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=404, detail=str(exc)) + # What: handle conflict by st manager status; why: operation converts that failure into this concrete recovery, response, or cleanup behavior. + except Conflict as exc: + # What: compute st from status and manager; why: current model st get model later reads st, so operation must retain the computed value under that name. + st = manager.status() + # What: return jsonresponse and str and exc and get from operation; why: operation exposes jsonresponse and str and exc and get so its caller can continue with the function\'s computed outcome. + return JSONResponse( + # What: supply status code to JSONResponse; why: operation binds this 409 value to JSONResponse's status code input. + status_code=409, + # What: supply content to JSONResponse; why: operation binds this str and exc and get and st and error value to JSONResponse's content input. + content={ + # What: map the error field as str and exc; why: operation carries error into "error": str(exc). + "error": str(exc), + # What: map the code field as serve conflict; why: operation carries code into "code": "serve_conflict". + "code": "serve_conflict", + # What: map the current model field as get and st and model; why: operation carries current model into "currentModel": st.get("model"). + "currentModel": st.get("model"), + # What: map the current port field as get and st and port; why: operation carries current port into "currentPort": st.get("port"). + "currentPort": st.get("port"), + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + }, + # What: complete the JSONResponse call with status code and content; why: operation groups the supplied clauses as one JSONResponse call before its value is consumed. + ) + # What: handle exception by raise httpexception status code 500 detail f profile; why: operation converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: # noqa: BLE001 + # What: raise HTTPException for the caller; why: operation stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=500, detail=f"profile start failed: {exc}") + + # What: return run manual transaction and operation from engine_start_profile; why: engine_start_profile exposes run manual transaction and operation so its caller can continue with the function\'s computed outcome. + return await run_manual_transaction(operation) + + # What: register POST /engine/switch-profile on the application router; why: clients reach engine_switch_profile's handler only through this method-and-path binding. + @app.post("/engine/switch-profile", dependencies=auth) + # What: define engine_switch_profile around body; why: the registered API client call engine_switch_profile for engine switch profile and rely on this exact input and result contract. + async def engine_switch_profile(body: ProfileBody): + # What: define operation around the current object state; why: its direct callers call operation for operation and rely on this exact input and result contract. + async def operation(): + # What: establish the handler boundary for the protected operation; why: operation routes failures to catalog error and accounting prepare error and accounting outbox error and exception while preserving cleanup and success flow. + try: + # What: compute model and port and args from profile request and name and body; why: lifecycle pool manager switch for readiness model port args body force later reads model and port and args, so operation must retain the computed value under that name. + model, port, args = profile_request(body.name) + # What: compute result and ticket from run and lifecycle pool and switch for readiness and model; why: response await run proxy pool profile result body name later reads result and ticket, so operation must retain the computed value under that name. + result, ticket = await run( + # What: apply the lifecycle pool manager switch for readiness model port args body force portion of result and ticket; why: operation uses this clause to evaluate result and ticket as one grouped value. + lifecycle_pool, manager.switch_for_readiness, model, port, args, body.force + # What: complete the run call with lifecycle pool and switch for readiness and model and port and args; why: operation groups the supplied clauses as one run call before its value is consumed. + ) + # What: compute response from run and proxy pool and profile result and name; why: if not isinstance response jsonresponse later reads response, so operation must retain the computed value under that name. + response = await run(proxy_pool, profile_result, body.name, result, port) + # What: gate on isinstance and response and jsonresponse before response; why: operation admits response only for this predicate and excludes the opposite state. + if not isinstance(response, JSONResponse): + # What: return response from operation; why: operation exposes response so its caller can continue with the function\'s computed outcome. + return response + # What: compute content from loads and body and json and response; why: content rollback rollback later reads content, so operation must retain the computed value under that name. + content = json.loads(response.body) + # What: compute rollback from run and lifecycle pool and recover switch and ticket; why: if rollback get launched later reads rollback, so operation must retain the computed value under that name. + rollback = await run(lifecycle_pool, manager.recover_switch, ticket, body.force) + # What: gate on get and rollback before profile and get and name and catalog and body; why: operation admits profile and get and name and catalog and body only for this predicate and excludes the opposite state. + if rollback.get("launched"): + # What: compute profile from get and name and catalog and body; why: port rollback port timeout s profile ready timeout s later reads profile, so operation must retain the computed value under that name. + profile = router.catalog.get(body.name) + # What: compute rollback entry from run and proxy pool and partial and wait for ready; why: wait for ready manager probe pid rollback pid later reads rollback entry, so operation must retain the computed value under that name. + rollback["readiness"] = await run(proxy_pool, functools.partial( + # What: supply pid to run; why: operation binds this rollback and pid value to run's pid input. + wait_for_ready, manager, probe, pid=rollback["pid"], + # What: supply port to run; why: operation binds this rollback and port value to run's port input. + port=rollback["port"], timeout_s=profile.ready_timeout_s, + # What: complete the run call with proxy pool and partial; why: operation groups the supplied clauses as one run call before its value is consumed. + )) + # What: compute content entry from rollback; why: return jsonresponse status code content content later reads content entry, so operation must retain the computed value under that name. + content["rollback"] = rollback + # What: return jsonresponse and content and 503 from operation; why: operation exposes jsonresponse and content and 503 so its caller can continue with the function\'s computed outcome. + return JSONResponse(status_code=503, content=content) + # What: handle catalog error by raise httpexception status code 404 detail str exc; why: operation converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError as exc: + # What: raise HTTPException for the caller; why: operation stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=404, detail=str(exc)) + # What: execute except AccountingPrepareError AccountingOutboxError as exc; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + except (AccountingPrepareError, AccountingOutboxError) as exc: + # What: return accounting error and exc from operation; why: operation exposes accounting error and exc so its caller can continue with the function\'s computed outcome. + return accounting_error(exc) + # What: handle exception by if isinstance exc switch launch error; why: operation converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: # noqa: BLE001 + # What: gate on isinstance and exc and switch launch error before the computed value; why: operation admits the computed value only for this predicate and excludes the opposite state. + if isinstance(exc, SwitchLaunchError): + # What: re-propagate the active failure to the caller; why: operation stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: raise HTTPException for the caller; why: operation stops this rejected path before it can mutate state, dispatch work, or report success. + raise HTTPException(status_code=500, detail=f"profile switch failed: {exc}") + + # What: return run manual transaction and operation from engine_switch_profile; why: engine_switch_profile exposes run manual transaction and operation so its caller can continue with the function\'s computed outcome. + return await run_manual_transaction(operation) # ---- durable accounting outbox ---- diff --git a/python/freetoken/daemon/catalog.py b/python/freetoken/daemon/catalog.py new file mode 100644 index 000000000..4253dbb52 --- /dev/null +++ b/python/freetoken/daemon/catalog.py @@ -0,0 +1,1955 @@ +"""Named, validated FreeToken engine profiles for ``ft daemon``. + +This intentionally borrows the useful *catalog* idea from llama-swap without +accepting its shell-command model. A profile describes only FreeToken's native +``--model``, ``--port`` and argument-vector contract, so loading a catalog never +creates a shell injection path and the daemon remains torch-free. +""" +# What: document named validated free token engine profiles for in the catalog docstring; why: introspection and maintainers read this exact docstring fragment to understand catalog behavior without executing it. +# What: document this intentionally borrows the useful catalog in the catalog docstring; why: introspection and maintainers read this exact docstring fragment to understand catalog behavior without executing it. +# What: document accepting its shell command model a profile in the catalog docstring; why: introspection and maintainers read this exact docstring fragment to understand catalog behavior without executing it. +# What: document model port and argument vector contract so in the catalog docstring; why: introspection and maintainers read this exact docstring fragment to understand catalog behavior without executing it. +# What: document creates a shell injection path and in the catalog docstring; why: introspection and maintainers read this exact docstring fragment to understand catalog behavior without executing it. +# What: preserve the paragraph boundary in the the catalog docstring; why: introspection and maintainers read this paragraph break to understand catalog behavior without executing it. + +# What: enable postponed evaluation of annotations; why: type hints in catalog can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import dataclass and replace for module initialization and init using dataclasses and dataclass and replace; why: module initialization and __init__ uses dataclass and replace, making that imported dependency available to its named operation. +from dataclasses import dataclass, replace +# What: import json for value using json; why: value uses json loads, making that imported dependency available to its named operation. +import json +# What: import re for module initialization using re; why: module initialization uses re compile, making that imported dependency available to its named operation. +import re +# What: import any for value using typing and any; why: value uses the any annotation in value, making that imported dependency available to its named operation. +from typing import Any + +# What: establish the handler boundary for the protected operation; why: catalog routes failures to module not found error while preserving cleanup and success flow. +try: # tomllib joined the stdlib in Python 3.11; FreeToken supports 3.10 too. + # What: import tomllib for load using tomllib; why: load uses tomllib tomldecode error, making that imported dependency available to its named operation. + import tomllib +# What: handle module not found error by import tomli as tomllib; why: catalog converts that failure into this concrete recovery, response, or cleanup behavior. +except ModuleNotFoundError: # pragma: no cover - exercised in the Python 3.10 package build + # What: import tomli for load using tomli and tomllib; why: load uses tomllib tomldecode error, making that imported dependency available to its named operation. + import tomli as tomllib + + +# What: compute simple name from compile and re and a za z0 9 and a za z0 9 and value; why: if not isinstance name str or later reads simple name, so catalog must retain the computed value under that name. +_SIMPLE_NAME = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$") +# What: compute model segment from compile and re and a za z0 9 and a za z0 9 and value; why: and all model segment fullmatch segment for segment later reads model segment, so catalog must retain the computed value under that name. +_MODEL_SEGMENT = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$") +# What: compute safe http path from compile and re and a za z0 9 and value and a za z0 9; why: or not safe http path fullmatch value later reads safe http path, so catalog must retain the computed value under that name. +_SAFE_HTTP_PATH = re.compile(r"^/(?:[A-Za-z0-9._~-]+(?:/[A-Za-z0-9._~-]+)*)?$") +# What: compute upstream suffix from compile and re and a za z0 9 and a za z0 9 and value; why: isinstance suffix str and upstream suffix fullmatch suffix later reads upstream suffix, so catalog must retain the computed value under that name. +_UPSTREAM_SUFFIX = re.compile(r"^\.[A-Za-z0-9][A-Za-z0-9._-]{0,31}$") +# What: compute http header name from compile and re and value and a za z; why: isinstance header str and http header name fullmatch header later reads http header name, so catalog must retain the computed value under that name. +_HTTP_HEADER_NAME = re.compile(r"^[!#$%&'*+.^_`|~0-9A-Za-z-]{1,64}$") +# What: compute proxy template from compile and re and http and port and p; why: match proxy template fullmatch value later reads proxy template, so catalog must retain the computed value under that name. +_PROXY_TEMPLATE = re.compile( + # What: apply the r http port p prefix a za z0 9 portion of proxy template; why: catalog uses this clause to evaluate proxy template as one grouped value. + r"^http://127\.0\.0\.1:\$\{PORT\}(?P/(?:[A-Za-z0-9._~-]+(?:/[A-Za-z0-9._~-]+)*)?)?$" +# What: complete the re.compile call with ordered positional inputs; why: catalog groups the supplied clauses as one re.compile call before its value is consumed. +) + +# What: compute default check endpoint from health; why: check endpoint str default check endpoint later reads default check endpoint, so catalog must retain the computed value under that name. +DEFAULT_CHECK_ENDPOINT = "/health" +# What: compute default proxy from http and port; why: proxy str default proxy later reads default proxy, so catalog must retain the computed value under that name. +DEFAULT_PROXY = "http://127.0.0.1:${PORT}" +# What: compute default upstream no activation suffixes from js and json and css and png and gif; why: catalog consumes default upstream no activation suffixes during upstream no activation suffixes tuple str default upstream no activation suffixes, so default upstream no activation suffixes value receives the compute. +DEFAULT_UPSTREAM_NO_ACTIVATION_SUFFIXES = ( + # What: apply the js json css png gif jpg portion of default upstream no activation suffixes; why: catalog uses this clause to evaluate default upstream no activation suffixes as one grouped value. + ".js", ".json", ".css", ".png", ".gif", ".jpg", ".jpeg", ".ico", ".txt", +# What: complete the DEFAULT_UPSTREAM_NO_ACTIVATION_SUFFIXES collection with js and json and css and png; why: catalog groups the supplied clauses as one DEFAULT_UPSTREAM_NO_ACTIVATION_SUFFIXES collection before its value is consumed. +) +# What: compute default activity session headers from x session id and x litellm session id; why: activity session headers tuple str default activity session headers later reads default activity session headers, so catalog must retain the computed value under that name. +DEFAULT_ACTIVITY_SESSION_HEADERS = ("x-session-id", "x-litellm-session-id") + + +# What: define CatalogError as the owner of its declared state; why: daemon callers use this class boundary so those methods share one catalog error state invariant. +class CatalogError(ValueError): + """A catalog is malformed or requests an unsafe/ambiguous profile.""" +# What: document a catalog is malformed or requests in the CatalogError docstring; why: introspection and maintainers read this exact docstring fragment to understand catalog error behavior without executing it. + + +# What: generate dataclass initialization and value semantics for RoutingGroup; why: RoutingGroup acts as a typed state record with consistent construction, comparison, and representation. +@dataclass(frozen=True) +# What: define RoutingGroup as the owner of its declared state; why: daemon callers use this class boundary so those methods share one routing group state invariant. +class RoutingGroup: + """An atomically validated native equivalent of a llama-swap group.""" +# What: document an atomically validated native equivalent of in the RoutingGroup docstring; why: introspection and maintainers read this exact docstring fragment to understand routing group behavior without executing it. + + # What: compute name from the named fixture input; why: name str later reads name, so catalog must retain the computed value under that name. + name: str + # What: compute members from the named fixture input; why: if name in group members later reads members, so catalog must retain the computed value under that name. + members: tuple[str, ...] + # What: compute swap from true; why: render the applicable pinned llama swap model list later reads swap, so catalog must retain the computed value under that name. + swap: bool = True + # What: compute exclusive from true; why: unknown sorted set raw group members swap later reads exclusive, so catalog must retain the computed value under that name. + exclusive: bool = True + # What: compute persistent from false; why: unknown sorted set raw group members swap later reads persistent, so catalog must retain the computed value under that name. + persistent: bool = False + + +# What: generate dataclass initialization and value semantics for RouterSettings; why: RouterSettings acts as a typed state record with consistent construction, comparison, and representation. +@dataclass(frozen=True) +# What: define RouterSettings as the owner of its declared state; why: daemon callers use this class boundary so those methods share one router settings state invariant. +class RouterSettings: + """Global router policy, deliberately free of command execution fields.""" +# What: document global router policy deliberately free of in the RouterSettings docstring; why: introspection and maintainers read this exact docstring fragment to understand router settings behavior without executing it. + + # What: compute api keys from the named fixture input; why: api keys default ttl s unload timeout s upstream timeout s later reads api keys, so catalog must retain the computed value under that name. + api_keys: tuple[str, ...] = () + # What: compute default ttl s from 0 0; why: api keys default ttl s unload timeout s upstream timeout s later reads default ttl s, so catalog must retain the computed value under that name. + default_ttl_s: float = 0.0 + # What: compute unload timeout s from 30 0; why: unload timeout s float later reads unload timeout s, so catalog must retain the computed value under that name. + unload_timeout_s: float = 30.0 + # What: compute upstream timeout s from 900 0; why: upstream timeout s float later reads upstream timeout s, so catalog must retain the computed value under that name. + upstream_timeout_s: float = 900.0 + # What: compute scheduler from fifo; why: scheduler groups include aliases in list global concurrency limit later reads scheduler, so catalog must retain the computed value under that name. + scheduler: str = "fifo" + # What: compute groups from the named fixture input; why: for group in self settings groups later reads groups, so catalog must retain the computed value under that name. + groups: tuple[RoutingGroup, ...] = () + # What: compute include aliases in list from false; why: if self settings include aliases in list later reads include aliases in list, so catalog must retain the computed value under that name. + include_aliases_in_list: bool = False + # What: compute global concurrency limit from 0; why: scheduler groups include aliases in list global concurrency limit later reads global concurrency limit, so catalog must retain the computed value under that name. + global_concurrency_limit: int = 0 + # What: compute send loading state from false; why: send loading state bool later reads send loading state, so catalog must retain the computed value under that name. + send_loading_state: bool = False + # What: compute preload model from the named fixture input; why: if settings preload model is not later reads preload model, so catalog must retain the computed value under that name. + preload_model: str | None = None + # What: compute startup routing profile from the named fixture input; why: settings startup routing profile is not later reads startup routing profile, so catalog must retain the computed value under that name. + startup_routing_profile: str | None = None + # What: compute upstream no activation suffixes from default upstream no activation suffixes; why: upstream no activation suffixes later reads upstream no activation suffixes, so catalog must retain the computed value under that name. + upstream_no_activation_suffixes: tuple[str, ...] = DEFAULT_UPSTREAM_NO_ACTIVATION_SUFFIXES + # What: compute activity max entries from 1000; why: activity max entries capture buffer mb later reads activity max entries, so catalog must retain the computed value under that name. + activity_max_entries: int = 1000 + # What: compute capture buffer mb from 0; why: activity max entries capture buffer mb later reads capture buffer mb, so catalog must retain the computed value under that name. + capture_buffer_mb: int = 0 + # What: compute activity session headers from default activity session headers; why: activity session headers later reads activity session headers, so catalog must retain the computed value under that name. + activity_session_headers: tuple[str, ...] = DEFAULT_ACTIVITY_SESSION_HEADERS + # What: compute performance disabled from false; why: performance disabled performance every s later reads performance disabled, so catalog must retain the computed value under that name. + performance_disabled: bool = False + # What: compute performance every s from 5 0; why: performance disabled performance every s later reads performance every s, so catalog must retain the computed value under that name. + performance_every_s: float = 5.0 + + +# What: generate dataclass initialization and value semantics for ModelCapabilities; why: ModelCapabilities acts as a typed state record with consistent construction, comparison, and representation. +@dataclass(frozen=True) +# What: define ModelCapabilities as the owner of empty and public and model_listing_fields; why: daemon callers use this class boundary so those methods share one model capabilities state invariant. +class ModelCapabilities: + """Validated model-list metadata; it never enables inference behavior.""" +# What: document validated model list metadata it never enables in the ModelCapabilities docstring; why: introspection and maintainers read this exact docstring fragment to understand model capabilities behavior without executing it. + + # What: compute input modalities from the named fixture input; why: self input modalities or self output modalities or self tools or later reads input modalities, so catalog must retain the computed value under that name. + input_modalities: tuple[str, ...] = () + # What: compute output modalities from the named fixture input; why: self input modalities or self output modalities or self tools or later reads output modalities, so catalog must retain the computed value under that name. + output_modalities: tuple[str, ...] = () + # What: compute tools from false; why: self input modalities or self output modalities or self tools or later reads tools, so catalog must retain the computed value under that name. + tools: bool = False + # What: compute context from 0; why: self input modalities or self output modalities or self tools or later reads context, so catalog must retain the computed value under that name. + context: int = 0 + + # What: define empty around the current object state; why: its direct callers call empty for empty and rely on this exact input and result contract. + def empty(self) -> bool: + # What: return input modalities and output modalities and tools and context from empty; why: empty exposes input modalities and output modalities and tools and context so its caller can continue with the function\'s computed outcome. + return not ( + # What: apply the self input modalities or self output modalities or self tools or portion of the enclosing predicate; why: this clause remains in empty\'s enclosing expression so its grouping and evaluation order stay intact. + self.input_modalities or self.output_modalities or self.tools or self.context + # What: complete the empty signature with self; why: ModelCapabilities.empty groups the supplied clauses as one empty signature before its value is consumed. + ) + + # What: define public around the current object state; why: its direct callers call public for public and rely on this exact input and result contract. + def public(self) -> dict[str, Any]: + # What: initialize doc as an empty runtime accumulator; why: ModelCapabilities.public appends or maps entries into it during doc in list self input modalities before consuming the aggregate. + doc: dict[str, Any] = {} + # What: gate on input modalities before doc and list and input modalities; why: public admits doc and list and input modalities only for this predicate and excludes the opposite state. + if self.input_modalities: + # What: compute doc entry from list and input modalities; why: doc out list self output modalities later reads doc entry, so public must retain the computed value under that name. + doc["in"] = list(self.input_modalities) + # What: gate on output modalities before doc and list and output modalities; why: public admits doc and list and output modalities only for this predicate and excludes the opposite state. + if self.output_modalities: + # What: compute doc entry from list and output modalities; why: doc tools later reads doc entry, so public must retain the computed value under that name. + doc["out"] = list(self.output_modalities) + # What: gate on tools before doc; why: public admits doc only for this predicate and excludes the opposite state. + if self.tools: + # What: compute doc entry from true; why: doc context self context later reads doc entry, so public must retain the computed value under that name. + doc["tools"] = True + # What: gate on context before context and doc; why: public admits context and doc only for this predicate and excludes the opposite state. + if self.context: + # What: compute doc entry from context; why: return doc later reads doc entry, so public must retain the computed value under that name. + doc["context"] = self.context + # What: return doc from public; why: public exposes doc so its caller can continue with the function\'s computed outcome. + return doc + + # What: define model_listing_fields around the current object state; why: its direct callers call model_listing_fields for model listing fields and rely on this exact input and result contract. + def model_listing_fields(self) -> dict[str, Any]: + """Render the applicable pinned llama-swap model-list contract.""" + # What: document render the applicable pinned llama swap model list in the model_listing_fields docstring; why: introspection and maintainers read this exact docstring fragment to understand model listing fields behavior without executing it. + # What: initialize doc as an empty runtime accumulator; why: ModelCapabilities.model_listing_fields appends or maps entries into it during doc architecture architecture before consuming the aggregate. + doc: dict[str, Any] = {} + # What: gate on input modalities and output modalities before architecture and dict and str and any; why: model_listing_fields admits architecture and dict and str and any only for this predicate and excludes the opposite state. + if self.input_modalities or self.output_modalities: + # What: initialize architecture as an empty runtime accumulator; why: ModelCapabilities.model_listing_fields appends or maps entries into it during architecture input modalities list self input modalities before consuming the aggregate. + architecture: dict[str, Any] = {} + # What: gate on input modalities before architecture and list and input modalities; why: model_listing_fields admits architecture and list and input modalities only for this predicate and excludes the opposite state. + if self.input_modalities: + # What: compute architecture entry from list and input modalities; why: architecture output modalities list self output modalities later reads architecture entry, so model_listing_fields must retain the computed value under that name. + architecture["input_modalities"] = list(self.input_modalities) + # What: gate on output modalities before architecture and list and output modalities; why: model_listing_fields admits architecture and list and output modalities only for this predicate and excludes the opposite state. + if self.output_modalities: + # What: compute architecture entry from list and output modalities; why: architecture modality later reads architecture entry, so model_listing_fields must retain the computed value under that name. + architecture["output_modalities"] = list(self.output_modalities) + # What: gate on input modalities and output modalities before architecture and join and input modalities and output modalities; why: model_listing_fields admits architecture and join and input modalities and output modalities only for this predicate and excludes the opposite state. + if self.input_modalities and self.output_modalities: + # What: compute architecture entry from join and input modalities and output modalities and value and value; why: doc architecture architecture later reads architecture entry, so model_listing_fields must retain the computed value under that name. + architecture["modality"] = ( + # What: call operation.join with input modalities; why: model_listing_fields consumes the operation.join return value while evaluating f"{'+'.join(self.input_modalities)}->{'+'.join(self.output_modalities)}". + f"{'+'.join(self.input_modalities)}->{'+'.join(self.output_modalities)}" + # What: complete the architecture entry expression with architecture modality f join self input modalities join self output modalities; why: ModelCapabilities.model_listing_fields groups the supplied clauses as one architecture entry expression before its value is consumed. + ) + # What: compute doc entry from architecture; why: doc capabilities function calling later reads doc entry, so model_listing_fields must retain the computed value under that name. + doc["architecture"] = architecture + # What: gate on tools before doc; why: model_listing_fields admits doc only for this predicate and excludes the opposite state. + if self.tools: + # What: map the function calling field as true; why: ModelCapabilities.model_listing_fields carries function calling through doc entry into doc supported parameters tools tool choice. + doc["capabilities"] = {"function_calling": True} + # What: compute doc entry from tools and tool choice; why: doc context length self context later reads doc entry, so model_listing_fields must retain the computed value under that name. + doc["supported_parameters"] = ["tools", "tool_choice"] + # What: gate on context before context and doc; why: model_listing_fields admits context and doc only for this predicate and excludes the opposite state. + if self.context: + # What: compute doc entry from context; why: doc context window self context later reads doc entry, so model_listing_fields must retain the computed value under that name. + doc["context_length"] = self.context + # What: compute doc entry from context; why: doc meta n ctx self context later reads doc entry, so model_listing_fields must retain the computed value under that name. + doc["context_window"] = self.context + # What: map the n ctx field as context; why: ModelCapabilities.model_listing_fields carries n ctx through doc entry into return doc. + doc["meta"] = {"n_ctx": self.context} + # What: return doc from model_listing_fields; why: model_listing_fields exposes doc so its caller can continue with the function\'s computed outcome. + return doc + + +# What: generate dataclass initialization and value semantics for RequestField; why: RequestField acts as a typed state record with consistent construction, comparison, and representation. +@dataclass(frozen=True) +# What: define RequestField as the owner of key and value; why: daemon callers use this class boundary so those methods share one request field state invariant. +class RequestField: + """One immutable, validated JSON field assignment.""" +# What: document one immutable validated json field assignment in the RequestField docstring; why: introspection and maintainers read this exact docstring fragment to understand request field behavior without executing it. + + # What: compute path from the named fixture input; why: return join self path later reads path, so catalog must retain the computed value under that name. + path: tuple[str, ...] + # What: compute value json from the named fixture input; why: return json loads self value json later reads value json, so catalog must retain the computed value under that name. + value_json: str + # What: compute soft from false; why: field key if field soft else field value later reads soft, so catalog must retain the computed value under that name. + soft: bool = False + + # What: expose key as a read-only computed property; why: callers read key through attribute access while its getter retains control of the derived value. + @property + # What: define key around the current object state; why: the registered API client call key for key and rely on this exact input and result contract. + def key(self) -> str: + # What: return join and path and value from key; why: key exposes join and path and value so its caller can continue with the function\'s computed outcome. + return ".".join(self.path) + + # What: define value around the current object state; why: its direct callers call value for value and rely on this exact input and result contract. + def value(self) -> Any: + # What: return loads and value json and json from value; why: value exposes loads and value json and json so its caller can continue with the function\'s computed outcome. + return json.loads(self.value_json) + + +# What: define _request_fields_public around fields; why: its direct callers call _request_fields_public for request fields public and rely on this exact input and result contract. +def _request_fields_public(fields: tuple[RequestField, ...]) -> dict[str, Any]: + # What: return key and value and field and fields from _request_fields_public; why: _request_fields_public exposes key and value and field and fields so its caller can continue with the function\'s computed outcome. + return { + # What: call field.value with the declared inputs; why: _request_fields_public invokes field.value while performing for field in fields; the call advances that operation through its result or side effect. + field.key + ("?" if field.soft else ""): field.value() + # What: apply the for field in fields portion of the enclosing predicate; why: this clause remains in _request_fields_public\'s enclosing expression so its grouping and evaluation order stay intact. + for field in fields + # What: complete the _request_fields_public signature with fields; why: _request_fields_public groups the supplied clauses as one _request_fields_public signature before its value is consumed. + } + + +# What: generate dataclass initialization and value semantics for ModelSelector; why: ModelSelector acts as a typed state record with consistent construction, comparison, and representation. +@dataclass(frozen=True) +# What: define ModelSelector as the owner of metadata and public; why: daemon callers use this class boundary so those methods share one model selector state invariant. +class ModelSelector: + """A per-request virtual model resolved to one concrete local profile.""" +# What: document a per request virtual model resolved to in the ModelSelector docstring; why: introspection and maintainers read this exact docstring fragment to understand model selector behavior without executing it. + + # What: compute name from the named fixture input; why: name self name later reads name, so catalog must retain the computed value under that name. + name: str + # What: compute strategy from the named fixture input; why: strategy self strategy later reads strategy, so catalog must retain the computed value under that name. + strategy: str + # What: compute targets from the named fixture input; why: targets list self targets later reads targets, so catalog must retain the computed value under that name. + targets: tuple[str, ...] + # What: compute display name from the named fixture input; why: if self display name later reads display name, so catalog must retain the computed value under that name. + display_name: str | None = None + # What: compute description from the named fixture input; why: if self description later reads description, so catalog must retain the computed value under that name. + description: str | None = None + # What: compute unlisted from false; why: if self unlisted later reads unlisted, so catalog must retain the computed value under that name. + unlisted: bool = False + # What: compute metadata json from value; why: return json loads self metadata json later reads metadata json, so catalog must retain the computed value under that name. + metadata_json: str = "{}" + + # What: define metadata around the current object state; why: its direct callers call metadata for metadata and rely on this exact input and result contract. + def metadata(self) -> dict[str, Any]: + # What: return loads and metadata json and json from metadata; why: metadata exposes loads and metadata json and json so its caller can continue with the function\'s computed outcome. + return json.loads(self.metadata_json) + + # What: define public around the current object state; why: its direct callers call public for public and rely on this exact input and result contract. + def public(self) -> dict[str, Any]: + # What: compute doc from name and strategy and list and targets and name; why: doc display name self display name later reads doc, so public must retain the computed value under that name. + doc: dict[str, Any] = { + # What: map the name field as name; why: ModelSelector.public carries name through doc into doc display name self display name. + "name": self.name, + # What: map the strategy field as strategy; why: ModelSelector.public carries strategy through doc into doc display name self display name. + "strategy": self.strategy, + # What: map the targets field as list and targets; why: ModelSelector.public carries targets through doc into doc display name self display name. + "targets": list(self.targets), + # What: complete the doc mapping with name and strategy and targets; why: ModelSelector.public groups the supplied clauses as one doc mapping before its value is consumed. + } + # What: gate on display name before display name and doc; why: public admits display name and doc only for this predicate and excludes the opposite state. + if self.display_name: + # What: compute doc entry from display name; why: doc description self description later reads doc entry, so public must retain the computed value under that name. + doc["displayName"] = self.display_name + # What: gate on description before description and doc; why: public admits description and doc only for this predicate and excludes the opposite state. + if self.description: + # What: compute doc entry from description; why: doc unlisted later reads doc entry, so public must retain the computed value under that name. + doc["description"] = self.description + # What: gate on unlisted before doc; why: public admits doc only for this predicate and excludes the opposite state. + if self.unlisted: + # What: compute doc entry from true; why: doc metadata metadata later reads doc entry, so public must retain the computed value under that name. + doc["unlisted"] = True + # What: compute metadata from metadata; why: if metadata later reads metadata, so public must retain the computed value under that name. + metadata = self.metadata() + # What: gate on metadata before metadata and doc; why: public admits metadata and doc only for this predicate and excludes the opposite state. + if metadata: + # What: compute doc entry from metadata; why: return doc later reads doc entry, so public must retain the computed value under that name. + doc["metadata"] = metadata + # What: return doc from public; why: public exposes doc so its caller can continue with the function\'s computed outcome. + return doc + + +# What: generate dataclass initialization and value semantics for RoutingProfile; why: RoutingProfile acts as a typed state record with consistent construction, comparison, and representation. +@dataclass(frozen=True) +# What: define RoutingProfile as the owner of replacement and public; why: daemon callers use this class boundary so those methods share one routing profile state invariant. +class RoutingProfile: + """A runtime-selectable set of client model-ID replacements.""" +# What: document a runtime selectable set of client model id in the RoutingProfile docstring; why: introspection and maintainers read this exact docstring fragment to understand routing profile behavior without executing it. + + # What: compute name from the named fixture input; why: name self name later reads name, so catalog must retain the computed value under that name. + name: str + # What: compute pins from the named fixture input; why: for pin target in self pins later reads pins, so catalog must retain the computed value under that name. + pins: tuple[tuple[str, str | None], ...] + # What: compute description from the named fixture input; why: if self description later reads description, so catalog must retain the computed value under that name. + description: str | None = None + + # What: define replacement around model id; why: its direct callers call replacement for replacement and rely on this exact input and result contract. + def replacement(self, model_id: str) -> tuple[bool, str | None]: + # What: iterate across pins to perform pin and model id and target; why: replacement repeats the body only while or for the loop header admits an iteration. + for pin, target in self.pins: + # What: gate on pin and model id before target; why: replacement admits target only for this predicate and excludes the opposite state. + if pin == model_id: + # What: return target and true from replacement; why: replacement exposes target and true so its caller can continue with the function\'s computed outcome. + return True, target + # What: return false from replacement; why: replacement exposes false so its caller can continue with the function\'s computed outcome. + return False, None + + # What: define public around the current object state; why: its direct callers call public for public and rely on this exact input and result contract. + def public(self) -> dict[str, Any]: + # What: compute doc from name and pin and target and pins and name; why: doc description self description later reads doc, so public must retain the computed value under that name. + doc: dict[str, Any] = { + # What: map the name field as name; why: RoutingProfile.public carries name through doc into doc description self description. + "name": self.name, + # What: map the pins field as pin and target and pins; why: RoutingProfile.public carries pins through doc into doc description self description. + "pins": {pin: target for pin, target in self.pins}, + # What: complete the doc mapping with name and pins; why: RoutingProfile.public groups the supplied clauses as one doc mapping before its value is consumed. + } + # What: gate on description before description and doc; why: public admits description and doc only for this predicate and excludes the opposite state. + if self.description: + # What: compute doc entry from description; why: return doc later reads doc entry, so public must retain the computed value under that name. + doc["description"] = self.description + # What: return doc from public; why: public exposes doc so its caller can continue with the function\'s computed outcome. + return doc + + +# What: generate dataclass initialization and value semantics for ModelProfile; why: ModelProfile acts as a typed state record with consistent construction, comparison, and representation. +@dataclass(frozen=True) +# What: define ModelProfile as the owner of metadata and proxy_base_url and request and public; why: daemon callers use this class boundary so those methods share one model profile state invariant. +class ModelProfile: + # What: compute name from the named fixture input; why: doc name self name later reads name, so catalog must retain the computed value under that name. + name: str + # What: compute model from the named fixture input; why: body dict str any model self model later reads model, so catalog must retain the computed value under that name. + model: str + # What: compute args from the named fixture input; why: body dict str any model self model later reads args, so catalog must retain the computed value under that name. + args: tuple[str, ...] + # What: compute port from the named fixture input; why: def proxy base url port int str later reads port, so catalog must retain the computed value under that name. + port: int | None = None + # What: compute description from the named fixture input; why: if self description later reads description, so catalog must retain the computed value under that name. + description: str | None = None + # What: compute ready timeout s from 120 0; why: doc ready timeout s self ready timeout s later reads ready timeout s, so catalog must retain the computed value under that name. + ready_timeout_s: float = 120.0 + # What: compute ttl s from the named fixture input; why: if self ttl s is not later reads ttl s, so catalog must retain the computed value under that name. + ttl_s: float | None = None + # What: compute unload timeout s from the named fixture input; why: if self unload timeout s is not later reads unload timeout s, so catalog must retain the computed value under that name. + unload_timeout_s: float | None = None + # What: compute priority from 0; why: if self priority later reads priority, so catalog must retain the computed value under that name. + priority: int = 0 + # What: compute group from the named fixture input; why: if self group is not later reads group, so catalog must retain the computed value under that name. + group: str | None = None + # What: compute drop fields from the named fixture input; why: if self drop fields later reads drop fields, so catalog must retain the computed value under that name. + drop_fields: tuple[str, ...] = () + # What: compute aliases from the named fixture input; why: if self aliases later reads aliases, so catalog must retain the computed value under that name. + aliases: tuple[str, ...] = () + # What: compute unlisted from false; why: if self unlisted later reads unlisted, so catalog must retain the computed value under that name. + unlisted: bool = False + # What: compute concurrency limit from 0; why: if self concurrency limit later reads concurrency limit, so catalog must retain the computed value under that name. + concurrency_limit: int = 0 + # What: compute send loading state from the named fixture input; why: if self send loading state is not later reads send loading state, so catalog must retain the computed value under that name. + send_loading_state: bool | None = None + # What: compute capabilities from model capabilities; why: if not self capabilities empty later reads capabilities, so catalog must retain the computed value under that name. + capabilities: ModelCapabilities = ModelCapabilities() + # What: compute set fields from the named fixture input; why: if self set fields later reads set fields, so catalog must retain the computed value under that name. + set_fields: tuple[RequestField, ...] = () + # What: compute set fields by id from the named fixture input; why: if self set fields by id later reads set fields by id, so catalog must retain the computed value under that name. + set_fields_by_id: tuple[tuple[str, tuple[RequestField, ...]], ...] = () + # What: compute check endpoint from default check endpoint; why: if self check endpoint default check endpoint later reads check endpoint, so catalog must retain the computed value under that name. + check_endpoint: str = DEFAULT_CHECK_ENDPOINT + # What: compute proxy from default proxy; why: return self proxy replace port str port later reads proxy, so catalog must retain the computed value under that name. + proxy: str = DEFAULT_PROXY + # What: compute use model name from the named fixture input; why: if self use model name is not later reads use model name, so catalog must retain the computed value under that name. + use_model_name: str | None = None + # What: compute display name from the named fixture input; why: if self display name later reads display name, so catalog must retain the computed value under that name. + display_name: str | None = None + # What: compute metadata json from value; why: return json loads self metadata json later reads metadata json, so catalog must retain the computed value under that name. + metadata_json: str = "{}" + # What: compute upstream timeout s from the named fixture input; why: if self upstream timeout s is not later reads upstream timeout s, so catalog must retain the computed value under that name. + upstream_timeout_s: float | None = None + + # What: define metadata around the current object state; why: its direct callers call metadata for metadata and rely on this exact input and result contract. + def metadata(self) -> dict[str, Any]: + # What: return loads and metadata json and json from metadata; why: metadata exposes loads and metadata json and json so its caller can continue with the function\'s computed outcome. + return json.loads(self.metadata_json) + + # What: define proxy_base_url around port; why: its direct callers call proxy_base_url for proxy base url and rely on this exact input and result contract. + def proxy_base_url(self, port: int) -> str: + """Resolve the validated loopback template to this owned child port.""" + # What: document resolve the validated loopback template to in the proxy_base_url docstring; why: introspection and maintainers read this exact docstring fragment to understand proxy base url behavior without executing it. + # What: return replace and proxy and str and port and port from proxy_base_url; why: proxy_base_url exposes replace and proxy and str and port and port so its caller can continue with the function\'s computed outcome. + return self.proxy.replace("${PORT}", str(port)) + + # What: define request around the current object state; why: its direct callers call request for request and rely on this exact input and result contract. + def request(self) -> dict[str, Any]: + # What: map the model field as model; why: ModelProfile.request sends this field through body so the router selects the canonical model or alias for upstream dispatch. + body: dict[str, Any] = {"model": self.model, "args": list(self.args)} + # What: gate on port before port and body; why: request admits port and body only for this predicate and excludes the opposite state. + if self.port is not None: + # What: compute body entry from port; why: body dynamic port later reads body entry, so request must retain the computed value under that name. + body["port"] = self.port + # What: gate on port before body; why: request admits body only for this predicate and excludes the opposite state. + if self.port == 0: + # What: compute body entry from true; why: return body later reads body entry, so request must retain the computed value under that name. + body["dynamicPort"] = True + # What: return body from request; why: request exposes body so its caller can continue with the function\'s computed outcome. + return body + + # What: define public around the current object state; why: its direct callers call public for public and rely on this exact input and result contract. + def public(self) -> dict[str, Any]: + # What: compute doc from request; why: doc name self name later reads doc, so public must retain the computed value under that name. + doc = self.request() + # What: compute doc entry from name; why: doc display name self display name later reads doc entry, so public must retain the computed value under that name. + doc["name"] = self.name + # What: gate on display name before display name and doc; why: public admits display name and doc only for this predicate and excludes the opposite state. + if self.display_name: + # What: compute doc entry from display name; why: doc description self description later reads doc entry, so public must retain the computed value under that name. + doc["displayName"] = self.display_name + # What: gate on description before description and doc; why: public admits description and doc only for this predicate and excludes the opposite state. + if self.description: + # What: compute doc entry from description; why: doc ready timeout s self ready timeout s later reads doc entry, so public must retain the computed value under that name. + doc["description"] = self.description + # What: compute doc entry from ready timeout s; why: doc ttl s self ttl s later reads doc entry, so public must retain the computed value under that name. + doc["readyTimeoutS"] = self.ready_timeout_s + # What: gate on ttl s before ttl s and doc; why: public admits ttl s and doc only for this predicate and excludes the opposite state. + if self.ttl_s is not None: + # What: compute doc entry from ttl s; why: doc unload timeout s self unload timeout s later reads doc entry, so public must retain the computed value under that name. + doc["ttlS"] = self.ttl_s + # What: gate on unload timeout s before unload timeout s and doc; why: public admits unload timeout s and doc only for this predicate and excludes the opposite state. + if self.unload_timeout_s is not None: + # What: compute doc entry from unload timeout s; why: doc priority self priority later reads doc entry, so public must retain the computed value under that name. + doc["unloadTimeoutS"] = self.unload_timeout_s + # What: gate on priority before priority and doc; why: public admits priority and doc only for this predicate and excludes the opposite state. + if self.priority: + # What: compute doc entry from priority; why: doc group self group later reads doc entry, so public must retain the computed value under that name. + doc["priority"] = self.priority + # What: gate on group before group and doc; why: public admits group and doc only for this predicate and excludes the opposite state. + if self.group is not None: + # What: compute doc entry from group; why: doc drop fields list self drop fields later reads doc entry, so public must retain the computed value under that name. + doc["group"] = self.group + # What: gate on drop fields before doc and list and drop fields; why: public admits doc and list and drop fields only for this predicate and excludes the opposite state. + if self.drop_fields: + # What: compute doc entry from list and drop fields; why: doc aliases list self aliases later reads doc entry, so public must retain the computed value under that name. + doc["dropFields"] = list(self.drop_fields) + # What: gate on aliases before doc and list and aliases; why: public admits doc and list and aliases only for this predicate and excludes the opposite state. + if self.aliases: + # What: compute doc entry from list and aliases; why: doc unlisted later reads doc entry, so public must retain the computed value under that name. + doc["aliases"] = list(self.aliases) + # What: gate on unlisted before doc; why: public admits doc only for this predicate and excludes the opposite state. + if self.unlisted: + # What: compute doc entry from true; why: doc concurrency limit self concurrency limit later reads doc entry, so public must retain the computed value under that name. + doc["unlisted"] = True + # What: gate on concurrency limit before concurrency limit and doc; why: public admits concurrency limit and doc only for this predicate and excludes the opposite state. + if self.concurrency_limit: + # What: compute doc entry from concurrency limit; why: doc send loading state self send loading state later reads doc entry, so public must retain the computed value under that name. + doc["concurrencyLimit"] = self.concurrency_limit + # What: gate on send loading state before send loading state and doc; why: public admits send loading state and doc only for this predicate and excludes the opposite state. + if self.send_loading_state is not None: + # What: compute doc entry from send loading state; why: doc capabilities self capabilities public later reads doc entry, so public must retain the computed value under that name. + doc["sendLoadingState"] = self.send_loading_state + # What: gate on empty and capabilities before doc and public and capabilities; why: public admits doc and public and capabilities only for this predicate and excludes the opposite state. + if not self.capabilities.empty(): + # What: compute doc entry from public and capabilities; why: doc set fields request fields public self set fields later reads doc entry, so public must retain the computed value under that name. + doc["capabilities"] = self.capabilities.public() + # What: gate on set fields before doc and request fields public and set fields; why: public admits doc and request fields public and set fields only for this predicate and excludes the opposite state. + if self.set_fields: + # What: compute doc entry from request fields public and set fields; why: doc set fields by id later reads doc entry, so public must retain the computed value under that name. + doc["setFields"] = _request_fields_public(self.set_fields) + # What: gate on set fields by id before doc and model id and request fields public and fields and set fields by id; why: public admits doc and model id and request fields public and fields and set fields by id only for this predicate and excludes the opposite state. + if self.set_fields_by_id: + # What: compute doc entry from model id and request fields public and fields and set fields by id; why: doc check endpoint self check endpoint later reads doc entry, so public must retain the computed value under that name. + doc["setFieldsById"] = { + # What: call _request_fields_public with fields; why: public invokes _request_fields_public while performing for model id fields in self set fields by id; the call advances that operation through its result or side effect. + model_id: _request_fields_public(fields) + # What: apply the for model id fields in self set fields by id portion of doc entry; why: public uses this clause to evaluate doc entry as one grouped value. + for model_id, fields in self.set_fields_by_id + # What: complete the doc entry expression with doc set fields by id model id request fields public fields for model id fields in; why: ModelProfile.public groups the supplied clauses as one doc entry expression before its value is consumed. + } + # What: gate on check endpoint and default check endpoint before check endpoint and doc; why: public admits check endpoint and doc only for this predicate and excludes the opposite state. + if self.check_endpoint != DEFAULT_CHECK_ENDPOINT: + # What: compute doc entry from check endpoint; why: doc proxy self proxy later reads doc entry, so public must retain the computed value under that name. + doc["checkEndpoint"] = self.check_endpoint + # What: gate on proxy and default proxy before proxy and doc; why: public admits proxy and doc only for this predicate and excludes the opposite state. + if self.proxy != DEFAULT_PROXY: + # What: compute doc entry from proxy; why: doc use model name self use model name later reads doc entry, so public must retain the computed value under that name. + doc["proxy"] = self.proxy + # What: gate on use model name before use model name and doc; why: public admits use model name and doc only for this predicate and excludes the opposite state. + if self.use_model_name is not None: + # What: compute doc entry from use model name; why: doc metadata metadata later reads doc entry, so public must retain the computed value under that name. + doc["useModelName"] = self.use_model_name + # What: compute metadata from metadata; why: if metadata later reads metadata, so public must retain the computed value under that name. + metadata = self.metadata() + # What: gate on metadata before metadata and doc; why: public admits metadata and doc only for this predicate and excludes the opposite state. + if metadata: + # What: compute doc entry from metadata; why: doc upstream timeout s self upstream timeout s later reads doc entry, so public must retain the computed value under that name. + doc["metadata"] = metadata + # What: gate on upstream timeout s before upstream timeout s and doc; why: public admits upstream timeout s and doc only for this predicate and excludes the opposite state. + if self.upstream_timeout_s is not None: + # What: compute doc entry from upstream timeout s; why: return doc later reads doc entry, so public must retain the computed value under that name. + doc["upstreamTimeoutS"] = self.upstream_timeout_s + # What: return doc from public; why: public exposes doc so its caller can continue with the function\'s computed outcome. + return doc + + +# What: define ModelCatalog as the owner of __init__ and empty and load and get and public; why: daemon callers use this class boundary so those methods share one model catalog state invariant. +class ModelCatalog: + # What: define __init__ around profiles and settings and selectors and routing profiles and path; why: its direct callers call __init__ for init and rely on this exact input and result contract. + def __init__( + # What: declare the self input for __init__; why: __init__ consumes self during self profiles dict profiles, so callers must bind it with the other signature inputs. + self, + # What: declare the profiles input for __init__; why: __init__ consumes profiles during self profiles dict profiles, so callers must bind it with the other signature inputs. + profiles: dict[str, ModelProfile], + # What: declare the settings input for __init__; why: __init__ consumes settings during settings settings or router settings, so callers must bind it with the other signature inputs. + settings: RouterSettings | None = None, + # What: mark the remaining parameters as keyword-only; why: __init__ prevents callers from confusing adjacent lifecycle and timing arguments. + *, + # What: declare the selectors input for __init__; why: __init__ consumes selectors during self selectors dict selectors or, so callers must bind it with the other signature inputs. + selectors: dict[str, ModelSelector] | None = None, + # What: declare the routing profiles input for __init__; why: __init__ consumes routing profiles during self routing profiles dict routing profiles or, so callers must bind it with the other signature inputs. + routing_profiles: dict[str, RoutingProfile] | None = None, + # What: declare the path input for __init__; why: __init__ consumes path during self path path, so callers must bind it with the other signature inputs. + path: str | None = None, + # What: complete the enclosing predicate with def init profiles dict str model profile settings router settings selectors; why: ModelCatalog.__init__ groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: compute profiles from dict and profiles; why: canonical set self profiles later reads profiles, so __init__ must retain the computed value under that name. + self._profiles = dict(profiles) + # What: initialize aliases as an empty runtime accumulator; why: ModelCatalog.__init__ appends or maps entries into it during for alias in profile aliases before consuming the aggregate. + aliases: dict[str, str] = {} + # What: compute canonical from set and profiles; why: if alias in canonical later reads canonical, so __init__ must retain the computed value under that name. + canonical = set(self._profiles) + # What: iterate across items and profiles to perform model id and name; why: __init__ repeats the body only while or for the loop header admits an iteration. + for name, profile in self._profiles.items(): + # What: call _model_id with name; why: __init__ invokes _model_id while performing model id profile name; the call advances that operation through its result or side effect. + _model_id(name) + # What: call _model_id with name and profile; why: __init__ invokes _model_id while performing if name profile name; the call advances that operation through its result or side effect. + _model_id(profile.name) + # What: gate on name and profile before catalog error and name and profile; why: __init__ admits catalog error and name and profile only for this predicate and excludes the opposite state. + if name != profile.name: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"profile key {name!r} must match profile name {profile.name!r}") + # What: iterate across aliases and profile to perform alias and model id; why: __init__ repeats the body only while or for the loop header admits an iteration. + for alias in profile.aliases: + # What: compute alias from model id and alias; why: if alias in canonical later reads alias, so __init__ must retain the computed value under that name. + alias = _model_id(alias) + # What: gate on alias and canonical before catalog error and alias; why: __init__ admits catalog error and alias only for this predicate and excludes the opposite state. + if alias in canonical: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"model alias {alias!r} conflicts with a configured profile") + # What: gate on alias and aliases before catalog error and alias and name and aliases; why: __init__ admits catalog error and alias and name and aliases only for this predicate and excludes the opposite state. + if alias in aliases: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f model alias alias r is portion of the enclosing predicate; why: this clause remains in __init__\'s enclosing expression so its grouping and evaluation order stay intact. + f"model alias {alias!r} is assigned to both {aliases[alias]!r} and {name!r}" + # What: complete the CatalogError call with alias; why: ModelCatalog.__init__ groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: compute aliases entry from name; why: self aliases aliases later reads aliases entry, so __init__ must retain the computed value under that name. + aliases[alias] = name + # What: compute aliases from aliases; why: the enclosing return or state update later reads aliases, so __init__ must retain the computed value under that name. + self._aliases = aliases + # What: compute selectors from dict and selectors; why: for name selector in self selectors items later reads selectors, so __init__ must retain the computed value under that name. + self._selectors = dict(selectors or {}) + # What: compute occupied from canonical and set and aliases; why: if name in occupied later reads occupied, so __init__ must retain the computed value under that name. + occupied = canonical | set(aliases) + # What: iterate across items and selectors to perform model id and name; why: __init__ repeats the body only while or for the loop header admits an iteration. + for name, selector in self._selectors.items(): + # What: call _model_id with name; why: __init__ invokes _model_id while performing if name selector name; the call advances that operation through its result or side effect. + _model_id(name) + # What: gate on name and selector before catalog error and name and selector; why: __init__ admits catalog error and name and selector only for this predicate and excludes the opposite state. + if name != selector.name: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f selector key name r must portion of the enclosing predicate; why: this clause remains in __init__\'s enclosing expression so its grouping and evaluation order stay intact. + f"selector key {name!r} must match selector name {selector.name!r}" + # What: complete the CatalogError call with name; why: ModelCatalog.__init__ groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: gate on name and occupied before catalog error and name; why: __init__ admits catalog error and name only for this predicate and excludes the opposite state. + if name in occupied: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"selector {name!r} conflicts with a model ID or alias") + # What: iterate across targets and selector to perform target and selectors and catalog error and name; why: __init__ repeats the body only while or for the loop header admits an iteration. + for target in selector.targets: + # What: gate on target and selectors before catalog error and name and target; why: __init__ admits catalog error and name and target only for this predicate and excludes the opposite state. + if target in self._selectors: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f selector name r target target portion of the enclosing predicate; why: this clause remains in __init__\'s enclosing expression so its grouping and evaluation order stay intact. + f"selector {name!r} target {target!r} cannot reference another selector" + # What: complete the CatalogError call with name; why: ModelCatalog.__init__ groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: establish the handler boundary for the protected operation; why: ModelCatalog.__init__ routes failures to catalog error while preserving cleanup and success flow. + try: + # What: call self.get with target; why: __init__ invokes self.get while performing except catalog error as exc; the call advances that operation through its result or side effect. + self.get(target) + # What: handle catalog error by raise catalog error; why: ModelCatalog.__init__ converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError as exc: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f selector name r target target portion of the enclosing predicate; why: this clause remains in __init__\'s enclosing expression so its grouping and evaluation order stay intact. + f"selector {name!r} target {target!r} is not a configured model or alias" + # What: apply the from exc portion of the enclosing predicate; why: this clause remains in __init__\'s enclosing expression so its grouping and evaluation order stay intact. + ) from exc + # What: compute routing profiles from dict and routing profiles; why: for name routing profile in self routing profiles items later reads routing profiles, so __init__ must retain the computed value under that name. + self._routing_profiles = dict(routing_profiles or {}) + # What: iterate across items and routing profiles to perform simple name and name; why: __init__ repeats the body only while or for the loop header admits an iteration. + for name, routing_profile in self._routing_profiles.items(): + # What: preserve the exact simple name name profile name literal fragment; why: __init__ passes this fragment verbatim through _simple_name(name, "profile name"), because changing it would alter a protocol payload, serialized fixture, or public message. + _simple_name(name, "profile name") + # What: gate on name and routing profile before catalog error and name and routing profile; why: __init__ admits catalog error and name and routing profile only for this predicate and excludes the opposite state. + if name != routing_profile.name: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f routing profile key name r portion of the enclosing predicate; why: this clause remains in __init__\'s enclosing expression so its grouping and evaluation order stay intact. + f"routing profile key {name!r} must match profile name {routing_profile.name!r}" + # What: complete the CatalogError call with name; why: ModelCatalog.__init__ groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: gate on pins and routing profile before catalog error and name; why: __init__ admits catalog error and name only for this predicate and excludes the opposite state. + if not routing_profile.pins: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"profiles.{name}.pins must contain at least one entry") + # What: iterate across pins and routing profile to perform model id and pin; why: __init__ repeats the body only while or for the loop header admits an iteration. + for pin, target in routing_profile.pins: + # What: call _model_id with pin; why: __init__ invokes _model_id while performing if target is not and not; the call advances that operation through its result or side effect. + _model_id(pin) + # What: gate on target and has routable id before catalog error and name and pin and target; why: __init__ admits catalog error and name and pin and target only for this predicate and excludes the opposite state. + if target is not None and not self.has_routable_id(target): + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f profiles name pins pin references portion of the enclosing predicate; why: this clause remains in __init__\'s enclosing expression so its grouping and evaluation order stay intact. + f"profiles.{name}.pins.{pin} references unknown model {target!r}" + # What: complete the CatalogError call with name; why: ModelCatalog.__init__ groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: compute settings from settings and router settings; why: if settings preload model is not later reads settings, so __init__ must retain the computed value under that name. + settings = settings or RouterSettings() + # What: gate on preload model and settings before selector and preload model and catalog error and settings; why: __init__ admits selector and preload model and catalog error and settings only for this predicate and excludes the opposite state. + if settings.preload_model is not None: + # What: gate on selector and preload model and settings before catalog error; why: __init__ admits catalog error only for this predicate and excludes the opposite state. + if self.selector(settings.preload_model) is not None: + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.preload_model must name a concrete model or alias") + # What: compute settings from replace and settings and name and get; why: settings startup routing profile is not later reads settings, so __init__ must retain the computed value under that name. + settings = replace(settings, preload_model=self.get(settings.preload_model).name) + # What: gate on startup routing profile and routing profiles and settings before catalog error; why: __init__ admits catalog error only for this predicate and excludes the opposite state. + if ( + # What: apply the settings startup routing profile is not portion of the enclosing predicate; why: this clause remains in __init__\'s enclosing expression so its grouping and evaluation order stay intact. + settings.startup_routing_profile is not None + # What: apply the and settings startup routing profile not in self routing profiles portion of the enclosing predicate; why: this clause remains in __init__\'s enclosing expression so its grouping and evaluation order stay intact. + and settings.startup_routing_profile not in self._routing_profiles + # What: complete the enclosing predicate with if settings startup routing profile is not and settings startup routing profile not in self routing profiles; why: ModelCatalog.__init__ groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: ModelCatalog.__init__ stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.startup_routing_profile references an unknown profile") + # What: compute settings from settings; why: the enclosing return or state update later reads settings, so __init__ must retain the computed value under that name. + self.settings = settings + # What: compute path from path; why: the enclosing return or state update later reads path, so __init__ must retain the computed value under that name. + self.path = path + + # What: bind empty to the class rather than an instance; why: factory and parser callers construct empty from class-level state without requiring an existing object. + @classmethod + # What: define empty around the current object state; why: the registered API client call empty for empty and rely on this exact input and result contract. + def empty(cls) -> "ModelCatalog": + # What: return no value from empty; why: empty returns no value to callers that depend on its completed result. + return cls({}) + + # What: bind load to the class rather than an instance; why: factory and parser callers construct load from class-level state without requiring an existing object. + @classmethod + # What: define load around path; why: the registered API client call load for load and rely on this exact input and result contract. + def load(cls, path: str) -> "ModelCatalog": + # What: establish the handler boundary for the protected operation; why: ModelCatalog.load routes failures to oserror and tomldecode error and tomllib while preserving cleanup and success flow. + try: + # What: enter the open managed context before raw tomllib load source; why: load releases this resource or lock after raw tomllib load source on both success and failure paths. + with open(path, "rb") as source: + # What: compute raw from load and source and tomllib; why: models raw get models later reads raw, so load must retain the computed value under that name. + raw = tomllib.load(source) + # What: handle oserror and tomldecode error and tomllib by raise catalog error f cannot read catalog path; why: ModelCatalog.load converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, tomllib.TOMLDecodeError) as exc: + # What: raise CatalogError for the caller; why: ModelCatalog.load stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"cannot read catalog {path!r}: {exc}") from exc + # What: compute models from get and raw and models; why: if not isinstance models dict later reads models, so load must retain the computed value under that name. + models = raw.get("models") + # What: gate on isinstance and models and dict before catalog error; why: load admits catalog error only for this predicate and excludes the opposite state. + if not isinstance(models, dict): + # What: raise CatalogError for the caller; why: ModelCatalog.load stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("catalog requires a [models] table") + # What: initialize profiles as an empty runtime accumulator; why: ModelCatalog.load appends or maps entries into it during profiles model id name profile model id name value before consuming the aggregate. + profiles: dict[str, ModelProfile] = {} + # What: iterate across items and models to perform profiles and profile and value and model id and name; why: load repeats the body only while or for the loop header admits an iteration. + for name, value in models.items(): + # What: compute profiles entry from profile and value and model id and name; why: raw profiles raw get profiles later reads profiles entry, so load must retain the computed value under that name. + profiles[_model_id(name)] = _profile(_model_id(name), value) + # What: compute raw selectors from get and raw and selectors; why: if not isinstance raw selectors dict later reads raw selectors, so load must retain the computed value under that name. + raw_selectors = raw.get("selectors", {}) + # What: gate on isinstance and raw selectors and dict before catalog error; why: load admits catalog error only for this predicate and excludes the opposite state. + if not isinstance(raw_selectors, dict): + # What: raise CatalogError for the caller; why: ModelCatalog.load stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("selectors must be a table") + # What: compute selectors from model id and name and selector and value; why: selectors selectors later reads selectors, so load must retain the computed value under that name. + selectors = { + # What: call _model_id with name; why: load invokes _model_id while performing for name value in raw selectors items; the call advances that operation through its result or side effect. + _model_id(name): _selector(_model_id(name), value) + # What: call raw_selectors.items with the declared inputs; why: load consumes the raw_selectors.items return value while evaluating for name, value in raw_selectors.items(). + for name, value in raw_selectors.items() + # What: complete the selectors expression with selectors model id name selector model id name value for name; why: ModelCatalog.load groups the supplied clauses as one selectors expression before its value is consumed. + } + # What: compute raw profiles from get and raw and profiles; why: if not isinstance raw profiles dict later reads raw profiles, so load must retain the computed value under that name. + raw_profiles = raw.get("profiles", {}) + # What: gate on isinstance and raw profiles and dict before catalog error; why: load admits catalog error only for this predicate and excludes the opposite state. + if not isinstance(raw_profiles, dict): + # What: raise CatalogError for the caller; why: ModelCatalog.load stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("profiles must be a table") + # What: compute routing profiles from simple name and name and routing profile and value; why: routing profiles routing profiles later reads routing profiles, so load must retain the computed value under that name. + routing_profiles = { + # What: call _simple_name with name and profile and name; why: load invokes _simple_name while performing simple name name profile name value; the call advances that operation through its result or side effect. + _simple_name(name, "profile name"): _routing_profile( + # What: call _simple_name with name and profile and name; why: load consumes the _simple_name return value while evaluating _simple_name(name, "profile name"), value. + _simple_name(name, "profile name"), value + # What: complete the _routing_profile call with simple name and value; why: ModelCatalog.load groups the supplied clauses as one _routing_profile call before its value is consumed. + ) + # What: call raw_profiles.items with the declared inputs; why: load consumes the raw_profiles.items return value while evaluating for name, value in raw_profiles.items(). + for name, value in raw_profiles.items() + # What: complete the routing_profiles expression with routing profiles simple name name profile name routing profile simple name name profile; why: ModelCatalog.load groups the supplied clauses as one routing_profiles expression before its value is consumed. + } + # What: return profiles and router settings and selectors and routing profiles from load; why: load exposes profiles and router settings and selectors and routing profiles so its caller can continue with the function\'s computed outcome. + return cls( + # What: apply the profiles portion of the enclosing predicate; why: this clause remains in load\'s enclosing expression so its grouping and evaluation order stay intact. + profiles, + # What: call _router_settings with get and raw and router and profiles; why: load invokes _router_settings while performing selectors selectors; the call advances that operation through its result or side effect. + _router_settings(raw.get("router", {}), profiles), + # What: supply selectors to cls; why: load binds this selectors value to cls's selectors input. + selectors=selectors, + # What: supply routing profiles to cls; why: load binds this routing profiles value to cls's routing profiles input. + routing_profiles=routing_profiles, + # What: supply path to cls; why: load binds this path value to cls's path input. + path=path, + # What: complete the cls call with selectors and routing profiles and path; why: ModelCatalog.load groups the supplied clauses as one cls call before its value is consumed. + ) + + # What: define get around name; why: its direct callers call get for get and rely on this exact input and result contract. + def get(self, name: str) -> ModelProfile: + # What: establish the handler boundary for the protected operation; why: ModelCatalog.get routes failures to key error while preserving cleanup and success flow. + try: + # What: return profiles and get and name and aliases from get; why: get exposes profiles and get and name and aliases so its caller can continue with the function\'s computed outcome. + return self._profiles[self._aliases.get(name, name)] + # What: handle key error by raise catalog error f unknown model profile name; why: ModelCatalog.get converts that failure into this concrete recovery, response, or cleanup behavior. + except KeyError as exc: + # What: raise CatalogError for the caller; why: ModelCatalog.get stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"unknown model profile {name!r}") from exc + + # What: define public around the current object state; why: its direct callers call public for public and rely on this exact input and result contract. + def public(self) -> list[dict[str, Any]]: + # What: return public and name and sorted and profiles from public; why: public exposes public and name and sorted and profiles so its caller can continue with the function\'s computed outcome. + return [self._profiles[name].public() for name in sorted(self._profiles)] + + # What: define public_selectors around the current object state; why: its direct callers call public_selectors for public selectors and rely on this exact input and result contract. + def public_selectors(self) -> list[dict[str, Any]]: + # What: return public and name and sorted and selectors from public_selectors; why: public_selectors exposes public and name and sorted and selectors so its caller can continue with the function\'s computed outcome. + return [self._selectors[name].public() for name in sorted(self._selectors)] + + # What: define public_routing_profiles around the current object state; why: its direct callers call public_routing_profiles for public routing profiles and rely on this exact input and result contract. + def public_routing_profiles(self) -> list[dict[str, Any]]: + # What: return public and name and sorted and routing profiles from public_routing_profiles; why: public_routing_profiles exposes public and name and sorted and routing profiles so its caller can continue with the function\'s computed outcome. + return [ + # What: call self._routing_profiles.public with the declared inputs; why: public_routing_profiles consumes the self._routing_profiles.public return value while evaluating self._routing_profiles[name].public() for name in sorted(self._routing_p. + self._routing_profiles[name].public() for name in sorted(self._routing_profiles) + # What: complete the public_routing_profiles signature with self; why: ModelCatalog.public_routing_profiles groups the supplied clauses as one public_routing_profiles signature before its value is consumed. + ] + + # What: define profiles around the current object state; why: its direct callers call profiles for profiles and rely on this exact input and result contract. + def profiles(self) -> tuple[ModelProfile, ...]: + """Return immutable profile values for internal identity matching.""" + # What: document return immutable profile values for internal in the profiles docstring; why: introspection and maintainers read this exact docstring fragment to understand profiles behavior without executing it. + # What: return tuple and profiles and name and sorted from profiles; why: profiles exposes tuple and profiles and name and sorted so its caller can continue with the function\'s computed outcome. + return tuple(self._profiles[name] for name in sorted(self._profiles)) + + # What: define selector around name; why: its direct callers call selector for selector and rely on this exact input and result contract. + def selector(self, name: str) -> ModelSelector | None: + # What: return get and name and selectors from selector; why: selector exposes get and name and selectors so its caller can continue with the function\'s computed outcome. + return self._selectors.get(name) + + # What: define routing_profile around name; why: its direct callers call routing_profile for routing profile and rely on this exact input and result contract. + def routing_profile(self, name: str) -> RoutingProfile | None: + # What: return get and name and routing profiles from routing_profile; why: routing_profile exposes get and name and routing profiles so its caller can continue with the function\'s computed outcome. + return self._routing_profiles.get(name) + + # What: define has_routable_id around name; why: its direct callers call has_routable_id for has routable id and rely on this exact input and result contract. + def has_routable_id(self, name: str) -> bool: + # What: return name and selectors and profiles and aliases from has_routable_id; why: has_routable_id exposes name and selectors and profiles and aliases so its caller can continue with the function\'s computed outcome. + return name in self._selectors or name in self._profiles or name in self._aliases + + # What: define listed_model_ids around the current object state; why: its direct callers call listed_model_ids for listed model ids and rely on this exact input and result contract. + def listed_model_ids(self) -> tuple[str, ...]: + """Return the OpenAI-visible IDs without exposing hidden canonical profiles.""" + # What: document return the open ai visible ids without exposing in the listed_model_ids docstring; why: introspection and maintainers read this exact docstring fragment to understand listed model ids behavior without executing it. + # What: initialize result as an empty runtime accumulator; why: ModelCatalog.listed_model_ids appends or maps entries into it during result append name before consuming the aggregate. + result: list[str] = [] + # What: iterate across sorted and profiles to perform profile and profiles and name; why: listed_model_ids repeats the body only while or for the loop header admits an iteration. + for name in sorted(self._profiles): + # What: compute profile from profiles and name; why: if profile unlisted later reads profile, so listed_model_ids must retain the computed value under that name. + profile = self._profiles[name] + # What: gate on unlisted and profile before the computed value; why: listed_model_ids admits the computed value only for this predicate and excludes the opposite state. + if profile.unlisted: + # What: apply the continue portion of the enclosing predicate; why: this clause remains in listed_model_ids\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: call result.append with name; why: listed_model_ids invokes result.append while performing if self settings include aliases in list; the call advances that operation through its result or side effect. + result.append(name) + # What: gate on include aliases in list and settings before extend and aliases and result and profile; why: listed_model_ids admits extend and aliases and result and profile only for this predicate and excludes the opposite state. + if self.settings.include_aliases_in_list: + # What: call result.extend with aliases and profile; why: listed_model_ids invokes result.extend while performing result extend; the call advances that operation through its result or side effect. + result.extend(profile.aliases) + # What: call result.extend with name and sorted and selectors and unlisted; why: listed_model_ids invokes result.extend while performing name for name in sorted self selectors; the call advances that operation through its result or side effect. + result.extend( + # What: call sorted with selectors; why: listed_model_ids consumes the sorted return value while evaluating name for name in sorted(self._selectors) if not self._selectors[name].un. + name for name in sorted(self._selectors) if not self._selectors[name].unlisted + # What: complete the result.extend call with name; why: ModelCatalog.listed_model_ids groups the supplied clauses as one result.extend call before its value is consumed. + ) + # What: return tuple and result from listed_model_ids; why: listed_model_ids exposes tuple and result so its caller can continue with the function\'s computed outcome. + return tuple(result) + + # What: define resolve_upstream_path around path; why: its direct callers call resolve_upstream_path for resolve upstream path and rely on this exact input and result contract. + def resolve_upstream_path(self, path: str) -> tuple[str, ModelProfile, str]: + """Resolve the longest configured model-ID prefix from a decoded path.""" + # What: document resolve the longest configured model id prefix in the resolve_upstream_path docstring; why: introspection and maintainers read this exact docstring fragment to understand resolve upstream path behavior without executing it. + # What: compute parts from split and strip and path and value and value; why: for index in range len parts later reads parts, so resolve_upstream_path must retain the computed value under that name. + parts = path.strip("/").split("/") + # What: compute match from the named fixture input; why: match candidate profile join parts index later reads match, so resolve_upstream_path must retain the computed value under that name. + match: tuple[str, ModelProfile, str] | None = None + # What: iterate across range and len and parts to perform candidate and join and parts and index; why: resolve_upstream_path repeats the body only while or for the loop header admits an iteration. + for index in range(1, len(parts) + 1): + # What: compute candidate from join and parts and index and value; why: canonical self aliases get candidate candidate later reads candidate, so resolve_upstream_path must retain the computed value under that name. + candidate = "/".join(parts[:index]) + # What: compute canonical from get and candidate and aliases; why: profile self profiles get canonical later reads canonical, so resolve_upstream_path must retain the computed value under that name. + canonical = self._aliases.get(candidate, candidate) + # What: compute profile from get and canonical and profiles; why: if profile is not later reads profile, so resolve_upstream_path must retain the computed value under that name. + profile = self._profiles.get(canonical) + # What: gate on profile before match and candidate and profile and join and parts; why: resolve_upstream_path admits match and candidate and profile and join and parts only for this predicate and excludes the opposite state. + if profile is not None: + # What: compute match from candidate and profile and join and parts; why: if match is later reads match, so resolve_upstream_path must retain the computed value under that name. + match = candidate, profile, "/" + "/".join(parts[index:]) + # What: gate on match before catalog error; why: resolve_upstream_path admits catalog error only for this predicate and excludes the opposite state. + if match is None: + # What: raise CatalogError for the caller; why: ModelCatalog.resolve_upstream_path stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("upstream path does not begin with a configured model ID") + # What: return match from resolve_upstream_path; why: resolve_upstream_path exposes match so its caller can continue with the function\'s computed outcome. + return match + + # What: define group_for around name; why: its direct callers call group_for for group for and rely on this exact input and result contract. + def group_for(self, name: str) -> RoutingGroup | None: + # What: iterate across groups and settings to perform name and members and group; why: group_for repeats the body only while or for the loop header admits an iteration. + for group in self.settings.groups: + # What: gate on name and members and group before group; why: group_for admits group only for this predicate and excludes the opposite state. + if name in group.members: + # What: return group from group_for; why: group_for exposes group so its caller can continue with the function\'s computed outcome. + return group + # What: return no value from group_for; why: group_for returns no value to callers that depend on its completed result. + return None + + +# What: define _finite_seconds around value and field and minimum and maximum; why: its direct callers call _finite_seconds for finite seconds and rely on this exact input and result contract. +def _finite_seconds(value: object, field: str, *, minimum: float, maximum: float) -> float: + # What: gate on isinstance and value and bool and minimum and maximum before catalog error and field and minimum and maximum; why: _finite_seconds admits catalog error and field and minimum and maximum only for this predicate and excludes the opposite state. + if (not isinstance(value, (int, float)) or isinstance(value, bool) + # What: apply the or not minimum value maximum portion of the enclosing predicate; why: this clause remains in _finite_seconds\'s enclosing expression so its grouping and evaluation order stay intact. + or not minimum <= value <= maximum): + # What: raise CatalogError for the caller; why: _finite_seconds stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be from {minimum:g} through {maximum:g} seconds") + # What: return float and value from _finite_seconds; why: _finite_seconds exposes float and value so its caller can continue with the function\'s computed outcome. + return float(value) + + +# What: define _router_settings around value and profiles; why: its direct callers call _router_settings for router settings and rely on this exact input and result contract. +def _router_settings(value: object, profiles: dict[str, ModelProfile]) -> RouterSettings: + # What: gate on value before value; why: _router_settings admits value only for this predicate and excludes the opposite state. + if value is None: + # What: initialize value as an empty runtime accumulator; why: _router_settings appends or maps entries into it during if not isinstance value dict before consuming the aggregate. + value = {} + # What: gate on isinstance and value and dict before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if not isinstance(value, dict): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router must be a table") + # What: compute allowed from api keys and default ttl s and unload timeout s and upstream timeout s and scheduler; why: unknown sorted set value allowed later reads allowed, so _router_settings must retain the computed value under that name. + allowed = { + # What: apply the api keys default ttl s unload timeout s upstream timeout s portion of allowed; why: _router_settings uses this clause to evaluate allowed as one grouped value. + "api_keys", "default_ttl_s", "unload_timeout_s", "upstream_timeout_s", + # What: apply the scheduler groups include aliases in list global concurrency limit portion of allowed; why: _router_settings uses this clause to evaluate allowed as one grouped value. + "scheduler", "groups", "include_aliases_in_list", "global_concurrency_limit", + # What: apply the send loading state preload model startup routing profile portion of allowed; why: _router_settings uses this clause to evaluate allowed as one grouped value. + "send_loading_state", "preload_model", "startup_routing_profile", + # What: apply the upstream no activation suffixes portion of allowed; why: _router_settings uses this clause to evaluate allowed as one grouped value. + "upstream_no_activation_suffixes", + # What: apply the activity max entries capture buffer mb portion of allowed; why: _router_settings uses this clause to evaluate allowed as one grouped value. + "activity_max_entries", "capture_buffer_mb", + # What: apply the activity session headers portion of allowed; why: _router_settings uses this clause to evaluate allowed as one grouped value. + "activity_session_headers", + # What: apply the performance disabled performance every s portion of allowed; why: _router_settings uses this clause to evaluate allowed as one grouped value. + "performance_disabled", "performance_every_s", + # What: complete the allowed collection with api keys and default ttl s and unload timeout s and upstream timeout s; why: _router_settings groups the supplied clauses as one allowed collection before its value is consumed. + } + # What: compute unknown from sorted and allowed and set and value; why: if unknown later reads unknown, so _router_settings must retain the computed value under that name. + unknown = sorted(set(value) - allowed) + # What: gate on unknown before catalog error and join and unknown; why: _router_settings admits catalog error and join and unknown only for this predicate and excludes the opposite state. + if unknown: + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"router: unsupported keys: {', '.join(unknown)}") + # What: compute raw keys from get and value and api keys; why: if not isinstance raw keys list or later reads raw keys, so _router_settings must retain the computed value under that name. + raw_keys = value.get("api_keys", []) + # What: gate on isinstance and raw keys and list and all and key before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if (not isinstance(raw_keys, list) or not all(isinstance(key, str) and key and "\x00" not in key + # What: apply the for key in raw keys portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + for key in raw_keys)): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.api_keys must be non-empty strings without NUL") + # What: gate on len and raw keys and set before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if len(set(raw_keys)) != len(raw_keys): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.api_keys must not contain duplicates") + # What: compute scheduler from get and value and scheduler and fifo; why: if scheduler fifo later reads scheduler, so _router_settings must retain the computed value under that name. + scheduler = value.get("scheduler", "fifo") + # What: gate on scheduler before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if scheduler != "fifo": + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.scheduler currently supports only fifo") + # What: compute include aliases in list from get and value and include aliases in list and false; why: if not isinstance include aliases in list bool later reads include aliases in list, so _router_settings must retain the computed value under that name. + include_aliases_in_list = value.get("include_aliases_in_list", False) + # What: gate on isinstance and include aliases in list and bool before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if not isinstance(include_aliases_in_list, bool): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.include_aliases_in_list must be a boolean") + # What: compute global concurrency limit from get and value and global concurrency limit and 0; why: not isinstance global concurrency limit int later reads global concurrency limit, so _router_settings must retain the computed value under that name. + global_concurrency_limit = value.get("global_concurrency_limit", 0) + # What: gate on isinstance and global concurrency limit and bool and int before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if ( + # What: call isinstance with global concurrency limit and int; why: _router_settings invokes isinstance while performing or isinstance global concurrency limit bool; the call advances that operation through its result or side effect. + not isinstance(global_concurrency_limit, int) + # What: call isinstance with global concurrency limit and bool; why: _router_settings invokes isinstance while performing or not global concurrency limit 000 000; the call advances that operation through its result or side effect. + or isinstance(global_concurrency_limit, bool) + # What: apply the or not global concurrency limit 000 000 portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + or not 0 <= global_concurrency_limit <= 1_000_000 + # What: complete the enclosing predicate with if not isinstance global concurrency limit int or isinstance global concurrency limit bool; why: _router_settings groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.global_concurrency_limit must be an integer from 0 through 1000000") + # What: compute send loading state from get and value and send loading state and false; why: if not isinstance send loading state bool later reads send loading state, so _router_settings must retain the computed value under that name. + send_loading_state = value.get("send_loading_state", False) + # What: gate on isinstance and send loading state and bool before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if not isinstance(send_loading_state, bool): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.send_loading_state must be a boolean") + # What: compute preload model from get and value and preload model; why: if preload model is not later reads preload model, so _router_settings must retain the computed value under that name. + preload_model = value.get("preload_model") + # What: gate on preload model before preload model and model id; why: _router_settings admits preload model and model id only for this predicate and excludes the opposite state. + if preload_model is not None: + # What: compute preload model from model id and preload model; why: preload model preload model later reads preload model, so _router_settings must retain the computed value under that name. + preload_model = _model_id(preload_model) + # What: compute startup routing profile from get and value and startup routing profile; why: if startup routing profile is not later reads startup routing profile, so _router_settings must retain the computed value under that name. + startup_routing_profile = value.get("startup_routing_profile") + # What: gate on startup routing profile before startup routing profile and simple name; why: _router_settings admits startup routing profile and simple name only for this predicate and excludes the opposite state. + if startup_routing_profile is not None: + # What: compute startup routing profile from simple name and startup routing profile and router and startup routing profile; why: startup routing profile router startup routing profile later reads startup routing profile, so _router_settings must retain the computed value under that name. + startup_routing_profile = _simple_name( + # What: apply the startup routing profile router startup routing profile portion of startup routing profile; why: _router_settings uses this clause to evaluate startup routing profile as one grouped value. + startup_routing_profile, "router.startup_routing_profile" + # What: complete the _simple_name call with startup routing profile; why: _router_settings groups the supplied clauses as one _simple_name call before its value is consumed. + ) + # What: evaluate and capture upstream no activation suffixes value get; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + upstream_no_activation_suffixes = value.get( + # What: call list with default upstream no activation suffixes; why: _router_settings consumes the list return value while evaluating "upstream_no_activation_suffixes", list(DEFAULT_UPSTREAM_NO_ACTIVATION_S. + "upstream_no_activation_suffixes", list(DEFAULT_UPSTREAM_NO_ACTIVATION_SUFFIXES) + # What: complete the value.get call with list; why: _router_settings groups the supplied clauses as one value.get call before its value is consumed. + ) + # What: gate on isinstance and upstream no activation suffixes and list and len and all before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if ( + # What: call isinstance with upstream no activation suffixes and list; why: _router_settings invokes isinstance while performing or len upstream no activation suffixes; the call advances that operation through its result or side effect. + not isinstance(upstream_no_activation_suffixes, list) + # What: call len with upstream no activation suffixes; why: _router_settings invokes len while performing or not all; the call advances that operation through its result or side effect. + or len(upstream_no_activation_suffixes) > 64 + # What: call all with suffix and upstream no activation suffixes and isinstance and str; why: _router_settings invokes all while performing isinstance suffix str and upstream suffix fullmatch suffix; the call advances that operation through its result or side effect. + or not all( + # What: call isinstance with suffix and str; why: _router_settings invokes isinstance while performing for suffix in upstream no activation suffixes; the call advances that operation through its result or side effect. + isinstance(suffix, str) and _UPSTREAM_SUFFIX.fullmatch(suffix) + # What: apply the for suffix in upstream no activation suffixes portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + for suffix in upstream_no_activation_suffixes + # What: complete the all call with suffix; why: _router_settings groups the supplied clauses as one all call before its value is consumed. + ) + # What: complete the enclosing predicate with if not isinstance upstream no activation suffixes list or len upstream no activation suffixes 64; why: _router_settings groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the router upstream no activation suffixes must contain at most safe portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + "router.upstream_no_activation_suffixes must contain at most 64 safe dot suffixes" + # What: complete the CatalogError call with ordered positional inputs; why: _router_settings groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: gate on len and upstream no activation suffixes and set before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if len(set(upstream_no_activation_suffixes)) != len(upstream_no_activation_suffixes): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.upstream_no_activation_suffixes must not contain duplicates") + # What: compute activity max entries from get and value and activity max entries and 1000; why: if not isinstance activity max entries int or later reads activity max entries, so _router_settings must retain the computed value under that name. + activity_max_entries = value.get("activity_max_entries", 1000) + # What: gate on isinstance and activity max entries and bool and int before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if (not isinstance(activity_max_entries, int) or isinstance(activity_max_entries, bool) + # What: apply the or not activity max entries 000 portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + or not 1 <= activity_max_entries <= 100_000): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.activity_max_entries must be an integer from 1 through 100000") + # What: compute capture buffer mb from get and value and capture buffer mb and 0; why: if not isinstance capture buffer mb int or later reads capture buffer mb, so _router_settings must retain the computed value under that name. + capture_buffer_mb = value.get("capture_buffer_mb", 0) + # What: gate on isinstance and capture buffer mb and bool and int before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if (not isinstance(capture_buffer_mb, int) or isinstance(capture_buffer_mb, bool) + # What: apply the or not capture buffer mb portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + or not 0 <= capture_buffer_mb <= 256): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.capture_buffer_mb must be an integer from 0 through 256") + # What: evaluate and capture activity session headers value get; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + activity_session_headers = value.get( + # What: call list with default activity session headers; why: _router_settings consumes the list return value while evaluating "activity_session_headers", list(DEFAULT_ACTIVITY_SESSION_HEADERS). + "activity_session_headers", list(DEFAULT_ACTIVITY_SESSION_HEADERS) + # What: complete the value.get call with list; why: _router_settings groups the supplied clauses as one value.get call before its value is consumed. + ) + # What: gate on isinstance and activity session headers and list and len and all before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if ( + # What: call isinstance with activity session headers and list; why: _router_settings invokes isinstance while performing or len activity session headers; the call advances that operation through its result or side effect. + not isinstance(activity_session_headers, list) + # What: call len with activity session headers; why: _router_settings invokes len while performing or not all; the call advances that operation through its result or side effect. + or len(activity_session_headers) > 16 + # What: call all with header and activity session headers and isinstance and str; why: _router_settings invokes all while performing isinstance header str and http header name fullmatch header; the call advances that operation through its result or side effect. + or not all( + # What: call isinstance with header and str; why: _router_settings invokes isinstance while performing for header in activity session headers; the call advances that operation through its result or side effect. + isinstance(header, str) and _HTTP_HEADER_NAME.fullmatch(header) + # What: apply the for header in activity session headers portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + for header in activity_session_headers + # What: complete the all call with header; why: _router_settings groups the supplied clauses as one all call before its value is consumed. + ) + # What: complete the enclosing predicate with if not isinstance activity session headers list or len activity session headers 16; why: _router_settings groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the router activity session headers must contain at most safe portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + "router.activity_session_headers must contain at most 16 safe HTTP header names" + # What: complete the CatalogError call with ordered positional inputs; why: _router_settings groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: compute normalized session headers from tuple and lower and header and activity session headers; why: if len set normalized session headers len normalized session headers later reads normalized session headers, so _router_settings must retain the computed value under that name. + normalized_session_headers = tuple(header.lower() for header in activity_session_headers) + # What: gate on len and normalized session headers and set before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if len(set(normalized_session_headers)) != len(normalized_session_headers): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.activity_session_headers must not contain duplicates") + # What: gate on any and header and normalized session headers and split before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if any( + # What: apply the authorization in header or token in portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + "authorization" in header or "token" in header or "secret" in header + # What: call header.split with value; why: _router_settings invokes header.split while performing for header in normalized session headers; the call advances that operation through its result or side effect. + or ("api" in header.split("-") and "key" in header.split("-")) + # What: apply the for header in normalized session headers portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + for header in normalized_session_headers + # What: complete the any call with header; why: _router_settings groups the supplied clauses as one any call before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.activity_session_headers must not name credential headers") + # What: compute performance disabled from get and value and performance disabled and false; why: if not isinstance performance disabled bool later reads performance disabled, so _router_settings must retain the computed value under that name. + performance_disabled = value.get("performance_disabled", False) + # What: gate on isinstance and performance disabled and bool before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if not isinstance(performance_disabled, bool): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.performance_disabled must be a boolean") + # What: compute performance every s from finite seconds and get and value and router and performance every s; why: value get performance every s later reads performance every s, so _router_settings must retain the computed value under that name. + performance_every_s = _finite_seconds( + # What: call value.get with performance every s and 5; why: _router_settings invokes value.get while performing router performance every s minimum maximum; the call advances that operation through its result or side effect. + value.get("performance_every_s", 5), + # What: supply minimum to _finite_seconds; why: _router_settings binds this 5 value to _finite_seconds's minimum input. + "router.performance_every_s", minimum=5, maximum=3600, + # What: complete the _finite_seconds call with minimum and maximum; why: _router_settings groups the supplied clauses as one _finite_seconds call before its value is consumed. + ) + # What: compute raw groups from get and value and groups; why: if not isinstance raw groups dict later reads raw groups, so _router_settings must retain the computed value under that name. + raw_groups = value.get("groups", {}) + # What: gate on isinstance and raw groups and dict before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if not isinstance(raw_groups, dict): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("router.groups must be a table") + # What: initialize groups as an empty runtime accumulator; why: _router_settings appends or maps entries into it during raise catalog error f router groups name must be before consuming the aggregate. + groups: list[RoutingGroup] = [] + # What: compute claimed from set; why: if len set members len members later reads claimed, so _router_settings must retain the computed value under that name. + claimed: set[str] = set() + # What: iterate across items and raw groups to perform name and simple name and raw name; why: _router_settings repeats the body only while or for the loop header admits an iteration. + for raw_name, raw_group in raw_groups.items(): + # What: compute name from simple name and raw name and router and group and names; why: raise catalog error f router groups name must later reads name, so _router_settings must retain the computed value under that name. + name = _simple_name(raw_name, "router group names") + # What: gate on isinstance and raw group and dict before catalog error and name; why: _router_settings admits catalog error and name only for this predicate and excludes the opposite state. + if not isinstance(raw_group, dict): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"router.groups.{name} must be a table") + # What: compute unknown from sorted and set and raw group and members and swap; why: if unknown later reads unknown, so _router_settings must retain the computed value under that name. + unknown = sorted(set(raw_group) - {"members", "swap", "exclusive", "persistent"}) + # What: gate on unknown before catalog error and name and join and unknown; why: _router_settings admits catalog error and name and join and unknown only for this predicate and excludes the opposite state. + if unknown: + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"router.groups.{name}: unsupported keys: {', '.join(unknown)}") + # What: compute members from get and raw group and members; why: if not isinstance members list or later reads members, so _router_settings must retain the computed value under that name. + members = raw_group.get("members") + # What: gate on members and isinstance and list and all and member before catalog error and name; why: _router_settings admits catalog error and name only for this predicate and excludes the opposite state. + if (not isinstance(members, list) or not members + # What: call all with member and members and isinstance and str; why: _router_settings invokes all while performing raise catalog error f router groups name members; the call advances that operation through its result or side effect. + or not all(isinstance(member, str) and member in profiles for member in members)): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"router.groups.{name}.members must name configured models") + # What: gate on intersection and members and len and claimed and set before catalog error; why: _router_settings admits catalog error only for this predicate and excludes the opposite state. + if len(set(members)) != len(members) or claimed.intersection(members): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError("a model can belong to only one router group") + # What: call claimed.update with members; why: _router_settings invokes claimed.update while performing flags key raw group get key default for; the call advances that operation through its result or side effect. + claimed.update(members) + # What: compute flags from key and get and default and raw group and swap; why: if not all isinstance flag bool later reads flags, so _router_settings must retain the computed value under that name. + flags = {key: raw_group.get(key, default) for key, default in + # What: apply the swap exclusive persistent portion of flags; why: _router_settings uses this clause to evaluate flags as one grouped value. + (("swap", True), ("exclusive", True), ("persistent", False))} + # What: gate on all and isinstance and flag and bool and values before catalog error and name; why: _router_settings admits catalog error and name only for this predicate and excludes the opposite state. + if not all(isinstance(flag, bool) for flag in flags.values()): + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"router.groups.{name} flags must be booleans") + # The native coordinator deliberately owns exactly one resident child. + # Accepting llama-swap's coexistence flags here would silently promise + # a scheduling policy we cannot implement. Fail atomically at reload + # time instead; an operator can express the supported policy as an + # exclusive swapping group, or a singleton persistent protected slot. + # What: gate on flags before catalog error and name; why: _router_settings admits catalog error and name only for this predicate and excludes the opposite state. + if not flags["exclusive"]: + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f router groups name single resident native routing portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + f"router.groups.{name}: single-resident native routing requires exclusive = true" + # What: complete the CatalogError call with name; why: _router_settings groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: gate on flags before catalog error and name; why: _router_settings admits catalog error and name only for this predicate and excludes the opposite state. + if flags["persistent"] and flags["swap"]: + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"router.groups.{name}: persistent groups must set swap = false") + # What: gate on flags before catalog error and name; why: _router_settings admits catalog error and name only for this predicate and excludes the opposite state. + if not flags["persistent"] and not flags["swap"]: + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f router groups name swap false requires portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + f"router.groups.{name}: swap = false requires multi-resident routing and is unsupported" + # What: complete the CatalogError call with name; why: _router_settings groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: gate on flags and len and members before catalog error and name; why: _router_settings admits catalog error and name only for this predicate and excludes the opposite state. + if flags["persistent"] and len(members) != 1: + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f router groups name a persistent group portion of the enclosing predicate; why: this clause remains in _router_settings\'s enclosing expression so its grouping and evaluation order stay intact. + f"router.groups.{name}: a persistent group needs exactly one member under single-resident routing" + # What: complete the CatalogError call with name; why: _router_settings groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: supply expanded arguments to groups.append; why: _router_settings binds this flags value to groups.append's expanded input. + groups.append(RoutingGroup(name, tuple(members), **flags)) + # What: compute membership from member and name and group and groups; why: if profile group is not and membership get later reads membership, so _router_settings must retain the computed value under that name. + membership = {member: group.name for group in groups for member in group.members} + # What: iterate across items and profiles to perform group and catalog error and profile and get and name; why: _router_settings repeats the body only while or for the loop header admits an iteration. + for name, profile in profiles.items(): + # What: gate on group and profile and get and name and membership before catalog error and name; why: _router_settings admits catalog error and name only for this predicate and excludes the opposite state. + if profile.group is not None and membership.get(name) != profile.group: + # What: raise CatalogError for the caller; why: _router_settings stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.group must match router group membership") + # What: return router settings and scheduler and include aliases in list and global concurrency limit from _router_settings; why: _router_settings exposes router settings and scheduler and include aliases in list and global concurrency limit so its caller can continue with the function\'s computed outcome. + return RouterSettings( + # What: supply api keys to tuple; why: _router_settings binds this tuple and raw keys value to tuple's api keys input. + api_keys=tuple(raw_keys), + # What: supply default ttl s to _finite_seconds; why: _router_settings binds this finite seconds and get and value and router and default ttl s value to _finite_seconds's default ttl s input. + default_ttl_s=_finite_seconds(value.get("default_ttl_s", 0), "router.default_ttl_s", minimum=0, maximum=86400), + # What: supply unload timeout s to _finite_seconds; why: _router_settings binds this finite seconds and get and value and router and unload timeout s value to _finite_seconds's unload timeout s input. + unload_timeout_s=_finite_seconds(value.get("unload_timeout_s", 30), "router.unload_timeout_s", minimum=1, maximum=900), + # What: supply upstream timeout s to _finite_seconds; why: _router_settings binds this finite seconds and get and value and router and upstream timeout s value to _finite_seconds's upstream timeout s input. + upstream_timeout_s=_finite_seconds(value.get("upstream_timeout_s", 900), "router.upstream_timeout_s", minimum=1, maximum=7200), + # What: supply scheduler to RouterSettings; why: _router_settings binds this scheduler value to RouterSettings's scheduler input. + scheduler=scheduler, + # What: supply groups to tuple; why: _router_settings binds this tuple and groups value to tuple's groups input. + groups=tuple(groups), + # What: supply include aliases in list to RouterSettings; why: _router_settings binds this include aliases in list value to RouterSettings's include aliases in list input. + include_aliases_in_list=include_aliases_in_list, + # What: supply global concurrency limit to RouterSettings; why: _router_settings binds this global concurrency limit value to RouterSettings's global concurrency limit input. + global_concurrency_limit=global_concurrency_limit, + # What: supply send loading state to RouterSettings; why: _router_settings binds this send loading state value to RouterSettings's send loading state input. + send_loading_state=send_loading_state, + # What: supply preload model to RouterSettings; why: _router_settings binds this preload model value to RouterSettings's preload model input. + preload_model=preload_model, + # What: supply startup routing profile to RouterSettings; why: _router_settings binds this startup routing profile value to RouterSettings's startup routing profile input. + startup_routing_profile=startup_routing_profile, + # What: supply upstream no activation suffixes to tuple; why: _router_settings binds this tuple and upstream no activation suffixes value to tuple's upstream no activation suffixes input. + upstream_no_activation_suffixes=tuple(upstream_no_activation_suffixes), + # What: supply activity max entries to RouterSettings; why: _router_settings binds this activity max entries value to RouterSettings's activity max entries input. + activity_max_entries=activity_max_entries, + # What: supply capture buffer mb to RouterSettings; why: _router_settings binds this capture buffer mb value to RouterSettings's capture buffer mb input. + capture_buffer_mb=capture_buffer_mb, + # What: supply activity session headers to RouterSettings; why: _router_settings binds this normalized session headers value to RouterSettings's activity session headers input. + activity_session_headers=normalized_session_headers, + # What: supply performance disabled to RouterSettings; why: _router_settings binds this performance disabled value to RouterSettings's performance disabled input. + performance_disabled=performance_disabled, + # What: supply performance every s to RouterSettings; why: _router_settings binds this performance every s value to RouterSettings's performance every s input. + performance_every_s=performance_every_s, + # What: complete the RouterSettings call with api keys and default ttl s and unload timeout s and upstream timeout s and scheduler; why: _router_settings groups the supplied clauses as one RouterSettings call before its value is consumed. + ) + + +# What: define _simple_name around name and label; why: its direct callers call _simple_name for simple name and rely on this exact input and result contract. +def _simple_name(name: object, label: str = "names") -> str: + # What: gate on isinstance and name and str and fullmatch and simple name before catalog error and label; why: _simple_name admits catalog error and label only for this predicate and excludes the opposite state. + if not isinstance(name, str) or not _SIMPLE_NAME.fullmatch(name): + # What: raise CatalogError for the caller; why: _simple_name stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{label} must match [A-Za-z0-9][A-Za-z0-9._-]{{0,127}}") + # What: return name from _simple_name; why: _simple_name exposes name so its caller can continue with the function\'s computed outcome. + return name + + +# What: define _valid_model_id around name; why: its direct callers call _valid_model_id for valid model id and rely on this exact input and result contract. +def _valid_model_id(name: object) -> bool: + # What: return bool and isinstance and name and str from _valid_model_id; why: _valid_model_id exposes bool and isinstance and name and str so its caller can continue with the function\'s computed outcome. + return bool( + # What: call isinstance with name and str; why: _valid_model_id invokes isinstance while performing and len name; the call advances that operation through its result or side effect. + isinstance(name, str) + # What: call len with name; why: _valid_model_id invokes len while performing and all model segment fullmatch segment for segment; the call advances that operation through its result or side effect. + and len(name) <= 128 + # What: call all with fullmatch and segment and model segment and split; why: _valid_model_id consumes the all return value while evaluating and all(_MODEL_SEGMENT.fullmatch(segment) for segment in name.split("/"). + and all(_MODEL_SEGMENT.fullmatch(segment) for segment in name.split("/")) + # What: complete the bool call with isinstance; why: _valid_model_id groups the supplied clauses as one bool call before its value is consumed. + ) + + +# What: define _model_id around name; why: its direct callers call _model_id for model id and rely on this exact input and result contract. +def _model_id(name: object) -> str: + # What: gate on valid model id and name before catalog error; why: _model_id admits catalog error only for this predicate and excludes the opposite state. + if not _valid_model_id(name): + # What: raise CatalogError for the caller; why: _model_id stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: preserve the exact model ids must be slash separated a za z0 9 literal fragment; why: _model_id passes this fragment verbatim through "model IDs must be slash-separated [A-Za-z0-9][A-Za-z0-9._:-] segments ", because changing it would alter a protocol payload, serialized fixture, or public message. + # What: preserve the exact with at most characters total literal fragment; why: _model_id passes this fragment verbatim through "model IDs must be slash-separated [A-Za-z0-9][A-Za-z0-9._:-] segments ", because changing it would alter a protocol payload, serialized fixture, or public message. + "model IDs must be slash-separated [A-Za-z0-9][A-Za-z0-9._:-] segments " + "with at most 128 characters total" + # What: complete the CatalogError call with ordered positional inputs; why: _model_id groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: return name from _model_id; why: _model_id exposes name so its caller can continue with the function\'s computed outcome. + return name + + +# What: define _profile around name and value; why: its direct callers call _profile for profile and rely on this exact input and result contract. +def _profile(name: str, value: object) -> ModelProfile: + # What: gate on isinstance and value and dict before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if not isinstance(value, dict): + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name} must be a table") + # What: compute allowed from model and args and port and description and ready timeout s; why: unknown sorted set value allowed later reads allowed, so _profile must retain the computed value under that name. + allowed = { + # What: apply the model args port description ready timeout s ttl s portion of allowed; why: _profile uses this clause to evaluate allowed as one grouped value. + "model", "args", "port", "description", "ready_timeout_s", "ttl_s", + # What: apply the unload timeout s priority group drop fields aliases unlisted portion of allowed; why: _profile uses this clause to evaluate allowed as one grouped value. + "unload_timeout_s", "priority", "group", "drop_fields", "aliases", "unlisted", + # What: apply the concurrency limit send loading state capabilities set fields portion of allowed; why: _profile uses this clause to evaluate allowed as one grouped value. + "concurrency_limit", "send_loading_state", "capabilities", "set_fields", + # What: apply the set fields by id check endpoint proxy use model name name metadata portion of allowed; why: _profile uses this clause to evaluate allowed as one grouped value. + "set_fields_by_id", "check_endpoint", "proxy", "use_model_name", "name", "metadata", + # What: apply the upstream timeout s portion of allowed; why: _profile uses this clause to evaluate allowed as one grouped value. + "upstream_timeout_s", + # What: complete the allowed collection with model and args and port and description; why: _profile groups the supplied clauses as one allowed collection before its value is consumed. + } + # What: compute unknown from sorted and allowed and set and value; why: if unknown later reads unknown, so _profile must retain the computed value under that name. + unknown = sorted(set(value) - allowed) + # What: gate on unknown before catalog error and name and join and unknown; why: _profile admits catalog error and name and join and unknown only for this predicate and excludes the opposite state. + if unknown: + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}: unsupported keys: {', '.join(unknown)}") + # What: compute model from get and value and model; why: if not isinstance model str or later reads model, so _profile must retain the computed value under that name. + model = value.get("model") + # What: gate on model and isinstance and str and strip before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if not isinstance(model, str) or not model.strip() or "\x00" in model: + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.model must be a non-empty string without NUL") + # What: compute raw args from get and value and args; why: if not isinstance raw args list or later reads raw args, so _profile must retain the computed value under that name. + raw_args = value.get("args", []) + # What: gate on isinstance and raw args and list and all and arg before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if not isinstance(raw_args, list) or not all(isinstance(arg, str) and "\x00" not in arg for arg in raw_args): + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.args must be an array of strings without NUL") + # The daemon owns these two options. Letting a profile smuggle them through + # produces ambiguous process state and defeats the lifecycle conflict guard. + # What: iterate across raw args to perform option and split and arg; why: _profile repeats the body only while or for the loop header admits an iteration. + for arg in raw_args: + # What: compute option from split and arg and 0 and value and 1; why: if arg or option p or later reads option, so _profile must retain the computed value under that name. + option = arg.split("=", 1)[0] + # What: compute reserved from model and model path and port; why: option startswith and any flag startswith option for later reads reserved, so _profile must retain the computed value under that name. + reserved = ("--model", "--model-path", "--port") + # What: gate on arg and option and startswith and any and flag before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if arg == "--" or option == "-p" or ( + # What: call option.startswith with value; why: _profile consumes the option.startswith return value while evaluating option.startswith("--") and any(flag.startswith(option) for flag in rese. + option.startswith("--") and any(flag.startswith(option) for flag in reserved) + # What: complete the enclosing predicate with arg equals or option equals p or option startswith and; why: _profile groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.args must not set --model or --port") + # What: compute port from get and value and port; why: if port is not and not later reads port, so _profile must retain the computed value under that name. + port = value.get("port") + # Port zero is an explicit request for a fresh loopback port on each + # activation. It is not passed through to uvicorn: the native router + # reserves an OS-selected candidate and records that concrete target for + # readiness, proxying, accounting, and re-adoption. ``None`` keeps the + # daemon-wide fixed default for backwards-compatible catalogs. + # What: gate on port and isinstance and bool and int before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if port is not None and (not isinstance(port, int) or isinstance(port, bool) or not 0 <= port <= 65535): + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.port must be an integer from 0 through 65535") + # What: compute description from public text and get and value and name and description; why: value get description f models name description later reads description, so _profile must retain the computed value under that name. + description = _public_text( + # What: call value.get with description; why: _profile consumes the value.get return value while evaluating value.get("description"), f"models.{name}.description". + value.get("description"), f"models.{name}.description" + # What: complete the _public_text call with get and name; why: _profile groups the supplied clauses as one _public_text call before its value is consumed. + ) + # What: compute display name from public text and get and value and name and name; why: check endpoint proxy use model name display name metadata json later reads display name, so _profile must retain the computed value under that name. + display_name = _public_text(value.get("name"), f"models.{name}.name") + # What: compute metadata json from metadata json and get and value and name and metadata; why: check endpoint proxy use model name display name metadata json later reads metadata json, so _profile must retain the computed value under that name. + metadata_json = _metadata_json( + # What: call value.get with metadata and the named fixture input; why: _profile consumes the value.get return value while evaluating value.get("metadata", {}), f"models.{name}.metadata". + value.get("metadata", {}), f"models.{name}.metadata" + # What: complete the _metadata_json call with get and name; why: _profile groups the supplied clauses as one _metadata_json call before its value is consumed. + ) + # What: compute upstream timeout s from get and value and upstream timeout s; why: if upstream timeout s is not later reads upstream timeout s, so _profile must retain the computed value under that name. + upstream_timeout_s = value.get("upstream_timeout_s") + # What: gate on upstream timeout s before upstream timeout s and finite seconds and name; why: _profile admits upstream timeout s and finite seconds and name only for this predicate and excludes the opposite state. + if upstream_timeout_s is not None: + # What: compute upstream timeout s from finite seconds and upstream timeout s and name and models and upstream timeout s; why: upstream timeout s f models name upstream timeout s later reads upstream timeout s, so _profile must retain the computed value under that name. + upstream_timeout_s = _finite_seconds( + # What: apply the upstream timeout s f models name upstream timeout s portion of upstream timeout s; why: _profile uses this clause to evaluate upstream timeout s as one grouped value. + upstream_timeout_s, f"models.{name}.upstream_timeout_s", + # What: supply minimum to _finite_seconds; why: _profile binds this 1 value to _finite_seconds's minimum input. + minimum=1, maximum=7200, + # What: complete the _finite_seconds call with minimum and maximum; why: _profile groups the supplied clauses as one _finite_seconds call before its value is consumed. + ) + # What: compute ready timeout s from finite seconds and get and value and name and ready timeout s; why: name model tuple raw args port description later reads ready timeout s, so _profile must retain the computed value under that name. + ready_timeout_s = _finite_seconds(value.get("ready_timeout_s", 120), f"models.{name}.ready_timeout_s", minimum=1, maximum=900) + # What: compute ttl s from get and value and ttl s; why: if ttl s is not later reads ttl s, so _profile must retain the computed value under that name. + ttl_s = value.get("ttl_s") + # What: gate on ttl s before ttl s and finite seconds and name; why: _profile admits ttl s and finite seconds and name only for this predicate and excludes the opposite state. + if ttl_s is not None: + # What: compute ttl s from finite seconds and ttl s and name and models and ttl s; why: ttl s unload timeout s priority group later reads ttl s, so _profile must retain the computed value under that name. + ttl_s = _finite_seconds(ttl_s, f"models.{name}.ttl_s", minimum=0, maximum=86400) + # What: compute unload timeout s from get and value and unload timeout s; why: if unload timeout s is not later reads unload timeout s, so _profile must retain the computed value under that name. + unload_timeout_s = value.get("unload_timeout_s") + # What: gate on unload timeout s before unload timeout s and finite seconds and name; why: _profile admits unload timeout s and finite seconds and name only for this predicate and excludes the opposite state. + if unload_timeout_s is not None: + # What: compute unload timeout s from finite seconds and unload timeout s and name and models and unload timeout s; why: ttl s unload timeout s priority group later reads unload timeout s, so _profile must retain the computed value under that name. + unload_timeout_s = _finite_seconds(unload_timeout_s, f"models.{name}.unload_timeout_s", minimum=1, maximum=900) + # What: compute priority from get and value and priority and 0; why: if not isinstance priority int or later reads priority, so _profile must retain the computed value under that name. + priority = value.get("priority", 0) + # What: gate on isinstance and priority and bool and int before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if not isinstance(priority, int) or isinstance(priority, bool) or not -1000 <= priority <= 1000: + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.priority must be an integer from -1000 through 1000") + # What: compute group from get and value and group; why: if group is not later reads group, so _profile must retain the computed value under that name. + group = value.get("group") + # What: gate on group before group and simple name and name; why: _profile admits group and simple name and name only for this predicate and excludes the opposite state. + if group is not None: + # What: compute group from simple name and group and name and models and group; why: ttl s unload timeout s priority group later reads group, so _profile must retain the computed value under that name. + group = _simple_name(group, f"models.{name}.group") + # What: compute drop fields from get and value and drop fields; why: if not isinstance drop fields list or later reads drop fields, so _profile must retain the computed value under that name. + drop_fields = value.get("drop_fields", []) + # What: gate on isinstance and drop fields and list and len before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if not isinstance(drop_fields, list) or len(drop_fields) > 64: + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f models name drop fields must be portion of the enclosing predicate; why: this clause remains in _profile\'s enclosing expression so its grouping and evaluation order stay intact. + f"models.{name}.drop_fields must be at most 64 safe JSON field paths" + # What: complete the CatalogError call with name; why: _profile groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: compute normalized drop fields from tuple and request field path and field and drop fields; why: if len set normalized drop fields len normalized drop fields later reads normalized drop fields, so _profile must retain the computed value under that name. + normalized_drop_fields = tuple( + # What: call _request_field_path with field and name and models and drop fields; why: _profile consumes the _request_field_path return value while evaluating _request_field_path(field, f"models.{name}.drop_fields") for field in dr. + _request_field_path(field, f"models.{name}.drop_fields") for field in drop_fields + # What: complete the tuple call with request field path; why: _profile groups the supplied clauses as one tuple call before its value is consumed. + ) + # What: gate on len and normalized drop fields and set before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if len(set(normalized_drop_fields)) != len(normalized_drop_fields): + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.drop_fields must not contain duplicates") + # What: gate on normalized drop fields before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if ("model",) in normalized_drop_fields: + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.drop_fields must not remove model") + # What: compute aliases from get and value and aliases; why: not isinstance aliases list later reads aliases, so _profile must retain the computed value under that name. + aliases = value.get("aliases", []) + # What: gate on isinstance and aliases and list and all and len before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if ( + # What: call isinstance with aliases and list; why: _profile invokes isinstance while performing or not all valid model id alias for; the call advances that operation through its result or side effect. + not isinstance(aliases, list) + # What: call all with valid model id and alias and aliases; why: _profile invokes all while performing or len set aliases len aliases; the call advances that operation through its result or side effect. + or not all(_valid_model_id(alias) for alias in aliases) + # What: call len with set and aliases; why: _profile consumes the len return value while evaluating or len(set(aliases)) != len(aliases). + or len(set(aliases)) != len(aliases) + # What: complete the enclosing predicate with if not isinstance aliases list or not all valid model id; why: _profile groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.aliases must be distinct valid profile names") + # What: compute unlisted from get and value and unlisted and false; why: if not isinstance unlisted bool later reads unlisted, so _profile must retain the computed value under that name. + unlisted = value.get("unlisted", False) + # What: gate on isinstance and unlisted and bool before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if not isinstance(unlisted, bool): + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.unlisted must be a boolean") + # What: compute concurrency limit from get and value and concurrency limit and 0; why: not isinstance concurrency limit int later reads concurrency limit, so _profile must retain the computed value under that name. + concurrency_limit = value.get("concurrency_limit", 0) + # What: gate on isinstance and concurrency limit and bool and int before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if ( + # What: call isinstance with concurrency limit and int; why: _profile invokes isinstance while performing or isinstance concurrency limit bool; the call advances that operation through its result or side effect. + not isinstance(concurrency_limit, int) + # What: call isinstance with concurrency limit and bool; why: _profile invokes isinstance while performing or not concurrency limit 000 000; the call advances that operation through its result or side effect. + or isinstance(concurrency_limit, bool) + # What: apply the or not concurrency limit 000 000 portion of the enclosing predicate; why: this clause remains in _profile\'s enclosing expression so its grouping and evaluation order stay intact. + or not 0 <= concurrency_limit <= 1_000_000 + # What: complete the enclosing predicate with if not isinstance concurrency limit int or isinstance concurrency limit bool; why: _profile groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f models name concurrency limit must be portion of the enclosing predicate; why: this clause remains in _profile\'s enclosing expression so its grouping and evaluation order stay intact. + f"models.{name}.concurrency_limit must be an integer from 0 through 1000000" + # What: complete the CatalogError call with name; why: _profile groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: compute send loading state from get and value and send loading state; why: if send loading state is not and not later reads send loading state, so _profile must retain the computed value under that name. + send_loading_state = value.get("send_loading_state") + # What: gate on send loading state and isinstance and bool before catalog error and name; why: _profile admits catalog error and name only for this predicate and excludes the opposite state. + if send_loading_state is not None and not isinstance(send_loading_state, bool): + # What: raise CatalogError for the caller; why: _profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"models.{name}.send_loading_state must be a boolean") + # What: compute capabilities from capabilities and name and get and value and capabilities; why: concurrency limit send loading state capabilities set fields set fields by id later reads capabilities, so _profile must retain the computed value under that name. + capabilities = _capabilities(name, value.get("capabilities", {})) + # What: compute set fields from request fields and name and get and value and set fields; why: concurrency limit send loading state capabilities set fields set fields by id later reads set fields, so _profile must retain the computed value under that name. + set_fields = _request_fields(name, "set_fields", value.get("set_fields", {})) + # What: compute set fields by id from request fields by id and name and get and value and set fields by id; why: name value get set fields by id later reads set fields by id, so _profile must retain the computed value under that name. + set_fields_by_id = _request_fields_by_id( + # What: call value.get with set fields by id and the named fixture input; why: _profile consumes the value.get return value while evaluating name, value.get("set_fields_by_id", {}). + name, value.get("set_fields_by_id", {}) + # What: complete the _request_fields_by_id call with name and get; why: _profile groups the supplied clauses as one _request_fields_by_id call before its value is consumed. + ) + # What: compute check endpoint from check endpoint and get and default check endpoint and value; why: value get check endpoint default check endpoint later reads check endpoint, so _profile must retain the computed value under that name. + check_endpoint = _check_endpoint( + # What: call value.get with check endpoint and default check endpoint; why: _profile invokes value.get while performing f models name check endpoint; the call advances that operation through its result or side effect. + value.get("check_endpoint", DEFAULT_CHECK_ENDPOINT), + # What: apply the f models name check endpoint portion of check endpoint; why: _profile uses this clause to evaluate check endpoint as one grouped value. + f"models.{name}.check_endpoint", + # What: complete the _check_endpoint call with get and name; why: _profile groups the supplied clauses as one _check_endpoint call before its value is consumed. + ) + # What: compute proxy from proxy template and get and default proxy and value; why: value get proxy default proxy f models name later reads proxy, so _profile must retain the computed value under that name. + proxy = _proxy_template( + # What: call value.get with proxy and default proxy; why: _profile consumes the value.get return value while evaluating value.get("proxy", DEFAULT_PROXY), f"models.{name}.proxy". + value.get("proxy", DEFAULT_PROXY), f"models.{name}.proxy" + # What: complete the _proxy_template call with get and name; why: _profile groups the supplied clauses as one _proxy_template call before its value is consumed. + ) + # What: compute use model name from upstream model name and get and value and name and use model name; why: value get use model name f models name use model name later reads use model name, so _profile must retain the computed value under that name. + use_model_name = _upstream_model_name( + # What: call value.get with use model name; why: _profile consumes the value.get return value while evaluating value.get("use_model_name"), f"models.{name}.use_model_name". + value.get("use_model_name"), f"models.{name}.use_model_name" + # What: complete the _upstream_model_name call with get and name; why: _profile groups the supplied clauses as one _upstream_model_name call before its value is consumed. + ) + # What: compute aliases from list and fromkeys and dict and aliases; why: aliases later reads aliases, so _profile must retain the computed value under that name. + aliases = list(dict.fromkeys([ + # What: apply the aliases portion of aliases; why: _profile uses this clause to evaluate aliases as one grouped value. + *aliases, + # What: apply the model id for model id value in set fields by id portion of aliases; why: _profile uses this clause to evaluate aliases as one grouped value. + *(model_id for model_id, _ in set_fields_by_id if model_id != name), + # What: complete the list call with fromkeys; why: _profile groups the supplied clauses as one list call before its value is consumed. + ])) + # What: return model profile and name and model and port from _profile; why: _profile exposes model profile and name and model and port so its caller can continue with the function\'s computed outcome. + return ModelProfile( + # What: call tuple with raw args; why: _profile invokes tuple while performing ttl s unload timeout s priority group; the call advances that operation through its result or side effect. + name, model, tuple(raw_args), port, description, ready_timeout_s, + # What: apply the ttl s unload timeout s priority group portion of the enclosing predicate; why: this clause remains in _profile\'s enclosing expression so its grouping and evaluation order stay intact. + ttl_s, unload_timeout_s, priority, group, + # What: call tuple with join and path and normalized drop fields and value; why: _profile invokes tuple while performing concurrency limit send loading state capabilities set fields set fields by id; the call advances that operation through its result or side effect. + tuple(".".join(path) for path in normalized_drop_fields), tuple(aliases), unlisted, + # What: apply the concurrency limit send loading state capabilities set fields set fields by id portion of the enclosing predicate; why: this clause remains in _profile\'s enclosing expression so its grouping and evaluation order stay intact. + concurrency_limit, send_loading_state, capabilities, set_fields, set_fields_by_id, + # What: apply the check endpoint proxy use model name display name metadata json portion of the enclosing predicate; why: this clause remains in _profile\'s enclosing expression so its grouping and evaluation order stay intact. + check_endpoint, proxy, use_model_name, display_name, metadata_json, + # What: apply the upstream timeout s portion of the enclosing predicate; why: this clause remains in _profile\'s enclosing expression so its grouping and evaluation order stay intact. + upstream_timeout_s, + # What: complete the ModelProfile call with name and model and tuple and port and description; why: _profile groups the supplied clauses as one ModelProfile call before its value is consumed. + ) + + +# What: define _check_endpoint around value and field; why: its direct callers call _check_endpoint for check endpoint and rely on this exact input and result contract. +def _check_endpoint(value: object, field: str) -> str: + # What: gate on any and isinstance and value and str and len before catalog error and field; why: _check_endpoint admits catalog error and field only for this predicate and excludes the opposite state. + if ( + # What: call isinstance with value and str; why: _check_endpoint invokes isinstance while performing or len value; the call advances that operation through its result or side effect. + not isinstance(value, str) + # What: call len with value; why: _check_endpoint invokes len while performing or not safe http path fullmatch value; the call advances that operation through its result or side effect. + or len(value) > 256 + # What: call _SAFE_HTTP_PATH.fullmatch with value; why: _check_endpoint invokes _SAFE_HTTP_PATH.fullmatch while performing or any segment in for segment; the call advances that operation through its result or side effect. + or not _SAFE_HTTP_PATH.fullmatch(value) + # What: call any with segment and split and value and value and value; why: _check_endpoint consumes the any return value while evaluating or any(segment in {".", ".."} for segment in value.split("/")). + or any(segment in {".", ".."} for segment in value.split("/")) + # What: complete the enclosing predicate with if not isinstance value str or len value 256; why: _check_endpoint groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _check_endpoint stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f field must be an absolute portion of the enclosing predicate; why: this clause remains in _check_endpoint\'s enclosing expression so its grouping and evaluation order stay intact. + f"{field} must be an absolute ASCII path without query, fragment, or traversal" + # What: complete the CatalogError call with field; why: _check_endpoint groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: return value from _check_endpoint; why: _check_endpoint exposes value so its caller can continue with the function\'s computed outcome. + return value + + +# What: define _proxy_template around value and field; why: its direct callers call _proxy_template for proxy template and rely on this exact input and result contract. +def _proxy_template(value: object, field: str) -> str: + # What: gate on isinstance and value and str and len before catalog error and field; why: _proxy_template admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(value, str) or len(value) > 512: + # What: raise CatalogError for the caller; why: _proxy_template stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be a safe loopback HTTP URL template") + # What: compute match from fullmatch and value and proxy template; why: if match is later reads match, so _proxy_template must retain the computed value under that name. + match = _PROXY_TEMPLATE.fullmatch(value) + # What: gate on match before catalog error and field; why: _proxy_template admits catalog error and field only for this predicate and excludes the opposite state. + if match is None: + # What: raise CatalogError for the caller; why: _proxy_template stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f field must be http port portion of the enclosing predicate; why: this clause remains in _proxy_template\'s enclosing expression so its grouping and evaluation order stay intact. + f"{field} must be http://127.0.0.1:${{PORT}} with an optional safe path prefix" + # What: complete the CatalogError call with field; why: _proxy_template groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: compute prefix from group and match and value and prefix; why: if any segment in for segment later reads prefix, so _proxy_template must retain the computed value under that name. + prefix = match.group("prefix") or "" + # What: gate on any and segment and split and prefix before catalog error and field; why: _proxy_template admits catalog error and field only for this predicate and excludes the opposite state. + if any(segment in {".", ".."} for segment in prefix.split("/")): + # What: raise CatalogError for the caller; why: _proxy_template stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f field must be http port portion of the enclosing predicate; why: this clause remains in _proxy_template\'s enclosing expression so its grouping and evaluation order stay intact. + f"{field} must be http://127.0.0.1:${{PORT}} with an optional safe path prefix" + # What: complete the CatalogError call with field; why: _proxy_template groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: gate on prefix before prefix; why: _proxy_template admits prefix only for this predicate and excludes the opposite state. + if prefix == "/": + # What: compute prefix from value; why: return default proxy prefix later reads prefix, so _proxy_template must retain the computed value under that name. + prefix = "" + # What: return default proxy and prefix from _proxy_template; why: _proxy_template exposes default proxy and prefix so its caller can continue with the function\'s computed outcome. + return DEFAULT_PROXY + prefix + + +# What: define _upstream_model_name around value and field; why: its direct callers call _upstream_model_name for upstream model name and rely on this exact input and result contract. +def _upstream_model_name(value: object, field: str) -> str | None: + # What: gate on value before the computed value; why: _upstream_model_name admits the computed value only for this predicate and excludes the opposite state. + if value is None: + # What: return no value from _upstream_model_name; why: _upstream_model_name returns no value to callers that depend on its completed result. + return None + # What: gate on value and any and isinstance and str and len before catalog error and field; why: _upstream_model_name admits catalog error and field only for this predicate and excludes the opposite state. + if ( + # What: call isinstance with value and str; why: _upstream_model_name invokes isinstance while performing or not value; the call advances that operation through its result or side effect. + not isinstance(value, str) + # What: apply the or not value portion of the enclosing predicate; why: this clause remains in _upstream_model_name\'s enclosing expression so its grouping and evaluation order stay intact. + or not value + # What: call len with value; why: _upstream_model_name invokes len while performing or value value strip; the call advances that operation through its result or side effect. + or len(value) > 256 + # What: call value.strip with the declared inputs; why: _upstream_model_name invokes value.strip while performing or any ord character or ord; the call advances that operation through its result or side effect. + or value != value.strip() + # What: call any with character and value and ord and 32 and 127; why: _upstream_model_name consumes the any return value while evaluating or any(ord(character) < 32 or ord(character) == 127 for character in val. + or any(ord(character) < 32 or ord(character) == 127 for character in value) + # What: complete the enclosing predicate with if not isinstance value str or not value or; why: _upstream_model_name groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _upstream_model_name stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f field must be a non empty portion of the enclosing predicate; why: this clause remains in _upstream_model_name\'s enclosing expression so its grouping and evaluation order stay intact. + f"{field} must be a non-empty trimmed string without control characters" + # What: complete the CatalogError call with field; why: _upstream_model_name groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: return value from _upstream_model_name; why: _upstream_model_name exposes value so its caller can continue with the function\'s computed outcome. + return value + + +# What: define _metadata_json around value and field; why: its direct callers call _metadata_json for metadata json and rely on this exact input and result contract. +def _metadata_json(value: object, field: str) -> str: + # What: gate on isinstance and value and dict and all and key before catalog error and field; why: _metadata_json admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(value, dict) or not all(isinstance(key, str) for key in value): + # What: raise CatalogError for the caller; why: _metadata_json stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be a table with string keys") + # What: establish the handler boundary for the protected operation; why: _metadata_json routes failures to type error and value error while preserving cleanup and success flow. + try: + # What: return dumps and value and json and false and false from _metadata_json; why: _metadata_json exposes dumps and value and json and false and false so its caller can continue with the function\'s computed outcome. + return json.dumps( + # What: supply ensure ascii to json.dumps; why: _metadata_json binds this false value to json.dumps's ensure ascii input. + value, ensure_ascii=False, allow_nan=False, separators=(",", ":"), sort_keys=True + # What: complete the json.dumps call with ensure ascii and allow nan and separators and sort keys; why: _metadata_json groups the supplied clauses as one json.dumps call before its value is consumed. + ) + # What: handle type error and value error by raise catalog error f field must be json compatible; why: _metadata_json converts that failure into this concrete recovery, response, or cleanup behavior. + except (TypeError, ValueError) as exc: + # What: raise CatalogError for the caller; why: _metadata_json stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be JSON-compatible") from exc + + +# What: define _public_text around value and field; why: its direct callers call _public_text for public text and rely on this exact input and result contract. +def _public_text(value: object, field: str) -> str | None: + # What: gate on value before the computed value; why: _public_text admits the computed value only for this predicate and excludes the opposite state. + if value is None: + # What: return no value from _public_text; why: _public_text returns no value to callers that depend on its completed result. + return None + # What: gate on value and isinstance and str before catalog error and field; why: _public_text admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(value, str) or "\x00" in value: + # What: raise CatalogError for the caller; why: _public_text stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be a string without NUL") + # What: return strip and value from _public_text; why: _public_text exposes strip and value so its caller can continue with the function\'s computed outcome. + return value.strip() or None + + +# What: define _request_field_path around value and field; why: its direct callers call _request_field_path for request field path and rely on this exact input and result contract. +def _request_field_path(value: object, field: str) -> tuple[str, ...]: + # What: gate on isinstance and value and str and len before catalog error and field; why: _request_field_path admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(value, str) or len(value) > 128: + # What: raise CatalogError for the caller; why: _request_field_path stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must use safe dot-delimited JSON object paths") + # What: compute path from tuple and split and value and value; why: if not path or len path later reads path, so _request_field_path must retain the computed value under that name. + path = tuple(value.split(".")) + # What: gate on path and len and all and fullmatch and part before catalog error and field; why: _request_field_path admits catalog error and field only for this predicate and excludes the opposite state. + if not path or len(path) > 16 or not all(_SIMPLE_NAME.fullmatch(part) for part in path): + # What: raise CatalogError for the caller; why: _request_field_path stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must use safe dot-delimited JSON object paths") + # What: return path from _request_field_path; why: _request_field_path exposes path so its caller can continue with the function\'s computed outcome. + return path + + +# What: define _request_fields around name and key and value; why: its direct callers call _request_fields for request fields and rely on this exact input and result contract. +def _request_fields(name: str, key: str, value: object) -> tuple[RequestField, ...]: + # What: compute field from name and key and models and value; why: raise catalog error f field must be later reads field, so _request_fields must retain the computed value under that name. + field = f"models.{name}.{key}" + # What: gate on isinstance and value and dict and len before catalog error and field; why: _request_fields admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(value, dict) or len(value) > 64: + # What: raise CatalogError for the caller; why: _request_fields stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be a table with at most 64 JSON field assignments") + # What: initialize hard as an empty runtime accumulator; why: _request_fields appends or maps entries into it during soft if is soft else hard path operation before consuming the aggregate. + hard: dict[tuple[str, ...], RequestField] = {} + # What: initialize soft as an empty runtime accumulator; why: _request_fields appends or maps entries into it during soft if is soft else hard path operation before consuming the aggregate. + soft: dict[tuple[str, ...], RequestField] = {} + # What: iterate across items and value to perform is soft and isinstance and raw key and str and endswith; why: _request_fields repeats the body only while or for the loop header admits an iteration. + for raw_key, raw_value in value.items(): + # What: compute is soft from isinstance and raw key and str and endswith and value; why: raw key if is soft else raw key later reads is soft, so _request_fields must retain the computed value under that name. + is_soft = isinstance(raw_key, str) and raw_key.endswith("?") + # What: compute path from request field path and field and is soft and raw key and 1; why: if path model later reads path, so _request_fields must retain the computed value under that name. + path = _request_field_path( + # What: apply the raw key if is soft else raw key portion of path; why: _request_fields uses this clause to evaluate path as one grouped value. + raw_key[:-1] if is_soft else raw_key, + # What: apply the field portion of path; why: _request_fields uses this clause to evaluate path as one grouped value. + field, + # What: complete the _request_field_path call with is soft and field; why: _request_fields groups the supplied clauses as one _request_field_path call before its value is consumed. + ) + # What: gate on path before catalog error and field; why: _request_fields admits catalog error and field only for this predicate and excludes the opposite state. + if path == ("model",): + # What: raise CatalogError for the caller; why: _request_fields stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must not set model") + # What: establish the handler boundary for the protected operation; why: _request_fields routes failures to type error and value error while preserving cleanup and success flow. + try: + # What: compute value json from dumps and raw value and json and false and false; why: if len value json encode utf 8 536 later reads value json, so _request_fields must retain the computed value under that name. + value_json = json.dumps( + # What: apply the raw value portion of value json; why: _request_fields uses this clause to evaluate value json as one grouped value. + raw_value, + # What: supply allow nan to json.dumps; why: _request_fields binds this false value to json.dumps's allow nan input. + allow_nan=False, + # What: supply ensure ascii to json.dumps; why: _request_fields binds this false value to json.dumps's ensure ascii input. + ensure_ascii=False, + # What: supply separators to json.dumps; why: _request_fields binds this value and value value to json.dumps's separators input. + separators=(",", ":"), + # What: supply sort keys to json.dumps; why: _request_fields binds this true value to json.dumps's sort keys input. + sort_keys=True, + # What: complete the json.dumps call with allow nan and ensure ascii and separators and sort keys; why: _request_fields groups the supplied clauses as one json.dumps call before its value is consumed. + ) + # What: handle type error and value error by raise catalog error f field raw key must be; why: _request_fields converts that failure into this concrete recovery, response, or cleanup behavior. + except (TypeError, ValueError) as exc: + # What: raise CatalogError for the caller; why: _request_fields stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.{raw_key} must be JSON-compatible") from exc + # What: gate on len and encode and value json before catalog error and field and raw key; why: _request_fields admits catalog error and field and raw key only for this predicate and excludes the opposite state. + if len(value_json.encode("utf-8")) > 65_536: + # What: raise CatalogError for the caller; why: _request_fields stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.{raw_key} exceeds the 65536-byte value limit") + # What: compute operation from request field and path and value json and is soft; why: soft if is soft else hard path later reads operation, so _request_fields must retain the computed value under that name. + operation = RequestField(path, value_json, is_soft) + # What: compute result entry from operation; why: the enclosing return or state update later reads result entry, so _request_fields must retain the computed value under that name. + (soft if is_soft else hard)[path] = operation + # What: iterate across intersection and soft and set and hard to perform pop and path and soft; why: _request_fields repeats the body only while or for the loop header admits an iteration. + for path in set(hard).intersection(soft): + # What: call soft.pop with path; why: _request_fields invokes soft.pop while performing return tuple hard path for path; the call advances that operation through its result or side effect. + soft.pop(path) + # What: return tuple and hard and path and soft from _request_fields; why: _request_fields exposes tuple and hard and path and soft so its caller can continue with the function\'s computed outcome. + return tuple(hard[path] for path in sorted(hard)) + tuple( + # What: call sorted with soft; why: _request_fields consumes the sorted return value while evaluating soft[path] for path in sorted(soft). + soft[path] for path in sorted(soft) + # What: complete the tuple call with soft; why: _request_fields groups the supplied clauses as one tuple call before its value is consumed. + ) + + +# What: define _request_fields_by_id around name and value; why: its direct callers call _request_fields_by_id for request fields by id and rely on this exact input and result contract. +def _request_fields_by_id( + # What: declare the name input for _request_fields_by_id; why: _request_fields_by_id consumes name during field f models name set fields by id, so callers must bind it with the other signature inputs. + name: str, value: object +# What: complete the enclosing predicate collection with tuple and str and request field and the named fixture input; why: _request_fields_by_id groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. +) -> tuple[tuple[str, tuple[RequestField, ...]], ...]: + # What: compute field from name and models and set fields by id; why: raise catalog error f field must be later reads field, so _request_fields_by_id must retain the computed value under that name. + field = f"models.{name}.set_fields_by_id" + # What: gate on isinstance and value and dict and len before catalog error and field; why: _request_fields_by_id admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(value, dict) or len(value) > 64: + # What: raise CatalogError for the caller; why: _request_fields_by_id stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be a table with at most 64 model IDs") + # What: initialize result as an empty runtime accumulator; why: _request_fields_by_id appends or maps entries into it during result append model id request fields name f set fields by id model id before consuming the aggregate. + result = [] + # What: iterate across items and value to perform model id and model id; why: _request_fields_by_id repeats the body only while or for the loop header admits an iteration. + for model_id, fields in value.items(): + # What: compute model id from model id and model id; why: result append model id request fields name f set fields by id later reads model id, so _request_fields_by_id must retain the computed value under that name. + model_id = _model_id(model_id) + # What: preserve the exact result append model id request fields name f set fields by id literal fragment; why: _request_fields_by_id passes this fragment verbatim through result.append((model_id, _request_fields(name, f"set_fields_by_id.{model, because changing it would alter a protocol payload, serialized fi. + result.append((model_id, _request_fields(name, f"set_fields_by_id.{model_id}", fields))) + # What: return tuple and sorted and result from _request_fields_by_id; why: _request_fields_by_id exposes tuple and sorted and result so its caller can continue with the function\'s computed outcome. + return tuple(sorted(result)) + + +# What: define _capabilities around name and value; why: its direct callers call _capabilities for capabilities and rely on this exact input and result contract. +def _capabilities(name: str, value: object) -> ModelCapabilities: + # What: compute field from name and models and capabilities; why: raise catalog error f field must be later reads field, so _capabilities must retain the computed value under that name. + field = f"models.{name}.capabilities" + # What: gate on isinstance and value and dict before catalog error and field; why: _capabilities admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(value, dict): + # What: raise CatalogError for the caller; why: _capabilities stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be a table") + # What: compute unknown from sorted and set and value and in and out; why: if unknown later reads unknown, so _capabilities must retain the computed value under that name. + unknown = sorted(set(value) - {"in", "out", "tools", "context"}) + # What: gate on unknown before catalog error and field and join and unknown; why: _capabilities admits catalog error and field and join and unknown only for this predicate and excludes the opposite state. + if unknown: + # What: raise CatalogError for the caller; why: _capabilities stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}: unsupported keys: {', '.join(unknown)}") + + # What: define modalities around key; why: its direct callers call modalities for modalities and rely on this exact input and result contract. + def modalities(key: str) -> tuple[str, ...]: + # What: compute raw from get and key and value; why: if not isinstance raw list or later reads raw, so modalities must retain the computed value under that name. + raw = value.get(key, []) + # What: gate on isinstance and raw and list and all and item before catalog error and field and key; why: modalities admits catalog error and field and key only for this predicate and excludes the opposite state. + if not isinstance(raw, list) or not all(isinstance(item, str) for item in raw): + # What: raise CatalogError for the caller; why: modalities stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.{key} must be an array of supported modalities") + # What: gate on len and raw and set before catalog error and field and key; why: modalities admits catalog error and field and key only for this predicate and excludes the opposite state. + if len(set(raw)) != len(raw): + # What: raise CatalogError for the caller; why: modalities stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.{key} must not contain duplicates") + # What: compute unsupported from sorted and set and raw and text; why: if unsupported later reads unsupported, so modalities must retain the computed value under that name. + unsupported = sorted(set(raw) - {"text"}) + # What: gate on unsupported before catalog error and field and key and join and unsupported; why: modalities admits catalog error and field and key and join and unsupported only for this predicate and excludes the opposite state. + if unsupported: + # What: raise CatalogError for the caller; why: modalities stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: call operation.join with unsupported; why: modalities consumes the operation.join return value while evaluating f"{field}.{key} contains unsupported modalities: {', '.join(unsupported). + f"{field}.{key} contains unsupported modalities: {', '.join(unsupported)}" + # What: complete the CatalogError call with field; why: modalities groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: return tuple and raw from modalities; why: modalities exposes tuple and raw so its caller can continue with the function\'s computed outcome. + return tuple(raw) + + # What: compute tools from get and value and tools and false; why: if not isinstance tools bool later reads tools, so _capabilities must retain the computed value under that name. + tools = value.get("tools", False) + # What: gate on isinstance and tools and bool before catalog error and field; why: _capabilities admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(tools, bool): + # What: raise CatalogError for the caller; why: _capabilities stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.tools must be a boolean") + # What: compute context from get and value and context and 0; why: not isinstance context int later reads context, so _capabilities must retain the computed value under that name. + context = value.get("context", 0) + # What: gate on isinstance and context and bool and int before catalog error and field; why: _capabilities admits catalog error and field only for this predicate and excludes the opposite state. + if ( + # What: call isinstance with context and int; why: _capabilities invokes isinstance while performing or isinstance context bool; the call advances that operation through its result or side effect. + not isinstance(context, int) + # What: call isinstance with context and bool; why: _capabilities invokes isinstance while performing or context; the call advances that operation through its result or side effect. + or isinstance(context, bool) + # What: apply the or context portion of the enclosing predicate; why: this clause remains in _capabilities\'s enclosing expression so its grouping and evaluation order stay intact. + or context < 0 + # What: complete the enclosing predicate with if not isinstance context int or isinstance context bool; why: _capabilities groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _capabilities stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.context must be a nonnegative integer") + # What: return model capabilities and tools and context and modalities and in from _capabilities; why: _capabilities exposes model capabilities and tools and context and modalities and in so its caller can continue with the function\'s computed outcome. + return ModelCapabilities(modalities("in"), modalities("out"), tools, context) + + +# What: define _routing_profile around name and value; why: its direct callers call _routing_profile for routing profile and rely on this exact input and result contract. +def _routing_profile(name: str, value: object) -> RoutingProfile: + # What: compute field from name and profiles; why: raise catalog error f field must be later reads field, so _routing_profile must retain the computed value under that name. + field = f"profiles.{name}" + # What: gate on isinstance and value and dict before catalog error and field; why: _routing_profile admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(value, dict): + # What: raise CatalogError for the caller; why: _routing_profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be a table with description and pins") + # What: compute unknown from sorted and set and value and description and pins; why: if unknown later reads unknown, so _routing_profile must retain the computed value under that name. + unknown = sorted(set(value) - {"description", "pins"}) + # What: gate on unknown before catalog error and field and join and unknown; why: _routing_profile admits catalog error and field and join and unknown only for this predicate and excludes the opposite state. + if unknown: + # What: raise CatalogError for the caller; why: _routing_profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}: unsupported keys: {', '.join(unknown)}") + # What: compute description from get and value and description; why: if description is not and later reads description, so _routing_profile must retain the computed value under that name. + description = value.get("description") + # What: gate on description and isinstance and str before catalog error and field; why: _routing_profile admits catalog error and field only for this predicate and excludes the opposite state. + if description is not None and ( + # What: call isinstance with description and str; why: _routing_profile consumes the isinstance return value while evaluating not isinstance(description, str) or "\x00" in description. + not isinstance(description, str) or "\x00" in description + # What: complete the enclosing predicate with description is not and not isinstance description str or; why: _routing_profile groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _routing_profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.description must be a string without NUL") + # What: compute raw pins from get and value and pins; why: if not isinstance raw pins dict or later reads raw pins, so _routing_profile must retain the computed value under that name. + raw_pins = value.get("pins") + # What: gate on raw pins and isinstance and dict before catalog error and field; why: _routing_profile admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(raw_pins, dict) or not raw_pins: + # What: raise CatalogError for the caller; why: _routing_profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.pins must contain at least one entry") + # What: initialize pins as an empty runtime accumulator; why: _routing_profile appends or maps entries into it during raise catalog error f field pins pin must before consuming the aggregate. + pins: list[tuple[str, str | None]] = [] + # What: iterate across items and raw pins to perform pin and model id and raw pin; why: _routing_profile repeats the body only while or for the loop header admits an iteration. + for raw_pin, raw_target in raw_pins.items(): + # What: compute pin from model id and raw pin; why: raise catalog error f field pins pin later reads pin, so _routing_profile must retain the computed value under that name. + pin = _model_id(raw_pin) + # What: gate on isinstance and raw target and str before catalog error and field and pin; why: _routing_profile admits catalog error and field and pin only for this predicate and excludes the opposite state. + if not isinstance(raw_target, str): + # What: raise CatalogError for the caller; why: _routing_profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.pins.{pin} must be a model ID or empty string") + # What: compute target from raw target and model id; why: pins append pin target later reads target, so _routing_profile must retain the computed value under that name. + target = _model_id(raw_target) if raw_target else None + # What: call pins.append with pin and target; why: _routing_profile invokes pins.append while performing return routing profile name tuple sorted pins; the call advances that operation through its result or side effect. + pins.append((pin, target)) + # What: return routing profile and name and tuple and description from _routing_profile; why: _routing_profile exposes routing profile and name and tuple and description so its caller can continue with the function\'s computed outcome. + return RoutingProfile(name, tuple(sorted(pins)), description or None) + + +# What: define _selector around name and value; why: its direct callers call _selector for selector and rely on this exact input and result contract. +def _selector(name: str, value: object) -> ModelSelector: + # What: compute field from name and selectors; why: raise catalog error f field must be later reads field, so _selector must retain the computed value under that name. + field = f"selectors.{name}" + # What: gate on isinstance and value and dict before catalog error and field; why: _selector admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(value, dict): + # What: raise CatalogError for the caller; why: _selector stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field} must be a table") + # What: compute unknown from sorted and set and value and strategy and targets; why: if unknown later reads unknown, so _selector must retain the computed value under that name. + unknown = sorted( + # What: call set with value; why: _selector consumes the set return value while evaluating set(value) - {"strategy", "targets", "name", "description", "unlisted",. + set(value) - {"strategy", "targets", "name", "description", "unlisted", "metadata"} + # What: complete the sorted call with set; why: _selector groups the supplied clauses as one sorted call before its value is consumed. + ) + # What: gate on unknown before catalog error and field and join and unknown; why: _selector admits catalog error and field and join and unknown only for this predicate and excludes the opposite state. + if unknown: + # What: raise CatalogError for the caller; why: _selector stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}: unsupported keys: {', '.join(unknown)}") + # What: compute strategy from get and value and strategy; why: if strategy spillover later reads strategy, so _selector must retain the computed value under that name. + strategy = value.get("strategy") + # What: gate on strategy before catalog error and field; why: _selector admits catalog error and field only for this predicate and excludes the opposite state. + if strategy == "spillover": + # What: raise CatalogError for the caller; why: _selector stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f field strategy spillover requires multi resident portion of the enclosing predicate; why: this clause remains in _selector\'s enclosing expression so its grouping and evaluation order stay intact. + f"{field}.strategy spillover requires multi-resident or peer capacity and is unsupported" + # What: complete the CatalogError call with field; why: _selector groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: gate on strategy before catalog error and field; why: _selector admits catalog error and field only for this predicate and excludes the opposite state. + if strategy not in {"pin", "warm"}: + # What: raise CatalogError for the caller; why: _selector stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.strategy must be pin or warm") + # What: compute targets from get and value and targets; why: not isinstance targets list later reads targets, so _selector must retain the computed value under that name. + targets = value.get("targets") + # What: gate on targets and isinstance and list and len and all before catalog error and field; why: _selector admits catalog error and field only for this predicate and excludes the opposite state. + if ( + # What: call isinstance with targets and list; why: _selector invokes isinstance while performing or not targets; the call advances that operation through its result or side effect. + not isinstance(targets, list) + # What: apply the or not targets portion of the enclosing predicate; why: this clause remains in _selector\'s enclosing expression so its grouping and evaluation order stay intact. + or not targets + # What: call len with targets; why: _selector invokes len while performing or not all valid model id target for; the call advances that operation through its result or side effect. + or len(targets) > 64 + # What: call all with valid model id and target and targets; why: _selector consumes the all return value while evaluating or not all(_valid_model_id(target) for target in targets). + or not all(_valid_model_id(target) for target in targets) + # What: complete the enclosing predicate with if not isinstance targets list or not targets or; why: _selector groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _selector stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.targets must contain 1 to 64 valid model IDs") + # What: compute display name from get and value and name; why: for key candidate in name display name later reads display name, so _selector must retain the computed value under that name. + display_name = value.get("name") + # What: compute description from get and value and description; why: for key candidate in name display name later reads description, so _selector must retain the computed value under that name. + description = value.get("description") + # What: iterate across display name and description to perform candidate and catalog error and isinstance and str and field; why: _selector repeats the body only while or for the loop header admits an iteration. + for key, candidate in (("name", display_name), ("description", description)): + # What: gate on candidate and isinstance and str before catalog error and field and key; why: _selector admits catalog error and field and key only for this predicate and excludes the opposite state. + if candidate is not None and ( + # What: call isinstance with candidate and str; why: _selector consumes the isinstance return value while evaluating not isinstance(candidate, str) or "\x00" in candidate. + not isinstance(candidate, str) or "\x00" in candidate + # What: complete the enclosing predicate with candidate is not and not isinstance candidate str or; why: _selector groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise CatalogError for the caller; why: _selector stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.{key} must be a string without NUL") + # What: compute unlisted from get and value and unlisted and false; why: if not isinstance unlisted bool later reads unlisted, so _selector must retain the computed value under that name. + unlisted = value.get("unlisted", False) + # What: gate on isinstance and unlisted and bool before catalog error and field; why: _selector admits catalog error and field only for this predicate and excludes the opposite state. + if not isinstance(unlisted, bool): + # What: raise CatalogError for the caller; why: _selector stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError(f"{field}.unlisted must be a boolean") + # What: compute metadata json from metadata json and get and value and field and metadata; why: metadata json later reads metadata json, so _selector must retain the computed value under that name. + metadata_json = _metadata_json(value.get("metadata", {}), f"{field}.metadata") + # What: return model selector and name and strategy and unlisted from _selector; why: _selector exposes model selector and name and strategy and unlisted so its caller can continue with the function\'s computed outcome. + return ModelSelector( + # What: apply the name portion of the enclosing predicate; why: this clause remains in _selector\'s enclosing expression so its grouping and evaluation order stay intact. + name, + # What: apply the strategy portion of the enclosing predicate; why: this clause remains in _selector\'s enclosing expression so its grouping and evaluation order stay intact. + strategy, + # What: call tuple with targets; why: _selector invokes tuple while performing display name or; the call advances that operation through its result or side effect. + tuple(targets), + # What: apply the display name or portion of the enclosing predicate; why: this clause remains in _selector\'s enclosing expression so its grouping and evaluation order stay intact. + display_name or None, + # What: apply the description or portion of the enclosing predicate; why: this clause remains in _selector\'s enclosing expression so its grouping and evaluation order stay intact. + description or None, + # What: apply the unlisted portion of the enclosing predicate; why: this clause remains in _selector\'s enclosing expression so its grouping and evaluation order stay intact. + unlisted, + # What: apply the metadata json portion of the enclosing predicate; why: this clause remains in _selector\'s enclosing expression so its grouping and evaluation order stay intact. + metadata_json, + # What: complete the ModelSelector call with name and strategy and tuple and display name and description; why: _selector groups the supplied clauses as one ModelSelector call before its value is consumed. + ) diff --git a/python/freetoken/daemon/client.py b/python/freetoken/daemon/client.py index 6616778e7..b5ce5360b 100644 --- a/python/freetoken/daemon/client.py +++ b/python/freetoken/daemon/client.py @@ -21,10 +21,21 @@ # prepare-stop (15s transport budget) + default SIGTERM grace (10s) + reap wait (10s), # with enough HTTP scheduling slack that a valid lifecycle transaction does not look failed. DEFAULT_LIFECYCLE_TIMEOUT = 40.0 +# What: compute default profile timeout from 1920 0; why: return default profile timeout later reads default profile timeout, so client must retain the computed value under that name. +DEFAULT_PROFILE_TIMEOUT = 1920.0 # replacement + recovery readiness (2 * 900s), lifecycle margin # Positional verbs that mean "act as a client"; anything else (bare, or a flag like --host) runs # the server. Kept in one place so the server dispatcher and this parser agree. -CLIENT_VERBS = ("self", "status", "health", "metrics", "stats", "start", "stop", "switch", "logs") +# What: compute client verbs from self and status and health and metrics and stats; why: the enclosing return or state update later reads client verbs, so client must retain the computed value under that name. +CLIENT_VERBS = ( + # What: apply the status health metrics stats models routing profiles portion of client verbs; why: client uses this clause to evaluate client verbs as one grouped value. + "self", "status", "health", "metrics", "stats", "models", "routing-profiles", + # What: apply the activate routing profile clear routing profile start stop shutdown portion of client verbs; why: client uses this clause to evaluate client verbs as one grouped value. + "activate-routing-profile", "clear-routing-profile", "start", "stop", "shutdown", + # What: apply the switch start profile switch profile logs portion of client verbs; why: client uses this clause to evaluate client verbs as one grouped value. + "switch", "start-profile", "switch-profile", "logs", +# What: complete the CLIENT_VERBS collection with self and status and health and metrics; why: client groups the supplied clauses as one CLIENT_VERBS collection before its value is consumed. +) class ClientError(Exception): @@ -36,9 +47,14 @@ def __init__(self, message: str, *, exit_code: int = 1) -> None: def _effective_timeout(verb: str, configured: float | None) -> float: if configured is not None: return configured + # What: gate on verb before default profile timeout; why: _effective_timeout admits default profile timeout only for this predicate and excludes the opposite state. + if verb in {"start-profile", "switch-profile"}: + # What: return default profile timeout from _effective_timeout; why: _effective_timeout exposes default profile timeout so its caller can continue with the function\'s computed outcome. + return DEFAULT_PROFILE_TIMEOUT return ( DEFAULT_LIFECYCLE_TIMEOUT - if verb in {"stop", "switch"} + # What: apply the if verb in stop shutdown switch portion of the enclosing predicate; why: this clause remains in _effective_timeout\'s enclosing expression so its grouping and evaluation order stay intact. + if verb in {"stop", "shutdown", "switch", "start-profile", "switch-profile"} else DEFAULT_TIMEOUT ) @@ -124,7 +140,8 @@ def _build_parser(prog: str) -> argparse.ArgumentParser: "--timeout", type=float, default=None, - help="HTTP timeout (default 10s; stop/switch 40s)", + # What: preserve the exact help http timeout default s stop literal fragment; why: _build_parser passes this fragment verbatim through help="HTTP timeout (default 10s; stop/switch 40s; profiles 960s)", because changing it would alter a protocol payload, serialized fixture, or public message. + help="HTTP timeout (default 10s; stop/switch 40s; profiles 960s)", ) p = argparse.ArgumentParser(prog=prog, description="Control a running ft daemon") @@ -134,12 +151,58 @@ def _build_parser(prog: str) -> argparse.ArgumentParser: sub.add_parser("health", parents=[common], help="Proxied serve health (GET /engine/health)") sub.add_parser("metrics", parents=[common], help="Engine footprint (GET /engine/metrics)") sub.add_parser("stats", parents=[common], help="Proxied serve stats (GET /engine/stats)") + # What: call sub.add_parser with models; why: _build_parser invokes sub.add_parser while performing models parents common; the call advances that operation through its result or side effect. + sub.add_parser( + # What: preserve the exact models parents common literal fragment; why: _build_parser passes this fragment verbatim through "models", parents=[common], because changing it would alter a protocol payload, serialized fixture, or public message. + "models", parents=[common], + # What: preserve the exact help list named freetoken swap model profiles literal fragment; why: _build_parser passes this fragment verbatim through help="List named freetoken-swap model profiles (GET /router/profiles)", because changing it would alter a protocol payload, serialized fixture, or public message. + help="List named freetoken-swap model profiles (GET /router/profiles)", + # What: complete the sub.add_parser call with parents and help; why: _build_parser groups the supplied clauses as one sub.add_parser call before its value is consumed. + ) + # What: call sub.add_parser with routing profiles; why: _build_parser invokes sub.add_parser while performing routing profiles parents common; the call advances that operation through its result or side effect. + sub.add_parser( + # What: preserve the exact routing profiles parents common literal fragment; why: _build_parser passes this fragment verbatim through "routing-profiles", parents=[common], because changing it would alter a protocol payload, serialized fixture, or public message. + "routing-profiles", parents=[common], + # What: preserve the exact help list runtime model id pin profiles literal fragment; why: _build_parser passes this fragment verbatim through help="List runtime model-ID pin profiles (GET /router/profiles)", because changing it would alter a protocol payload, serialized fixture, or public message. + help="List runtime model-ID pin profiles (GET /router/profiles)", + # What: complete the sub.add_parser call with parents and help; why: _build_parser groups the supplied clauses as one sub.add_parser call before its value is consumed. + ) + # What: compute activate routing from add parser and sub and common and activate routing profile and activate; why: activate routing add argument name help routing profile name later reads activate routing, so _build_parser must retain the computed value under that name. + activate_routing = sub.add_parser( + # What: supply parents to sub.add_parser; why: _build_parser binds this common value to sub.add_parser's parents input. + "activate-routing-profile", parents=[common], + # What: supply help to sub.add_parser; why: _build_parser binds this activate and a and runtime and model id value to sub.add_parser's help input. + help="Activate a runtime model-ID pin profile", + # What: complete the sub.add_parser call with parents and help; why: _build_parser groups the supplied clauses as one sub.add_parser call before its value is consumed. + ) + # What: preserve the exact activate routing add argument name help routing profile name literal fragment; why: _build_parser passes this fragment verbatim through activate_routing.add_argument("name", help="Routing profile name"), because changing it would alter a protocol payload, serialized fixture, or public me. + activate_routing.add_argument("name", help="Routing profile name") + # What: call sub.add_parser with clear routing profile; why: _build_parser invokes sub.add_parser while performing clear routing profile parents common; the call advances that operation through its result or side effect. + sub.add_parser( + # What: preserve the exact clear routing profile parents common literal fragment; why: _build_parser passes this fragment verbatim through "clear-routing-profile", parents=[common], because changing it would alter a protocol payload, serialized fixture, or public message. + "clear-routing-profile", parents=[common], + # What: preserve the exact help clear the active runtime model id literal fragment; why: _build_parser passes this fragment verbatim through help="Clear the active runtime model-ID pin profile", because changing it would alter a protocol payload, serialized fixture, or public message. + help="Clear the active runtime model-ID pin profile", + # What: complete the sub.add_parser call with parents and help; why: _build_parser groups the supplied clauses as one sub.add_parser call before its value is consumed. + ) stop = sub.add_parser("stop", parents=[common], help="Stop the serve (POST /engine/stop)") stop.add_argument( "--force", action="store_true", help="stop even if final accounting cannot be sealed (may lose the unobserved token tail)", ) + # What: compute shutdown from add parser and sub and common and shutdown and stop; why: shutdown add argument later reads shutdown, so _build_parser must retain the computed value under that name. + shutdown = sub.add_parser("shutdown", parents=[common], help="Stop the serve and daemon (POST /shutdown)") + # What: call shutdown.add_argument with force; why: _build_parser invokes shutdown.add_argument while performing force; the call advances that operation through its result or side effect. + shutdown.add_argument( + # What: preserve the exact force literal fragment; why: _build_parser passes this fragment verbatim through "--force", because changing it would alter a protocol payload, serialized fixture, or public message. + "--force", + # What: preserve the exact action store true literal fragment; why: _build_parser passes this fragment verbatim through action="store_true", because changing it would alter a protocol payload, serialized fixture, or public message. + action="store_true", + # What: preserve the exact help stop even if final accounting literal fragment; why: _build_parser passes this fragment verbatim through help="stop even if final accounting cannot be sealed (may lose the unobs, because changing it would alter a protocol payload, serialized fixture, or public message. + help="stop even if final accounting cannot be sealed (may lose the unobserved token tail)", + # What: complete the shutdown.add_argument call with action and help; why: _build_parser groups the supplied clauses as one shutdown.add_argument call before its value is consumed. + ) for name in ("start", "switch"): sp = sub.add_parser(name, parents=[common], help=f"POST /engine/{name}") sp.add_argument("model", help="Model path/id") @@ -153,6 +216,16 @@ def _build_parser(prog: str) -> argparse.ArgumentParser: # Everything after `--` is forwarded verbatim to ft serve (opaque passthrough): # ft daemon start MODEL --port 1919 -- --moe-cache-auto --graph 256 sp.add_argument("serve_args", nargs="*", default=[], help="Extra ft serve args (after --)") + # What: iterate across the computed value to perform sp and add parser and name and sub and common; why: _build_parser repeats the body only while or for the loop header admits an iteration. + for name in ("start-profile", "switch-profile"): + # What: compute sp from add parser and name and sub and common and post; why: sp add argument name help named model profile later reads sp, so _build_parser must retain the computed value under that name. + sp = sub.add_parser(name, parents=[common], help=f"POST /engine/{name}") + # What: preserve the exact sp add argument name help named model profile literal fragment; why: _build_parser passes this fragment verbatim through sp.add_argument("name", help="Named model profile from the daemon catalo, because changing it would alter a protocol payload, serialized fixture, or public message. + sp.add_argument("name", help="Named model profile from the daemon catalog") + # What: gate on name before add argument and sp; why: _build_parser admits add argument and sp only for this predicate and excludes the opposite state. + if name == "switch-profile": + # What: preserve the exact sp add argument force action store true help replace literal fragment; why: _build_parser passes this fragment verbatim through sp.add_argument("--force", action="store_true", help="replace even if fi, because changing it would alter a protocol payload, serialized fixture, or pub. + sp.add_argument("--force", action="store_true", help="replace even if final accounting cannot be sealed") lg = sub.add_parser("logs", parents=[common], help="Stream engine logs (SSE, GET /engine/logs)") lg.add_argument("--since", type=int, default=0, help="Replay from this seq cursor") return p @@ -171,23 +244,60 @@ def main(argv: Sequence[str] | None = None, *, prog: str = "ft daemon") -> int: "health": ("GET", "/engine/health", None), "metrics": ("GET", "/engine/metrics", None), "stats": ("GET", "/engine/stats", None), + # What: map the models field as get and router and profiles; why: main carries models through table into method path body table args verb. + "models": ("GET", "/router/profiles", None), + # What: map the routing profiles field as get and router and profiles; why: main carries routing profiles through table into method path body table args verb. + "routing-profiles": ("GET", "/router/profiles", None), "stop": ( "POST", "/engine/stop", {"force": True} if getattr(args, "force", False) else {}, ), + # What: map the shutdown field as getattr and args and post and shutdown and force; why: main carries shutdown through table into method path body table args verb. + "shutdown": ( + # What: apply the post portion of table; why: main uses this clause to evaluate table as one grouped value. + "POST", + # What: apply the shutdown portion of table; why: main uses this clause to evaluate table as one grouped value. + "/shutdown", + # What: map the force field as true; why: main carries force through table into method path body table args verb. + {"force": True} if getattr(args, "force", False) else {}, + # What: complete the table collection with post and shutdown and getattr and args and force and false and force; why: main groups the supplied clauses as one table collection before its value is consumed. + ), } - if args.verb in ("start", "switch"): + # What: gate on verb and args before method and path and body and name and args; why: main admits method and path and body and name and args only for this predicate and excludes the opposite state. + if args.verb == "activate-routing-profile": + # What: map the name field as name and args; why: main carries name through method and path and body into method path body put router profiles active. + method, path, body = "PUT", "/router/profiles/active", {"name": args.name} + # What: gate on verb and args before method and path and body; why: main admits method and path and body only for this predicate and excludes the opposite state. + elif args.verb == "clear-routing-profile": + # What: map the name field as the fixture input; why: main carries name through method and path and body into method path post f engine args verb. + method, path, body = "PUT", "/router/profiles/active", {"name": None} + # What: gate on verb and args before body and dict and model and str and any; why: main admits body and dict and model and str and any only for this predicate and excludes the opposite state. + elif args.verb in ("start", "switch"): body: dict[str, Any] = {"model": args.model, "args": list(args.serve_args)} if args.port is not None: body["port"] = args.port if args.verb == "switch" and args.force: body["force"] = True method, path = "POST", f"/engine/{args.verb}" + # What: gate on verb and args before body and name and args; why: main admits body and name and args only for this predicate and excludes the opposite state. + elif args.verb in ("start-profile", "switch-profile"): + # What: map the name field as name and args; why: main carries name through body into body force true. + body = {"name": args.name} + # What: gate on force and verb and args before body; why: main admits body only for this predicate and excludes the opposite state. + if args.verb == "switch-profile" and args.force: + # What: compute body entry from true; why: method path body table args verb later reads body entry, so main must retain the computed value under that name. + body["force"] = True + # What: compute method and path from verb and args and post and engine; why: method path body table args verb later reads method and path, so main must retain the computed value under that name. + method, path = "POST", f"/engine/{args.verb}" else: method, path, body = table[args.verb] doc = _request_json(method, args.url, path, body=body, token=args.token, timeout=timeout) print(json.dumps(doc, ensure_ascii=False, indent=2, sort_keys=True)) + # What: gate on verb and args and get and doc before the computed value; why: main admits the computed value only for this predicate and excludes the opposite state. + if args.verb in {"start-profile", "switch-profile"} and not doc.get("readiness", {}).get("ready"): + # What: return 1 from main; why: main exposes 1 so its caller can continue with the function\'s computed outcome. + return 1 return 0 except ClientError as exc: print(str(exc), file=sys.stderr) diff --git a/python/freetoken/daemon/inference_proxy.py b/python/freetoken/daemon/inference_proxy.py new file mode 100644 index 000000000..2080e7b06 --- /dev/null +++ b/python/freetoken/daemon/inference_proxy.py @@ -0,0 +1,315 @@ +"""Small request-preserving HTTP bridge from freetoken-swap to ``ft serve``. + +No inference dependency is imported here. The daemon only parses the request +JSON long enough to select an allowlisted profile, then forwards the original +bytes and safe HTTP headers to the selected FreeToken engine. +""" +# What: document small request preserving http bridge from freetoken swap in the inference_proxy docstring; why: introspection and maintainers read this exact docstring fragment to understand inference proxy behavior without executing it. +# What: document no inference dependency is imported here in the inference_proxy docstring; why: introspection and maintainers read this exact docstring fragment to understand inference proxy behavior without executing it. +# What: document json long enough to select an in the inference_proxy docstring; why: introspection and maintainers read this exact docstring fragment to understand inference proxy behavior without executing it. +# What: document bytes and safe http headers to in the inference_proxy docstring; why: introspection and maintainers read this exact docstring fragment to understand inference proxy behavior without executing it. +# What: preserve the paragraph boundary in the the inference_proxy docstring; why: introspection and maintainers read this paragraph break to understand inference proxy behavior without executing it. + +# What: enable postponed evaluation of annotations; why: type hints in inference_proxy can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import json for request model using json; why: request_model uses json loads, making that imported dependency available to its named operation. +import json +# What: import re for open upstream using re; why: open_upstream uses re fullmatch, making that imported dependency available to its named operation. +import re +# What: import dataclass for module initialization using dataclasses and dataclass; why: module initialization uses the dataclass annotation in module initialization, making that imported dependency available to its named operation. +from dataclasses import dataclass +# What: import iterator and mapping for chunks and forward headers using typing and iterator and mapping; why: chunks and forward_headers uses the iterator annotation in chunks and the mapping annotation in forward headers, making that imported dependency available to its named operation. +from typing import Iterator, Mapping +# What: import httperror for open upstream using urllib and error and httperror; why: open_upstream uses the httperror annotation in open upstream, making that imported dependency available to its named operation. +from urllib.error import HTTPError +# What: import request and urlopen for open upstream using urllib and request and request and urlopen; why: open_upstream uses request and urlopen, making that imported dependency available to its named operation. +from urllib.request import Request, urlopen + +# What: import request field for set fields using catalog and request field; why: _set_fields uses the request field annotation in set fields, making that imported dependency available to its named operation. +from .catalog import RequestField + + +# What: define RequestModelError as the owner of its declared state; why: daemon callers use this class boundary so those methods share one request model error state invariant. +class RequestModelError(ValueError): + """The request cannot be routed because it has no valid model identifier.""" +# What: document the request cannot be routed because in the RequestModelError docstring; why: introspection and maintainers read this exact docstring fragment to understand request model error behavior without executing it. + + +# What: compute hop by hop from connection and content length and host and keep alive and proxy authenticate; why: excluded hop by hop local auth headers later reads hop by hop, so inference_proxy must retain the computed value under that name. +_HOP_BY_HOP = {"connection", "content-length", "host", "keep-alive", "proxy-authenticate", + # What: apply the proxy authorization te trailer transfer encoding upgrade portion of hop by hop; why: inference_proxy uses this clause to evaluate hop by hop as one grouped value. + "proxy-authorization", "te", "trailer", "transfer-encoding", "upgrade"} +# What: compute local auth headers from authorization and x api key and x ft token; why: excluded hop by hop local auth headers later reads local auth headers, so inference_proxy must retain the computed value under that name. +_LOCAL_AUTH_HEADERS = {"authorization", "x-api-key", "x-ft-token"} + + +# What: define request_model around body; why: its direct callers call request_model for request model and rely on this exact input and result contract. +def request_model(body: bytes) -> str: + # What: establish the handler boundary for the protected operation; why: request_model routes failures to unicode decode error and jsondecode error and json while preserving cleanup and success flow. + try: + # What: compute doc from loads and body and json; why: model doc get model if isinstance doc later reads doc, so request_model must retain the computed value under that name. + doc = json.loads(body) + # What: handle unicode decode error and jsondecode error and json by raise request model error request body must be valid; why: request_model converts that failure into this concrete recovery, response, or cleanup behavior. + except (UnicodeDecodeError, json.JSONDecodeError) as exc: + # What: raise RequestModelError for the caller; why: request_model stops this rejected path before it can mutate state, dispatch work, or report success. + raise RequestModelError("request body must be valid JSON with a model string") from exc + # What: compute model from isinstance and doc and dict and get and model; why: if not isinstance model str or later reads model, so request_model must retain the computed value under that name. + model = doc.get("model") if isinstance(doc, dict) else None + # What: gate on model and isinstance and str and strip before request model error; why: request_model admits request model error only for this predicate and excludes the opposite state. + if not isinstance(model, str) or not model.strip() or "\x00" in model: + # What: raise RequestModelError for the caller; why: request_model stops this rejected path before it can mutate state, dispatch work, or report success. + raise RequestModelError("request body must include a non-empty model string") + # What: return model from request_model; why: request_model exposes model so its caller can continue with the function\'s computed outcome. + return model + + +# What: define _path_parent around doc and path and create; why: its direct callers call _path_parent for path parent and rely on this exact input and result contract. +def _path_parent(doc: dict, path: tuple[str, ...], *, create: bool) -> dict | None: + # What: compute current from doc; why: child current get part later reads current, so _path_parent must retain the computed value under that name. + current = doc + # What: iterate across path to perform child and get and part and current; why: _path_parent repeats the body only while or for the loop header admits an iteration. + for part in path[:-1]: + # What: compute child from get and part and current; why: if not isinstance child dict later reads child, so _path_parent must retain the computed value under that name. + child = current.get(part) + # What: gate on isinstance and child and dict before create; why: _path_parent admits create only for this predicate and excludes the opposite state. + if not isinstance(child, dict): + # What: gate on create before the computed value; why: _path_parent admits the computed value only for this predicate and excludes the opposite state. + if not create: + # What: return no value from _path_parent; why: _path_parent returns no value to callers that depend on its completed result. + return None + # What: initialize child as an empty runtime accumulator; why: _path_parent appends or maps entries into it during current part child before consuming the aggregate. + child = {} + # What: compute current entry from child; why: current child later reads current entry, so _path_parent must retain the computed value under that name. + current[part] = child + # What: compute current from child; why: return current later reads current, so _path_parent must retain the computed value under that name. + current = child + # What: return current from _path_parent; why: _path_parent exposes current so its caller can continue with the function\'s computed outcome. + return current + + +# What: define _set_fields around doc and fields; why: its direct callers call _set_fields for set fields and rely on this exact input and result contract. +def _set_fields(doc: dict, fields: tuple[RequestField, ...]) -> None: + # What: iterate across fields to perform parent and path parent and doc and path and field; why: _set_fields repeats the body only while or for the loop header admits an iteration. + for field in fields: + # What: compute parent from path parent and doc and path and field and true; why: assert parent is not later reads parent, so _set_fields must retain the computed value under that name. + parent = _path_parent(doc, field.path, create=True) + # What: assert that parent is not group delimiter; why: _set_fields requires parent is not group delimiter to be true, so a false result stops the invalid state. + assert parent is not None + # What: compute leaf from path and field and 1; why: if field soft and leaf in parent later reads leaf, so _set_fields must retain the computed value under that name. + leaf = field.path[-1] + # What: gate on soft and field and leaf and parent before the computed value; why: _set_fields admits the computed value only for this predicate and excludes the opposite state. + if field.soft and leaf in parent: + # What: apply the continue portion of the enclosing predicate; why: this clause remains in _set_fields\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: compute parent entry from value and field; why: the enclosing return or state update later reads parent entry, so _set_fields must retain the computed value under that name. + parent[leaf] = field.value() + + +# What: define filter_request_body around body and drop fields and set fields and set fields by id and requested model and rewrite model; why: its direct callers call filter_request_body for filter request body and rely on this exact input and result contract. +def filter_request_body( + # What: declare the body input for filter_request_body; why: filter_request_body consumes body during return body, so callers must bind it with the other signature inputs. + body: bytes, + # What: declare the drop fields input for filter_request_body; why: filter_request_body consumes drop fields during for field in drop fields, so callers must bind it with the other signature inputs. + drop_fields: tuple[str, ...], + # What: declare the set fields input for filter_request_body; why: filter_request_body consumes set fields during set fields doc set fields, so callers must bind it with the other signature inputs. + set_fields: tuple[RequestField, ...] = (), + # What: declare the set fields by id input for filter_request_body; why: filter_request_body consumes set fields by id during by id dict set fields by id get requested model, so callers must bind it with the other signature inputs. + set_fields_by_id: tuple[tuple[str, tuple[RequestField, ...]], ...] = (), + # What: mark the remaining parameters as keyword-only; why: filter_request_body prevents callers from confusing adjacent lifecycle and timing arguments. + *, + # What: declare the requested model input for filter_request_body; why: filter_request_body consumes requested model during by id dict set fields by id get requested model, so callers must bind it with the other signature inputs. + requested_model: str | None = None, + # What: declare the rewrite model input for filter_request_body; why: filter_request_body consumes rewrite model during if rewrite model is not, so callers must bind it with the other signature inputs. + rewrite_model: str | None = None, +# What: complete the enclosing predicate with bytes; why: filter_request_body groups the supplied clauses as one enclosing predicate expression before its value is consumed. +) -> bytes: + """Apply safe configured JSON-field transformations in pinned order. + + The default empty policy returns the original bytes exactly. A requested + rewrite runs before drop/global/by-ID fields, matching the pinned filter + order. There is no expression or hook language, so a catalog cannot + execute code in the daemon. + """ + # What: document apply safe configured json field transformations in in the filter_request_body docstring; why: introspection and maintainers read this exact docstring fragment to understand filter request body behavior without executing it. + # What: document the default empty policy returns the in the filter_request_body docstring; why: introspection and maintainers read this exact docstring fragment to understand filter request body behavior without executing it. + # What: document rewrite runs before drop global by id in the filter_request_body docstring; why: introspection and maintainers read this exact docstring fragment to understand filter request body behavior without executing it. + # What: document order there is no expression or in the filter_request_body docstring; why: introspection and maintainers read this exact docstring fragment to understand filter request body behavior without executing it. + # What: document execute code in the daemon in the filter_request_body docstring; why: introspection and maintainers read this exact docstring fragment to understand filter request body behavior without executing it. + # What: preserve the paragraph boundary in the the filter_request_body docstring; why: introspection and maintainers read this paragraph break to understand filter request body behavior without executing it. + # What: compute by id from get and requested model and dict and set fields by id; why: if rewrite model is and not drop fields later reads by id, so filter_request_body must retain the computed value under that name. + by_id = dict(set_fields_by_id).get(requested_model, ()) + # What: gate on rewrite model and drop fields and set fields and by id before body; why: filter_request_body admits body only for this predicate and excludes the opposite state. + if rewrite_model is None and not drop_fields and not set_fields and not by_id: + # What: return body from filter_request_body; why: filter_request_body exposes body so its caller can continue with the function\'s computed outcome. + return body + # What: establish the handler boundary for the protected operation; why: filter_request_body routes failures to unicode decode error and jsondecode error and json while preserving cleanup and success flow. + try: + # What: compute doc from loads and body and json; why: if not isinstance doc dict later reads doc, so filter_request_body must retain the computed value under that name. + doc = json.loads(body) + # What: handle unicode decode error and jsondecode error and json by raise request model error request body must be valid; why: filter_request_body converts that failure into this concrete recovery, response, or cleanup behavior. + except (UnicodeDecodeError, json.JSONDecodeError) as exc: + # What: raise RequestModelError for the caller; why: filter_request_body stops this rejected path before it can mutate state, dispatch work, or report success. + raise RequestModelError("request body must be valid JSON") from exc + # What: gate on isinstance and doc and dict before request model error; why: filter_request_body admits request model error only for this predicate and excludes the opposite state. + if not isinstance(doc, dict): + # What: raise RequestModelError for the caller; why: filter_request_body stops this rejected path before it can mutate state, dispatch work, or report success. + raise RequestModelError("request body must be a JSON object") + # What: gate on rewrite model before rewrite model and doc; why: filter_request_body admits rewrite model and doc only for this predicate and excludes the opposite state. + if rewrite_model is not None: + # What: compute doc entry from rewrite model; why: parent path parent doc path create later reads doc entry, so filter_request_body must retain the computed value under that name. + doc["model"] = rewrite_model + # What: iterate across drop fields to perform path and tuple and split and field; why: filter_request_body repeats the body only while or for the loop header admits an iteration. + for field in drop_fields: + # What: compute path from tuple and split and field and value; why: parent path parent doc path create later reads path, so filter_request_body must retain the computed value under that name. + path = tuple(field.split(".")) + # What: compute parent from path parent and doc and path and false; why: if parent is not later reads parent, so filter_request_body must retain the computed value under that name. + parent = _path_parent(doc, path, create=False) + # What: gate on parent before pop and parent and path; why: filter_request_body admits pop and parent and path only for this predicate and excludes the opposite state. + if parent is not None: + # What: call parent.pop with path and 1 and the named fixture input; why: filter_request_body invokes parent.pop while performing set fields doc set fields; the call advances that operation through its result or side effect. + parent.pop(path[-1], None) + # What: call _set_fields with doc and set fields; why: filter_request_body invokes _set_fields while performing set fields doc by id; the call advances that operation through its result or side effect. + _set_fields(doc, set_fields) + # What: call _set_fields with doc and by id; why: filter_request_body invokes _set_fields while performing return json dumps doc separators ensure ascii encode; the call advances that operation through its result or side effect. + _set_fields(doc, by_id) + # What: return encode and dumps and doc and json and utf 8 from filter_request_body; why: filter_request_body exposes encode and dumps and doc and json and utf 8 so its caller can continue with the function\'s computed outcome. + return json.dumps(doc, separators=(",", ":"), ensure_ascii=False).encode("utf-8") + + +# What: define forward_headers around headers; why: its direct callers call forward_headers for forward headers and rely on this exact input and result contract. +def forward_headers(headers: Mapping[str, str]) -> dict[str, str]: + """Preserve application headers without forwarding daemon authentication. + + The router terminates its bearer/Basic/``x-api-key`` credential and optional + ``X-FT-Token`` locally. None is an engine credential, so forwarding one would + disclose a control-plane secret to the child process and its logs. + """ + # What: document preserve application headers without forwarding daemon in the forward_headers docstring; why: introspection and maintainers read this exact docstring fragment to understand forward headers behavior without executing it. + # What: document the router terminates its bearer basic in the forward_headers docstring; why: introspection and maintainers read this exact docstring fragment to understand forward headers behavior without executing it. + # What: document x ft token locally is an engine credential in the forward_headers docstring; why: introspection and maintainers read this exact docstring fragment to understand forward headers behavior without executing it. + # What: document disclose a control plane secret to the in the forward_headers docstring; why: introspection and maintainers read this exact docstring fragment to understand forward headers behavior without executing it. + # What: preserve the paragraph boundary in the the forward_headers docstring; why: introspection and maintainers read this paragraph break to understand forward headers behavior without executing it. + # What: compute excluded from hop by hop and local auth headers; why: return key value for key value later reads excluded, so forward_headers must retain the computed value under that name. + excluded = _HOP_BY_HOP | _LOCAL_AUTH_HEADERS + # What: return key and value and items and excluded from forward_headers; why: forward_headers exposes key and value and items and excluded so its caller can continue with the function\'s computed outcome. + return {key: value for key, value in headers.items() if key.lower() not in excluded} + + +# What: define response_headers around headers; why: its direct callers call response_headers for response headers and rely on this exact input and result contract. +def response_headers(headers: Mapping[str, str]) -> dict[str, str]: + """Remove only hop-by-hop fields from an engine response. + + Local router credentials are an inbound-only concern. A response may + legitimately contain an application authentication challenge or similarly + named metadata, which must retain normal upstream-header semantics. + """ + # What: document remove only hop by hop fields from an in the response_headers docstring; why: introspection and maintainers read this exact docstring fragment to understand response headers behavior without executing it. + # What: document local router credentials are an inbound only in the response_headers docstring; why: introspection and maintainers read this exact docstring fragment to understand response headers behavior without executing it. + # What: document legitimately contain an application authentication challenge in the response_headers docstring; why: introspection and maintainers read this exact docstring fragment to understand response headers behavior without executing it. + # What: document named metadata which must retain normal in the response_headers docstring; why: introspection and maintainers read this exact docstring fragment to understand response headers behavior without executing it. + # What: preserve the paragraph boundary in the the response_headers docstring; why: introspection and maintainers read this paragraph break to understand response headers behavior without executing it. + # What: return key and value and items and hop by hop from response_headers; why: response_headers exposes key and value and items and hop by hop so its caller can continue with the function\'s computed outcome. + return {key: value for key, value in headers.items() if key.lower() not in _HOP_BY_HOP} + + +# What: generate dataclass initialization and value semantics for UpstreamResponse; why: UpstreamResponse acts as a typed state record with consistent construction, comparison, and representation. +@dataclass +# What: define UpstreamResponse as the owner of chunks and close; why: daemon callers use this class boundary so those methods share one upstream response state invariant. +class UpstreamResponse: + # What: compute status from the named fixture input; why: status raw getcode later reads status, so inference_proxy must retain the computed value under that name. + status: int + # What: compute headers from the named fixture input; why: def open upstream port int path and query str later reads headers, so inference_proxy must retain the computed value under that name. + headers: dict[str, str] + # What: compute raw from the named fixture input; why: chunk self raw read size later reads raw, so inference_proxy must retain the computed value under that name. + raw: object + + # What: define chunks around size; why: its direct callers call chunks for chunks and rely on this exact input and result contract. + def chunks(self, size: int = 64 * 1024) -> Iterator[bytes]: + # What: establish the handler boundary for the protected operation; why: UpstreamResponse.chunks routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: iterate across the computed value to perform chunk and read and size and raw; why: chunks repeats the body only while or for the loop header admits an iteration. + while True: + # What: compute chunk from read and size and raw; why: if not chunk later reads chunk, so chunks must retain the computed value under that name. + chunk = self.raw.read(size) + # What: gate on chunk before the computed value; why: chunks admits the computed value only for this predicate and excludes the opposite state. + if not chunk: + # What: apply the break portion of the enclosing predicate; why: this clause remains in chunks\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: apply the yield chunk portion of the enclosing predicate; why: this clause remains in chunks\'s enclosing expression so its grouping and evaluation order stay intact. + yield chunk + # What: run self close on every exit path; why: chunks performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: call self.close with the declared inputs; why: chunks invokes self.close while performing the enclosing return; the call advances that operation through its result or side effect. + self.close() + + # What: define close around the current object state; why: its direct callers call close for close and rely on this exact input and result contract. + def close(self) -> None: + # What: compute close from getattr and raw and close; why: if close is not later reads close, so close must retain the computed value under that name. + close = getattr(self.raw, "close", None) + # What: gate on close before close; why: close admits close only for this predicate and excludes the opposite state. + if close is not None: + # What: call close with the declared inputs; why: close invokes close while performing the enclosing return; the call advances that operation through its result or side effect. + close() + + +# What: define open_upstream around port and path and query and headers and body and method and timeout s and base url; why: its direct callers call open_upstream for open upstream and rely on this exact input and result contract. +def open_upstream(*, port: int, path_and_query: str, headers: Mapping[str, str], body: bytes, + # What: declare the method input for open_upstream; why: open_upstream consumes method during method method, so callers must bind it with the other signature inputs. + method: str = "POST", timeout_s: float = 900.0, + # What: declare the base url input for open_upstream; why: open_upstream consumes base url during base url base url or owned base, so callers must bind it with the other signature inputs. + base_url: str | None = None) -> UpstreamResponse: + # What: compute owned base from port and http; why: base url base url or owned base later reads owned base, so open_upstream must retain the computed value under that name. + owned_base = f"http://127.0.0.1:{port}" + # What: compute base url from base url and owned base; why: rf http port a za z0 9 base url later reads base url, so open_upstream must retain the computed value under that name. + base_url = base_url or owned_base + # What: gate on any and fullmatch and base url and re and segment before value error; why: open_upstream admits value error only for this predicate and excludes the opposite state. + if ( + # What: call re.fullmatch with port and http and a za z0 9 and value and base url; why: open_upstream invokes re.fullmatch while performing rf http port a za z0 9 base url; the call advances that operation through its result or side effect. + re.fullmatch( + # What: apply the rf http port a za z0 9 base url portion of the enclosing predicate; why: this clause remains in open_upstream\'s enclosing expression so its grouping and evaluation order stay intact. + rf"http://127\.0\.0\.1:{port}(?:/[A-Za-z0-9._~-]+)*", base_url + # What: complete the re.fullmatch call with port and base url; why: open_upstream groups the supplied clauses as one re.fullmatch call before its value is consumed. + ) + # What: apply the is portion of the enclosing predicate; why: this clause remains in open_upstream\'s enclosing expression so its grouping and evaluation order stay intact. + is None + # What: call any with segment and split and base url and value and value; why: open_upstream consumes the any return value while evaluating or any(segment in {".", ".."} for segment in base_url.split("/")). + or any(segment in {".", ".."} for segment in base_url.split("/")) + # What: complete the enclosing predicate with if re fullmatch f http 127 0 0 1 port; why: open_upstream groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise ValueError for the caller; why: open_upstream stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("upstream base URL must target the manager-owned loopback port") + # What: gate on startswith and path and query before value error; why: open_upstream admits value error only for this predicate and excludes the opposite state. + if not path_and_query.startswith("/"): + # What: raise ValueError for the caller; why: open_upstream stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("upstream path must be absolute") + # What: compute request from request and body and method and path and query; why: raw urlopen request timeout timeout s later reads request, so open_upstream must retain the computed value under that name. + request = Request( + # What: call base_url.rstrip with value; why: open_upstream invokes base_url.rstrip while performing data body; the call advances that operation through its result or side effect. + f"{base_url.rstrip('/')}{path_and_query}", + # What: supply data to Request; why: open_upstream binds this body value to Request's data input. + data=body, + # What: supply headers to forward_headers; why: open_upstream binds this forward headers and headers value to forward_headers's headers input. + headers=forward_headers(headers), + # What: supply method to Request; why: open_upstream binds this method value to Request's method input. + method=method, + # What: complete the Request call with data and headers and method; why: open_upstream groups the supplied clauses as one Request call before its value is consumed. + ) + # What: establish the handler boundary for the protected operation; why: open_upstream routes failures to httperror while preserving cleanup and success flow. + try: + # What: compute raw from urlopen and request and timeout s; why: raw exc later reads raw, so open_upstream must retain the computed value under that name. + raw = urlopen(request, timeout=timeout_s) + # What: handle httperror by raw exc; why: open_upstream converts that failure into this concrete recovery, response, or cleanup behavior. + except HTTPError as exc: + # What: compute raw from exc; why: status raw getcode later reads raw, so open_upstream must retain the computed value under that name. + raw = exc + # What: return upstream response and raw and getcode and response headers from open_upstream; why: open_upstream exposes upstream response and raw and getcode and response headers so its caller can continue with the function\'s computed outcome. + return UpstreamResponse( + # What: supply status to raw.getcode; why: open_upstream binds this getcode and raw value to raw.getcode's status input. + status=raw.getcode(), + # What: supply headers to response_headers; why: open_upstream binds this response headers and dict and items and headers value to response_headers's headers input. + headers=response_headers(dict(raw.headers.items())), + # What: supply raw to UpstreamResponse; why: open_upstream binds this raw value to UpstreamResponse's raw input. + raw=raw, + # What: complete the UpstreamResponse call with status and headers and raw; why: open_upstream groups the supplied clauses as one UpstreamResponse call before its value is consumed. + ) diff --git a/python/freetoken/daemon/metrics.py b/python/freetoken/daemon/metrics.py index a2a42d03b..ace8401db 100644 --- a/python/freetoken/daemon/metrics.py +++ b/python/freetoken/daemon/metrics.py @@ -1,13 +1,19 @@ -"""The engine's OWN footprint. Boundary: only the serve tree's RAM/VRAM — system-wide host -telemetry is not this daemon's job. +"""The engine's own process-tree footprint, never system-wide host telemetry. -RAM = summed PSS across the serve process group (shared pages counted once, the honest number). -VRAM = per-process GPU memory for those pids, via ``pynvml`` if importable (optional), else -parsed from ``nvidia-smi``, else 0. All best-effort and off the event loop — a missing GPU or -absent NVML returns 0, never an error.""" +RAM is summed Linux PSS. VRAM is per-process GPU memory from NVML/``nvidia-smi`` or +``amd-smi``. Byte fields remain integers for API compatibility; availability fields prevent an +unavailable best-effort probe from being misrepresented as a measured zero. +""" +# What: document the engine s own process tree footprint in the metrics docstring; why: introspection and maintainers read this exact docstring fragment to understand metrics behavior without executing it. +# What: document ram is summed linux pss vram in the metrics docstring; why: introspection and maintainers read this exact docstring fragment to understand metrics behavior without executing it. +# What: document amd smi byte fields remain integers for in the metrics docstring; why: introspection and maintainers read this exact docstring fragment to understand metrics behavior without executing it. +# What: document unavailable best effort probe from being misrepresented in the metrics docstring; why: introspection and maintainers read this exact docstring fragment to understand metrics behavior without executing it. +# What: preserve the paragraph boundary in the the metrics docstring; why: introspection and maintainers read this paragraph break to understand metrics behavior without executing it. from __future__ import annotations +# What: import json for amd smi process vram using json; why: _amd_smi_process_vram uses json loads, making that imported dependency available to its named operation. +import json import subprocess import threading import time @@ -18,11 +24,43 @@ def engine_footprint(pid: int | None) -> dict: if pid is None: - return {"ramBytes": 0, "vramBytes": 0, "pids": []} + # What: return ram bytes and vram bytes and pids and ram available and vram available from engine_footprint; why: engine_footprint exposes ram bytes and vram bytes and pids and ram available and vram available so its caller can continue with the function\'s computed outcome. + return { + # What: map the ram bytes field as 0; why: engine_footprint carries ram bytes into "ramBytes": 0, "vramBytes": 0, "pids": []. + "ramBytes": 0, "vramBytes": 0, "pids": [], + # What: map the ram available field as false; why: engine_footprint carries ram available into "ramAvailable": False, "vramAvailable": False. + "ramAvailable": False, "vramAvailable": False, + # What: map the ram source field as the fixture input; why: engine_footprint carries ram source into "ramSource": None, "vramSource": None. + "ramSource": None, "vramSource": None, + # What: complete the enclosing predicate mapping with ram bytes and vram bytes and pids and ram available and vram available; why: engine_footprint groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } pids = osproc.tree_pids(pid) - ram = sum(osproc.read_pss_bytes(p) for p in pids) - vram = vram_bytes_for_pids(pids) - return {"ramBytes": ram, "vramBytes": vram, "pids": pids} + # What: compute ram parts from read pss bytes if available and p and pids and osproc; why: ram available bool ram parts and all value later reads ram parts, so engine_footprint must retain the computed value under that name. + ram_parts = [osproc.read_pss_bytes_if_available(p) for p in pids] + # What: compute ram available from bool and ram parts and all and value; why: ram available ram available later reads ram available, so engine_footprint must retain the computed value under that name. + ram_available = bool(ram_parts) and all(value is not None for value in ram_parts) + # What: compute ram from sum and value and ram parts and 0; why: ram bytes ram later reads ram, so engine_footprint must retain the computed value under that name. + ram = sum(value or 0 for value in ram_parts) + # What: compute vram and vram available and vram source from vram measurement for pids and pids; why: vram bytes vram later reads vram and vram available and vram source, so engine_footprint must retain the computed value under that name. + vram, vram_available, vram_source = _vram_measurement_for_pids(pids) + # What: return ram and vram and pids and ram available from engine_footprint; why: engine_footprint exposes ram and vram and pids and ram available so its caller can continue with the function\'s computed outcome. + return { + # What: map the ram bytes field as ram; why: engine_footprint carries ram bytes into "ramBytes": ram. + "ramBytes": ram, + # What: map the vram bytes field as vram; why: engine_footprint carries vram bytes into "vramBytes": vram. + "vramBytes": vram, + # What: map the pids field as pids; why: engine_footprint carries pids into "pids": pids. + "pids": pids, + # What: map the ram available field as ram available; why: engine_footprint carries ram available into "ramAvailable": ram_available. + "ramAvailable": ram_available, + # What: map the vram available field as vram available; why: engine_footprint carries vram available into "vramAvailable": vram_available. + "vramAvailable": vram_available, + # What: map the ram source field as ram available and proc smaps rollup pss; why: engine_footprint carries ram source into "ramSource": "proc-smaps-rollup-pss" if ram_available else None. + "ramSource": "proc-smaps-rollup-pss" if ram_available else None, + # What: map the vram source field as vram source; why: engine_footprint carries vram source into "vramSource": vram_source. + "vramSource": vram_source, + # What: complete the enclosing predicate mapping with ram bytes and vram bytes and pids and ram available and vram available; why: engine_footprint groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } class FootprintCache: @@ -48,15 +86,44 @@ def get(self, pid: int | None) -> dict: def vram_bytes_for_pids(pids: list[int]) -> int: + # What: return vram measurement for pids and pids and 0 from vram_bytes_for_pids; why: vram_bytes_for_pids exposes vram measurement for pids and pids and 0 so its caller can continue with the function\'s computed outcome. + return _vram_measurement_for_pids(pids)[0] + + +# What: define _vram_measurement_for_pids around pids; why: its direct callers call _vram_measurement_for_pids for vram measurement for pids and rely on this exact input and result contract. +def _vram_measurement_for_pids(pids: list[int]) -> tuple[int, bool, str | None]: want = set(pids) if not want: - return 0 - usage = _nvml_process_vram() - if usage is None: - usage = _smi_process_vram() - if not usage: - return 0 - return sum(nbytes for p, nbytes in usage.items() if p in want) + # What: return 0 and false from _vram_measurement_for_pids; why: _vram_measurement_for_pids exposes 0 and false so its caller can continue with the function\'s computed outcome. + return 0, False, None + # What: compute available source from the named fixture input; why: available source available source or source later reads available source, so _vram_measurement_for_pids must retain the computed value under that name. + available_source = None + # What: iterate across nvml process vram and smi process vram and amd smi process vram to perform usage and probe; why: _vram_measurement_for_pids repeats the body only while or for the loop header admits an iteration. + for source, probe in ( + # What: apply the nvml nvml process vram portion of the enclosing predicate; why: this clause remains in _vram_measurement_for_pids\'s enclosing expression so its grouping and evaluation order stay intact. + ("nvml", _nvml_process_vram), + # What: apply the nvidia smi smi process vram portion of the enclosing predicate; why: this clause remains in _vram_measurement_for_pids\'s enclosing expression so its grouping and evaluation order stay intact. + ("nvidia-smi", _smi_process_vram), + # What: apply the amd smi amd smi process vram portion of the enclosing predicate; why: this clause remains in _vram_measurement_for_pids\'s enclosing expression so its grouping and evaluation order stay intact. + ("amd-smi", _amd_smi_process_vram), + # What: complete the enclosing predicate collection with nvml process vram and nvml and smi process vram and nvidia smi and amd smi process vram and amd smi; why: _vram_measurement_for_pids groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + ): + # What: compute usage from probe; why: if usage is not later reads usage, so _vram_measurement_for_pids must retain the computed value under that name. + usage = probe() + # What: gate on usage before any and source and pid and usage and want; why: _vram_measurement_for_pids admits any and source and pid and usage and want only for this predicate and excludes the opposite state. + if usage is not None: + # What: gate on any and pid and usage and want before source and sum and nbytes and p and items; why: _vram_measurement_for_pids admits source and sum and nbytes and p and items only for this predicate and excludes the opposite state. + if any(pid in usage for pid in want): + # What: return source and sum and nbytes and p from _vram_measurement_for_pids; why: _vram_measurement_for_pids exposes source and sum and nbytes and p so its caller can continue with the function\'s computed outcome. + return sum(nbytes for p, nbytes in usage.items() if p in want), True, source + # What: compute available source from available source and source; why: if available source is not later reads available source, so _vram_measurement_for_pids must retain the computed value under that name. + available_source = available_source or source + # What: gate on available source before available source; why: _vram_measurement_for_pids admits available source only for this predicate and excludes the opposite state. + if available_source is not None: + # What: return available source and 0 and true from _vram_measurement_for_pids; why: _vram_measurement_for_pids exposes available source and 0 and true so its caller can continue with the function\'s computed outcome. + return 0, True, available_source + # What: return 0 and false from _vram_measurement_for_pids; why: _vram_measurement_for_pids exposes 0 and false so its caller can continue with the function\'s computed outcome. + return 0, False, None # NVML is initialized ONCE and held for the daemon's life — nvmlInit()+nvmlShutdown() on every @@ -83,6 +150,8 @@ def _nvml_process_vram() -> dict[int, int] | None: if not pynvml: return None out: dict[int, int] = {} + # What: compute queried from false; why: queried later reads queried, so _nvml_process_vram must retain the computed value under that name. + queried = False try: count = pynvml.nvmlDeviceGetCount() for i in range(count): @@ -98,18 +167,21 @@ def _nvml_process_vram() -> dict[int, int] | None: used = getattr(proc, "usedGpuMemory", None) if used: # None == "not available", per NVML out[int(proc.pid)] = out.get(int(proc.pid), 0) + int(used) + # What: compute queried from true; why: return out if queried else later reads queried, so _nvml_process_vram must retain the computed value under that name. + queried = True break except Exception: # noqa: BLE001 continue except Exception: # noqa: BLE001 return out or None - # Empty → NVML enumeration gave nothing usable (e.g. every process getter raised on a - # driver/MIG mismatch); signal that with None so the nvidia-smi fallback still runs, matching - # the error path above. - return out or None + # A successfully queried empty process list is a real zero. If every getter failed, + # ``queried`` stays false and the command-line fallbacks still run. + # What: return queried and out from _nvml_process_vram; why: _nvml_process_vram exposes queried and out so its caller can continue with the function\'s computed outcome. + return out if queried else None -def _smi_process_vram() -> dict[int, int]: +# What: define _smi_process_vram around the current object state; why: its direct callers call _smi_process_vram for smi process vram and rely on this exact input and result contract. +def _smi_process_vram() -> dict[int, int] | None: try: out = subprocess.run( [ @@ -122,13 +194,163 @@ def _smi_process_vram() -> dict[int, int]: timeout=3.0, ) except (OSError, subprocess.SubprocessError): - return {} + # What: return no value from _smi_process_vram; why: _smi_process_vram returns no value to callers that depend on its completed result. + return None if out.returncode != 0: - return {} + # What: return no value from _smi_process_vram; why: _smi_process_vram returns no value to callers that depend on its completed result. + return None usage: dict[int, int] = {} + # What: compute malformed from false; why: malformed later reads malformed, so _smi_process_vram must retain the computed value under that name. + malformed = False for line in out.stdout.splitlines(): parts = [p.strip() for p in line.split(",")] if len(parts) != 2 or not parts[0].isdigit() or not parts[1].isdigit(): + # What: gate on strip and line before malformed; why: _smi_process_vram admits malformed only for this predicate and excludes the opposite state. + if line.strip(): + # What: compute malformed from true; why: return if malformed else usage later reads malformed, so _smi_process_vram must retain the computed value under that name. + malformed = True continue usage[int(parts[0])] = usage.get(int(parts[0]), 0) + int(parts[1]) * 1024 * 1024 # MiB - return usage + # What: return malformed and usage from _smi_process_vram; why: _smi_process_vram exposes malformed and usage so its caller can continue with the function\'s computed outcome. + return None if malformed else usage + + +# What: define _memory_bytes around value; why: its direct callers call _memory_bytes for memory bytes and rely on this exact input and result contract. +def _memory_bytes(value) -> int | None: + """Parse AMD SMI's version-dependent JSON scalar or ``{value, unit}`` form.""" + # What: document parse amd smi s version dependent json in the _memory_bytes docstring; why: introspection and maintainers read this exact docstring fragment to understand memory bytes behavior without executing it. + # What: gate on isinstance and value and dict before unit and get and value; why: _memory_bytes admits unit and get and value only for this predicate and excludes the opposite state. + if isinstance(value, dict) and "value" in value: + # What: compute unit from get and value and unit and b; why: value unit parts parts if len later reads unit, so _memory_bytes must retain the computed value under that name. + unit = value.get("unit", "B") + # What: compute value from value and value; why: elif isinstance value str later reads value, so _memory_bytes must retain the computed value under that name. + value = value["value"] + # What: gate on isinstance and value and str before parts and split and strip and value; why: _memory_bytes admits parts and split and strip and value only for this predicate and excludes the opposite state. + elif isinstance(value, str): + # What: compute parts from split and strip and value; why: if not parts later reads parts, so _memory_bytes must retain the computed value under that name. + parts = value.strip().split() + # What: gate on parts before the computed value; why: _memory_bytes admits the computed value only for this predicate and excludes the opposite state. + if not parts: + # What: return no value from _memory_bytes; why: _memory_bytes returns no value to callers that depend on its completed result. + return None + # What: compute value and unit from parts and len and 0 and b and 1; why: if isinstance value bool later reads value and unit, so _memory_bytes must retain the computed value under that name. + value, unit = parts[0], parts[1] if len(parts) > 1 else "B" + # What: select the remaining branch that performs unit b; why: _memory_bytes covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute unit from b; why: scale scales get str unit strip lower later reads unit, so _memory_bytes must retain the computed value under that name. + unit = "B" + # What: gate on isinstance and value and bool before the computed value; why: _memory_bytes admits the computed value only for this predicate and excludes the opposite state. + if isinstance(value, bool): + # What: return no value from _memory_bytes; why: _memory_bytes returns no value to callers that depend on its completed result. + return None + # What: establish the handler boundary for the protected operation; why: _memory_bytes routes failures to type error and value error while preserving cleanup and success flow. + try: + # What: compute amount from float and value; why: if scale is or amount later reads amount, so _memory_bytes must retain the computed value under that name. + amount = float(value) + # What: handle type error and value error by return; why: _memory_bytes converts that failure into this concrete recovery, response, or cleanup behavior. + except (TypeError, ValueError): + # What: return no value from _memory_bytes; why: _memory_bytes returns no value to callers that depend on its completed result. + return None + # What: compute scales from b and kb and mb and gb and tb; why: scale scales get str unit strip lower later reads scales, so _memory_bytes must retain the computed value under that name. + scales = { + # What: map the b field as 1; why: _memory_bytes carries b through scales into scale scales get str unit strip lower. + "b": 1, "kb": 1000, "mb": 1000**2, "gb": 1000**3, "tb": 1000**4, + # What: map the kib field as 1024; why: _memory_bytes carries kib through scales into scale scales get str unit strip lower. + "kib": 1024, "mib": 1024**2, "gib": 1024**3, "tib": 1024**4, + # What: complete the scales mapping with b and kb and mb and gb and tb; why: _memory_bytes groups the supplied clauses as one scales mapping before its value is consumed. + } + # What: compute scale from get and scales and lower and strip; why: if scale is or amount later reads scale, so _memory_bytes must retain the computed value under that name. + scale = scales.get(str(unit).strip().lower()) + # What: gate on scale and amount before the computed value; why: _memory_bytes admits the computed value only for this predicate and excludes the opposite state. + if scale is None or amount < 0: + # What: return no value from _memory_bytes; why: _memory_bytes returns no value to callers that depend on its completed result. + return None + # What: return int and amount and scale from _memory_bytes; why: _memory_bytes exposes int and amount and scale so its caller can continue with the function\'s computed outcome. + return int(amount * scale) + + +# What: define _amd_smi_process_vram around the current object state; why: its direct callers call _amd_smi_process_vram for amd smi process vram and rely on this exact input and result contract. +def _amd_smi_process_vram() -> dict[int, int] | None: + """Read process VRAM from the documented ``amd-smi process --json`` schema.""" + # What: document read process vram from the documented in the _amd_smi_process_vram docstring; why: introspection and maintainers read this exact docstring fragment to understand amd smi process vram behavior without executing it. + # What: establish the handler boundary for the protected operation; why: _amd_smi_process_vram routes failures to oserror and subprocess error and subprocess while preserving cleanup and success flow. + try: + # What: compute out from run and subprocess and amd smi and process and json; why: if out returncode later reads out, so _amd_smi_process_vram must retain the computed value under that name. + out = subprocess.run( + # What: apply the amd smi process json general portion of out; why: _amd_smi_process_vram uses this clause to evaluate out as one grouped value. + ["amd-smi", "process", "--json", "--general"], + # What: supply capture output to subprocess.run; why: _amd_smi_process_vram binds this true value to subprocess.run's capture output input. + capture_output=True, + # What: supply text to subprocess.run; why: _amd_smi_process_vram binds this true value to subprocess.run's text input. + text=True, + # What: supply timeout to subprocess.run; why: _amd_smi_process_vram binds this 3 0 value to subprocess.run's timeout input. + timeout=3.0, + # What: complete the subprocess.run call with capture output and text and timeout; why: _amd_smi_process_vram groups the supplied clauses as one subprocess.run call before its value is consumed. + ) + # What: handle oserror and subprocess error and subprocess by return; why: _amd_smi_process_vram converts that failure into this concrete recovery, response, or cleanup behavior. + except (OSError, subprocess.SubprocessError): + # What: return no value from _amd_smi_process_vram; why: _amd_smi_process_vram returns no value to callers that depend on its completed result. + return None + # What: gate on returncode and out before the computed value; why: _amd_smi_process_vram admits the computed value only for this predicate and excludes the opposite state. + if out.returncode != 0: + # What: return no value from _amd_smi_process_vram; why: _amd_smi_process_vram returns no value to callers that depend on its completed result. + return None + # What: establish the handler boundary for the protected operation; why: _amd_smi_process_vram routes failures to jsondecode error and type error and json while preserving cleanup and success flow. + try: + # What: compute doc from loads and stdout and json and out; why: visit doc later reads doc, so _amd_smi_process_vram must retain the computed value under that name. + doc = json.loads(out.stdout) + # What: handle jsondecode error and type error and json by return; why: _amd_smi_process_vram converts that failure into this concrete recovery, response, or cleanup behavior. + except (json.JSONDecodeError, TypeError): + # What: return no value from _amd_smi_process_vram; why: _amd_smi_process_vram returns no value to callers that depend on its completed result. + return None + + # What: initialize usage as an empty runtime accumulator; why: _amd_smi_process_vram appends or maps entries into it during usage pid usage get pid 0 vram before consuming the aggregate. + usage: dict[int, int] = {} + # What: compute saw process from false; why: nonlocal saw process saw vram later reads saw process, so _amd_smi_process_vram must retain the computed value under that name. + saw_process = False + # What: compute saw vram from false; why: nonlocal saw process saw vram later reads saw vram, so _amd_smi_process_vram must retain the computed value under that name. + saw_vram = False + + # What: define visit around node; why: its direct callers call visit for visit and rely on this exact input and result contract. + def visit(node) -> None: + # What: apply the nonlocal saw process saw vram portion of the enclosing predicate; why: this clause remains in visit\'s enclosing expression so its grouping and evaluation order stay intact. + nonlocal saw_process, saw_vram + # What: gate on isinstance and node and dict before fields and value and lower and key and items; why: visit admits fields and value and lower and key and items only for this predicate and excludes the opposite state. + if isinstance(node, dict): + # What: compute fields from value and lower and key and items; why: pid fields get pid later reads fields, so visit must retain the computed value under that name. + fields = {str(key).lower(): value for key, value in node.items()} + # What: compute pid from get and fields and pid; why: if isinstance pid int and not later reads pid, so visit must retain the computed value under that name. + pid = fields.get("pid") + # What: compute memory from get and fields and memory usage; why: if isinstance memory dict later reads memory, so visit must retain the computed value under that name. + memory = fields.get("memory_usage") + # What: gate on isinstance and pid and int and bool before saw process; why: visit admits saw process only for this predicate and excludes the opposite state. + if isinstance(pid, int) and not isinstance(pid, bool): + # What: compute saw process from true; why: the enclosing return or state update later reads saw process, so visit must retain the computed value under that name. + saw_process = True + # What: gate on isinstance and memory and dict before memory fields and value and lower and key and items; why: visit admits memory fields and value and lower and key and items only for this predicate and excludes the opposite state. + if isinstance(memory, dict): + # What: compute memory fields from value and lower and key and items; why: vram memory bytes memory fields get vram mem later reads memory fields, so visit must retain the computed value under that name. + memory_fields = {str(key).lower(): value for key, value in memory.items()} + # What: compute vram from memory bytes and get and memory fields and vram mem; why: if vram is not later reads vram, so visit must retain the computed value under that name. + vram = _memory_bytes(memory_fields.get("vram_mem")) + # What: gate on vram before saw vram; why: visit admits saw vram only for this predicate and excludes the opposite state. + if vram is not None: + # What: compute saw vram from true; why: the enclosing return or state update later reads saw vram, so visit must retain the computed value under that name. + saw_vram = True + # What: compute usage entry from vram and get and pid and usage and 0; why: the enclosing return or state update later reads usage entry, so visit must retain the computed value under that name. + usage[pid] = usage.get(pid, 0) + vram + # What: iterate across values and node to perform visit and child; why: visit repeats the body only while or for the loop header admits an iteration. + for child in node.values(): + # What: call visit with child; why: visit invokes visit while performing elif isinstance node list; the call advances that operation through its result or side effect. + visit(child) + # What: gate on isinstance and node and list before child and node and visit; why: visit admits child and node and visit only for this predicate and excludes the opposite state. + elif isinstance(node, list): + # What: iterate across node to perform visit and child; why: visit repeats the body only while or for the loop header admits an iteration. + for child in node: + # What: call visit with child; why: visit invokes visit while performing the enclosing return; the call advances that operation through its result or side effect. + visit(child) + + # What: call visit with doc; why: _amd_smi_process_vram invokes visit while performing return if saw process and not saw vram; the call advances that operation through its result or side effect. + visit(doc) + # What: return usage and saw process and saw vram from _amd_smi_process_vram; why: _amd_smi_process_vram exposes usage and saw process and saw vram so its caller can continue with the function\'s computed outcome. + return None if saw_process and not saw_vram else usage diff --git a/python/freetoken/daemon/osproc.py b/python/freetoken/daemon/osproc.py index acf6447ef..f04a32187 100644 --- a/python/freetoken/daemon/osproc.py +++ b/python/freetoken/daemon/osproc.py @@ -156,6 +156,39 @@ def read_pss_bytes(pid: int) -> int: return 0 +# What: define read_pss_bytes_if_available around pid; why: its direct callers call read_pss_bytes_if_available for read pss bytes if available and rely on this exact input and result contract. +def read_pss_bytes_if_available(pid: int) -> int | None: + """PSS in bytes, or ``None`` when this host/process cannot provide it. + + Unlike :func:`read_pss_bytes`, this preserves the distinction between an + actual zero and an unavailable ``/proc`` measurement for observability + callers that must not present a safe default as measured data. + """ + # What: document pss in bytes or when this in the read_pss_bytes_if_available docstring; why: introspection and maintainers read this exact docstring fragment to understand read pss bytes if available behavior without executing it. + # What: document unlike func read pss bytes this preserves the in the read_pss_bytes_if_available docstring; why: introspection and maintainers read this exact docstring fragment to understand read pss bytes if available behavior without executing it. + # What: document actual zero and an unavailable proc in the read_pss_bytes_if_available docstring; why: introspection and maintainers read this exact docstring fragment to understand read pss bytes if available behavior without executing it. + # What: document callers that must not present a in the read_pss_bytes_if_available docstring; why: introspection and maintainers read this exact docstring fragment to understand read pss bytes if available behavior without executing it. + # What: preserve the paragraph boundary in the the read_pss_bytes_if_available docstring; why: introspection and maintainers read this paragraph break to understand read pss bytes if available behavior without executing it. + # What: compute raw from read proc and pid and smaps rollup; why: if not raw later reads raw, so read_pss_bytes_if_available must retain the computed value under that name. + raw = _read_proc(pid, "smaps_rollup") + # What: gate on raw before the computed value; why: read_pss_bytes_if_available admits the computed value only for this predicate and excludes the opposite state. + if not raw: + # What: return no value from read_pss_bytes_if_available; why: read_pss_bytes_if_available returns no value to callers that depend on its completed result. + return None + # What: iterate across splitlines and raw to perform startswith and parts and line and split and isdigit; why: read_pss_bytes_if_available repeats the body only while or for the loop header admits an iteration. + for line in raw.splitlines(): + # What: gate on startswith and line before parts and split and line; why: read_pss_bytes_if_available admits parts and split and line only for this predicate and excludes the opposite state. + if line.startswith("Pss:"): + # What: compute parts from split and line; why: if len parts and parts isdigit later reads parts, so read_pss_bytes_if_available must retain the computed value under that name. + parts = line.split() + # What: gate on isdigit and len and parts before int and parts; why: read_pss_bytes_if_available admits int and parts only for this predicate and excludes the opposite state. + if len(parts) >= 2 and parts[1].isdigit(): + # What: return int and parts and 1024 and 1 from read_pss_bytes_if_available; why: read_pss_bytes_if_available exposes int and parts and 1024 and 1 so its caller can continue with the function\'s computed outcome. + return int(parts[1]) * 1024 + # What: return no value from read_pss_bytes_if_available; why: read_pss_bytes_if_available returns no value to callers that depend on its completed result. + return None + + def is_ft_serve_on_port(pid: int, port: int, *, starttime: int | None = None) -> bool: """Verify ``pid`` is (still) an ``ft serve`` bound to ``port`` — the re-adoption / liveness identity check. Requires: alive, unchanged start time (PID-reuse diff --git a/python/freetoken/daemon/performance.py b/python/freetoken/daemon/performance.py new file mode 100644 index 000000000..db4efa1b0 --- /dev/null +++ b/python/freetoken/daemon/performance.py @@ -0,0 +1,223 @@ +"""Bounded periodic history for the owned engine process tree only.""" +# What: document bounded periodic history for the owned in the performance docstring; why: introspection and maintainers read this exact docstring fragment to understand performance behavior without executing it. + +# What: enable postponed evaluation of annotations; why: type hints in performance can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import deque for init using collections and deque; why: __init__ uses deque, making that imported dependency available to its named operation. +from collections import deque +# What: import datetime and timezone for current and sample once using datetime and datetime and timezone; why: current and sample_once uses the datetime annotation in current and timezone utc, making that imported dependency available to its named operation. +from datetime import datetime, timezone +# What: import threading for init using threading; why: __init__ uses threading lock, making that imported dependency available to its named operation. +import threading +# What: import time for init using time; why: __init__ uses time time, making that imported dependency available to its named operation. +import time +# What: import callable for init using typing and callable; why: __init__ uses the callable annotation in init, making that imported dependency available to its named operation. +from typing import Callable + + +# What: define PerformanceMonitor as the owner of __init__ and start and stop and reconfigure and sample_once; why: daemon callers use this class boundary so those methods share one performance monitor state invariant. +class PerformanceMonitor: + """Sample a privacy-bounded probe for at most one hour.""" +# What: document sample a privacy bounded probe for at in the PerformanceMonitor docstring; why: introspection and maintainers read this exact docstring fragment to understand performance monitor behavior without executing it. + + # What: define __init__ around sample fn and every s and disabled and wall now; why: its direct callers call __init__ for init and rely on this exact input and result contract. + def __init__( + # What: declare the self input for __init__; why: __init__ consumes self during self sample fn sample fn, so callers must bind it with the other signature inputs. + self, sample_fn: Callable[[], dict], *, every_s: float = 5.0, + # What: declare the disabled input for __init__; why: __init__ consumes disabled during self disabled disabled, so callers must bind it with the other signature inputs. + disabled: bool = False, wall_now: Callable[[], float] = time.time, + # What: complete the enclosing predicate with group delimiter; why: PerformanceMonitor.__init__ groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> None: + # What: compute sample fn from sample fn; why: the enclosing return or state update later reads sample fn, so __init__ must retain the computed value under that name. + self._sample_fn = sample_fn + # What: compute wall now from wall now; why: the enclosing return or state update later reads wall now, so __init__ must retain the computed value under that name. + self._wall_now = wall_now + # What: compute lock from lock and threading; why: the enclosing return or state update later reads lock, so __init__ must retain the computed value under that name. + self._lock = threading.Lock() + # What: compute stop from the named fixture input; why: the enclosing return or state update later reads stop, so __init__ must retain the computed value under that name. + self._stop: threading.Event | None = None + # What: compute thread from the named fixture input; why: the enclosing return or state update later reads thread, so __init__ must retain the computed value under that name. + self._thread: threading.Thread | None = None + # What: compute started from false; why: the enclosing return or state update later reads started, so __init__ must retain the computed value under that name. + self._started = False + # What: compute rows from deque; why: the enclosing return or state update later reads rows, so __init__ must retain the computed value under that name. + self._rows: deque[dict] = deque() + # What: compute error from the named fixture input; why: the enclosing return or state update later reads error, so __init__ must retain the computed value under that name. + self._error: str | None = None + # What: compute every s from every s; why: the enclosing return or state update later reads every s, so __init__ must retain the computed value under that name. + self._every_s = every_s + # What: compute disabled from disabled; why: the enclosing return or state update later reads disabled, so __init__ must retain the computed value under that name. + self._disabled = disabled + # What: compute capacity from max and int and every s and 1 and 3600; why: the enclosing return or state update later reads capacity, so __init__ must retain the computed value under that name. + self._capacity = max(1, int(3600 / every_s)) + + # What: define start around the current object state; why: its direct callers call start for start and rely on this exact input and result contract. + def start(self) -> None: + # What: enter the lock managed context before self started; why: start releases this resource or lock after self started on both success and failure paths. + with self._lock: + # What: compute started from true; why: the enclosing return or state update later reads started, so start must retain the computed value under that name. + self._started = True + # What: gate on disabled and thread before the computed value; why: start admits the computed value only for this predicate and excludes the opposite state. + if self._disabled or self._thread is not None: + # What: return no value from start; why: start returns no value to callers that depend on its completed result. + return + # What: compute stop from event and threading; why: self stop stop later reads stop, so start must retain the computed value under that name. + stop = threading.Event() + # What: compute stop from stop; why: the enclosing return or state update later reads stop, so start must retain the computed value under that name. + self._stop = stop + # What: compute thread from thread and threading and run and stop and ft daemon performance; why: self thread start later reads thread, so start must retain the computed value under that name. + self._thread = threading.Thread( + # What: supply target to threading.Thread; why: start binds this run value to threading.Thread's target input. + target=self._run, args=(stop,), name="ft-daemon-performance", daemon=True + # What: complete the threading.Thread call with target and args and name and daemon; why: PerformanceMonitor.start groups the supplied clauses as one threading.Thread call before its value is consumed. + ) + # What: call self._thread.start with the declared inputs; why: start invokes self._thread.start while performing the enclosing return; the call advances that operation through its result or side effect. + self._thread.start() + + # What: define stop around the current object state; why: its direct callers call stop for stop and rely on this exact input and result contract. + def stop(self) -> None: + # What: enter the lock managed context before self started; why: stop releases this resource or lock after self started on both success and failure paths. + with self._lock: + # What: compute started from false; why: the enclosing return or state update later reads started, so stop must retain the computed value under that name. + self._started = False + # What: compute thread from thread; why: if thread is not later reads thread, so stop must retain the computed value under that name. + thread = self._thread + # What: compute thread from the named fixture input; why: the enclosing return or state update later reads thread, so stop must retain the computed value under that name. + self._thread = None + # What: compute stop from stop; why: if stop is not later reads stop, so stop must retain the computed value under that name. + stop = self._stop + # What: compute stop from the named fixture input; why: the enclosing return or state update later reads stop, so stop must retain the computed value under that name. + self._stop = None + # What: gate on stop before set and stop; why: stop admits set and stop only for this predicate and excludes the opposite state. + if stop is not None: + # What: call stop.set with the declared inputs; why: stop invokes stop.set while performing if thread is not; the call advances that operation through its result or side effect. + stop.set() + # What: gate on thread before join and thread and max and min and every s; why: stop admits join and thread and max and min and every s only for this predicate and excludes the opposite state. + if thread is not None: + # What: supply timeout to thread.join; why: stop binds this max and min and every s and 1 0 and 5 0 value to thread.join's timeout input. + thread.join(timeout=max(1.0, min(self._every_s, 5.0))) + + # What: define reconfigure around every s and disabled; why: its direct callers call reconfigure for reconfigure and rely on this exact input and result contract. + def reconfigure(self, every_s: float, disabled: bool) -> None: + # What: enter the lock managed context before changed every s self every s or disabled self disabled; why: reconfigure releases this resource or lock after changed every s self every s or disabled self disabled on both success and failure paths. + with self._lock: + # What: compute changed from every s and every s and disabled and disabled; why: if not changed later reads changed, so reconfigure must retain the computed value under that name. + changed = every_s != self._every_s or disabled != self._disabled + # What: compute restart from started; why: if restart later reads restart, so reconfigure must retain the computed value under that name. + restart = self._started + # What: gate on changed before the computed value; why: reconfigure admits the computed value only for this predicate and excludes the opposite state. + if not changed: + # What: return no value from reconfigure; why: reconfigure returns no value to callers that depend on its completed result. + return + # What: call self.stop with the declared inputs; why: reconfigure invokes self.stop while performing with self lock; the call advances that operation through its result or side effect. + self.stop() + # What: enter the lock managed context before self every s every s; why: reconfigure releases this resource or lock after self every s every s on both success and failure paths. + with self._lock: + # What: compute every s from every s; why: the enclosing return or state update later reads every s, so reconfigure must retain the computed value under that name. + self._every_s = every_s + # What: compute disabled from disabled; why: the enclosing return or state update later reads disabled, so reconfigure must retain the computed value under that name. + self._disabled = disabled + # What: compute capacity from max and int and every s and 1 and 3600; why: the enclosing return or state update later reads capacity, so reconfigure must retain the computed value under that name. + self._capacity = max(1, int(3600 / every_s)) + # What: call self._rows.clear with the declared inputs; why: reconfigure invokes self._rows.clear while performing self error; the call advances that operation through its result or side effect. + self._rows.clear() + # What: compute error from the named fixture input; why: the enclosing return or state update later reads error, so reconfigure must retain the computed value under that name. + self._error = None + # What: gate on restart before start; why: reconfigure admits start only for this predicate and excludes the opposite state. + if restart: + # What: call self.start with the declared inputs; why: reconfigure invokes self.start while performing the enclosing return; the call advances that operation through its result or side effect. + self.start() + + # What: define sample_once around the current object state; why: its direct callers call sample_once for sample once and rely on this exact input and result contract. + def sample_once(self) -> None: + # What: establish the handler boundary for the protected operation; why: PerformanceMonitor.sample_once routes failures to exception while preserving cleanup and success flow. + try: + # What: compute measured from sample fn; why: ram bytes int measured get ram bytes later reads measured, so sample_once must retain the computed value under that name. + measured = self._sample_fn() + # What: compute row from replace and int and bool and get; why: self rows append row later reads row, so sample_once must retain the computed value under that name. + row = { + # What: map the timestamp field as replace and isoformat and fromtimestamp and utc; why: PerformanceMonitor.sample_once carries timestamp through row into self rows append row. + "timestamp": datetime.fromtimestamp( + # What: call self._wall_now with the declared inputs; why: sample_once invokes self._wall_now while performing isoformat replace z; the call advances that operation through its result or side effect. + self._wall_now(), timezone.utc + # What: apply the isoformat replace z portion of row; why: sample_once uses this clause to evaluate row as one grouped value. + ).isoformat().replace("+00:00", "Z"), + # What: map the scope field as engine process tree; why: PerformanceMonitor.sample_once carries scope through row into self rows append row. + "scope": "engine-process-tree", + # What: map the ram bytes field as int and get and measured and ram bytes and 0; why: PerformanceMonitor.sample_once carries ram bytes through row into self rows append row. + "ram_bytes": int(measured.get("ramBytes", 0)), + # What: map the vram bytes field as int and get and measured and vram bytes and 0; why: PerformanceMonitor.sample_once carries vram bytes through row into self rows append row. + "vram_bytes": int(measured.get("vramBytes", 0)), + # What: map the ram available field as bool and get and measured and ram available and false; why: PerformanceMonitor.sample_once carries ram available through row into self rows append row. + "ram_available": bool(measured.get("ramAvailable", False)), + # What: map the vram available field as bool and get and measured and vram available and false; why: PerformanceMonitor.sample_once carries vram available through row into self rows append row. + "vram_available": bool(measured.get("vramAvailable", False)), + # What: map the ram source field as get and measured and ram source; why: PerformanceMonitor.sample_once carries ram source through row into self rows append row. + "ram_source": measured.get("ramSource"), + # What: map the vram source field as get and measured and vram source; why: PerformanceMonitor.sample_once carries vram source through row into self rows append row. + "vram_source": measured.get("vramSource"), + # What: complete the row mapping with timestamp and scope and ram bytes and vram bytes and ram available; why: PerformanceMonitor.sample_once groups the supplied clauses as one row mapping before its value is consumed. + } + # What: handle exception by with self lock; why: PerformanceMonitor.sample_once converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception: # noqa: BLE001 - monitoring must never break routing + # What: enter the lock managed context before self error sample failed; why: sample_once releases this resource or lock after self error sample failed on both success and failure paths. + with self._lock: + # What: compute error from sample failed; why: self error later reads error, so sample_once must retain the computed value under that name. + self._error = "sample_failed" + # What: return no value from sample_once; why: sample_once returns no value to callers that depend on its completed result. + return + # What: enter the lock managed context before self rows append row; why: sample_once releases this resource or lock after self rows append row on both success and failure paths. + with self._lock: + # What: call self._rows.append with row; why: sample_once invokes self._rows.append while performing while len self rows self capacity; the call advances that operation through its result or side effect. + self._rows.append(row) + # What: iterate across capacity and len and rows to perform popleft and rows; why: sample_once repeats the body only while or for the loop header admits an iteration. + while len(self._rows) > self._capacity: + # What: call self._rows.popleft with the declared inputs; why: sample_once invokes self._rows.popleft while performing self error; the call advances that operation through its result or side effect. + self._rows.popleft() + # What: compute error from the named fixture input; why: the enclosing return or state update later reads error, so sample_once must retain the computed value under that name. + self._error = None + + # What: define current around after; why: its direct callers call current for current and rely on this exact input and result contract. + def current(self, *, after: datetime | None = None) -> dict: + # What: compute cutoff from after and timestamp; why: if cutoff is not later reads cutoff, so current must retain the computed value under that name. + cutoff = after.timestamp() if after is not None else None + # What: enter the lock managed context before rows dict row for row in; why: current releases this resource or lock after rows dict row for row in on both success and failure paths. + with self._lock: + # What: compute rows from dict and row and rows; why: rows later reads rows, so current must retain the computed value under that name. + rows = [dict(row) for row in self._rows] + # What: compute state from every s and error and disabled and enabled and every s; why: return state sys stats rows gpu stats later reads state, so current must retain the computed value under that name. + state = { + # What: map the enabled field as disabled; why: PerformanceMonitor.current carries enabled through state into return state sys stats rows gpu stats. + "enabled": not self._disabled, + # What: map the every s field as every s; why: PerformanceMonitor.current carries every s through state into return state sys stats rows gpu stats. + "everyS": self._every_s, + # What: map the retention s field as 3600; why: PerformanceMonitor.current carries retention s through state into return state sys stats rows gpu stats. + "retentionS": 3600, + # What: map the healthy field as error; why: PerformanceMonitor.current carries healthy through state into return state sys stats rows gpu stats. + "healthy": self._error is None, + # What: map the error field as error; why: PerformanceMonitor.current carries error through state into return state sys stats rows gpu stats. + "error": self._error, + # What: complete the state mapping with enabled and every s and retention s and healthy and error; why: PerformanceMonitor.current groups the supplied clauses as one state mapping before its value is consumed. + } + # What: gate on cutoff before rows and row and cutoff and timestamp and fromisoformat; why: current admits rows and row and cutoff and timestamp and fromisoformat only for this predicate and excludes the opposite state. + if cutoff is not None: + # What: compute rows from row and rows and cutoff and timestamp; why: row for row in rows later reads rows, so current must retain the computed value under that name. + rows = [ + # What: apply the row for row in rows portion of rows; why: current uses this clause to evaluate rows as one grouped value. + row for row in rows + # What: call operation.timestamp with the declared inputs; why: current invokes operation.timestamp while performing cutoff; the call advances that operation through its result or side effect. + if datetime.fromisoformat(row["timestamp"].replace("Z", "+00:00")).timestamp() + # What: apply the cutoff portion of rows; why: current uses this clause to evaluate rows as one grouped value. + > cutoff + # What: complete the rows expression with rows row for row in rows if datetime fromisoformat row; why: PerformanceMonitor.current groups the supplied clauses as one rows expression before its value is consumed. + ] + # What: map the sys stats field as rows; why: PerformanceMonitor.current carries sys stats into return {**state, "sys_stats": rows, "gpu_stats": []}. + return {**state, "sys_stats": rows, "gpu_stats": []} + + # What: define _run around stop; why: its direct callers call _run for run and rely on this exact input and result contract. + def _run(self, stop: threading.Event) -> None: + # What: iterate across wait and every s and stop to perform sample once; why: _run repeats the body only while or for the loop header admits an iteration. + while not stop.wait(self._every_s): + # What: call self.sample_once with the declared inputs; why: _run invokes self.sample_once while performing the enclosing return; the call advances that operation through its result or side effect. + self.sample_once() diff --git a/python/freetoken/daemon/proxy.py b/python/freetoken/daemon/proxy.py index 9e382a4d7..9673d2c44 100644 --- a/python/freetoken/daemon/proxy.py +++ b/python/freetoken/daemon/proxy.py @@ -63,6 +63,24 @@ def __init__( def health(self, port: int) -> dict: return self._cached("health", "/health", port) + # What: define an uncached health probe for the active engine port; why: readiness checks must bypass a replaced generation's cached response before accepting the new process. + def fresh_health(self, port: int) -> dict: + """Read this generation, never a cached response from a replaced engine.""" + # What: document read this generation never a cached in the fresh_health docstring; why: introspection and maintainers read this exact docstring fragment to understand fresh health behavior without executing it. + # What: return fetch and port and health from fresh_health; why: fresh_health exposes fetch and port and health so its caller can continue with the function\'s computed outcome. + return self._fetch("/health", port) + + # What: define fresh_readiness around the active port and probe path; why: callers route health probes through fresh_health and fetch other readiness paths directly, preserving generation-local evidence. + def fresh_readiness(self, port: int, path: str) -> dict: + """Probe a validated profile path without reusing prior-generation state.""" + # What: document probe a validated profile path without in the fresh_readiness docstring; why: introspection and maintainers read this exact docstring fragment to understand fresh readiness behavior without executing it. + # What: gate on path before fresh health and port; why: fresh_readiness admits fresh health and port only for this predicate and excludes the opposite state. + if path == "/health": + # What: return fresh health and port from fresh_readiness; why: fresh_readiness exposes fresh health and port so its caller can continue with the function\'s computed outcome. + return self.fresh_health(port) + # What: return fetch and path and port from fresh_readiness; why: fresh_readiness exposes fetch and path and port so its caller can continue with the function\'s computed outcome. + return self._fetch(path, port) + def stats(self, port: int) -> dict: return self._cached("stats", "/v1/stats", port) @@ -114,7 +132,16 @@ def _urlopen(url: str, timeout: float) -> dict: req = urllib.request.Request(url, headers={"Accept": "application/json"}, method="GET") with urllib.request.urlopen(req, timeout=timeout) as resp: raw = resp.read() - return json.loads(raw.decode("utf-8")) + # What: establish the handler boundary for the protected operation; why: ServeProbe._urlopen routes failures to unicode decode error and jsondecode error and json while preserving cleanup and success flow. + try: + # What: return loads and json and decode and raw and utf 8 from _urlopen; why: _urlopen exposes loads and json and decode and raw and utf 8 so its caller can continue with the function\'s computed outcome. + return json.loads(raw.decode("utf-8")) + # What: handle unicode decode error and jsondecode error and json by return; why: ServeProbe._urlopen converts that failure into this concrete recovery, response, or cleanup behavior. + except (UnicodeDecodeError, json.JSONDecodeError): + # A custom readiness endpoint follows HTTP-status semantics. Do + # not retain or surface an arbitrary successful response body. + # What: return no value from _urlopen; why: _urlopen returns no value to callers that depend on its completed result. + return {} @staticmethod def _urlopen_prepare(url: str, timeout: float) -> dict: diff --git a/python/freetoken/daemon/readiness.py b/python/freetoken/daemon/readiness.py new file mode 100644 index 000000000..74b323eeb --- /dev/null +++ b/python/freetoken/daemon/readiness.py @@ -0,0 +1,104 @@ +"""Wait for a newly launched FreeToken serve to report its own readiness. + +The daemon never treats a listening socket as ready. ``/health`` is the +engine's lifecycle authority and reports ``loading``, ``ok``, or ``error``. +This helper intentionally does not kill an engine on timeout: model loads can +be slow, and the existing manager must keep the still-visible process available +for logs, diagnosis, or an explicit operator stop. +""" +# What: document wait for a newly launched free token in the readiness docstring; why: introspection and maintainers read this exact docstring fragment to understand readiness behavior without executing it. +# What: document the daemon never treats a listening in the readiness docstring; why: introspection and maintainers read this exact docstring fragment to understand readiness behavior without executing it. +# What: document engine s lifecycle authority and reports in the readiness docstring; why: introspection and maintainers read this exact docstring fragment to understand readiness behavior without executing it. +# What: document this helper intentionally does not kill in the readiness docstring; why: introspection and maintainers read this exact docstring fragment to understand readiness behavior without executing it. +# What: document be slow and the existing manager in the readiness docstring; why: introspection and maintainers read this exact docstring fragment to understand readiness behavior without executing it. +# What: document for logs diagnosis or an explicit in the readiness docstring; why: introspection and maintainers read this exact docstring fragment to understand readiness behavior without executing it. +# What: preserve the paragraph boundary in the the readiness docstring; why: introspection and maintainers read this paragraph break to understand readiness behavior without executing it. + +# What: enable postponed evaluation of annotations; why: type hints in readiness can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import time for wait for ready using time; why: wait_for_ready uses time monotonic, making that imported dependency available to its named operation. +import time +# What: import any and callable for wait for ready using typing and any and callable; why: wait_for_ready uses the any annotation in wait for ready and the callable annotation in wait for ready, making that imported dependency available to its named operation. +from typing import Any, Callable + + +# What: define wait_for_ready around manager and probe and pid and port and timeout s and path and now and sleep; why: its direct callers call wait_for_ready for wait for ready and rely on this exact input and result contract. +def wait_for_ready( + # What: declare the manager input for wait_for_ready; why: wait_for_ready consumes manager during state manager status, so callers must bind it with the other signature inputs. + manager, + # What: declare the probe input for wait_for_ready; why: wait_for_ready consumes probe during probe fresh health port, so callers must bind it with the other signature inputs. + probe, + # What: mark the remaining parameters as keyword-only; why: wait_for_ready prevents callers from confusing adjacent lifecycle and timing arguments. + *, + # What: declare the pid input for wait_for_ready; why: wait_for_ready consumes pid during if not state get running or pid, so callers must bind it with the other signature inputs. + pid: int | None, + # What: declare the port input for wait_for_ready; why: wait_for_ready consumes port during probe fresh health port, so callers must bind it with the other signature inputs. + port: int, + # What: declare the timeout s input for wait_for_ready; why: wait_for_ready consumes timeout s during deadline now timeout s, so callers must bind it with the other signature inputs. + timeout_s: float, + # What: declare the path input for wait_for_ready; why: wait_for_ready consumes path during if path health, so callers must bind it with the other signature inputs. + path: str = "/health", + # What: declare the now input for wait_for_ready; why: wait_for_ready consumes now during deadline now timeout s, so callers must bind it with the other signature inputs. + now: Callable[[], float] = time.monotonic, + # What: declare the sleep input for wait_for_ready; why: wait_for_ready consumes sleep during sleep min remaining, so callers must bind it with the other signature inputs. + sleep: Callable[[float], None] = time.sleep, +# What: declare a heterogeneous readiness-result mapping; why: callers receive ready, reason, and health fields whose values include booleans, strings, and nested health data. +) -> dict[str, Any]: + # What: compute deadline from timeout s and now; why: remaining deadline now later reads deadline, so wait_for_ready must retain the computed value under that name. + deadline = now() + timeout_s + # What: map the reachable field as false; why: wait_for_ready carries reachable through last into return ready false reason superseded health last. + last: dict[str, Any] = {"reachable": False, "status": "unreachable"} + # What: poll readiness until an explicit terminal condition returns; why: supersession, ready, engine-error, and timeout returns bound this loop despite its unconditional header. + while True: + # What: compute state from status and manager; why: if not state get running or pid later reads state, so wait_for_ready must retain the computed value under that name. + state = manager.status() + # What: gate on get and pid and state before last; why: wait_for_ready admits last only for this predicate and excludes the opposite state. + if not state.get("running") or (pid is not None and state.get("pid") != pid): + # What: map the ready field as false; why: wait_for_ready carries ready into return {"ready": False, "reason": "superseded", "health": last}. + return {"ready": False, "reason": "superseded", "health": last} + # What: compute last from path and fresh health and port and fresh readiness; why: return ready reason superseded health last later reads last, so wait_for_ready must retain the computed value under that name. + last = ( + # What: call probe.fresh_health with port; why: wait_for_ready invokes probe.fresh_health while performing if path health; the call advances that operation through its result or side effect. + probe.fresh_health(port) + # What: apply the if path health portion of last; why: wait_for_ready uses this clause to evaluate last as one grouped value. + if path == "/health" + # What: call probe.fresh_readiness with port and path; why: wait_for_ready consumes the probe.fresh_readiness return value while evaluating else probe.fresh_readiness(port, path). + else probe.fresh_readiness(port, path) + # What: complete the last expression with last probe fresh health port if path equals health else probe fresh readiness; why: wait_for_ready groups the supplied clauses as one last expression before its value is consumed. + ) + # Replacement or exit can happen while the HTTP request is in flight. + # What: compute state from status and manager; why: if not state get running or pid later reads state, so wait_for_ready must retain the computed value under that name. + state = manager.status() + # What: gate on get and pid and state before last; why: wait_for_ready admits last only for this predicate and excludes the opposite state. + if not state.get("running") or (pid is not None and state.get("pid") != pid): + # What: map the ready field as false; why: wait_for_ready carries ready into return {"ready": False, "reason": "superseded", "health": last}. + return {"ready": False, "reason": "superseded", "health": last} + # What: gate on get and last and path before last; why: wait_for_ready admits last only for this predicate and excludes the opposite state. + if last.get("reachable") and ( + # What: apply the path health portion of the enclosing predicate; why: this clause remains in wait_for_ready\'s enclosing expression so its grouping and evaluation order stay intact. + path != "/health" + # What: apply the or portion of the enclosing predicate; why: this clause remains in wait_for_ready\'s enclosing expression so its grouping and evaluation order stay intact. + or ( + # What: call last.get with status; why: wait_for_ready invokes last.get while performing and last get maintenance serving serving; the call advances that operation through its result or side effect. + last.get("status") == "ok" + # What: call last.get with maintenance and serving; why: wait_for_ready consumes the last.get return value while evaluating and last.get("maintenance", "serving") == "serving". + and last.get("maintenance", "serving") == "serving" + # What: complete the enclosing predicate with path differs from health or last get status equals ok; why: wait_for_ready groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) + # What: complete the enclosing predicate with last get reachable and path differs from health or last get; why: wait_for_ready groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: map the ready field as true; why: wait_for_ready carries ready into return {"ready": True, "health": last}. + return {"ready": True, "health": last} + # What: gate on get and last before last; why: wait_for_ready admits last only for this predicate and excludes the opposite state. + if last.get("status") == "error": + # What: map the ready field as false; why: wait_for_ready carries ready into return {"ready": False, "reason": "engine-error", "health": last}. + return {"ready": False, "reason": "engine-error", "health": last} + # What: compute remaining from deadline and now; why: if remaining later reads remaining, so wait_for_ready must retain the computed value under that name. + remaining = deadline - now() + # What: gate on remaining before last; why: wait_for_ready admits last only for this predicate and excludes the opposite state. + if remaining <= 0: + # What: map the ready field as false; why: wait_for_ready carries ready into return {"ready": False, "reason": "timeout", "health": last}. + return {"ready": False, "reason": "timeout", "health": last} + # What: pace the next readiness poll with the injected sleep callback; why: the quarter-second cap avoids busy-waiting while the remaining deadline prevents oversleeping the timeout. + sleep(min(0.25, remaining)) diff --git a/python/freetoken/daemon/router.py b/python/freetoken/daemon/router.py new file mode 100644 index 000000000..b7b0e8998 --- /dev/null +++ b/python/freetoken/daemon/router.py @@ -0,0 +1,1926 @@ +"""Native, transport-independent admission and model activation for freetoken-swap. + +The router owns the decision to retain an already ready engine or to make a +safe lifecycle transition before an inference request is forwarded. It does +not implement HTTP itself: keeping this boundary small makes FIFO priority, +leases, readiness, and rollback directly testable without a model runtime. +""" +# What: document native transport independent admission and model activation in the router docstring; why: introspection and maintainers read this exact docstring fragment to understand router behavior without executing it. +# What: document the router owns the decision to in the router docstring; why: introspection and maintainers read this exact docstring fragment to understand router behavior without executing it. +# What: document safe lifecycle transition before an inference in the router docstring; why: introspection and maintainers read this exact docstring fragment to understand router behavior without executing it. +# What: document not implement http itself keeping this in the router docstring; why: introspection and maintainers read this exact docstring fragment to understand router behavior without executing it. +# What: document leases readiness and rollback directly testable in the router docstring; why: introspection and maintainers read this exact docstring fragment to understand router behavior without executing it. +# What: preserve the paragraph boundary in the the router docstring; why: introspection and maintainers read this paragraph break to understand router behavior without executing it. + +# What: enable postponed evaluation of annotations; why: type hints in router can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import threading for init using threading; why: __init__ uses threading condition, making that imported dependency available to its named operation. +import threading +# What: import socket for allocate loopback port using socket; why: allocate_loopback_port uses socket socket, making that imported dependency available to its named operation. +import socket +# What: import time for acquire using time; why: acquire uses time monotonic, making that imported dependency available to its named operation. +import time +# What: import dataclass and field for module initialization using dataclasses and dataclass and field; why: module initialization uses the dataclass annotation in module initialization and field, making that imported dependency available to its named operation. +from dataclasses import dataclass, field +# What: import callable for init using typing and callable; why: __init__ uses the callable annotation in init, making that imported dependency available to its named operation. +from typing import Callable + +# What: import from catalog import DEFAULT CHECK ENDPOINT CatalogError ModelCatalog ModelProfile; why: this module calls or annotates these symbols in the branch-created operations below. +from .catalog import DEFAULT_CHECK_ENDPOINT, CatalogError, ModelCatalog, ModelProfile +# What: import wait for ready for init using readiness and wait for ready; why: __init__ uses the wait for ready annotation in init, making that imported dependency available to its named operation. +from .readiness import wait_for_ready +# What: import conflict and switch launch error for acquire using serve manager and conflict and switch launch error; why: acquire uses the conflict annotation in acquire and the switch launch error annotation in acquire, making that imported dependency available to its named operation. +from .serve_manager import Conflict, SwitchLaunchError + + +# What: compute default profile concurrency limit from 10; why: default profile concurrency limit default profile concurrency limit later reads default profile concurrency limit, so router must retain the computed value under that name. +DEFAULT_PROFILE_CONCURRENCY_LIMIT = 10 + + +# What: define allocate_loopback_port around the current object state; why: its direct callers call allocate_loopback_port for allocate loopback port and rely on this exact input and result contract. +def allocate_loopback_port() -> int: + """Ask the kernel for an ephemeral loopback TCP port. + + The listener is intentionally closed before the child starts: FreeToken's + serve process, not the daemon, must own the listening socket. The manager + serializes the immediately following launch; a hostile or unrelated local + process can still win that unavoidable bind race, in which case readiness + fails closed and the normal rollback path applies. + """ + # What: document ask the kernel for an ephemeral in the allocate_loopback_port docstring; why: introspection and maintainers read this exact docstring fragment to understand allocate loopback port behavior without executing it. + # What: document the listener is intentionally closed before in the allocate_loopback_port docstring; why: introspection and maintainers read this exact docstring fragment to understand allocate loopback port behavior without executing it. + # What: document serve process not the daemon must in the allocate_loopback_port docstring; why: introspection and maintainers read this exact docstring fragment to understand allocate loopback port behavior without executing it. + # What: document serializes the immediately following launch a in the allocate_loopback_port docstring; why: introspection and maintainers read this exact docstring fragment to understand allocate loopback port behavior without executing it. + # What: document process can still win that unavoidable in the allocate_loopback_port docstring; why: introspection and maintainers read this exact docstring fragment to understand allocate loopback port behavior without executing it. + # What: document fails closed and the normal rollback in the allocate_loopback_port docstring; why: introspection and maintainers read this exact docstring fragment to understand allocate loopback port behavior without executing it. + # What: preserve the paragraph boundary in the the allocate_loopback_port docstring; why: introspection and maintainers read this paragraph break to understand allocate loopback port behavior without executing it. + # What: enter the socket.socket managed context before sock setsockopt socket sol socket socket so reuseaddr; why: allocate_loopback_port releases this resource or lock after sock setsockopt socket sol socket socket so reuseaddr on both success and failure paths. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + # What: call sock.setsockopt with sol socket and socket and so reuseaddr and socket and 0; why: allocate_loopback_port invokes sock.setsockopt while performing sock bind; the call advances that operation through its result or side effect. + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 0) + # What: preserve the exact sock bind literal fragment; why: allocate_loopback_port passes this fragment verbatim through sock.bind(("127.0.0.1", 0)), because changing it would alter a protocol payload, serialized fixture, or public message. + sock.bind(("127.0.0.1", 0)) + # What: return int and getsockname and sock and 1 from allocate_loopback_port; why: allocate_loopback_port exposes int and getsockname and sock and 1 so its caller can continue with the function\'s computed outcome. + return int(sock.getsockname()[1]) + + +# What: define RoutingError as the owner of __init__; why: daemon callers use this class boundary so those methods share one routing error state invariant. +class RoutingError(RuntimeError): + """A request could not be admitted to a ready native engine.""" +# What: document a request could not be admitted in the RoutingError docstring; why: introspection and maintainers read this exact docstring fragment to understand routing error behavior without executing it. + + # What: define __init__ around code and detail and status code and recovery; why: its direct callers call __init__ for init and rely on this exact input and result contract. + def __init__(self, code: str, detail: str, *, status_code: int = 503, recovery: dict | None = None): + # What: call operation.__init__ with detail; why: __init__ invokes operation.__init__ while performing self code code; the call advances that operation through its result or side effect. + super().__init__(detail) + # What: compute code from code; why: the enclosing return or state update later reads code, so __init__ must retain the computed value under that name. + self.code = code + # What: compute status code from status code; why: the enclosing return or state update later reads status code, so __init__ must retain the computed value under that name. + self.status_code = status_code + # What: compute recovery from recovery; why: the enclosing return or state update later reads recovery, so __init__ must retain the computed value under that name. + self.recovery = recovery + + +# What: generate dataclass initialization and value semantics for RouteLease; why: RouteLease acts as a typed state record with consistent construction, comparison, and representation. +@dataclass +# What: define RouteLease as the owner of release and proxy_base_url; why: daemon callers use this class boundary so those methods share one route lease state invariant. +class RouteLease: + """One admitted request. Call :meth:`release` exactly once when it ends.""" +# What: document one admitted request call meth release in the RouteLease docstring; why: introspection and maintainers read this exact docstring fragment to understand route lease behavior without executing it. + + # What: compute router from the named fixture input; why: self router release later reads router, so router must retain the computed value under that name. + router: "RoutingCoordinator" + # What: compute profile from the named fixture input; why: return self profile proxy base url self port later reads profile, so router must retain the computed value under that name. + profile: ModelProfile + # What: compute port from the named fixture input; why: return self profile proxy base url self port later reads port, so router must retain the computed value under that name. + port: int + # What: compute pid from the named fixture input; why: state get pid later reads pid, so router must retain the computed value under that name. + pid: int | None + # What: compute model id from the named fixture input; why: model id profile selector id routing profile id pin id later reads model id, so router must retain the computed value under that name. + model_id: str | None = None + # What: compute selector id from the named fixture input; why: model id profile selector id routing profile id pin id later reads selector id, so router must retain the computed value under that name. + selector_id: str | None = None + # What: compute routing profile id from the named fixture input; why: model id profile selector id routing profile id pin id later reads routing profile id, so router must retain the computed value under that name. + routing_profile_id: str | None = None + # What: compute pin id from the named fixture input; why: model id profile selector id routing profile id pin id later reads pin id, so router must retain the computed value under that name. + pin_id: str | None = None + # What: compute released from field and false and false and false; why: if lease released or self leases later reads released, so router must retain the computed value under that name. + _released: bool = field(default=False, init=False, repr=False) + + # What: define release around the current object state; why: its direct callers call release for release and rely on this exact input and result contract. + def release(self) -> None: + # What: call self.router.release with the named fixture input; why: release invokes self.router.release while performing the enclosing return; the call advances that operation through its result or side effect. + self.router.release(self) + + # What: expose proxy_base_url as a read-only computed property; why: callers read proxy_base_url through attribute access while its getter retains control of the derived value. + @property + # What: define proxy_base_url around the current object state; why: the registered API client call proxy_base_url for proxy base url and rely on this exact input and result contract. + def proxy_base_url(self) -> str: + # What: return proxy base url and port and profile from proxy_base_url; why: proxy_base_url exposes proxy base url and port and profile so its caller can continue with the function\'s computed outcome. + return self.profile.proxy_base_url(self.port) + + +# What: define RoutingCoordinator as the owner of __init__ and _adopt_exact_catalog_resident and acquire and cancel_acquire and queue_position; why: daemon callers use this class boundary so those methods share one routing coordinator state invariant. +class RoutingCoordinator: + """Serialize unsafe swaps while allowing concurrent requests for one engine. + + A higher profile priority wins over lower priority requests that have not + begun an activation. Equal priorities use strict FIFO ordering. A swap is + never started while an admitted lease exists, which preserves streaming + requests and their cancellation semantics. + """ +# What: document serialize unsafe swaps while allowing concurrent in the RoutingCoordinator docstring; why: introspection and maintainers read this exact docstring fragment to understand routing coordinator behavior without executing it. +# What: document a higher profile priority wins over in the RoutingCoordinator docstring; why: introspection and maintainers read this exact docstring fragment to understand routing coordinator behavior without executing it. +# What: document begun an activation equal priorities use in the RoutingCoordinator docstring; why: introspection and maintainers read this exact docstring fragment to understand routing coordinator behavior without executing it. +# What: document never started while an admitted lease in the RoutingCoordinator docstring; why: introspection and maintainers read this exact docstring fragment to understand routing coordinator behavior without executing it. +# What: document requests and their cancellation semantics in the RoutingCoordinator docstring; why: introspection and maintainers read this exact docstring fragment to understand routing coordinator behavior without executing it. +# What: preserve the paragraph boundary in the the RoutingCoordinator docstring; why: introspection and maintainers read this paragraph break to understand routing coordinator behavior without executing it. + + # What: define __init__ around manager and catalog and probe and default port and ready fn and timer factory and port allocator; why: its direct callers call __init__ for init and rely on this exact input and result contract. + def __init__( + # What: declare the self input for __init__; why: __init__ consumes self during self manager manager, so callers must bind it with the other signature inputs. + self, + # What: declare the manager input for __init__; why: __init__ consumes manager during self manager manager, so callers must bind it with the other signature inputs. + manager, + # What: declare the catalog input for __init__; why: __init__ consumes catalog during self catalog catalog, so callers must bind it with the other signature inputs. + catalog: ModelCatalog, + # What: declare the probe input for __init__; why: __init__ consumes probe during self probe probe, so callers must bind it with the other signature inputs. + probe, + # What: mark the remaining parameters as keyword-only; why: __init__ prevents callers from confusing adjacent lifecycle and timing arguments. + *, + # What: declare the default port input for __init__; why: __init__ consumes default port during self default port default port, so callers must bind it with the other signature inputs. + default_port: int = 1919, + # What: declare the ready fn input for __init__; why: __init__ consumes ready fn during self ready fn ready fn, so callers must bind it with the other signature inputs. + ready_fn: Callable = wait_for_ready, + # What: declare the timer factory input for __init__; why: __init__ consumes timer factory during self timer factory timer factory or self new timer, so callers must bind it with the other signature inputs. + timer_factory: Callable[[float, Callable[[], None]], object] | None = None, + # What: declare the port allocator input for __init__; why: __init__ consumes port allocator during self port allocator port allocator, so callers must bind it with the other signature inputs. + port_allocator: Callable[[], int] = allocate_loopback_port, + # What: complete the enclosing predicate with group delimiter; why: RoutingCoordinator.__init__ groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> None: + # What: compute manager from manager; why: the enclosing return or state update later reads manager, so __init__ must retain the computed value under that name. + self._manager = manager + # What: compute catalog from catalog; why: the enclosing return or state update later reads catalog, so __init__ must retain the computed value under that name. + self._catalog = catalog + # What: compute probe from probe; why: the enclosing return or state update later reads probe, so __init__ must retain the computed value under that name. + self._probe = probe + # What: compute default port from default port; why: the enclosing return or state update later reads default port, so __init__ must retain the computed value under that name. + self._default_port = default_port + # What: compute ready fn from ready fn; why: the enclosing return or state update later reads ready fn, so __init__ must retain the computed value under that name. + self._ready_fn = ready_fn + # What: compute timer factory from timer factory and new timer; why: the enclosing return or state update later reads timer factory, so __init__ must retain the computed value under that name. + self._timer_factory = timer_factory or self._new_timer + # What: compute port allocator from port allocator; why: the enclosing return or state update later reads port allocator, so __init__ must retain the computed value under that name. + self._port_allocator = port_allocator + # What: compute cond from condition and threading and lock; why: the enclosing return or state update later reads cond, so __init__ must retain the computed value under that name. + self._cond = threading.Condition(threading.Lock()) + # What: compute next sequence from 0; why: the enclosing return or state update later reads next sequence, so __init__ must retain the computed value under that name. + self._next_sequence = 0 + # What: initialize pending as an empty runtime accumulator; why: RoutingCoordinator.__init__ appends or maps entries into it during the enclosing return or state update before consuming the aggregate. + self._pending: list[tuple[int, int, str]] = [] + # What: initialize pending by cancellation as an empty runtime accumulator; why: RoutingCoordinator.__init__ appends or maps entries into it during the enclosing return or state update before consuming the aggregate. + self._pending_by_cancellation: dict[ + # What: apply the threading event tuple tuple int int str portion of pending by cancellation; why: __init__ uses this clause to evaluate pending by cancellation as one grouped value. + threading.Event, tuple[tuple[int, int, str], ModelProfile] + # What: apply the grouped expression portion of pending by cancellation; why: __init__ uses this clause to evaluate pending by cancellation as one grouped value. + ] = {} + # What: compute leases from 0; why: the enclosing return or state update later reads leases, so __init__ must retain the computed value under that name. + self._leases = 0 + # What: compute reservations from 0; why: the enclosing return or state update later reads reservations, so __init__ must retain the computed value under that name. + self._reservations = 0 + # What: initialize profile reservations as an empty runtime accumulator; why: RoutingCoordinator.__init__ appends or maps entries into it during the enclosing return or state update before consuming the aggregate. + self._profile_reservations: dict[str, int] = {} + # What: compute active name from the named fixture input; why: the enclosing return or state update later reads active name, so __init__ must retain the computed value under that name. + self._active_name: str | None = None + # What: compute active routing profile from the named fixture input; why: the enclosing return or state update later reads active routing profile, so __init__ must retain the computed value under that name. + self._active_routing_profile: str | None = None + # What: compute activating name from the named fixture input; why: the enclosing return or state update later reads activating name, so __init__ must retain the computed value under that name. + self._activating_name: str | None = None + # What: compute activating port from the named fixture input; why: the enclosing return or state update later reads activating port, so __init__ must retain the computed value under that name. + self._activating_port: int | None = None + # What: compute switching from false; why: the enclosing return or state update later reads switching, so __init__ must retain the computed value under that name. + self._switching = False + # What: compute manual lifecycle owner from the named fixture input; why: the enclosing return or state update later reads manual lifecycle owner, so __init__ must retain the computed value under that name. + self._manual_lifecycle_owner: object | None = None + # What: compute manual lifecycle tokens from set; why: the enclosing return or state update later reads manual lifecycle tokens, so __init__ must retain the computed value under that name. + self._manual_lifecycle_tokens: set[object] = set() + # What: compute shutdown requested from false; why: the enclosing return or state update later reads shutdown requested, so __init__ must retain the computed value under that name. + self._shutdown_requested = False + # What: compute shutdown owner from the named fixture input; why: the enclosing return or state update later reads shutdown owner, so __init__ must retain the computed value under that name. + self._shutdown_owner: object | None = None + # What: compute idle timer from the named fixture input; why: the enclosing return or state update later reads idle timer, so __init__ must retain the computed value under that name. + self._idle_timer: object | None = None + # What: compute evictions from 0; why: the enclosing return or state update later reads evictions, so __init__ must retain the computed value under that name. + self._evictions = 0 + # What: compute admissions from 0; why: the enclosing return or state update later reads admissions, so __init__ must retain the computed value under that name. + self._admissions = 0 + # What: compute activations from 0; why: the enclosing return or state update later reads activations, so __init__ must retain the computed value under that name. + self._activations = 0 + # What: compute activation failures from 0; why: the enclosing return or state update later reads activation failures, so __init__ must retain the computed value under that name. + self._activation_failures = 0 + # What: compute cancellations from 0; why: the enclosing return or state update later reads cancellations, so __init__ must retain the computed value under that name. + self._cancellations = 0 + # What: compute terminal streams from 0; why: the enclosing return or state update later reads terminal streams, so __init__ must retain the computed value under that name. + self._terminal_streams = 0 + # What: compute last ttft ms from the named fixture input; why: the enclosing return or state update later reads last ttft ms, so __init__ must retain the computed value under that name. + self._last_ttft_ms: float | None = None + # What: compute last duration ms from the named fixture input; why: the enclosing return or state update later reads last duration ms, so __init__ must retain the computed value under that name. + self._last_duration_ms: float | None = None + # What: compute last activation ms from the named fixture input; why: the enclosing return or state update later reads last activation ms, so __init__ must retain the computed value under that name. + self._last_activation_ms: float | None = None + # What: compute last queue wait ms from the named fixture input; why: the enclosing return or state update later reads last queue wait ms, so __init__ must retain the computed value under that name. + self._last_queue_wait_ms: float | None = None + # What: compute last response bytes from the named fixture input; why: the enclosing return or state update later reads last response bytes, so __init__ must retain the computed value under that name. + self._last_response_bytes: int | None = None + # What: compute last proxy bytes per second from the named fixture input; why: the enclosing return or state update later reads last proxy bytes per second, so __init__ must retain the computed value under that name. + self._last_proxy_bytes_per_second: float | None = None + # What: call self._adopt_exact_catalog_resident with the declared inputs; why: __init__ invokes self._adopt_exact_catalog_resident while performing the enclosing return; the call advances that operation through its result or side effect. + self._adopt_exact_catalog_resident() + + # What: define _adopt_exact_catalog_resident around the current object state; why: its direct callers call _adopt_exact_catalog_resident for adopt exact catalog resident and rely on this exact input and result contract. + def _adopt_exact_catalog_resident(self) -> None: + """Bind one unambiguous catalog profile to a manager-re-adopted engine. + + Omitted ports match the configured default and dynamic-port profiles + match the concrete persisted port. If multiple profiles describe the + same process identity, fail closed rather than inventing which one owns + residency. + """ + # What: document bind one unambiguous catalog profile to in the _adopt_exact_catalog_resident docstring; why: introspection and maintainers read this exact docstring fragment to understand adopt exact catalog resident behavior without executing it. + # What: document omitted ports match the configured default in the _adopt_exact_catalog_resident docstring; why: introspection and maintainers read this exact docstring fragment to understand adopt exact catalog resident behavior without executing it. + # What: document match the concrete persisted port if in the _adopt_exact_catalog_resident docstring; why: introspection and maintainers read this exact docstring fragment to understand adopt exact catalog resident behavior without executing it. + # What: document same process identity fail closed rather in the _adopt_exact_catalog_resident docstring; why: introspection and maintainers read this exact docstring fragment to understand adopt exact catalog resident behavior without executing it. + # What: document residency in the _adopt_exact_catalog_resident docstring; why: introspection and maintainers read this exact docstring fragment to understand adopt exact catalog resident behavior without executing it. + # What: preserve the paragraph boundary in the the _adopt_exact_catalog_resident docstring; why: introspection and maintainers read this paragraph break to understand adopt exact catalog resident behavior without executing it. + # What: compute state from status and manager; why: port state get port later reads state, so _adopt_exact_catalog_resident must retain the computed value under that name. + state = self._manager.status() + # What: compute port from get and state and port; why: if not state get running or not later reads port, so _adopt_exact_catalog_resident must retain the computed value under that name. + port = state.get("port") + # What: gate on port and get and isinstance and int and state before the computed value; why: _adopt_exact_catalog_resident admits the computed value only for this predicate and excludes the opposite state. + if not state.get("running") or not isinstance(port, int) or port <= 0: + # What: return no value from _adopt_exact_catalog_resident; why: _adopt_exact_catalog_resident returns no value to callers that depend on its completed result. + return + # What: compute args from serve args and manager; why: and list profile args args later reads args, so _adopt_exact_catalog_resident must retain the computed value under that name. + args = self._manager.serve_args() + # What: compute matches from profile and profiles and catalog and model; why: if len matches later reads matches, so _adopt_exact_catalog_resident must retain the computed value under that name. + matches = [ + # What: call self._catalog.profiles with the declared inputs; why: _adopt_exact_catalog_resident invokes self._catalog.profiles while performing if profile model state get model; the call advances that operation through its result or side effect. + profile for profile in self._catalog.profiles() + # What: call state.get with model; why: _adopt_exact_catalog_resident invokes state.get while performing and; the call advances that operation through its result or side effect. + if profile.model == state.get("model") + # What: apply the and portion of matches; why: _adopt_exact_catalog_resident uses this clause to evaluate matches as one grouped value. + and ( + # What: apply the profile port port portion of matches; why: _adopt_exact_catalog_resident uses this clause to evaluate matches as one grouped value. + profile.port == port + # What: apply the or profile port portion of matches; why: _adopt_exact_catalog_resident uses this clause to evaluate matches as one grouped value. + or profile.port == 0 + # What: apply the or profile port is and port self default port portion of matches; why: _adopt_exact_catalog_resident uses this clause to evaluate matches as one grouped value. + or (profile.port is None and port == self._default_port) + # What: complete the matches expression with profile model equals state get model and profile port equals port or; why: RoutingCoordinator._adopt_exact_catalog_resident groups the supplied clauses as one matches expression before its value is consumed. + ) + # What: call list with args and profile; why: _adopt_exact_catalog_resident consumes the list return value while evaluating and list(profile.args) == args. + and list(profile.args) == args + # What: complete the matches expression with matches profile for profile in self catalog profiles if profile model equals; why: RoutingCoordinator._adopt_exact_catalog_resident groups the supplied clauses as one matches expression before its value is consumed. + ] + # What: gate on len and matches before the computed value; why: _adopt_exact_catalog_resident admits the computed value only for this predicate and excludes the opposite state. + if len(matches) != 1: + # What: return no value from _adopt_exact_catalog_resident; why: _adopt_exact_catalog_resident returns no value to callers that depend on its completed result. + return + # What: compute active name from name and matches and 0; why: the enclosing return or state update later reads active name, so _adopt_exact_catalog_resident must retain the computed value under that name. + self._active_name = matches[0].name + # What: call self._schedule_idle_eviction with the declared inputs; why: _adopt_exact_catalog_resident invokes self._schedule_idle_eviction while performing the enclosing return; the call advances that operation through its result or side effect. + self._schedule_idle_eviction() + + # What: define acquire around name and cancellation and on reserved and apply loading policy and apply routing profile; why: its direct callers call acquire for acquire and rely on this exact input and result contract. + def acquire( + # What: declare the self input for acquire; why: acquire consumes self while evaluating self, so callers must bind it with the other signature inputs. + self, + # What: declare the name input for acquire; why: acquire consumes name during name apply routing profile apply routing profile, so callers must bind it with the other signature inputs. + name: str, + # What: declare the cancellation input for acquire; why: acquire consumes cancellation during if cancellation is not, so callers must bind it with the other signature inputs. + cancellation: threading.Event | None = None, + # What: declare the on reserved input for acquire; why: acquire consumes on reserved during if on reserved is not, so callers must bind it with the other signature inputs. + on_reserved: Callable[[bool, int], None] | None = None, + # What: mark the remaining parameters as keyword-only; why: acquire prevents callers from confusing adjacent lifecycle and timing arguments. + *, + # What: declare the apply loading policy input for acquire; why: acquire consumes apply loading policy during on reserved loading enabled and cold if apply loading policy, so callers must bind it with the other signature inputs. + apply_loading_policy: bool = False, + # What: declare the apply routing profile input for acquire; why: acquire consumes apply routing profile during name apply routing profile apply routing profile, so callers must bind it with the other signature inputs. + apply_routing_profile: bool = True, + # What: complete the enclosing predicate with route lease; why: RoutingCoordinator.acquire groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> RouteLease: + """Return a lease only after *name* has a health-verified engine.""" + # What: document return a lease only after name in the acquire docstring; why: introspection and maintainers read this exact docstring fragment to understand acquire behavior without executing it. + # What: compute queued at from monotonic and time; why: self last queue wait ms round time monotonic queued at later reads queued at, so acquire must retain the computed value under that name. + queued_at = time.monotonic() + # What: enter the cond managed context before if self shutdown requested; why: acquire releases this resource or lock after if self shutdown requested on both success and failure paths. + with self._cond: + # What: gate on shutdown requested before routing error; why: acquire admits routing error only for this predicate and excludes the opposite state. + if self._shutdown_requested: + # What: raise RoutingError for the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: supply status code to RoutingError; why: acquire binds this 503 value to RoutingError's status code input. + "router_shutting_down", "router shutdown is in progress", status_code=503 + # What: complete the RoutingError call with status code; why: RoutingCoordinator.acquire groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.acquire routes failures to catalog error while preserving cleanup and success flow. + try: + # What: evaluate and capture model id profile selector id routing profile id pin id; why: the enclosing qualifier uses the captured result in its next validation or artifact step. + model_id, profile, selector_id, routing_profile_id, pin_id = ( + # What: call self._resolve_request_locked with name; why: acquire invokes self._resolve_request_locked while performing name apply routing profile apply routing profile; the call advances that operation through its result or side effect. + self._resolve_request_locked( + # What: supply apply routing profile to self._resolve_request_locked; why: acquire binds this apply routing profile value to self._resolve_request_locked's apply routing profile input. + name, apply_routing_profile=apply_routing_profile + # What: complete the self._resolve_request_locked call with apply routing profile; why: RoutingCoordinator.acquire groups the supplied clauses as one self._resolve_request_locked call before its value is consumed. + ) + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + ) + # What: handle catalog error by raise routing error unknown model str exc status code 404; why: RoutingCoordinator.acquire converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError as exc: + # What: raise RoutingError for the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError("unknown_model", str(exc), status_code=404) from exc + # What: gate on cancellation and is set before routing error; why: acquire admits routing error only for this predicate and excludes the opposite state. + if cancellation is not None and cancellation.is_set(): + # What: raise RoutingError for the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: supply status code to RoutingError; why: acquire binds this 409 value to RoutingError's status code input. + "request_cancelled", "request cancelled before admission", status_code=409 + # What: complete the RoutingError call with status code; why: RoutingCoordinator.acquire groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: call self._reserve_concurrency_locked with profile; why: acquire invokes self._reserve_concurrency_locked while performing ticket profile priority self next sequence profile name; the call advances that operation through its result or side effect. + self._reserve_concurrency_locked(profile) + # What: compute ticket from next sequence and name and priority and profile; why: self pending append ticket later reads ticket, so acquire must retain the computed value under that name. + ticket = (-profile.priority, self._next_sequence, profile.name) + # What: compute next sequence from 1; why: the enclosing return or state update later reads next sequence, so acquire must retain the computed value under that name. + self._next_sequence += 1 + # What: call self._pending.append with ticket; why: acquire invokes self._pending.append while performing if cancellation is not; the call advances that operation through its result or side effect. + self._pending.append(ticket) + # What: gate on cancellation before pending by cancellation and cancellation and ticket and profile; why: acquire admits pending by cancellation and cancellation and ticket and profile only for this predicate and excludes the opposite state. + if cancellation is not None: + # What: compute pending by cancellation entry from ticket and profile; why: the enclosing return or state update later reads pending by cancellation entry, so acquire must retain the computed value under that name. + self._pending_by_cancellation[cancellation] = (ticket, profile) + # What: gate on on reserved before position and loading enabled and cold and base exception and send loading state; why: acquire admits position and loading enabled and cold and base exception and send loading state only for this predicate and excludes the opposite state. + if on_reserved is not None: + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.acquire routes failures to base exception while preserving cleanup and success flow. + try: + # What: compute position from index and ticket and sorted and pending and 1; why: on reserved loading enabled and cold if apply loading policy later reads position, so acquire must retain the computed value under that name. + position = sorted(self._pending).index(ticket) + 1 + # What: compute loading enabled from send loading state and profile and settings and catalog; why: on reserved loading enabled and cold if apply loading policy later reads loading enabled, so acquire must retain the computed value under that name. + loading_enabled = ( + # What: apply the profile send loading state portion of loading enabled; why: acquire uses this clause to evaluate loading enabled as one grouped value. + profile.send_loading_state + # What: apply the if profile send loading state is not portion of loading enabled; why: acquire uses this clause to evaluate loading enabled as one grouped value. + if profile.send_loading_state is not None + # What: apply the else self catalog settings send loading state portion of loading enabled; why: acquire uses this clause to evaluate loading enabled as one grouped value. + else self._catalog.settings.send_loading_state + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + ) + # What: compute cold from active profile ready locked and profile; why: on reserved loading enabled and cold if apply loading policy later reads cold, so acquire must retain the computed value under that name. + cold = not self._active_profile_ready_locked(profile) + # What: call on_reserved with apply loading policy and cold and loading enabled and position; why: acquire invokes on_reserved while performing except base exception; the call advances that operation through its result or side effect. + on_reserved(loading_enabled and cold if apply_loading_policy else cold, position) + # What: handle base exception by self remove pending locked ticket cancellation; why: RoutingCoordinator.acquire converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: call self._remove_pending_locked with ticket and cancellation; why: acquire invokes self._remove_pending_locked while performing self drop concurrency reservation locked profile; the call advances that operation through its result or side effect. + self._remove_pending_locked(ticket, cancellation) + # What: call self._drop_concurrency_reservation_locked with profile; why: acquire invokes self._drop_concurrency_reservation_locked while performing self cond notify all; the call advances that operation through its result or side effect. + self._drop_concurrency_reservation_locked(profile) + # What: call self._cond.notify_all with the declared inputs; why: acquire invokes self._cond.notify_all while performing raise; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: re-propagate the active failure to the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: iterate across the computed value to perform shutdown requested and remove pending locked and ticket and cancellation and notify all; why: acquire repeats the body only while or for the loop header admits an iteration. + while True: + # What: execute if self shutdown requested; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + if self._shutdown_requested: + # What: gate on remove pending locked and ticket and cancellation before drop concurrency reservation locked and profile; why: acquire admits drop concurrency reservation locked and profile only for this predicate and excludes the opposite state. + if self._remove_pending_locked(ticket, cancellation): + # What: call self._drop_concurrency_reservation_locked with profile; why: acquire invokes self._drop_concurrency_reservation_locked while performing self cond notify all; the call advances that operation through its result or side effect. + self._drop_concurrency_reservation_locked(profile) + # What: call self._cond.notify_all with the declared inputs; why: acquire invokes self._cond.notify_all while performing raise routing error; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: raise RoutingError for the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: supply status code to RoutingError; why: acquire binds this 503 value to RoutingError's status code input. + "router_shutting_down", "router shutdown is in progress", status_code=503 + # What: complete the RoutingError call with status code; why: RoutingCoordinator.acquire groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: execute if cancellation is not None and cancellation is set; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + if cancellation is not None and cancellation.is_set(): + # What: gate on remove pending locked and ticket and cancellation before drop concurrency reservation locked and profile; why: acquire admits drop concurrency reservation locked and profile only for this predicate and excludes the opposite state. + if self._remove_pending_locked(ticket, cancellation): + # What: call self._drop_concurrency_reservation_locked with profile; why: acquire invokes self._drop_concurrency_reservation_locked while performing self cond notify all; the call advances that operation through its result or side effect. + self._drop_concurrency_reservation_locked(profile) + # What: call self._cond.notify_all with the declared inputs; why: acquire invokes self._cond.notify_all while performing raise routing error; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: raise RoutingError for the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: supply status code to RoutingError; why: acquire binds this 409 value to RoutingError's status code input. + "request_cancelled", "request cancelled before admission", status_code=409 + # What: complete the RoutingError call with status code; why: RoutingCoordinator.acquire groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: compute head from min and pending; why: if ticket head later reads head, so acquire must retain the computed value under that name. + head = min(self._pending) + # What: gate on ticket and head before wait and cond; why: acquire admits wait and cond only for this predicate and excludes the opposite state. + if ticket != head: + # What: call self._cond.wait with the declared inputs; why: acquire invokes self._cond.wait while performing continue; the call advances that operation through its result or side effect. + self._cond.wait() + # What: apply the continue portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: gate on switching before wait and cond; why: acquire admits wait and cond only for this predicate and excludes the opposite state. + if self._switching: + # What: call self._cond.wait with the declared inputs; why: acquire invokes self._cond.wait while performing continue; the call advances that operation through its result or side effect. + self._cond.wait() + # What: apply the continue portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.acquire routes failures to base exception while preserving cleanup and success flow. + try: + # Dynamic binding happens only for the head ticket. Other + # cold requests then reuse the committed resident target. + # What: compute port from port for and profile; why: if self matches active profile port later reads port, so acquire must retain the computed value under that name. + port = self._port_for(profile) + # What: handle base exception by self remove pending locked ticket cancellation; why: RoutingCoordinator.acquire converts that failure into this concrete recovery, response, or cleanup behavior. + except BaseException: + # What: call self._remove_pending_locked with ticket and cancellation; why: acquire invokes self._remove_pending_locked while performing self drop concurrency reservation locked profile; the call advances that operation through its result or side effect. + self._remove_pending_locked(ticket, cancellation) + # What: call self._drop_concurrency_reservation_locked with profile; why: acquire invokes self._drop_concurrency_reservation_locked while performing self cond notify all; the call advances that operation through its result or side effect. + self._drop_concurrency_reservation_locked(profile) + # What: call self._cond.notify_all with the declared inputs; why: acquire invokes self._cond.notify_all while performing raise; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: re-propagate the active failure to the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: gate on matches active and profile and port before cancel idle timer; why: acquire admits cancel idle timer only for this predicate and excludes the opposite state. + if self._matches_active(profile, port): + # What: call self._cancel_idle_timer with the declared inputs; why: acquire invokes self._cancel_idle_timer while performing self remove pending locked ticket cancellation; the call advances that operation through its result or side effect. + self._cancel_idle_timer() + # What: call self._remove_pending_locked with ticket and cancellation; why: acquire invokes self._remove_pending_locked while performing self leases; the call advances that operation through its result or side effect. + self._remove_pending_locked(ticket, cancellation) + # What: compute leases from 1; why: if self leases later reads leases, so acquire must retain the computed value under that name. + self._leases += 1 + # What: compute admissions from 1; why: self admissions later reads admissions, so acquire must retain the computed value under that name. + self._admissions += 1 + # What: compute last queue wait ms from round and queued at and monotonic and time and 3; why: self last queue wait ms round activated at queued at later reads last queue wait ms, so acquire must retain the computed value under that name. + self._last_queue_wait_ms = round((time.monotonic() - queued_at) * 1000, 3) + # What: compute state from status and manager; why: state get pid later reads state, so acquire must retain the computed value under that name. + state = self._manager.status() + # What: call self._cond.notify_all with the declared inputs; why: acquire invokes self._cond.notify_all while performing return route lease; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: return route lease and profile and port and get from acquire; why: acquire exposes route lease and profile and port and get so its caller can continue with the function\'s computed outcome. + return RouteLease( + # What: apply the grouped expression portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + self, + # What: apply the profile portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + profile, + # What: apply the port portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + port, + # What: call state.get with pid; why: acquire invokes state.get while performing model id model id; the call advances that operation through its result or side effect. + state.get("pid"), + # What: supply model id to RouteLease; why: acquire binds this model id value to RouteLease's model id input. + model_id=model_id, + # What: supply selector id to RouteLease; why: acquire binds this selector id value to RouteLease's selector id input. + selector_id=selector_id, + # What: supply routing profile id to RouteLease; why: acquire binds this routing profile id value to RouteLease's routing profile id input. + routing_profile_id=routing_profile_id, + # What: supply pin id to RouteLease; why: acquire binds this pin id value to RouteLease's pin id input. + pin_id=pin_id, + # What: complete the RouteLease call with model id and selector id and routing profile id and pin id; why: RoutingCoordinator.acquire groups the supplied clauses as one RouteLease call before its value is consumed. + ) + # What: gate on leases before wait and cond; why: acquire admits wait and cond only for this predicate and excludes the opposite state. + if self._leases: + # What: call self._cond.wait with the declared inputs; why: acquire invokes self._cond.wait while performing continue; the call advances that operation through its result or side effect. + self._cond.wait() + # What: apply the continue portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + continue + # What: compute block from capacity block and profile; why: if block is not later reads block, so acquire must retain the computed value under that name. + block = self._capacity_block(profile) + # What: gate on block before remove pending locked and ticket and cancellation; why: acquire admits remove pending locked and ticket and cancellation only for this predicate and excludes the opposite state. + if block is not None: + # What: call self._remove_pending_locked with ticket and cancellation; why: acquire invokes self._remove_pending_locked while performing self drop concurrency reservation locked profile; the call advances that operation through its result or side effect. + self._remove_pending_locked(ticket, cancellation) + # What: call self._drop_concurrency_reservation_locked with profile; why: acquire invokes self._drop_concurrency_reservation_locked while performing self cond notify all; the call advances that operation through its result or side effect. + self._drop_concurrency_reservation_locked(profile) + # What: call self._cond.notify_all with the declared inputs; why: acquire invokes self._cond.notify_all while performing raise routing error capacity unavailable block status code; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: raise RoutingError for the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError("capacity_unavailable", block, status_code=409) + # What: compute switching from true; why: self switching later reads switching, so acquire must retain the computed value under that name. + self._switching = True + # What: compute activating name from name and profile; why: self activating name later reads activating name, so acquire must retain the computed value under that name. + self._activating_name = profile.name + # What: compute activating port from port; why: self activating port later reads activating port, so acquire must retain the computed value under that name. + self._activating_port = port + # What: call self._remove_pending_locked with ticket and cancellation; why: acquire invokes self._remove_pending_locked while performing break; the call advances that operation through its result or side effect. + self._remove_pending_locked(ticket, cancellation) + # What: apply the break portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + break + + # What: compute activated at from monotonic and time; why: self last queue wait ms round activated at queued at later reads activated at, so acquire must retain the computed value under that name. + activated_at = time.monotonic() + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.acquire routes failures to exception while preserving cleanup and success flow. + try: + # What: compute pid from activate and profile and port; why: pid later reads pid, so acquire must retain the computed value under that name. + pid = self._activate(profile, port) + # What: handle exception by with self cond; why: RoutingCoordinator.acquire converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: + # What: enter the cond managed context before self switching; why: acquire releases this resource or lock after self switching on both success and failure paths. + with self._cond: + # What: compute switching from false; why: self switching later reads switching, so acquire must retain the computed value under that name. + self._switching = False + # What: compute activating name from the named fixture input; why: self activating name later reads activating name, so acquire must retain the computed value under that name. + self._activating_name = None + # What: compute activating port from the named fixture input; why: self activating port later reads activating port, so acquire must retain the computed value under that name. + self._activating_port = None + # What: compute activation failures from 1; why: the enclosing return or state update later reads activation failures, so acquire must retain the computed value under that name. + self._activation_failures += 1 + # What: call self._drop_concurrency_reservation_locked with profile; why: acquire invokes self._drop_concurrency_reservation_locked while performing self cond notify all; the call advances that operation through its result or side effect. + self._drop_concurrency_reservation_locked(profile) + # What: call self._cond.notify_all with the declared inputs; why: acquire invokes self._cond.notify_all while performing if isinstance exc routing error; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: gate on isinstance and exc and routing error before the computed value; why: acquire admits the computed value only for this predicate and excludes the opposite state. + if isinstance(exc, RoutingError): + # What: re-propagate the active failure to the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: gate on isinstance and exc and switch launch error before exc and routing error and str and rollback; why: acquire admits exc and routing error and str and rollback only for this predicate and excludes the opposite state. + if isinstance(exc, SwitchLaunchError): + # What: raise RoutingError for the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError("switch_launch_failed", str(exc), recovery=exc.rollback) from exc + # What: gate on isinstance and exc and conflict before exc and routing error and str; why: acquire admits exc and routing error and str only for this predicate and excludes the opposite state. + if isinstance(exc, Conflict): + # What: raise RoutingError for the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError("serve_conflict", str(exc), status_code=409) from exc + # What: raise RoutingError for the caller; why: RoutingCoordinator.acquire stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError("activation_failed", str(exc)) from exc + # What: enter the cond managed context before self active name profile name; why: acquire releases this resource or lock after self active name profile name on both success and failure paths. + with self._cond: + # What: compute active name from name and profile; why: the enclosing return or state update later reads active name, so acquire must retain the computed value under that name. + self._active_name = profile.name + # What: compute activating name from the named fixture input; why: the enclosing return or state update later reads activating name, so acquire must retain the computed value under that name. + self._activating_name = None + # What: compute activating port from the named fixture input; why: the enclosing return or state update later reads activating port, so acquire must retain the computed value under that name. + self._activating_port = None + # What: compute switching from false; why: the enclosing return or state update later reads switching, so acquire must retain the computed value under that name. + self._switching = False + # What: call self._cancel_idle_timer with the declared inputs; why: acquire invokes self._cancel_idle_timer while performing self leases; the call advances that operation through its result or side effect. + self._cancel_idle_timer() + # What: compute leases from 1; why: the enclosing return or state update later reads leases, so acquire must retain the computed value under that name. + self._leases += 1 + # What: compute admissions from 1; why: the enclosing return or state update later reads admissions, so acquire must retain the computed value under that name. + self._admissions += 1 + # What: compute last queue wait ms from round and activated at and queued at and 3 and 1000; why: the enclosing return or state update later reads last queue wait ms, so acquire must retain the computed value under that name. + self._last_queue_wait_ms = round((activated_at - queued_at) * 1000, 3) + # What: compute last activation ms from round and activated at and monotonic and time and 3; why: the enclosing return or state update later reads last activation ms, so acquire must retain the computed value under that name. + self._last_activation_ms = round((time.monotonic() - activated_at) * 1000, 3) + # What: call self._cond.notify_all with the declared inputs; why: acquire invokes self._cond.notify_all while performing return route lease; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: return route lease and profile and port and pid from acquire; why: acquire exposes route lease and profile and port and pid so its caller can continue with the function\'s computed outcome. + return RouteLease( + # What: apply the grouped expression portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + self, + # What: apply the profile portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + profile, + # What: apply the port portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + port, + # What: apply the pid portion of the enclosing predicate; why: this clause remains in acquire\'s enclosing expression so its grouping and evaluation order stay intact. + pid, + # What: supply model id to RouteLease; why: acquire binds this model id value to RouteLease's model id input. + model_id=model_id, + # What: supply selector id to RouteLease; why: acquire binds this selector id value to RouteLease's selector id input. + selector_id=selector_id, + # What: supply routing profile id to RouteLease; why: acquire binds this routing profile id value to RouteLease's routing profile id input. + routing_profile_id=routing_profile_id, + # What: supply pin id to RouteLease; why: acquire binds this pin id value to RouteLease's pin id input. + pin_id=pin_id, + # What: complete the RouteLease call with model id and selector id and routing profile id and pin id; why: RoutingCoordinator.acquire groups the supplied clauses as one RouteLease call before its value is consumed. + ) + + # What: define cancel_acquire around cancellation; why: its direct callers call cancel_acquire for cancel acquire and rely on this exact input and result contract. + def cancel_acquire(self, cancellation: threading.Event) -> None: + """Atomically retire queued ownership, then wake its admission worker.""" + # What: document atomically retire queued ownership then wake in the cancel_acquire docstring; why: introspection and maintainers read this exact docstring fragment to understand cancel acquire behavior without executing it. + # What: enter the cond managed context before cancellation set; why: cancel_acquire releases this resource or lock after cancellation set on both success and failure paths. + with self._cond: + # What: call cancellation.set with the declared inputs; why: cancel_acquire invokes cancellation.set while performing pending self pending by cancellation get cancellation; the call advances that operation through its result or side effect. + cancellation.set() + # What: compute pending from get and cancellation and pending by cancellation; why: if pending is not later reads pending, so cancel_acquire must retain the computed value under that name. + pending = self._pending_by_cancellation.get(cancellation) + # What: gate on pending before pending and ticket and profile; why: cancel_acquire admits pending and ticket and profile only for this predicate and excludes the opposite state. + if pending is not None: + # What: compute ticket and profile from pending; why: if self remove pending locked ticket cancellation later reads ticket and profile, so cancel_acquire must retain the computed value under that name. + ticket, profile = pending + # What: gate on remove pending locked and ticket and cancellation before drop concurrency reservation locked and profile; why: cancel_acquire admits drop concurrency reservation locked and profile only for this predicate and excludes the opposite state. + if self._remove_pending_locked(ticket, cancellation): + # What: call self._drop_concurrency_reservation_locked with profile; why: cancel_acquire invokes self._drop_concurrency_reservation_locked while performing self cond notify all; the call advances that operation through its result or side effect. + self._drop_concurrency_reservation_locked(profile) + # What: call self._cond.notify_all with the declared inputs; why: cancel_acquire invokes self._cond.notify_all while performing the enclosing return; the call advances that operation through its result or side effect. + self._cond.notify_all() + + # What: define queue_position around cancellation; why: its direct callers call queue_position for queue position and rely on this exact input and result contract. + def queue_position(self, cancellation: threading.Event) -> int | None: + """Return the current one-based scheduler position for a reserved request.""" + # What: document return the current one based scheduler position in the queue_position docstring; why: introspection and maintainers read this exact docstring fragment to understand queue position behavior without executing it. + # What: enter the cond managed context before pending self pending by cancellation get cancellation; why: queue_position releases this resource or lock after pending self pending by cancellation get cancellation on both success and failure paths. + with self._cond: + # What: compute pending from get and cancellation and pending by cancellation; why: if pending is later reads pending, so queue_position must retain the computed value under that name. + pending = self._pending_by_cancellation.get(cancellation) + # What: gate on pending before the computed value; why: queue_position admits the computed value only for this predicate and excludes the opposite state. + if pending is None: + # What: return no value from queue_position; why: queue_position returns no value to callers that depend on its completed result. + return None + # What: compute ticket and profile from pending; why: return sorted self pending index ticket later reads ticket and profile, so queue_position must retain the computed value under that name. + ticket, _profile = pending + # What: return index and ticket and sorted and pending and 1 from queue_position; why: queue_position exposes index and ticket and sorted and pending and 1 so its caller can continue with the function\'s computed outcome. + return sorted(self._pending).index(ticket) + 1 + + # What: define loading_feedback_enabled around name; why: its direct callers call loading_feedback_enabled for loading feedback enabled and rely on this exact input and result contract. + def loading_feedback_enabled(self, name: str) -> bool: + """Resolve the per-profile loading setting over the global default atomically.""" + # What: document resolve the per profile loading setting over in the loading_feedback_enabled docstring; why: introspection and maintainers read this exact docstring fragment to understand loading feedback enabled behavior without executing it. + # What: enter the cond managed context before try; why: loading_feedback_enabled releases this resource or lock after try on both success and failure paths. + with self._cond: + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.loading_feedback_enabled routes failures to catalog error while preserving cleanup and success flow. + try: + # What: compute and profile and and and from resolve request locked and name; why: the enclosing return or state update later reads and profile and and and, so loading_feedback_enabled must retain the computed value under that name. + _, profile, _, _, _ = self._resolve_request_locked(name) + # What: handle catalog error by return false; why: RoutingCoordinator.loading_feedback_enabled converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError: + # Admission owns the authoritative unknown-model response. A + # concurrent catalog replacement must not leak an exception + # from this optional pre-admission presentation policy. + # What: return false from loading_feedback_enabled; why: loading_feedback_enabled exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: gate on send loading state and profile before send loading state and profile; why: loading_feedback_enabled admits send loading state and profile only for this predicate and excludes the opposite state. + if profile.send_loading_state is not None: + # What: return send loading state and profile from loading_feedback_enabled; why: loading_feedback_enabled exposes send loading state and profile so its caller can continue with the function\'s computed outcome. + return profile.send_loading_state + # What: return send loading state and settings and catalog from loading_feedback_enabled; why: loading_feedback_enabled exposes send loading state and settings and catalog so its caller can continue with the function\'s computed outcome. + return self._catalog.settings.send_loading_state + + # What: define _resolve_request_locked around name and apply routing profile; why: its direct callers call _resolve_request_locked for resolve request locked and rely on this exact input and result contract. + def _resolve_request_locked( + # What: declare the self input for _resolve_request_locked; why: _resolve_request_locked consumes self during if apply routing profile and self active routing profile is not, so callers must bind it with the other signature inputs. + self, name: str, *, apply_routing_profile: bool = True + # What: complete the enclosing predicate collection with str and model profile and str and str; why: RoutingCoordinator._resolve_request_locked groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + ) -> tuple[str, ModelProfile, str | None, str | None, str | None]: + # What: compute routing profile id from the named fixture input; why: routing profile id routing profile name later reads routing profile id, so _resolve_request_locked must retain the computed value under that name. + routing_profile_id = None + # What: compute pin id from the named fixture input; why: pin id name later reads pin id, so _resolve_request_locked must retain the computed value under that name. + pin_id = None + # What: gate on apply routing profile and active routing profile before routing profile and active routing profile and catalog; why: _resolve_request_locked admits routing profile and active routing profile and catalog only for this predicate and excludes the opposite state. + if apply_routing_profile and self._active_routing_profile is not None: + # What: compute routing profile from routing profile and active routing profile and catalog; why: if routing profile is not later reads routing profile, so _resolve_request_locked must retain the computed value under that name. + routing_profile = self._catalog.routing_profile(self._active_routing_profile) + # What: gate on routing profile before pinned and target and replacement and name and routing profile; why: _resolve_request_locked admits pinned and target and replacement and name and routing profile only for this predicate and excludes the opposite state. + if routing_profile is not None: + # What: compute pinned and target from replacement and name and routing profile; why: if pinned later reads pinned and target, so _resolve_request_locked must retain the computed value under that name. + pinned, target = routing_profile.replacement(name) + # What: gate on pinned before routing profile id and name and routing profile; why: _resolve_request_locked admits routing profile id and name and routing profile only for this predicate and excludes the opposite state. + if pinned: + # What: compute routing profile id from name and routing profile; why: return name self catalog get name routing profile id pin id later reads routing profile id, so _resolve_request_locked must retain the computed value under that name. + routing_profile_id = routing_profile.name + # What: compute pin id from name; why: return name self catalog get name routing profile id pin id later reads pin id, so _resolve_request_locked must retain the computed value under that name. + pin_id = name + # What: gate on target before catalog error and name and routing profile; why: _resolve_request_locked admits catalog error and name and routing profile only for this predicate and excludes the opposite state. + if target is None: + # What: raise CatalogError for the caller; why: RoutingCoordinator._resolve_request_locked stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f model id name r is portion of the enclosing predicate; why: this clause remains in _resolve_request_locked\'s enclosing expression so its grouping and evaluation order stay intact. + f"model ID {name!r} is disabled by routing profile {routing_profile.name!r}" + # What: complete the CatalogError call with name; why: RoutingCoordinator._resolve_request_locked groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: compute name from target; why: selector self catalog selector name later reads name, so _resolve_request_locked must retain the computed value under that name. + name = target + # What: compute selector from selector and name and catalog; why: if selector is later reads selector, so _resolve_request_locked must retain the computed value under that name. + selector = self._catalog.selector(name) + # What: gate on selector before name and routing profile id and pin id and get and catalog; why: _resolve_request_locked admits name and routing profile id and pin id and get and catalog only for this predicate and excludes the opposite state. + if selector is None: + # What: return name and routing profile id and pin id and get from _resolve_request_locked; why: _resolve_request_locked exposes name and routing profile id and pin id and get so its caller can continue with the function\'s computed outcome. + return name, self._catalog.get(name), None, routing_profile_id, pin_id + # What: gate on strategy and selector before target and targets and selector and profile and get; why: _resolve_request_locked admits target and targets and selector and profile and get only for this predicate and excludes the opposite state. + if selector.strategy == "warm": + # What: iterate across targets and selector to perform profile and get and target and catalog; why: _resolve_request_locked repeats the body only while or for the loop header admits an iteration. + for target in selector.targets: + # What: compute profile from get and target and catalog; why: if self active profile ready locked profile later reads profile, so _resolve_request_locked must retain the computed value under that name. + profile = self._catalog.get(target) + # What: gate on active profile ready locked and profile before target and profile and name and routing profile id and pin id; why: _resolve_request_locked admits target and profile and name and routing profile id and pin id only for this predicate and excludes the opposite state. + if self._active_profile_ready_locked(profile): + # What: return target and profile and name and routing profile id from _resolve_request_locked; why: _resolve_request_locked exposes target and profile and name and routing profile id so its caller can continue with the function\'s computed outcome. + return target, profile, selector.name, routing_profile_id, pin_id + # What: iterate across targets and selector to perform profile and get and target and catalog; why: _resolve_request_locked repeats the body only while or for the loop header admits an iteration. + for target in selector.targets: + # What: compute profile from get and target and catalog; why: if self activating name profile name later reads profile, so _resolve_request_locked must retain the computed value under that name. + profile = self._catalog.get(target) + # What: gate on activating name and name and profile before target and profile and name and routing profile id and pin id; why: _resolve_request_locked admits target and profile and name and routing profile id and pin id only for this predicate and excludes the opposite state. + if self._activating_name == profile.name: + # What: return target and profile and name and routing profile id from _resolve_request_locked; why: _resolve_request_locked exposes target and profile and name and routing profile id so its caller can continue with the function\'s computed outcome. + return target, profile, selector.name, routing_profile_id, pin_id + # What: compute target from targets and selector and 0; why: return target self catalog get target selector name routing profile id later reads target, so _resolve_request_locked must retain the computed value under that name. + target = selector.targets[0] + # What: return target and name and routing profile id and pin id from _resolve_request_locked; why: _resolve_request_locked exposes target and name and routing profile id and pin id so its caller can continue with the function\'s computed outcome. + return target, self._catalog.get(target), selector.name, routing_profile_id, pin_id + + # What: define has_routable_id around name; why: its direct callers call has_routable_id for has routable id and rely on this exact input and result contract. + def has_routable_id(self, name: str) -> bool: + """Whether *name* resolves under the current runtime profile snapshot.""" + # What: document whether name resolves under the current in the has_routable_id docstring; why: introspection and maintainers read this exact docstring fragment to understand has routable id behavior without executing it. + # What: enter the cond managed context before try; why: has_routable_id releases this resource or lock after try on both success and failure paths. + with self._cond: + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.has_routable_id routes failures to catalog error while preserving cleanup and success flow. + try: + # What: call self._resolve_request_locked with name; why: has_routable_id invokes self._resolve_request_locked while performing except catalog error; the call advances that operation through its result or side effect. + self._resolve_request_locked(name) + # What: handle catalog error by return false; why: RoutingCoordinator.has_routable_id converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError: + # What: return false from has_routable_id; why: has_routable_id exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: return true from has_routable_id; why: has_routable_id exposes true so its caller can continue with the function\'s computed outcome. + return True + + # What: define set_active_routing_profile around name; why: its direct callers call set_active_routing_profile for set active routing profile and rely on this exact input and result contract. + def set_active_routing_profile(self, name: str | None) -> str | None: + """Atomically activate one pin map, or clear runtime pinning with ``None``.""" + # What: document atomically activate one pin map or in the set_active_routing_profile docstring; why: introspection and maintainers read this exact docstring fragment to understand set active routing profile behavior without executing it. + # What: enter the cond managed context before if name is not and self catalog routing profile; why: set_active_routing_profile releases this resource or lock after if name is not and self catalog routing profile on both success and failure paths. + with self._cond: + # What: gate on name and routing profile and catalog before routing error and name; why: set_active_routing_profile admits routing error and name only for this predicate and excludes the opposite state. + if name is not None and self._catalog.routing_profile(name) is None: + # What: raise RoutingError for the caller; why: RoutingCoordinator.set_active_routing_profile stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: supply status code to RoutingError; why: set_active_routing_profile binds this 404 value to RoutingError's status code input. + "unknown_profile", f"routing profile {name!r} not found", status_code=404 + # What: complete the RoutingError call with status code; why: RoutingCoordinator.set_active_routing_profile groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: compute active routing profile from name; why: return self active routing profile later reads active routing profile, so set_active_routing_profile must retain the computed value under that name. + self._active_routing_profile = name + # What: call self._cond.notify_all with the declared inputs; why: set_active_routing_profile invokes self._cond.notify_all while performing return self active routing profile; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: return active routing profile from set_active_routing_profile; why: set_active_routing_profile exposes active routing profile so its caller can continue with the function\'s computed outcome. + return self._active_routing_profile + + # What: define resolve_upstream_path around path; why: its direct callers call resolve_upstream_path for resolve upstream path and rely on this exact input and result contract. + def resolve_upstream_path( + # What: declare the self input for resolve_upstream_path; why: resolve_upstream_path consumes self during with self cond, so callers must bind it with the other signature inputs. + self, path: str + # What: complete the enclosing predicate collection with str and str and model profile and str; why: RoutingCoordinator.resolve_upstream_path groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + ) -> tuple[str, str, ModelProfile, str]: + """Apply the active profile's longest pin before concrete upstream lookup.""" + # What: document apply the active profile s longest in the resolve_upstream_path docstring; why: introspection and maintainers read this exact docstring fragment to understand resolve upstream path behavior without executing it. + # What: enter the cond managed context before normalized path strip; why: resolve_upstream_path releases this resource or lock after normalized path strip on both success and failure paths. + with self._cond: + # What: compute normalized from strip and path and value; why: rewritten normalized later reads normalized, so resolve_upstream_path must retain the computed value under that name. + normalized = path.strip("/") + # What: compute source id from the named fixture input; why: if source id is or len pin later reads source id, so resolve_upstream_path must retain the computed value under that name. + source_id = None + # What: compute rewritten from normalized; why: rewritten later reads rewritten, so resolve_upstream_path must retain the computed value under that name. + rewritten = normalized + # What: gate on active routing profile before routing profile and active routing profile and catalog; why: resolve_upstream_path admits routing profile and active routing profile and catalog only for this predicate and excludes the opposite state. + if self._active_routing_profile is not None: + # What: compute routing profile from routing profile and active routing profile and catalog; why: if routing profile is not later reads routing profile, so resolve_upstream_path must retain the computed value under that name. + routing_profile = self._catalog.routing_profile(self._active_routing_profile) + # What: gate on routing profile before pins and pin and target and routing profile and normalized; why: resolve_upstream_path admits pins and pin and target and routing profile and normalized only for this predicate and excludes the opposite state. + if routing_profile is not None: + # What: iterate across pins and routing profile to perform normalized and pin and startswith and source id and target; why: resolve_upstream_path repeats the body only while or for the loop header admits an iteration. + for pin, target in routing_profile.pins: + # What: gate on normalized and pin and startswith before source id and pin and target and rewritten and len; why: resolve_upstream_path admits source id and pin and target and rewritten and len only for this predicate and excludes the opposite state. + if normalized == pin or normalized.startswith(pin + "/"): + # What: gate on source id and len and pin before source id and pin; why: resolve_upstream_path admits source id and pin only for this predicate and excludes the opposite state. + if source_id is None or len(pin) > len(source_id): + # What: compute source id from pin; why: if source id is not and not later reads source id, so resolve_upstream_path must retain the computed value under that name. + source_id = pin + # What: gate on target before rewritten; why: resolve_upstream_path admits rewritten only for this predicate and excludes the opposite state. + if target is None: + # What: compute rewritten from value; why: rewritten target normalized len pin later reads rewritten, so resolve_upstream_path must retain the computed value under that name. + rewritten = "" + # What: select the remaining branch that performs rewritten target normalized len pin; why: resolve_upstream_path covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute rewritten from target and normalized and len and pin; why: if source id is not and not later reads rewritten, so resolve_upstream_path must retain the computed value under that name. + rewritten = target + normalized[len(pin):] + # What: gate on source id and rewritten before catalog error and source id; why: resolve_upstream_path admits catalog error and source id only for this predicate and excludes the opposite state. + if source_id is not None and not rewritten: + # What: raise CatalogError for the caller; why: RoutingCoordinator.resolve_upstream_path stops this rejected path before it can mutate state, dispatch work, or report success. + raise CatalogError( + # What: apply the f upstream model id source id r portion of the enclosing predicate; why: this clause remains in resolve_upstream_path\'s enclosing expression so its grouping and evaluation order stay intact. + f"upstream model ID {source_id!r} is disabled by the active routing profile" + # What: complete the CatalogError call with source id; why: RoutingCoordinator.resolve_upstream_path groups the supplied clauses as one CatalogError call before its value is consumed. + ) + # What: compute routed id and profile and remaining from resolve upstream path and rewritten and catalog; why: return source id or routed id routed id profile later reads routed id and profile and remaining, so resolve_upstream_path must retain the computed value under that name. + routed_id, profile, remaining = self._catalog.resolve_upstream_path(rewritten) + # What: return routed id and profile and remaining and source id from resolve_upstream_path; why: resolve_upstream_path exposes routed id and profile and remaining and source id so its caller can continue with the function\'s computed outcome. + return source_id or routed_id, routed_id, profile, remaining + + # What: define begin_manual_lifecycle and its declared inputs; why: callers use begin_manual_lifecycle to perform the behavior named by this helper without duplicating its boundary checks. + def begin_manual_lifecycle(self, *, preempt_manual: bool = False) -> object: + """Reserve the lifecycle barrier for one legacy engine operation.""" + # What: document reserve the lifecycle barrier for one in the begin_manual_lifecycle docstring; why: introspection and maintainers read this exact docstring fragment to understand begin manual lifecycle behavior without executing it. + # What: enter the cond managed context before if self shutdown requested; why: begin_manual_lifecycle releases this resource or lock after if self shutdown requested on both success and failure paths. + with self._cond: + # What: gate on shutdown requested before routing error; why: begin_manual_lifecycle admits routing error only for this predicate and excludes the opposite state. + if self._shutdown_requested: + # What: raise RoutingError for the caller; why: RoutingCoordinator.begin_manual_lifecycle stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: supply status code to RoutingError; why: begin_manual_lifecycle binds this 503 value to RoutingError's status code input. + "router_shutting_down", "router shutdown is in progress", status_code=503 + # What: complete the RoutingError call with status code; why: RoutingCoordinator.begin_manual_lifecycle groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: compute manual owned from manual lifecycle owner; why: or self pending and not manual owned later reads manual owned, so begin_manual_lifecycle must retain the computed value under that name. + manual_owned = self._manual_lifecycle_owner is not None + # What: compute routed owned from bool and leases and active name and pending; why: if routed owned or self switching and not later reads routed owned, so begin_manual_lifecycle must retain the computed value under that name. + routed_owned = bool( + # What: apply the self active name is not or self leases portion of routed owned; why: begin_manual_lifecycle uses this clause to evaluate routed owned as one grouped value. + self._active_name is not None or self._leases + # What: apply the or self pending and not manual owned portion of routed owned; why: begin_manual_lifecycle uses this clause to evaluate routed owned as one grouped value. + or (self._pending and not manual_owned) + # What: complete the bool call with leases; why: RoutingCoordinator.begin_manual_lifecycle groups the supplied clauses as one bool call before its value is consumed. + ) + # What: gate on routed owned and switching and preempt manual and manual owned before routing error; why: begin_manual_lifecycle admits routing error only for this predicate and excludes the opposite state. + if (routed_owned or (self._switching and not (preempt_manual and manual_owned))): + # What: raise RoutingError for the caller; why: RoutingCoordinator.begin_manual_lifecycle stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: apply the router owned portion of the enclosing predicate; why: this clause remains in begin_manual_lifecycle\'s enclosing expression so its grouping and evaluation order stay intact. + "router_owned", + # What: apply the router owns or is admitting an portion of the enclosing predicate; why: this clause remains in begin_manual_lifecycle\'s enclosing expression so its grouping and evaluation order stay intact. + "router owns or is admitting an engine; use router controls or wait", + # What: supply status code to RoutingError; why: begin_manual_lifecycle binds this 409 value to RoutingError's status code input. + status_code=409, + # What: complete the RoutingError call with status code; why: RoutingCoordinator.begin_manual_lifecycle groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: compute owner from object; why: self manual lifecycle tokens add owner later reads owner, so begin_manual_lifecycle must retain the computed value under that name. + owner = object() + # What: call self._manual_lifecycle_tokens.add with owner; why: begin_manual_lifecycle invokes self._manual_lifecycle_tokens.add while performing self manual lifecycle owner owner; the call advances that operation through its result or side effect. + self._manual_lifecycle_tokens.add(owner) + # What: compute manual lifecycle owner from owner; why: the enclosing return or state update later reads manual lifecycle owner, so begin_manual_lifecycle must retain the computed value under that name. + self._manual_lifecycle_owner = owner + # What: compute switching from true; why: the enclosing return or state update later reads switching, so begin_manual_lifecycle must retain the computed value under that name. + self._switching = True + # What: return owner from begin_manual_lifecycle; why: begin_manual_lifecycle exposes owner so its caller can continue with the function\'s computed outcome. + return owner + + # What: define end_manual_lifecycle around owner; why: its direct callers call end_manual_lifecycle for end manual lifecycle and rely on this exact input and result contract. + def end_manual_lifecycle(self, owner: object) -> None: + """Release a matching legacy lifecycle reservation.""" + # What: document release a matching legacy lifecycle reservation in the end_manual_lifecycle docstring; why: introspection and maintainers read this exact docstring fragment to understand end manual lifecycle behavior without executing it. + # What: enter the cond managed context before if owner not in self manual lifecycle tokens; why: end_manual_lifecycle releases this resource or lock after if owner not in self manual lifecycle tokens on both success and failure paths. + with self._cond: + # What: gate on owner and manual lifecycle tokens before value error; why: end_manual_lifecycle admits value error only for this predicate and excludes the opposite state. + if owner not in self._manual_lifecycle_tokens: + # What: raise ValueError for the caller; why: RoutingCoordinator.end_manual_lifecycle stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("manual lifecycle reservation is not owned by caller") + # What: call self._manual_lifecycle_tokens.remove with owner; why: end_manual_lifecycle invokes self._manual_lifecycle_tokens.remove while performing if self manual lifecycle owner is owner; the call advances that operation through its result or side effect. + self._manual_lifecycle_tokens.remove(owner) + # What: gate on manual lifecycle owner and owner before manual lifecycle owner; why: end_manual_lifecycle admits manual lifecycle owner only for this predicate and excludes the opposite state. + if self._manual_lifecycle_owner is owner: + # What: compute manual lifecycle owner from the named fixture input; why: the enclosing return or state update later reads manual lifecycle owner, so end_manual_lifecycle must retain the computed value under that name. + self._manual_lifecycle_owner = None + # What: compute switching from false; why: the enclosing return or state update later reads switching, so end_manual_lifecycle must retain the computed value under that name. + self._switching = False + # What: call self._cond.notify_all with the declared inputs; why: end_manual_lifecycle invokes self._cond.notify_all while performing the enclosing return; the call advances that operation through its result or side effect. + self._cond.notify_all() + + # What: define release around lease; why: its direct callers call release for release and rely on this exact input and result contract. + def release(self, lease: RouteLease) -> None: + # What: enter the cond managed context before if lease router is not; why: release releases this resource or lock after if lease router is not on both success and failure paths. + with self._cond: + # What: gate on router and lease before value error; why: release admits value error only for this predicate and excludes the opposite state. + if lease.router is not self: + # What: raise ValueError for the caller; why: RoutingCoordinator.release stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("lease belongs to a different routing coordinator") + # What: gate on released and lease and leases before value error; why: release admits value error only for this predicate and excludes the opposite state. + if lease._released or self._leases <= 0: + # What: raise ValueError for the caller; why: RoutingCoordinator.release stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("routing lease was already released") + # What: compute released from true; why: the enclosing return or state update later reads released, so release must retain the computed value under that name. + lease._released = True + # What: compute leases from 1; why: if self leases later reads leases, so release must retain the computed value under that name. + self._leases -= 1 + # What: call self._drop_concurrency_reservation_locked with profile and lease; why: release invokes self._drop_concurrency_reservation_locked while performing if self leases; the call advances that operation through its result or side effect. + self._drop_concurrency_reservation_locked(lease.profile) + # What: gate on leases before schedule idle eviction; why: release admits schedule idle eviction only for this predicate and excludes the opposite state. + if self._leases == 0: + # What: call self._schedule_idle_eviction with the declared inputs; why: release invokes self._schedule_idle_eviction while performing self cond notify all; the call advances that operation through its result or side effect. + self._schedule_idle_eviction() + # What: call self._cond.notify_all with the declared inputs; why: release invokes self._cond.notify_all while performing the enclosing return; the call advances that operation through its result or side effect. + self._cond.notify_all() + + # What: define status around the current object state; why: its direct callers call status for status and rely on this exact input and result contract. + def status(self) -> dict: + # What: enter the cond managed context before return self status locked; why: status releases this resource or lock after return self status locked on both success and failure paths. + with self._cond: + # What: return status locked from status; why: status exposes status locked so its caller can continue with the function\'s computed outcome. + return self._status_locked() + + # What: define _status_locked around the current object state; why: its direct callers call _status_locked for status locked and rely on this exact input and result contract. + def _status_locked(self) -> dict: + # What: compute group from active name and group for and catalog; why: active group group name if group else later reads group, so _status_locked must retain the computed value under that name. + group = self._catalog.group_for(self._active_name) if self._active_name else None + # What: compute active identity matches from active matches engine locked; why: resident profiles self active name if active identity matches else later reads active identity matches, so _status_locked must retain the computed value under that name. + active_identity_matches = self._active_matches_engine_locked() + # What: return active name and active routing profile and activating name and active identity matches from _status_locked; why: _status_locked exposes active name and active routing profile and activating name and active identity matches so its caller can continue with the function\'s computed outcome. + return { + # What: map the active profile field as active name; why: RoutingCoordinator._status_locked carries active profile into "activeProfile": self._active_name. + "activeProfile": self._active_name, + # What: map the active routing profile field as active routing profile; why: RoutingCoordinator._status_locked carries active routing profile into "activeRoutingProfile": self._active_routing_profile. + "activeRoutingProfile": self._active_routing_profile, + # What: map the activating profile field as activating name; why: RoutingCoordinator._status_locked carries activating profile into "activatingProfile": self._activating_name. + "activatingProfile": self._activating_name, + # What: map the active group field as group and name; why: RoutingCoordinator._status_locked carries active group into "activeGroup": group.name if group else None. + "activeGroup": group.name if group else None, + # What: map the resident profiles field as active identity matches and active name; why: RoutingCoordinator._status_locked carries resident profiles into "residentProfiles": [self._active_name] if active_identity_matches else. + "residentProfiles": [self._active_name] if active_identity_matches else [], + # What: map the active identity matches engine field as active identity matches; why: RoutingCoordinator._status_locked carries active identity matches engine into "activeIdentityMatchesEngine": active_identity_matches. + "activeIdentityMatchesEngine": active_identity_matches, + # What: map the persistent field as bool and active identity matches and group and persistent; why: RoutingCoordinator._status_locked carries persistent into "persistent": bool(active_identity_matches and group and group.persisten. + "persistent": bool(active_identity_matches and group and group.persistent), + # What: map the max resident models field as 1; why: RoutingCoordinator._status_locked carries max resident models into "capacity": {"maxResidentModels": 1, "availableResidentSlots": 0 if self. + "capacity": {"maxResidentModels": 1, "availableResidentSlots": 0 if self._active_name else 1}, + # What: map the active requests field as leases; why: RoutingCoordinator._status_locked carries active requests into "activeRequests": self._leases. + "activeRequests": self._leases, + # What: map the reserved requests field as reservations; why: RoutingCoordinator._status_locked carries reserved requests into "reservedRequests": self._reservations. + "reservedRequests": self._reservations, + # What: map the shutting down field as shutdown requested; why: RoutingCoordinator._status_locked carries shutting down into "shuttingDown": self._shutdown_requested. + "shuttingDown": self._shutdown_requested, + # What: map the switching field as switching; why: RoutingCoordinator._status_locked carries switching into "switching": self._switching. + "switching": self._switching, + # What: map the queued requests field as len and pending; why: RoutingCoordinator._status_locked carries queued requests into "queuedRequests": len(self._pending). + "queuedRequests": len(self._pending), + # What: map the idle eviction scheduled field as idle timer; why: RoutingCoordinator._status_locked carries idle eviction scheduled into "idleEvictionScheduled": self._idle_timer is not None. + "idleEvictionScheduled": self._idle_timer is not None, + # What: map the evictions field as evictions; why: RoutingCoordinator._status_locked carries evictions into "evictions": self._evictions. + "evictions": self._evictions, + # What: map the admissions field as admissions; why: RoutingCoordinator._status_locked carries admissions into "admissions": self._admissions. + "admissions": self._admissions, + # What: map the activations field as activations; why: RoutingCoordinator._status_locked carries activations into "activations": self._activations. + "activations": self._activations, + # What: map the activation failures field as activation failures; why: RoutingCoordinator._status_locked carries activation failures into "activationFailures": self._activation_failures. + "activationFailures": self._activation_failures, + # What: map the cancellations field as cancellations; why: RoutingCoordinator._status_locked carries cancellations into "cancellations": self._cancellations. + "cancellations": self._cancellations, + # What: map the terminal streams field as terminal streams; why: RoutingCoordinator._status_locked carries terminal streams into "terminalStreams": self._terminal_streams. + "terminalStreams": self._terminal_streams, + # What: map the last ttft ms field as last ttft ms; why: RoutingCoordinator._status_locked carries last ttft ms into "lastTtftMs": self._last_ttft_ms. + "lastTtftMs": self._last_ttft_ms, + # What: map the last duration ms field as last duration ms; why: RoutingCoordinator._status_locked carries last duration ms into "lastDurationMs": self._last_duration_ms. + "lastDurationMs": self._last_duration_ms, + # What: map the last activation ms field as last activation ms; why: RoutingCoordinator._status_locked carries last activation ms into "lastActivationMs": self._last_activation_ms. + "lastActivationMs": self._last_activation_ms, + # What: map the last queue wait ms field as last queue wait ms; why: RoutingCoordinator._status_locked carries last queue wait ms into "lastQueueWaitMs": self._last_queue_wait_ms. + "lastQueueWaitMs": self._last_queue_wait_ms, + # What: map the last response bytes field as last response bytes; why: RoutingCoordinator._status_locked carries last response bytes into "lastResponseBytes": self._last_response_bytes. + "lastResponseBytes": self._last_response_bytes, + # What: map the last proxy bytes per second field as last proxy bytes per second; why: RoutingCoordinator._status_locked carries last proxy bytes per second into "lastProxyBytesPerSecond": self._last_proxy_bytes_per_second. + "lastProxyBytesPerSecond": self._last_proxy_bytes_per_second, + # What: map the scheduler field as scheduler and settings and catalog; why: RoutingCoordinator._status_locked carries scheduler into "scheduler": self._catalog.settings.scheduler. + "scheduler": self._catalog.settings.scheduler, + # What: map the global concurrency limit field as global concurrency limit and settings and catalog; why: RoutingCoordinator._status_locked carries global concurrency limit into "globalConcurrencyLimit": self._catalog.settings.global_concurrency_limi. + "globalConcurrencyLimit": self._catalog.settings.global_concurrency_limit, + # What: map the default profile concurrency limit field as default profile concurrency limit; why: RoutingCoordinator._status_locked carries default profile concurrency limit into "defaultProfileConcurrencyLimit": DEFAULT_PROFILE_CONCURRENCY_LIMIT. + "defaultProfileConcurrencyLimit": DEFAULT_PROFILE_CONCURRENCY_LIMIT, + # What: complete the enclosing predicate mapping with active profile and active routing profile and activating profile and active group and resident profiles; why: RoutingCoordinator._status_locked groups the supplied clauses as one enclosing predicate mapping mapping before its value is consumed. + } + + # What: expose catalog as a read-only computed property; why: callers read catalog through attribute access while its getter retains control of the derived value. + @property + # What: define catalog around the current object state; why: the registered API client call catalog for catalog and rely on this exact input and result contract. + def catalog(self) -> ModelCatalog: + # What: enter the cond managed context before return self catalog; why: catalog releases this resource or lock after return self catalog on both success and failure paths. + with self._cond: + # What: return catalog from catalog; why: catalog exposes catalog so its caller can continue with the function\'s computed outcome. + return self._catalog + + # What: define control_plane_snapshot around the current object state; why: its direct callers call control_plane_snapshot for control plane snapshot and rely on this exact input and result contract. + def control_plane_snapshot(self) -> tuple[ModelCatalog, dict]: + """Return one catalog and routing-state snapshot for control responses.""" + # What: document return one catalog and routing state snapshot in the control_plane_snapshot docstring; why: introspection and maintainers read this exact docstring fragment to understand control plane snapshot behavior without executing it. + # What: enter the cond managed context before return self catalog self status locked; why: control_plane_snapshot releases this resource or lock after return self catalog self status locked on both success and failure paths. + with self._cond: + # What: return catalog and status locked from control_plane_snapshot; why: control_plane_snapshot exposes catalog and status locked so its caller can continue with the function\'s computed outcome. + return self._catalog, self._status_locked() + + # What: define model_listing_snapshot around the current object state; why: its direct callers call model_listing_snapshot for model listing snapshot and rely on this exact input and result contract. + def model_listing_snapshot(self) -> tuple[ModelCatalog, frozenset[str]]: + """Return one atomic public-catalog and loaded/starting identity snapshot.""" + # What: document return one atomic public catalog and loaded in the model_listing_snapshot docstring; why: introspection and maintainers read this exact docstring fragment to understand model listing snapshot behavior without executing it. + # What: enter the cond managed context before loaded set str set; why: model_listing_snapshot releases this resource or lock after loaded set str set on both success and failure paths. + with self._cond: + # What: compute loaded from set; why: loaded add self active name later reads loaded, so model_listing_snapshot must retain the computed value under that name. + loaded: set[str] = set() + # What: gate on active name and active matches engine locked before add and active name and loaded; why: model_listing_snapshot admits add and active name and loaded only for this predicate and excludes the opposite state. + if self._active_name is not None and self._active_matches_engine_locked(): + # What: call loaded.add with active name; why: model_listing_snapshot invokes loaded.add while performing if self activating name is not and self activating port; the call advances that operation through its result or side effect. + loaded.add(self._active_name) + # What: gate on activating name and activating port before profile and get and activating name and catalog; why: model_listing_snapshot admits profile and get and activating name and catalog only for this predicate and excludes the opposite state. + if self._activating_name is not None and self._activating_port is not None: + # What: compute profile from get and activating name and catalog; why: if self engine matches profile self activating port later reads profile, so model_listing_snapshot must retain the computed value under that name. + profile = self._catalog.get(self._activating_name) + # What: gate on engine matches and profile and activating port before add and activating name and loaded; why: model_listing_snapshot admits add and activating name and loaded only for this predicate and excludes the opposite state. + if self._engine_matches(profile, self._activating_port): + # What: call loaded.add with activating name; why: model_listing_snapshot invokes loaded.add while performing return self catalog frozenset loaded; the call advances that operation through its result or side effect. + loaded.add(self._activating_name) + # What: return catalog and frozenset and loaded from model_listing_snapshot; why: model_listing_snapshot exposes catalog and frozenset and loaded so its caller can continue with the function\'s computed outcome. + return self._catalog, frozenset(loaded) + + # What: define public_model_listing_snapshot around the current object state; why: its direct callers call public_model_listing_snapshot for public model listing snapshot and rely on this exact input and result contract. + def public_model_listing_snapshot( + # What: declare the self input for public_model_listing_snapshot; why: public_model_listing_snapshot consumes self during with self cond, so callers must bind it with the other signature inputs. + self, + # What: complete the enclosing predicate collection with model catalog and frozenset and str and str; why: RoutingCoordinator.public_model_listing_snapshot groups the supplied clauses as one enclosing predicate collection collection before its value is consumed. + ) -> tuple[ModelCatalog, frozenset[str], str | None]: + """Include the active runtime pin map in the same catalog/residency snapshot.""" + # What: document include the active runtime pin map in the public_model_listing_snapshot docstring; why: introspection and maintainers read this exact docstring fragment to understand public model listing snapshot behavior without executing it. + # What: enter the cond managed context before loaded set str set; why: public_model_listing_snapshot releases this resource or lock after loaded set str set on both success and failure paths. + with self._cond: + # What: compute loaded from set; why: loaded add self active name later reads loaded, so public_model_listing_snapshot must retain the computed value under that name. + loaded: set[str] = set() + # What: gate on active name and active matches engine locked before add and active name and loaded; why: public_model_listing_snapshot admits add and active name and loaded only for this predicate and excludes the opposite state. + if self._active_name is not None and self._active_matches_engine_locked(): + # What: call loaded.add with active name; why: public_model_listing_snapshot invokes loaded.add while performing if self activating name is not and self activating port; the call advances that operation through its result or side effect. + loaded.add(self._active_name) + # What: gate on activating name and activating port before profile and get and activating name and catalog; why: public_model_listing_snapshot admits profile and get and activating name and catalog only for this predicate and excludes the opposite state. + if self._activating_name is not None and self._activating_port is not None: + # What: compute profile from get and activating name and catalog; why: if self engine matches profile self activating port later reads profile, so public_model_listing_snapshot must retain the computed value under that name. + profile = self._catalog.get(self._activating_name) + # What: gate on engine matches and profile and activating port before add and activating name and loaded; why: public_model_listing_snapshot admits add and activating name and loaded only for this predicate and excludes the opposite state. + if self._engine_matches(profile, self._activating_port): + # What: call loaded.add with activating name; why: public_model_listing_snapshot invokes loaded.add while performing return self catalog frozenset loaded self active routing profile; the call advances that operation through its result or side effect. + loaded.add(self._activating_name) + # What: return catalog and active routing profile and frozenset and loaded from public_model_listing_snapshot; why: public_model_listing_snapshot exposes catalog and active routing profile and frozenset and loaded so its caller can continue with the function\'s computed outcome. + return self._catalog, frozenset(loaded), self._active_routing_profile + + # What: define active_matches_engine around the current object state; why: its direct callers call active_matches_engine for active matches engine and rely on this exact input and result contract. + def active_matches_engine(self) -> bool: + """Whether the manager still owns the exact resident routed profile. + + A listening child alone is not a readiness signal: an out-of-band or + stale child must not make the stable router URL appear healthy for the + alias recorded by the coordinator. + """ + # What: document whether the manager still owns the in the active_matches_engine docstring; why: introspection and maintainers read this exact docstring fragment to understand active matches engine behavior without executing it. + # What: document a listening child alone is not in the active_matches_engine docstring; why: introspection and maintainers read this exact docstring fragment to understand active matches engine behavior without executing it. + # What: document stale child must not make the in the active_matches_engine docstring; why: introspection and maintainers read this exact docstring fragment to understand active matches engine behavior without executing it. + # What: document alias recorded by the coordinator in the active_matches_engine docstring; why: introspection and maintainers read this exact docstring fragment to understand active matches engine behavior without executing it. + # What: preserve the paragraph boundary in the the active_matches_engine docstring; why: introspection and maintainers read this paragraph break to understand active matches engine behavior without executing it. + # What: enter the cond managed context before return self active matches engine locked; why: active_matches_engine releases this resource or lock after return self active matches engine locked on both success and failure paths. + with self._cond: + # What: return active matches engine locked from active_matches_engine; why: active_matches_engine exposes active matches engine locked so its caller can continue with the function\'s computed outcome. + return self._active_matches_engine_locked() + + # What: define profile_is_resident around model id; why: its direct callers call profile_is_resident for profile is resident and rely on this exact input and result contract. + def profile_is_resident(self, model_id: str) -> bool: + """Whether *model_id* resolves to the exact readiness-gated resident. + + This lifecycle-state query intentionally does not perform network I/O. + It lets direct static-asset requests refuse a cold activation while + using the same exact identity check as ordinary warm admission. + """ + # What: document whether model id resolves to the exact in the profile_is_resident docstring; why: introspection and maintainers read this exact docstring fragment to understand profile is resident behavior without executing it. + # What: document this lifecycle state query intentionally does not in the profile_is_resident docstring; why: introspection and maintainers read this exact docstring fragment to understand profile is resident behavior without executing it. + # What: document it lets direct static asset requests refuse in the profile_is_resident docstring; why: introspection and maintainers read this exact docstring fragment to understand profile is resident behavior without executing it. + # What: document using the same exact identity check in the profile_is_resident docstring; why: introspection and maintainers read this exact docstring fragment to understand profile is resident behavior without executing it. + # What: preserve the paragraph boundary in the the profile_is_resident docstring; why: introspection and maintainers read this paragraph break to understand profile is resident behavior without executing it. + # What: enter the cond managed context before try; why: profile_is_resident releases this resource or lock after try on both success and failure paths. + with self._cond: + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.profile_is_resident routes failures to catalog error while preserving cleanup and success flow. + try: + # What: compute profile from get and model id and catalog; why: and self active profile ready locked profile later reads profile, so profile_is_resident must retain the computed value under that name. + profile = self._catalog.get(model_id) + # What: handle catalog error by return false; why: RoutingCoordinator.profile_is_resident converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError: + # What: return false from profile_is_resident; why: profile_is_resident exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: return shutdown requested and switching and active profile ready locked and profile from profile_is_resident; why: profile_is_resident exposes shutdown requested and switching and active profile ready locked and profile so its caller can continue with the function\'s computed outcome. + return ( + # What: apply the not self shutdown requested portion of the enclosing predicate; why: this clause remains in profile_is_resident\'s enclosing expression so its grouping and evaluation order stay intact. + not self._shutdown_requested + # What: apply the and not self switching portion of the enclosing predicate; why: this clause remains in profile_is_resident\'s enclosing expression so its grouping and evaluation order stay intact. + and not self._switching + # What: call self._active_profile_ready_locked with profile; why: profile_is_resident consumes the self._active_profile_ready_locked return value while evaluating and self._active_profile_ready_locked(profile). + and self._active_profile_ready_locked(profile) + # What: complete the profile_is_resident signature with self and model id; why: RoutingCoordinator.profile_is_resident groups the supplied clauses as one profile_is_resident signature before its value is consumed. + ) + + # What: define is_ready around probe; why: its direct callers call is_ready for is ready and rely on this exact input and result contract. + def is_ready(self, probe=None) -> bool: + """Atomically verify resident identity and fresh engine readiness. + + Holding the admission condition across the bounded loopback probe keeps + a conflicting swap from committing between an identity snapshot and a + stale successful health response. + """ + # What: document atomically verify resident identity and fresh in the is_ready docstring; why: introspection and maintainers read this exact docstring fragment to understand is ready behavior without executing it. + # What: document holding the admission condition across the in the is_ready docstring; why: introspection and maintainers read this exact docstring fragment to understand is ready behavior without executing it. + # What: document a conflicting swap from committing around in the is_ready docstring; why: introspection and maintainers read this exact docstring fragment to understand is ready behavior without executing it. + # What: document stale successful health response in the is_ready docstring; why: introspection and maintainers read this exact docstring fragment to understand is ready behavior without executing it. + # What: preserve the paragraph boundary in the the is_ready docstring; why: introspection and maintainers read this paragraph break to understand is ready behavior without executing it. + # What: enter the cond managed context before if self shutdown requested or self switching or not; why: is_ready releases this resource or lock after if self shutdown requested or self switching or not on both success and failure paths. + with self._cond: + # What: gate on shutdown requested and switching and active matches engine locked before the computed value; why: is_ready admits the computed value only for this predicate and excludes the opposite state. + if self._shutdown_requested or self._switching or not self._active_matches_engine_locked(): + # What: return false from is_ready; why: is_ready exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: compute state from status and manager; why: port state get port later reads state, so is_ready must retain the computed value under that name. + state = self._manager.status() + # What: compute port from get and state and port; why: if not isinstance port int or later reads port, so is_ready must retain the computed value under that name. + port = state.get("port") + # What: gate on port and isinstance and int before the computed value; why: is_ready admits the computed value only for this predicate and excludes the opposite state. + if not isinstance(port, int) or port <= 0: + # What: return false from is_ready; why: is_ready exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: compute profile from get and active name and catalog; why: if profile check endpoint default check endpoint later reads profile, so is_ready must retain the computed value under that name. + profile = self._catalog.get(self._active_name) + # What: compute active probe from probe and probe; why: active probe fresh health port later reads active probe, so is_ready must retain the computed value under that name. + active_probe = probe or self._probe + # What: compute health from check endpoint and default check endpoint and fresh health and port; why: health get reachable later reads health, so is_ready must retain the computed value under that name. + health = ( + # What: call active_probe.fresh_health with port; why: is_ready invokes active_probe.fresh_health while performing if profile check endpoint default check endpoint; the call advances that operation through its result or side effect. + active_probe.fresh_health(port) + # What: apply the if profile check endpoint default check endpoint portion of health; why: is_ready uses this clause to evaluate health as one grouped value. + if profile.check_endpoint == DEFAULT_CHECK_ENDPOINT + # What: call active_probe.fresh_readiness with port and check endpoint and profile; why: is_ready consumes the active_probe.fresh_readiness return value while evaluating else active_probe.fresh_readiness(port, profile.check_endpoint). + else active_probe.fresh_readiness(port, profile.check_endpoint) + # What: complete the health expression with health active probe fresh health port if profile check endpoint equals default check endpoint else active probe fresh re; why: RoutingCoordinator.is_ready groups the supplied clauses as one health expression before its value is consumed. + ) + # What: gate on shutdown requested and switching and active matches engine locked before the computed value; why: is_ready admits the computed value only for this predicate and excludes the opposite state. + if self._shutdown_requested or self._switching or not self._active_matches_engine_locked(): + # What: return false from is_ready; why: is_ready exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: return bool and get and health and check endpoint from is_ready; why: is_ready exposes bool and get and health and check endpoint so its caller can continue with the function\'s computed outcome. + return bool( + # What: call health.get with reachable; why: is_ready invokes health.get while performing and; the call advances that operation through its result or side effect. + health.get("reachable") + # What: apply the and portion of the enclosing predicate; why: this clause remains in is_ready\'s enclosing expression so its grouping and evaluation order stay intact. + and ( + # What: apply the profile check endpoint default check endpoint portion of the enclosing predicate; why: this clause remains in is_ready\'s enclosing expression so its grouping and evaluation order stay intact. + profile.check_endpoint != DEFAULT_CHECK_ENDPOINT + # What: apply the or portion of the enclosing predicate; why: this clause remains in is_ready\'s enclosing expression so its grouping and evaluation order stay intact. + or ( + # What: call health.get with status; why: is_ready invokes health.get while performing and health get maintenance serving serving; the call advances that operation through its result or side effect. + health.get("status") == "ok" + # What: call health.get with maintenance and serving; why: is_ready consumes the health.get return value while evaluating and health.get("maintenance", "serving") == "serving". + and health.get("maintenance", "serving") == "serving" + # What: complete the bool call with get; why: RoutingCoordinator.is_ready groups the supplied clauses as one bool call before its value is consumed. + ) + # What: complete the bool call with get; why: RoutingCoordinator.is_ready groups the supplied clauses as one bool call before its value is consumed. + ) + # What: complete the bool call with get; why: RoutingCoordinator.is_ready groups the supplied clauses as one bool call before its value is consumed. + ) + + # What: expose upstream_timeout_s as a read-only computed property; why: callers read upstream_timeout_s through attribute access while its getter retains control of the derived value. + @property + # What: define upstream_timeout_s around the current object state; why: the registered API client call upstream_timeout_s for upstream timeout s and rely on this exact input and result contract. + def upstream_timeout_s(self) -> float: + # What: enter the cond managed context before return self catalog settings upstream timeout s; why: upstream_timeout_s releases this resource or lock after return self catalog settings upstream timeout s on both success and failure paths. + with self._cond: + # What: return upstream timeout s and settings and catalog from upstream_timeout_s; why: upstream_timeout_s exposes upstream timeout s and settings and catalog so its caller can continue with the function\'s computed outcome. + return self._catalog.settings.upstream_timeout_s + + # What: define replace_catalog around catalog; why: its direct callers call replace_catalog for replace catalog and rely on this exact input and result contract. + def replace_catalog(self, catalog: ModelCatalog) -> None: + """Atomically install a validated catalog without changing a live engine. + + Removing or redefining the active profile is refused. The operator can + explicitly unload first, which keeps configuration reload from silently + changing the ownership contract of an existing engine. + """ + # What: document atomically install a validated catalog without in the replace_catalog docstring; why: introspection and maintainers read this exact docstring fragment to understand replace catalog behavior without executing it. + # What: document removing or redefining the active profile in the replace_catalog docstring; why: introspection and maintainers read this exact docstring fragment to understand replace catalog behavior without executing it. + # What: document explicitly unload first which keeps configuration in the replace_catalog docstring; why: introspection and maintainers read this exact docstring fragment to understand replace catalog behavior without executing it. + # What: document changing the ownership contract of an in the replace_catalog docstring; why: introspection and maintainers read this exact docstring fragment to understand replace catalog behavior without executing it. + # What: preserve the paragraph boundary in the the replace_catalog docstring; why: introspection and maintainers read this paragraph break to understand replace catalog behavior without executing it. + # What: enter the cond managed context before if; why: replace_catalog releases this resource or lock after if on both success and failure paths. + with self._cond: + # What: gate on shutdown requested and switching and pending and manual lifecycle tokens before routing error; why: replace_catalog admits routing error only for this predicate and excludes the opposite state. + if ( + # What: apply the self shutdown requested portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + self._shutdown_requested + # What: apply the or self switching portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + or self._switching + # What: apply the or self pending portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + or self._pending + # What: apply the or self manual lifecycle tokens portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + or self._manual_lifecycle_tokens + # What: complete the enclosing predicate with if self shutdown requested or self switching or self pending or self manual lifecycle tokens raise; why: RoutingCoordinator.replace_catalog groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RoutingError for the caller; why: RoutingCoordinator.replace_catalog stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: apply the reload conflict portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + "reload_conflict", + # What: apply the cannot reload while admission or lifecycle portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + "cannot reload while admission or lifecycle work is in progress", + # What: supply status code to RoutingError; why: replace_catalog binds this 409 value to RoutingError's status code input. + status_code=409, + # What: complete the RoutingError call with status code; why: RoutingCoordinator.replace_catalog groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: gate on active name before replacement and current and catalog error and get and active name; why: replace_catalog admits replacement and current and catalog error and get and active name only for this predicate and excludes the opposite state. + if self._active_name is not None: + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.replace_catalog routes failures to catalog error while preserving cleanup and success flow. + try: + # What: compute replacement from get and active name and catalog; why: replacement ttl s if replacement ttl s is not else later reads replacement, so replace_catalog must retain the computed value under that name. + replacement = catalog.get(self._active_name) + # What: compute current from get and active name and catalog; why: current ttl s if current ttl s is not else later reads current, so replace_catalog must retain the computed value under that name. + current = self._catalog.get(self._active_name) + # What: handle catalog error by raise routing error; why: RoutingCoordinator.replace_catalog converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError as exc: + # What: raise RoutingError for the caller; why: RoutingCoordinator.replace_catalog stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: apply the reload conflict portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + "reload_conflict", + # What: apply the cannot remove the active profile until portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + "cannot remove the active profile until it is unloaded", + # What: supply status code to RoutingError; why: replace_catalog binds this 409 value to RoutingError's status code input. + status_code=409, + # What: apply the from exc portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + ) from exc + # What: compute current group from group for and active name and catalog; why: or replacement group current group later reads current group, so replace_catalog must retain the computed value under that name. + current_group = self._catalog.group_for(self._active_name) + # What: compute replacement group from group for and active name and catalog; why: or replacement group current group later reads replacement group, so replace_catalog must retain the computed value under that name. + replacement_group = catalog.group_for(self._active_name) + # What: compute current ttl from ttl s and default ttl s and current and settings; why: or replacement ttl current ttl later reads current ttl, so replace_catalog must retain the computed value under that name. + current_ttl = ( + # What: apply the current ttl s if current ttl s is not else portion of current ttl; why: replace_catalog uses this clause to evaluate current ttl as one grouped value. + current.ttl_s if current.ttl_s is not None else self._catalog.settings.default_ttl_s + # What: complete the current_ttl expression with current ttl current ttl s if current ttl s is not else self catalog settings default ttl s; why: RoutingCoordinator.replace_catalog groups the supplied clauses as one current_ttl expression before its value is consumed. + ) + # What: compute replacement ttl from ttl s and default ttl s and replacement and settings; why: or replacement ttl current ttl later reads replacement ttl, so replace_catalog must retain the computed value under that name. + replacement_ttl = ( + # What: apply the replacement ttl s if replacement ttl s is not else portion of replacement ttl; why: replace_catalog uses this clause to evaluate replacement ttl as one grouped value. + replacement.ttl_s if replacement.ttl_s is not None else catalog.settings.default_ttl_s + # What: complete the replacement_ttl expression with replacement ttl replacement ttl s if replacement ttl s is not else catalog settings default ttl s; why: RoutingCoordinator.replace_catalog groups the supplied clauses as one replacement_ttl expression before its value is consumed. + ) + # What: compute current unload timeout from unload timeout s and current and settings and catalog; why: or replacement unload timeout current unload timeout later reads current unload timeout, so replace_catalog must retain the computed value under that name. + current_unload_timeout = ( + # What: apply the current unload timeout s portion of current unload timeout; why: replace_catalog uses this clause to evaluate current unload timeout as one grouped value. + current.unload_timeout_s + # What: apply the if current unload timeout s is not portion of current unload timeout; why: replace_catalog uses this clause to evaluate current unload timeout as one grouped value. + if current.unload_timeout_s is not None + # What: apply the else self catalog settings unload timeout s portion of current unload timeout; why: replace_catalog uses this clause to evaluate current unload timeout as one grouped value. + else self._catalog.settings.unload_timeout_s + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + ) + # What: compute replacement unload timeout from unload timeout s and replacement and settings and catalog; why: or replacement unload timeout current unload timeout later reads replacement unload timeout, so replace_catalog must retain the computed value under that name. + replacement_unload_timeout = ( + # What: apply the replacement unload timeout s portion of replacement unload timeout; why: replace_catalog uses this clause to evaluate replacement unload timeout as one grouped value. + replacement.unload_timeout_s + # What: apply the if replacement unload timeout s is not portion of replacement unload timeout; why: replace_catalog uses this clause to evaluate replacement unload timeout as one grouped value. + if replacement.unload_timeout_s is not None + # What: apply the else catalog settings unload timeout s portion of replacement unload timeout; why: replace_catalog uses this clause to evaluate replacement unload timeout as one grouped value. + else catalog.settings.unload_timeout_s + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + ) + # What: gate on replacement and current and replacement group and current group and replacement ttl before routing error; why: replace_catalog admits routing error only for this predicate and excludes the opposite state. + if ( + # What: apply the replacement current portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + replacement != current + # What: apply the or replacement group current group portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + or replacement_group != current_group + # What: apply the or replacement ttl current ttl portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + or replacement_ttl != current_ttl + # What: apply the or replacement unload timeout current unload timeout portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + or replacement_unload_timeout != current_unload_timeout + # What: complete the enclosing predicate with if replacement differs from current or replacement group differs from; why: RoutingCoordinator.replace_catalog groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ): + # What: raise RoutingError for the caller; why: RoutingCoordinator.replace_catalog stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: apply the reload conflict portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + "reload_conflict", + # What: apply the cannot redefine the active profile until portion of the enclosing predicate; why: this clause remains in replace_catalog\'s enclosing expression so its grouping and evaluation order stay intact. + "cannot redefine the active profile until it is unloaded", + # What: supply status code to RoutingError; why: replace_catalog binds this 409 value to RoutingError's status code input. + status_code=409, + # What: complete the RoutingError call with status code; why: RoutingCoordinator.replace_catalog groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: compute catalog from catalog; why: the enclosing return or state update later reads catalog, so replace_catalog must retain the computed value under that name. + self._catalog = catalog + # Match the pinned runtime contract: config reload starts with no + # active routing profile rather than silently carrying pin state + # into a potentially different profile definition. + # What: compute active routing profile from the named fixture input; why: the enclosing return or state update later reads active routing profile, so replace_catalog must retain the computed value under that name. + self._active_routing_profile = None + # What: call self._cond.notify_all with the declared inputs; why: replace_catalog invokes self._cond.notify_all while performing the enclosing return; the call advances that operation through its result or side effect. + self._cond.notify_all() + + # What: define prometheus around the current object state; why: its direct callers call prometheus for prometheus and rely on this exact input and result contract. + def prometheus(self) -> str: + """Render bounded router counters without importing a metrics package.""" + # What: document render bounded router counters without importing in the prometheus docstring; why: introspection and maintainers read this exact docstring fragment to understand prometheus behavior without executing it. + # What: compute status from status; why: active requests status active requests later reads status, so prometheus must retain the computed value under that name. + status = self.status() + # What: compute values from status and int and active requests and reserved requests and queued requests; why: for name value in values items later reads values, so prometheus must retain the computed value under that name. + values = { + # What: map the active requests field as status and active requests; why: RoutingCoordinator.prometheus carries active requests through values into for name value in values items. + "active_requests": status["activeRequests"], + # What: map the reserved requests field as status and reserved requests; why: RoutingCoordinator.prometheus carries reserved requests through values into for name value in values items. + "reserved_requests": status["reservedRequests"], + # What: map the queued requests field as status and queued requests; why: RoutingCoordinator.prometheus carries queued requests through values into for name value in values items. + "queued_requests": status["queuedRequests"], + # What: map the shutting down field as int and status and shutting down; why: RoutingCoordinator.prometheus carries shutting down through values into for name value in values items. + "shutting_down": int(status["shuttingDown"]), + # What: map the active identity matches engine field as int and status and active identity matches engine; why: RoutingCoordinator.prometheus carries active identity matches engine through values into for name value in values items. + "active_identity_matches_engine": int(status["activeIdentityMatchesEngine"]), + # What: map the admissions total field as status and admissions; why: RoutingCoordinator.prometheus carries admissions total through values into for name value in values items. + "admissions_total": status["admissions"], + # What: map the activations total field as status and activations; why: RoutingCoordinator.prometheus carries activations total through values into for name value in values items. + "activations_total": status["activations"], + # What: map the activation failures total field as status and activation failures; why: RoutingCoordinator.prometheus carries activation failures total through values into for name value in values items. + "activation_failures_total": status["activationFailures"], + # What: map the cancellations total field as status and cancellations; why: RoutingCoordinator.prometheus carries cancellations total through values into for name value in values items. + "cancellations_total": status["cancellations"], + # What: map the terminal streams total field as status and terminal streams; why: RoutingCoordinator.prometheus carries terminal streams total through values into for name value in values items. + "terminal_streams_total": status["terminalStreams"], + # What: map the evictions total field as status and evictions; why: RoutingCoordinator.prometheus carries evictions total through values into for name value in values items. + "evictions_total": status["evictions"], + # What: complete the values mapping with active requests and reserved requests and queued requests and shutting down and active identity matches engine; why: RoutingCoordinator.prometheus groups the supplied clauses as one values mapping before its value is consumed. + } + # What: initialize lines as an empty runtime accumulator; why: RoutingCoordinator.prometheus appends or maps entries into it during lines extend f type metric metric type f metric before consuming the aggregate. + lines = [] + # What: iterate across items and values to perform metric and name; why: prometheus repeats the body only while or for the loop header admits an iteration. + for name, value in values.items(): + # What: compute metric from name and freetoken swap; why: lines extend f type metric metric type f later reads metric, so prometheus must retain the computed value under that name. + metric = f"freetoken_swap_{name}" + # What: compute metric type from endswith and name and counter and gauge and total; why: lines extend f type metric metric type f later reads metric type, so prometheus must retain the computed value under that name. + metric_type = "counter" if name.endswith("_total") else "gauge" + # What: preserve the exact lines extend f type metric metric type f literal fragment; why: prometheus passes this fragment verbatim through lines.extend((f"# TYPE {metric} {metric_type}", f"{metric} {value}")), because changing it would alter a protocol payload, serialized fixture, or public message. + lines.extend((f"# TYPE {metric} {metric_type}", f"{metric} {value}")) + # What: iterate across status to perform value and metric and extend and name and lines; why: prometheus repeats the body only while or for the loop header admits an iteration. + for name, value in ( + # What: apply the last ttft ms status last ttft ms portion of the enclosing predicate; why: this clause remains in prometheus\'s enclosing expression so its grouping and evaluation order stay intact. + ("last_ttft_ms", status["lastTtftMs"]), + # What: apply the last duration ms status last duration ms portion of the enclosing predicate; why: this clause remains in prometheus\'s enclosing expression so its grouping and evaluation order stay intact. + ("last_duration_ms", status["lastDurationMs"]), + # What: apply the last activation ms status last activation ms portion of the enclosing predicate; why: this clause remains in prometheus\'s enclosing expression so its grouping and evaluation order stay intact. + ("last_activation_ms", status["lastActivationMs"]), + # What: apply the last queue wait ms status last queue wait ms portion of the enclosing predicate; why: this clause remains in prometheus\'s enclosing expression so its grouping and evaluation order stay intact. + ("last_queue_wait_ms", status["lastQueueWaitMs"]), + # What: apply the last response bytes status last response bytes portion of the enclosing predicate; why: this clause remains in prometheus\'s enclosing expression so its grouping and evaluation order stay intact. + ("last_response_bytes", status["lastResponseBytes"]), + # What: apply the last proxy bytes per second status last proxy bytes per second portion of the enclosing predicate; why: this clause remains in prometheus\'s enclosing expression so its grouping and evaluation order stay intact. + ("last_proxy_bytes_per_second", status["lastProxyBytesPerSecond"]), + # What: execute the grouped source fragment; why: the enclosing symbol requires this operation for its concrete qualification or routing path. + ): + # What: gate on value before metric and name; why: prometheus admits metric and name only for this predicate and excludes the opposite state. + if value is not None: + # What: compute metric from name and freetoken swap; why: lines extend f type metric gauge f later reads metric, so prometheus must retain the computed value under that name. + metric = f"freetoken_swap_{name}" + # What: preserve the exact lines extend f type metric gauge f literal fragment; why: prometheus passes this fragment verbatim through lines.extend((f"# TYPE {metric} gauge", f"{metric} {value}")), because changing it would alter a protocol payload, serialized fixture, or public message. + lines.extend((f"# TYPE {metric} gauge", f"{metric} {value}")) + # What: return join and lines and value and value from prometheus; why: prometheus exposes join and lines and value and value so its caller can continue with the function\'s computed outcome. + return "\n".join(lines) + "\n" + + # What: define record_cancellation around the current object state; why: its direct callers call record_cancellation for record cancellation and rely on this exact input and result contract. + def record_cancellation(self) -> None: + # What: enter the cond managed context before self cancellations; why: record_cancellation releases this resource or lock after self cancellations on both success and failure paths. + with self._cond: + # What: compute cancellations from 1; why: the enclosing return or state update later reads cancellations, so record_cancellation must retain the computed value under that name. + self._cancellations += 1 + + # What: define record_stream around ttft s and duration s and response bytes and completed; why: its direct callers call record_stream for record stream and rely on this exact input and result contract. + def record_stream( + # What: declare the self input for record_stream; why: record_stream consumes self during with self cond, so callers must bind it with the other signature inputs. + self, *, ttft_s: float | None, duration_s: float, response_bytes: int, completed: bool = True + # What: complete the enclosing predicate with group delimiter; why: RoutingCoordinator.record_stream groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> None: + """Record transport timing without crediting a router-cancelled stream as complete.""" + # What: document record transport timing without crediting a in the record_stream docstring; why: introspection and maintainers read this exact docstring fragment to understand record stream behavior without executing it. + # What: enter the cond managed context before if completed; why: record_stream releases this resource or lock after if completed on both success and failure paths. + with self._cond: + # What: gate on completed before terminal streams; why: record_stream admits terminal streams only for this predicate and excludes the opposite state. + if completed: + # What: compute terminal streams from 1; why: the enclosing return or state update later reads terminal streams, so record_stream must retain the computed value under that name. + self._terminal_streams += 1 + # What: compute last ttft ms from ttft s and round and 3 and 1000; why: the enclosing return or state update later reads last ttft ms, so record_stream must retain the computed value under that name. + self._last_ttft_ms = round(ttft_s * 1000, 3) if ttft_s is not None else None + # What: compute last duration ms from round and duration s and 3 and 1000; why: the enclosing return or state update later reads last duration ms, so record_stream must retain the computed value under that name. + self._last_duration_ms = round(duration_s * 1000, 3) + # What: compute last response bytes from response bytes; why: the enclosing return or state update later reads last response bytes, so record_stream must retain the computed value under that name. + self._last_response_bytes = response_bytes + # What: compute last proxy bytes per second from duration s and round and response bytes and 0 and 3; why: the enclosing return or state update later reads last proxy bytes per second, so record_stream must retain the computed value under that name. + self._last_proxy_bytes_per_second = round(response_bytes / duration_s, 3) if duration_s > 0 else None + + # What: define evict_idle around name; why: its direct callers call evict_idle for evict idle and rely on this exact input and result contract. + def evict_idle(self, name: str | None = None) -> bool: + """Unload a truly idle matching engine, preserving lifecycle accounting. + + The timer calls this method, and tests may call it directly. A stale + timer cannot unload a newer profile because identity is checked under + admission before entering the manager lifecycle transaction. + """ + # What: document unload a truly idle matching engine in the evict_idle docstring; why: introspection and maintainers read this exact docstring fragment to understand evict idle behavior without executing it. + # What: document the timer calls this method and in the evict_idle docstring; why: introspection and maintainers read this exact docstring fragment to understand evict idle behavior without executing it. + # What: document timer cannot unload a newer profile in the evict_idle docstring; why: introspection and maintainers read this exact docstring fragment to understand evict idle behavior without executing it. + # What: document admission before entering the manager lifecycle in the evict_idle docstring; why: introspection and maintainers read this exact docstring fragment to understand evict idle behavior without executing it. + # What: preserve the paragraph boundary in the the evict_idle docstring; why: introspection and maintainers read this paragraph break to understand evict idle behavior without executing it. + # What: enter the cond managed context before if self shutdown requested; why: evict_idle releases this resource or lock after if self shutdown requested on both success and failure paths. + with self._cond: + # What: gate on shutdown requested before the computed value; why: evict_idle admits the computed value only for this predicate and excludes the opposite state. + if self._shutdown_requested: + # What: return false from evict_idle; why: evict_idle exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: gate on name before name and catalog error and get and catalog; why: evict_idle admits name and catalog error and get and catalog only for this predicate and excludes the opposite state. + if name is not None: + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.evict_idle routes failures to catalog error while preserving cleanup and success flow. + try: + # What: compute name from name and get and catalog; why: if name is not and active later reads name, so evict_idle must retain the computed value under that name. + name = self._catalog.get(name).name + # What: handle catalog error by return false; why: RoutingCoordinator.evict_idle converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError: + # What: return false from evict_idle; why: evict_idle exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: compute active from active name; why: if name is not and active later reads active, so evict_idle must retain the computed value under that name. + active = self._active_name + # What: gate on name and active before the computed value; why: evict_idle admits the computed value only for this predicate and excludes the opposite state. + if name is not None and active != name: + # What: return false from evict_idle; why: evict_idle exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: gate on leases and switching and active before the computed value; why: evict_idle admits the computed value only for this predicate and excludes the opposite state. + if active is None or self._leases or self._switching: + # What: return false from evict_idle; why: evict_idle exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: compute profile from get and active and catalog; why: port self port for profile later reads profile, so evict_idle must retain the computed value under that name. + profile = self._catalog.get(active) + # What: compute port from port for and profile; why: if not self matches active profile port later reads port, so evict_idle must retain the computed value under that name. + port = self._port_for(profile) + # What: gate on matches active and profile and port before active name; why: evict_idle admits active name only for this predicate and excludes the opposite state. + if not self._matches_active(profile, port): + # What: compute active name from the named fixture input; why: self active name later reads active name, so evict_idle must retain the computed value under that name. + self._active_name = None + # What: compute idle timer from the named fixture input; why: self idle timer later reads idle timer, so evict_idle must retain the computed value under that name. + self._idle_timer = None + # What: call self._cond.notify_all with the declared inputs; why: evict_idle invokes self._cond.notify_all while performing return; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: return false from evict_idle; why: evict_idle exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: compute switching from true; why: self switching later reads switching, so evict_idle must retain the computed value under that name. + self._switching = True + # What: compute idle timer from the named fixture input; why: the enclosing return or state update later reads idle timer, so evict_idle must retain the computed value under that name. + self._idle_timer = None + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.evict_idle routes failures to exception while preserving cleanup and success flow. + try: + # What: compute timeout from unload timeout s and profile and settings and catalog; why: self manager stop timeout timeout later reads timeout, so evict_idle must retain the computed value under that name. + timeout = profile.unload_timeout_s or self._catalog.settings.unload_timeout_s + # What: supply timeout to self._manager.stop; why: evict_idle binds this timeout value to self._manager.stop's timeout input. + self._manager.stop(timeout=timeout) + # What: handle exception by with self cond; why: RoutingCoordinator.evict_idle converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception: + # What: enter the cond managed context before self switching; why: evict_idle releases this resource or lock after self switching on both success and failure paths. + with self._cond: + # What: compute switching from false; why: self switching later reads switching, so evict_idle must retain the computed value under that name. + self._switching = False + # What: call self._cond.notify_all with the declared inputs; why: evict_idle invokes self._cond.notify_all while performing raise; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: re-propagate the active failure to the caller; why: RoutingCoordinator.evict_idle stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: enter the cond managed context before self active name; why: evict_idle releases this resource or lock after self active name on both success and failure paths. + with self._cond: + # What: compute active name from the named fixture input; why: the enclosing return or state update later reads active name, so evict_idle must retain the computed value under that name. + self._active_name = None + # What: compute switching from false; why: the enclosing return or state update later reads switching, so evict_idle must retain the computed value under that name. + self._switching = False + # What: compute evictions from 1; why: the enclosing return or state update later reads evictions, so evict_idle must retain the computed value under that name. + self._evictions += 1 + # What: call self._cond.notify_all with the declared inputs; why: evict_idle invokes self._cond.notify_all while performing return; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: return true from evict_idle; why: evict_idle exposes true so its caller can continue with the function\'s computed outcome. + return True + + # What: define begin_shutdown around the current object state; why: its direct callers call begin_shutdown for begin shutdown and rely on this exact input and result contract. + def begin_shutdown(self) -> object: + """Close admission immediately, before executor-side lifecycle work can queue.""" + # What: document close admission immediately before executor side lifecycle in the begin_shutdown docstring; why: introspection and maintainers read this exact docstring fragment to understand begin shutdown behavior without executing it. + # What: enter the cond managed context before if self shutdown requested; why: begin_shutdown releases this resource or lock after if self shutdown requested on both success and failure paths. + with self._cond: + # What: gate on shutdown requested before routing error; why: begin_shutdown admits routing error only for this predicate and excludes the opposite state. + if self._shutdown_requested: + # What: raise RoutingError for the caller; why: RoutingCoordinator.begin_shutdown stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: supply status code to RoutingError; why: begin_shutdown binds this 409 value to RoutingError's status code input. + "router_shutting_down", "router shutdown is already in progress", status_code=409 + # What: complete the RoutingError call with status code; why: RoutingCoordinator.begin_shutdown groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: compute owner from object; why: self shutdown owner owner later reads owner, so begin_shutdown must retain the computed value under that name. + owner = object() + # What: compute shutdown requested from true; why: the enclosing return or state update later reads shutdown requested, so begin_shutdown must retain the computed value under that name. + self._shutdown_requested = True + # What: compute shutdown owner from owner; why: the enclosing return or state update later reads shutdown owner, so begin_shutdown must retain the computed value under that name. + self._shutdown_owner = owner + # What: call self._cancel_idle_timer with the declared inputs; why: begin_shutdown invokes self._cancel_idle_timer while performing self cond notify all; the call advances that operation through its result or side effect. + self._cancel_idle_timer() + # What: call self._cond.notify_all with the declared inputs; why: begin_shutdown invokes self._cond.notify_all while performing return owner; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: return owner from begin_shutdown; why: begin_shutdown exposes owner so its caller can continue with the function\'s computed outcome. + return owner + + # What: define finish_shutdown around owner and timeout and force; why: its direct callers call finish_shutdown for finish shutdown and rely on this exact input and result contract. + def finish_shutdown( + # What: declare the self input for finish_shutdown; why: finish_shutdown consumes self during return self finish exit owner lambda self manager shutdown timeout, so callers must bind it with the other signature inputs. + self, owner: object, timeout: float | None = None, force: bool = False + # What: complete the enclosing predicate with dict; why: RoutingCoordinator.finish_shutdown groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> dict: + """Drain existing ownership and permanently stop the sole managed child.""" + # What: document drain existing ownership and permanently stop in the finish_shutdown docstring; why: introspection and maintainers read this exact docstring fragment to understand finish shutdown behavior without executing it. + # What: return finish exit and owner and shutdown and timeout from finish_shutdown; why: finish_shutdown exposes finish exit and owner and shutdown and timeout so its caller can continue with the function\'s computed outcome. + return self._finish_exit(owner, lambda: self._manager.shutdown(timeout, force)) + + # What: define finish_detach around owner; why: its direct callers call finish_detach for finish detach and rely on this exact input and result contract. + def finish_detach(self, owner: object) -> None: + """Drain existing ownership, then leave the child persisted for re-adoption.""" + # What: document drain existing ownership then leave the in the finish_detach docstring; why: introspection and maintainers read this exact docstring fragment to understand finish detach behavior without executing it. + # What: call self._finish_exit with owner and detach and manager; why: finish_detach invokes self._finish_exit while performing the enclosing return; the call advances that operation through its result or side effect. + self._finish_exit(owner, self._manager.detach) + + # What: define _finish_exit around owner and action; why: its direct callers call _finish_exit for finish exit and rely on this exact input and result contract. + def _finish_exit(self, owner: object, action: Callable[[], object]): + # What: enter the cond managed context before if self shutdown owner is not owner; why: _finish_exit releases this resource or lock after if self shutdown owner is not owner on both success and failure paths. + with self._cond: + # What: gate on shutdown owner and owner before value error; why: _finish_exit admits value error only for this predicate and excludes the opposite state. + if self._shutdown_owner is not owner: + # What: raise ValueError for the caller; why: RoutingCoordinator._finish_exit stops this rejected path before it can mutate state, dispatch work, or report success. + raise ValueError("shutdown reservation is not owned by caller") + # What: iterate across leases and switching and manual lifecycle tokens to perform wait and cond; why: _finish_exit repeats the body only while or for the loop header admits an iteration. + while self._leases or self._switching or self._manual_lifecycle_tokens: + # What: call self._cond.wait with the declared inputs; why: _finish_exit invokes self._cond.wait while performing self switching; the call advances that operation through its result or side effect. + self._cond.wait() + # What: compute switching from true; why: self switching later reads switching, so _finish_exit must retain the computed value under that name. + self._switching = True + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator._finish_exit routes failures to exception while preserving cleanup and success flow. + try: + # What: compute result from action; why: return result later reads result, so _finish_exit must retain the computed value under that name. + result = action() + # What: handle exception by with self cond; why: RoutingCoordinator._finish_exit converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception: + # What: enter the cond managed context before self shutdown requested; why: _finish_exit releases this resource or lock after self shutdown requested on both success and failure paths. + with self._cond: + # What: compute shutdown requested from false; why: the enclosing return or state update later reads shutdown requested, so _finish_exit must retain the computed value under that name. + self._shutdown_requested = False + # What: compute shutdown owner from the named fixture input; why: self shutdown owner later reads shutdown owner, so _finish_exit must retain the computed value under that name. + self._shutdown_owner = None + # What: compute switching from false; why: self switching later reads switching, so _finish_exit must retain the computed value under that name. + self._switching = False + # What: call self._schedule_idle_eviction with the declared inputs; why: _finish_exit invokes self._schedule_idle_eviction while performing self cond notify all; the call advances that operation through its result or side effect. + self._schedule_idle_eviction() + # What: call self._cond.notify_all with the declared inputs; why: _finish_exit invokes self._cond.notify_all while performing raise; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: re-propagate the active failure to the caller; why: RoutingCoordinator._finish_exit stops this rejected path before it can mutate state, dispatch work, or report success. + raise + # What: enter the cond managed context before self active name; why: _finish_exit releases this resource or lock after self active name on both success and failure paths. + with self._cond: + # What: compute active name from the named fixture input; why: the enclosing return or state update later reads active name, so _finish_exit must retain the computed value under that name. + self._active_name = None + # What: compute shutdown owner from the named fixture input; why: the enclosing return or state update later reads shutdown owner, so _finish_exit must retain the computed value under that name. + self._shutdown_owner = None + # What: compute switching from false; why: the enclosing return or state update later reads switching, so _finish_exit must retain the computed value under that name. + self._switching = False + # What: call self._cond.notify_all with the declared inputs; why: _finish_exit invokes self._cond.notify_all while performing return result; the call advances that operation through its result or side effect. + self._cond.notify_all() + # What: return result from _finish_exit; why: _finish_exit exposes result so its caller can continue with the function\'s computed outcome. + return result + + # What: define shutdown around timeout and force; why: its direct callers call shutdown for shutdown and rely on this exact input and result contract. + def shutdown(self, timeout: float | None = None, force: bool = False) -> dict: + """Synchronous convenience wrapper for a complete shutdown transaction.""" + # What: document synchronous convenience wrapper for a complete in the shutdown docstring; why: introspection and maintainers read this exact docstring fragment to understand shutdown behavior without executing it. + # What: return finish shutdown and timeout and force and begin shutdown from shutdown; why: shutdown exposes finish shutdown and timeout and force and begin shutdown so its caller can continue with the function\'s computed outcome. + return self.finish_shutdown(self.begin_shutdown(), timeout, force) + + # What: define coordinated_exit around stop child; why: its direct callers call coordinated_exit for coordinated exit and rely on this exact input and result contract. + def coordinated_exit(self, *, stop_child: bool) -> object | None: + """Idempotently quiesce for an OS/lifespan exit using the configured child policy.""" + # What: document idempotently quiesce for an os lifespan in the coordinated_exit docstring; why: introspection and maintainers read this exact docstring fragment to understand coordinated exit behavior without executing it. + # What: iterate across the computed value to perform owner and routing error and begin shutdown and cond and shutdown requested; why: coordinated_exit repeats the body only while or for the loop header admits an iteration. + while True: + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator.coordinated_exit routes failures to routing error while preserving cleanup and success flow. + try: + # What: compute owner from begin shutdown; why: return self finish shutdown owner later reads owner, so coordinated_exit must retain the computed value under that name. + owner = self.begin_shutdown() + # What: apply the break portion of the enclosing predicate; why: this clause remains in coordinated_exit\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: handle routing error by with self cond; why: RoutingCoordinator.coordinated_exit converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError: + # What: enter the cond managed context before while self shutdown owner is not; why: coordinated_exit releases this resource or lock after while self shutdown owner is not on both success and failure paths. + with self._cond: + # What: iterate across shutdown owner to perform wait and cond; why: coordinated_exit repeats the body only while or for the loop header admits an iteration. + while self._shutdown_owner is not None: + # What: call self._cond.wait with the declared inputs; why: coordinated_exit invokes self._cond.wait while performing if self shutdown requested; the call advances that operation through its result or side effect. + self._cond.wait() + # What: gate on shutdown requested before the computed value; why: coordinated_exit admits the computed value only for this predicate and excludes the opposite state. + if self._shutdown_requested: + # What: return no value from coordinated_exit; why: coordinated_exit returns no value to callers that depend on its completed result. + return None + # What: gate on stop child before finish shutdown and owner; why: coordinated_exit admits finish shutdown and owner only for this predicate and excludes the opposite state. + if stop_child: + # What: return finish shutdown and owner from coordinated_exit; why: coordinated_exit exposes finish shutdown and owner so its caller can continue with the function\'s computed outcome. + return self.finish_shutdown(owner) + # What: return finish detach and owner from coordinated_exit; why: coordinated_exit exposes finish detach and owner so its caller can continue with the function\'s computed outcome. + return self.finish_detach(owner) + + # What: apply staticmethod behavior to _new_timer; why: Python attaches this named decorator's registration or descriptor semantics to _new_timer. + @staticmethod + # What: define _new_timer around delay and callback; why: the registered API client call _new_timer for new timer and rely on this exact input and result contract. + def _new_timer(delay: float, callback: Callable[[], None]): + # What: compute timer from timer and delay and callback and threading; why: timer daemon later reads timer, so _new_timer must retain the computed value under that name. + timer = threading.Timer(delay, callback) + # What: compute daemon from true; why: the enclosing return or state update later reads daemon, so _new_timer must retain the computed value under that name. + timer.daemon = True + # What: return timer from _new_timer; why: _new_timer exposes timer so its caller can continue with the function\'s computed outcome. + return timer + + # What: define _cancel_idle_timer around the current object state; why: its direct callers call _cancel_idle_timer for cancel idle timer and rely on this exact input and result contract. + def _cancel_idle_timer(self) -> None: + # What: gate on idle timer before cancel and idle timer; why: _cancel_idle_timer admits cancel and idle timer only for this predicate and excludes the opposite state. + if self._idle_timer is not None: + # What: call self._idle_timer.cancel with the declared inputs; why: _cancel_idle_timer invokes self._idle_timer.cancel while performing self idle timer; the call advances that operation through its result or side effect. + self._idle_timer.cancel() + # What: compute idle timer from the named fixture input; why: the enclosing return or state update later reads idle timer, so _cancel_idle_timer must retain the computed value under that name. + self._idle_timer = None + + # What: define _schedule_idle_eviction around the current object state; why: its direct callers call _schedule_idle_eviction for schedule idle eviction and rely on this exact input and result contract. + def _schedule_idle_eviction(self) -> None: + # What: gate on active name before the computed value; why: _schedule_idle_eviction admits the computed value only for this predicate and excludes the opposite state. + if self._active_name is None: + # What: return no value from _schedule_idle_eviction; why: _schedule_idle_eviction returns no value to callers that depend on its completed result. + return + # What: compute profile from get and active name and catalog; why: ttl profile ttl s if profile ttl s is not later reads profile, so _schedule_idle_eviction must retain the computed value under that name. + profile = self._catalog.get(self._active_name) + # What: compute ttl from ttl s and default ttl s and profile and settings; why: if ttl later reads ttl, so _schedule_idle_eviction must retain the computed value under that name. + ttl = profile.ttl_s if profile.ttl_s is not None else self._catalog.settings.default_ttl_s + # What: gate on ttl before the computed value; why: _schedule_idle_eviction admits the computed value only for this predicate and excludes the opposite state. + if ttl <= 0: + # What: return no value from _schedule_idle_eviction; why: _schedule_idle_eviction returns no value to callers that depend on its completed result. + return + # What: call self._cancel_idle_timer with the declared inputs; why: _schedule_idle_eviction invokes self._cancel_idle_timer while performing timer self timer factory ttl lambda self evict idle profile name; the call advances that operation through its result or side effect. + self._cancel_idle_timer() + # What: compute timer from timer factory and ttl and evict idle and name; why: self idle timer timer later reads timer, so _schedule_idle_eviction must retain the computed value under that name. + timer = self._timer_factory(ttl, lambda: self.evict_idle(profile.name)) + # What: compute idle timer from timer; why: the enclosing return or state update later reads idle timer, so _schedule_idle_eviction must retain the computed value under that name. + self._idle_timer = timer + # What: call timer.start with the declared inputs; why: _schedule_idle_eviction invokes timer.start while performing the enclosing return; the call advances that operation through its result or side effect. + timer.start() + + # What: define _capacity_block around target; why: its direct callers call _capacity_block for capacity block and rely on this exact input and result contract. + def _capacity_block(self, target: ModelProfile) -> str | None: + """Return a capacity-policy explanation, if a swap cannot be admitted.""" + # What: document return a capacity policy explanation if a in the _capacity_block docstring; why: introspection and maintainers read this exact docstring fragment to understand capacity block behavior without executing it. + # What: gate on active name and name and target before the computed value; why: _capacity_block admits the computed value only for this predicate and excludes the opposite state. + if self._active_name is None or self._active_name == target.name: + # What: return no value from _capacity_block; why: _capacity_block returns no value to callers that depend on its completed result. + return None + # What: compute active group from group for and active name and catalog; why: if active group is not and active group persistent later reads active group, so _capacity_block must retain the computed value under that name. + active_group = self._catalog.group_for(self._active_name) + # What: compute target group from group for and name and catalog and target; why: if target group is not and target group persistent later reads target group, so _capacity_block must retain the computed value under that name. + target_group = self._catalog.group_for(target.name) + # What: gate on persistent and active group before active name; why: _capacity_block admits active name only for this predicate and excludes the opposite state. + if active_group is not None and active_group.persistent: + # What: return active name and active and profile and is and persistent from _capacity_block; why: _capacity_block exposes active name and active and profile and is and persistent so its caller can continue with the function\'s computed outcome. + return ( + # What: preserve the exact f active profile self active name r is literal fragment; why: _capacity_block passes this fragment verbatim through f"active profile {self._active_name!r} is persistent and consumes the ", because changing it would alter a protocol payload, serialized fixture, or public messa. + # What: preserve the exact single resident model slot unload it before literal fragment; why: _capacity_block passes this fragment verbatim through f"active profile {self._active_name!r} is persistent and consumes the ", because changing it would alter a protocol payload, serialized fixture, or public. + f"active profile {self._active_name!r} is persistent and consumes the " + "single resident-model slot; unload it before selecting another profile" + # What: complete the enclosing predicate with return f active profile self active name r is persistent and; why: RoutingCoordinator._capacity_block groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) + # What: gate on persistent and target group before name and target; why: _capacity_block admits name and target only for this predicate and excludes the opposite state. + if target_group is not None and target_group.persistent: + # What: return name and target and profile and requires and a from _capacity_block; why: _capacity_block exposes name and target and profile and requires and a so its caller can continue with the function\'s computed outcome. + return ( + # What: preserve the exact f profile target name r requires a literal fragment; why: _capacity_block passes this fragment verbatim through f"profile {target.name!r} requires a persistent resident slot; unload th, because changing it would alter a protocol payload, serialized fixture, or public message. + # What: preserve the exact current profile before selecting it literal fragment; why: _capacity_block passes this fragment verbatim through f"profile {target.name!r} requires a persistent resident slot; unload th, because changing it would alter a protocol payload, serialized fixture, or public message. + f"profile {target.name!r} requires a persistent resident slot; unload the " + "current profile before selecting it" + # What: complete the enclosing predicate with return f profile target name r requires a persistent resident; why: RoutingCoordinator._capacity_block groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) + # What: return no value from _capacity_block; why: _capacity_block returns no value to callers that depend on its completed result. + return None + + # What: define _remove_pending_locked around ticket and cancellation; why: its direct callers call _remove_pending_locked for remove pending locked and rely on this exact input and result contract. + def _remove_pending_locked( + # What: declare the self input for _remove_pending_locked; why: _remove_pending_locked consumes self during self pending remove ticket, so callers must bind it with the other signature inputs. + self, + # What: declare the ticket input for _remove_pending_locked; why: _remove_pending_locked consumes ticket during self pending remove ticket, so callers must bind it with the other signature inputs. + ticket: tuple[int, int, str], + # What: declare the cancellation input for _remove_pending_locked; why: _remove_pending_locked consumes cancellation during pending self pending by cancellation get cancellation if cancellation is, so callers must bind it with the other signature inputs. + cancellation: threading.Event | None, + # What: complete the enclosing predicate with bool; why: RoutingCoordinator._remove_pending_locked groups the supplied clauses as one enclosing predicate expression before its value is consumed. + ) -> bool: + """Idempotently remove one ticket and its optional progress lookup.""" + # What: document idempotently remove one ticket and its in the _remove_pending_locked docstring; why: introspection and maintainers read this exact docstring fragment to understand remove pending locked behavior without executing it. + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator._remove_pending_locked routes failures to value error while preserving cleanup and success flow. + try: + # What: call self._pending.remove with ticket; why: _remove_pending_locked invokes self._pending.remove while performing except value error; the call advances that operation through its result or side effect. + self._pending.remove(ticket) + # What: handle value error by removed false; why: RoutingCoordinator._remove_pending_locked converts that failure into this concrete recovery, response, or cleanup behavior. + except ValueError: + # What: compute removed from false; why: removed later reads removed, so _remove_pending_locked must retain the computed value under that name. + removed = False + # What: select the remaining branch that performs removed; why: _remove_pending_locked covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute removed from true; why: return removed later reads removed, so _remove_pending_locked must retain the computed value under that name. + removed = True + # What: compute pending from cancellation and get and pending by cancellation; why: if pending is not and pending later reads pending, so _remove_pending_locked must retain the computed value under that name. + pending = self._pending_by_cancellation.get(cancellation) if cancellation is not None else None + # What: gate on pending and ticket before pop and cancellation and pending by cancellation; why: _remove_pending_locked admits pop and cancellation and pending by cancellation only for this predicate and excludes the opposite state. + if pending is not None and pending[0] == ticket: + # What: call self._pending_by_cancellation.pop with cancellation and the named fixture input; why: _remove_pending_locked invokes self._pending_by_cancellation.pop while performing return removed; the call advances that operation through its result or side effect. + self._pending_by_cancellation.pop(cancellation, None) + # What: return removed from _remove_pending_locked; why: _remove_pending_locked exposes removed so its caller can continue with the function\'s computed outcome. + return removed + + # What: define _active_profile_ready_locked around profile; why: its direct callers call _active_profile_ready_locked for active profile ready locked and rely on this exact input and result contract. + def _active_profile_ready_locked(self, profile: ModelProfile) -> bool: + """Whether *profile* is the exact readiness-gated resident engine.""" + # What: document whether profile is the exact readiness gated in the _active_profile_ready_locked docstring; why: introspection and maintainers read this exact docstring fragment to understand active profile ready locked behavior without executing it. + # What: gate on active name and name and profile before the computed value; why: _active_profile_ready_locked admits the computed value only for this predicate and excludes the opposite state. + if self._active_name != profile.name: + # What: return false from _active_profile_ready_locked; why: _active_profile_ready_locked exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: return engine matches and profile and port for from _active_profile_ready_locked; why: _active_profile_ready_locked exposes engine matches and profile and port for so its caller can continue with the function\'s computed outcome. + return self._engine_matches(profile, self._port_for(profile)) + + # What: define _reserve_concurrency_locked around profile; why: its direct callers call _reserve_concurrency_locked for reserve concurrency locked and rely on this exact input and result contract. + def _reserve_concurrency_locked(self, profile: ModelProfile) -> None: + """Reserve active/queued capacity or reject immediately like the pinned scheduler.""" + # What: document reserve active queued capacity or reject in the _reserve_concurrency_locked docstring; why: introspection and maintainers read this exact docstring fragment to understand reserve concurrency locked behavior without executing it. + # What: compute global limit from global concurrency limit and settings and catalog; why: if global limit and self reservations global limit or later reads global limit, so _reserve_concurrency_locked must retain the computed value under that name. + global_limit = self._catalog.settings.global_concurrency_limit + # What: compute profile limit from concurrency limit and default profile concurrency limit and profile; why: if global limit and self reservations global limit or later reads profile limit, so _reserve_concurrency_locked must retain the computed value under that name. + profile_limit = profile.concurrency_limit or DEFAULT_PROFILE_CONCURRENCY_LIMIT + # What: compute profile reserved from get and name and profile reservations and profile and 0; why: if global limit and self reservations global limit or later reads profile reserved, so _reserve_concurrency_locked must retain the computed value under that name. + profile_reserved = self._profile_reservations.get(profile.name, 0) + # What: gate on global limit and profile reserved and profile limit and reservations before routing error and name and profile; why: _reserve_concurrency_locked admits routing error and name and profile only for this predicate and excludes the opposite state. + if (global_limit and self._reservations >= global_limit) or profile_reserved >= profile_limit: + # What: raise RoutingError for the caller; why: RoutingCoordinator._reserve_concurrency_locked stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: apply the concurrency limit portion of the enclosing predicate; why: this clause remains in _reserve_concurrency_locked\'s enclosing expression so its grouping and evaluation order stay intact. + "concurrency_limit", + # What: apply the f concurrency limit reached for profile portion of the enclosing predicate; why: this clause remains in _reserve_concurrency_locked\'s enclosing expression so its grouping and evaluation order stay intact. + f"concurrency limit reached for profile {profile.name!r}", + # What: supply status code to RoutingError; why: _reserve_concurrency_locked binds this 429 value to RoutingError's status code input. + status_code=429, + # What: complete the RoutingError call with status code; why: RoutingCoordinator._reserve_concurrency_locked groups the supplied clauses as one RoutingError call before its value is consumed. + ) + # What: compute reservations from 1; why: the enclosing return or state update later reads reservations, so _reserve_concurrency_locked must retain the computed value under that name. + self._reservations += 1 + # What: compute profile reservations entry from profile reserved and 1; why: the enclosing return or state update later reads profile reservations entry, so _reserve_concurrency_locked must retain the computed value under that name. + self._profile_reservations[profile.name] = profile_reserved + 1 + + # What: define _drop_concurrency_reservation_locked around profile; why: its direct callers call _drop_concurrency_reservation_locked for drop concurrency reservation locked and rely on this exact input and result contract. + def _drop_concurrency_reservation_locked(self, profile: ModelProfile) -> None: + # What: compute count from get and name and profile reservations and profile and 0; why: if self reservations or count later reads count, so _drop_concurrency_reservation_locked must retain the computed value under that name. + count = self._profile_reservations.get(profile.name, 0) + # What: gate on reservations and count before runtime error; why: _drop_concurrency_reservation_locked admits runtime error only for this predicate and excludes the opposite state. + if self._reservations <= 0 or count <= 0: + # What: raise RuntimeError for the caller; why: RoutingCoordinator._drop_concurrency_reservation_locked stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("routing concurrency reservation underflow") + # What: compute reservations from 1; why: the enclosing return or state update later reads reservations, so _drop_concurrency_reservation_locked must retain the computed value under that name. + self._reservations -= 1 + # What: gate on count before pop and name and profile reservations and profile; why: _drop_concurrency_reservation_locked admits pop and name and profile reservations and profile only for this predicate and excludes the opposite state. + if count == 1: + # What: call self._profile_reservations.pop with name and profile; why: _drop_concurrency_reservation_locked invokes self._profile_reservations.pop while performing else; the call advances that operation through its result or side effect. + self._profile_reservations.pop(profile.name) + # What: select the remaining branch that performs self profile reservations profile name count; why: _drop_concurrency_reservation_locked covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: compute profile reservations entry from count and 1; why: the enclosing return or state update later reads profile reservations entry, so _drop_concurrency_reservation_locked must retain the computed value under that name. + self._profile_reservations[profile.name] = count - 1 + + # What: define _matches_active around profile and port; why: its direct callers call _matches_active for matches active and rely on this exact input and result contract. + def _matches_active(self, profile: ModelProfile, port: int) -> bool: + # What: return active name and name and engine matches and profile from _matches_active; why: _matches_active exposes active name and name and engine matches and profile so its caller can continue with the function\'s computed outcome. + return self._active_name == profile.name and self._engine_matches(profile, port) + + # What: define _engine_matches around profile and port; why: its direct callers call _engine_matches for engine matches and rely on this exact input and result contract. + def _engine_matches(self, profile: ModelProfile, port: int) -> bool: + # What: compute state from status and manager; why: state get running later reads state, so _engine_matches must retain the computed value under that name. + state = self._manager.status() + # What: return bool and get and model and port from _engine_matches; why: _engine_matches exposes bool and get and model and port so its caller can continue with the function\'s computed outcome. + return bool( + # What: call state.get with running; why: _engine_matches invokes state.get while performing and state get model profile model; the call advances that operation through its result or side effect. + state.get("running") + # What: call state.get with model; why: _engine_matches invokes state.get while performing and state get port port; the call advances that operation through its result or side effect. + and state.get("model") == profile.model + # What: call state.get with port; why: _engine_matches invokes state.get while performing and self manager serve args list profile args; the call advances that operation through its result or side effect. + and state.get("port") == port + # What: call self._manager.serve_args with the declared inputs; why: _engine_matches consumes the self._manager.serve_args return value while evaluating and self._manager.serve_args() == list(profile.args). + and self._manager.serve_args() == list(profile.args) + # What: complete the bool call with get; why: RoutingCoordinator._engine_matches groups the supplied clauses as one bool call before its value is consumed. + ) + + # What: define _active_matches_engine_locked around the current object state; why: its direct callers call _active_matches_engine_locked for active matches engine locked and rely on this exact input and result contract. + def _active_matches_engine_locked(self) -> bool: + """Internal exact-identity check; caller holds ``self._cond``.""" + # What: document internal exact identity check caller holds self cond in the _active_matches_engine_locked docstring; why: introspection and maintainers read this exact docstring fragment to understand active matches engine locked behavior without executing it. + # What: gate on active name before the computed value; why: _active_matches_engine_locked admits the computed value only for this predicate and excludes the opposite state. + if self._active_name is None: + # What: return false from _active_matches_engine_locked; why: _active_matches_engine_locked exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: establish the handler boundary for the protected operation; why: RoutingCoordinator._active_matches_engine_locked routes failures to catalog error while preserving cleanup and success flow. + try: + # What: compute profile from get and active name and catalog; why: return self matches active profile self port for profile later reads profile, so _active_matches_engine_locked must retain the computed value under that name. + profile = self._catalog.get(self._active_name) + # What: handle catalog error by return false; why: RoutingCoordinator._active_matches_engine_locked converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError: + # What: return false from _active_matches_engine_locked; why: _active_matches_engine_locked exposes false so its caller can continue with the function\'s computed outcome. + return False + # What: return matches active and profile and port for from _active_matches_engine_locked; why: _active_matches_engine_locked exposes matches active and profile and port for so its caller can continue with the function\'s computed outcome. + return self._matches_active(profile, self._port_for(profile)) + + # What: define _port_for around profile; why: its direct callers call _port_for for port for and rely on this exact input and result contract. + def _port_for(self, profile: ModelProfile) -> int: + """Resolve a profile's proxy/readiness target under router ownership.""" + # What: document resolve a profile s proxy readiness in the _port_for docstring; why: introspection and maintainers read this exact docstring fragment to understand port for behavior without executing it. + # What: gate on port and profile before default port; why: _port_for admits default port only for this predicate and excludes the opposite state. + if profile.port is None: + # What: return default port from _port_for; why: _port_for exposes default port so its caller can continue with the function\'s computed outcome. + return self._default_port + # What: gate on port and profile before port and profile; why: _port_for admits port and profile only for this predicate and excludes the opposite state. + if profile.port != 0: + # What: return port and profile from _port_for; why: _port_for exposes port and profile so its caller can continue with the function\'s computed outcome. + return profile.port + # What: compute state from status and manager; why: if self active name profile name and state get running later reads state, so _port_for must retain the computed value under that name. + state = self._manager.status() + # A dynamic profile retains its concrete port for its whole residency; + # a fresh activation gets a new kernel-selected one. + # What: gate on active name and name and get and isinstance and int before state; why: _port_for admits state only for this predicate and excludes the opposite state. + if (self._active_name == profile.name and state.get("running") + # What: call isinstance with get and state and port and int; why: _port_for invokes isinstance while performing return state port; the call advances that operation through its result or side effect. + and isinstance(state.get("port"), int) and state["port"] > 0): + # What: return state and port from _port_for; why: _port_for exposes state and port so its caller can continue with the function\'s computed outcome. + return state["port"] + # What: return port allocator from _port_for; why: _port_for exposes port allocator so its caller can continue with the function\'s computed outcome. + return self._port_allocator() + + # What: define _activate around profile and port; why: its direct callers call _activate for activate and rely on this exact input and result contract. + def _activate(self, profile: ModelProfile, port: int) -> int | None: + # What: compute state from status and manager; why: state get running later reads state, so _activate must retain the computed value under that name. + state = self._manager.status() + # What: compute exact from get and model and port and state; why: if exact later reads exact, so _activate must retain the computed value under that name. + exact = ( + # What: call state.get with running; why: _activate invokes state.get while performing and state get model profile model; the call advances that operation through its result or side effect. + state.get("running") + # What: call state.get with model; why: _activate invokes state.get while performing and state get port port; the call advances that operation through its result or side effect. + and state.get("model") == profile.model + # What: call state.get with port; why: _activate invokes state.get while performing and self manager serve args list profile args; the call advances that operation through its result or side effect. + and state.get("port") == port + # What: call self._manager.serve_args with the declared inputs; why: _activate consumes the self._manager.serve_args return value while evaluating and self._manager.serve_args() == list(profile.args). + and self._manager.serve_args() == list(profile.args) + # What: complete the exact expression with exact state get running and state get model equals profile model and; why: RoutingCoordinator._activate groups the supplied clauses as one exact expression before its value is consumed. + ) + # What: compute ticket from the named fixture input; why: result ticket self manager switch for readiness later reads ticket, so _activate must retain the computed value under that name. + ticket = None + # What: gate on exact before result and get and state; why: _activate admits result and get and state only for this predicate and excludes the opposite state. + if exact: + # What: map the pid field as get and state and pid; why: RoutingCoordinator._activate carries pid through result into result ticket self manager switch for readiness. + result = {"pid": state.get("pid"), "idempotent": True} + # What: gate on get and state before cond and activations; why: _activate admits cond and activations only for this predicate and excludes the opposite state. + elif state.get("running"): + # What: enter the cond managed context before self activations; why: _activate releases this resource or lock after self activations on both success and failure paths. + with self._cond: + # What: compute activations from 1; why: self activations later reads activations, so _activate must retain the computed value under that name. + self._activations += 1 + # What: compute result and ticket from switch for readiness and model and port and manager; why: result self manager start profile model port list profile args later reads result and ticket, so _activate must retain the computed value under that name. + result, ticket = self._manager.switch_for_readiness( + # What: call list with args and profile; why: _activate consumes the list return value while evaluating profile.model, port, list(profile.args). + profile.model, port, list(profile.args) + # What: complete the self._manager.switch_for_readiness call with model and port and list; why: RoutingCoordinator._activate groups the supplied clauses as one self._manager.switch_for_readiness call before its value is consumed. + ) + # What: select the remaining branch that performs with self cond; why: _activate covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: enter the cond managed context before self activations; why: _activate releases this resource or lock after self activations on both success and failure paths. + with self._cond: + # What: compute activations from 1; why: the enclosing return or state update later reads activations, so _activate must retain the computed value under that name. + self._activations += 1 + # What: compute result from start and model and port and manager; why: pid result get pid later reads result, so _activate must retain the computed value under that name. + result = self._manager.start(profile.model, port, list(profile.args)) + # What: compute readiness args from port and ready timeout s and get and profile; why: readiness args path profile check endpoint later reads readiness args, so _activate must retain the computed value under that name. + readiness_args = { + # What: map the pid field as get and result and pid; why: RoutingCoordinator._activate carries pid through readiness args into readiness args path profile check endpoint. + "pid": result.get("pid"), + # What: map the port field as port; why: RoutingCoordinator._activate carries port through readiness args into readiness args path profile check endpoint. + "port": port, + # What: map the timeout s field as ready timeout s and profile; why: RoutingCoordinator._activate carries timeout s through readiness args into readiness args path profile check endpoint. + "timeout_s": profile.ready_timeout_s, + # What: complete the readiness_args mapping with pid and port and timeout s; why: RoutingCoordinator._activate groups the supplied clauses as one readiness_args mapping before its value is consumed. + } + # What: gate on check endpoint and default check endpoint and profile before check endpoint and readiness args and profile; why: _activate admits check endpoint and readiness args and profile only for this predicate and excludes the opposite state. + if profile.check_endpoint != DEFAULT_CHECK_ENDPOINT: + # What: compute readiness args entry from check endpoint and profile; why: readiness self ready fn self manager self probe readiness args later reads readiness args entry, so _activate must retain the computed value under that name. + readiness_args["path"] = profile.check_endpoint + # What: compute readiness from ready fn and manager and probe and readiness args; why: if readiness get ready later reads readiness, so _activate must retain the computed value under that name. + readiness = self._ready_fn(self._manager, self._probe, **readiness_args) + # What: gate on get and readiness before get and result; why: _activate admits get and result only for this predicate and excludes the opposite state. + if readiness.get("ready"): + # What: return get and result and pid from _activate; why: _activate exposes get and result and pid so its caller can continue with the function\'s computed outcome. + return result.get("pid") + # What: compute recovery from the named fixture input; why: recovery self manager recover switch ticket later reads recovery, so _activate must retain the computed value under that name. + recovery = None + # What: gate on ticket before recovery and recover switch and ticket and manager; why: _activate admits recovery and recover switch and ticket and manager only for this predicate and excludes the opposite state. + if ticket is not None: + # What: compute recovery from recover switch and ticket and manager; why: recovery recovery later reads recovery, so _activate must retain the computed value under that name. + recovery = self._manager.recover_switch(ticket) + # What: compute reason from get and readiness and reason and not ready; why: f profile profile name r is not later reads reason, so _activate must retain the computed value under that name. + reason = readiness.get("reason", "not-ready") + # What: raise RoutingError for the caller; why: RoutingCoordinator._activate stops this rejected path before it can mutate state, dispatch work, or report success. + raise RoutingError( + # What: apply the engine not ready portion of the enclosing predicate; why: this clause remains in _activate\'s enclosing expression so its grouping and evaluation order stay intact. + "engine_not_ready", + # What: apply the f profile profile name r is not portion of the enclosing predicate; why: this clause remains in _activate\'s enclosing expression so its grouping and evaluation order stay intact. + f"profile {profile.name!r} is not ready: {reason}", + # What: supply recovery to RoutingError; why: _activate binds this recovery value to RoutingError's recovery input. + recovery=recovery, + # What: complete the RoutingError call with recovery; why: RoutingCoordinator._activate groups the supplied clauses as one RoutingError call before its value is consumed. + ) diff --git a/python/freetoken/daemon/serve_manager.py b/python/freetoken/daemon/serve_manager.py index 681bdef49..6b9682feb 100644 --- a/python/freetoken/daemon/serve_manager.py +++ b/python/freetoken/daemon/serve_manager.py @@ -40,12 +40,39 @@ class Conflict(RuntimeError): """A different serve (model/port/args) is already running; the client should switch().""" +# What: define SwitchLaunchError as the owner of __init__; why: daemon callers use this class boundary so those methods share one switch launch error state invariant. +class SwitchLaunchError(RuntimeError): + """Replacement failed; rollback describes launch recovery, not readiness.""" +# What: document replacement failed rollback describes launch recovery in the SwitchLaunchError docstring; why: introspection and maintainers read this exact docstring fragment to understand switch launch error behavior without executing it. + + # What: define __init__ around error and rollback and accounting; why: its direct callers call __init__ for init and rely on this exact input and result contract. + def __init__(self, error: Exception, rollback: dict, accounting: dict | None): + # What: preserve the exact super init f replacement launch failed literal fragment; why: __init__ passes this fragment verbatim through super().__init__(f"replacement launch failed: {error}"), because changing it would alter a protocol payload, serialized fixture, or public message. + super().__init__(f"replacement launch failed: {error}") + # What: compute rollback from rollback; why: the enclosing return or state update later reads rollback, so __init__ must retain the computed value under that name. + self.rollback = rollback + # What: compute accounting from accounting; why: the enclosing return or state update later reads accounting, so __init__ must retain the computed value under that name. + self.accounting = accounting + + @dataclass class ExitInfo: code: int | None # Popen convention: >=0 exit status, <0 == -signal; None if unknowable source: str # "exited" | "signalled" | "stopped" | "adopted-vanished" | "unknown" +# What: generate dataclass initialization and value semantics for SwitchRecovery; why: SwitchRecovery acts as a typed state record with consistent construction, comparison, and representation. +@dataclass(frozen=True) +# What: define SwitchRecovery as the owner of its declared state; why: daemon callers use this class boundary so those methods share one switch recovery state invariant. +class SwitchRecovery: + # What: compute epoch from the named fixture input; why: superseded self lifecycle epoch ticket epoch later reads epoch, so serve_manager must retain the computed value under that name. + epoch: int + # What: compute child from the named fixture input; why: def close popen owns its child later reads child, so serve_manager must retain the computed value under that name. + child: object + # What: compute previous from the named fixture input; why: previous self model self port list self args later reads previous, so serve_manager must retain the computed value under that name. + previous: tuple[str, int, list[str]] | None + + # --------------------------------------------------------------------------- child abstractions @@ -211,6 +238,8 @@ def __init__( # Serialize complete lifecycle transactions, including prepare -> durable receipt -> signal. # RLock lets switch() compose stop+start without opening an interleaving window. self._lifecycle = threading.RLock() + # What: compute lifecycle epoch from 0; why: the enclosing return or state update later reads lifecycle epoch, so __init__ must retain the computed value under that name. + self._lifecycle_epoch = 0 self._cond = threading.Condition(threading.Lock()) # state guarded by _cond self._child: object | None = None @@ -259,6 +288,8 @@ def start( self, model: str, port: int, args: list[str] | None = None, *, _auto: bool = False ) -> dict: with self._lifecycle: + # What: compute lifecycle epoch from 1; why: the enclosing return or state update later reads lifecycle epoch, so start must retain the computed value under that name. + self._lifecycle_epoch += 1 return self._start(model, port, args, _auto=_auto) def _start( @@ -326,6 +357,8 @@ def _start( def stop(self, timeout: float | None = None, force: bool = False) -> dict: with self._lifecycle: + # What: compute lifecycle epoch from 1; why: the enclosing return or state update later reads lifecycle epoch, so stop must retain the computed value under that name. + self._lifecycle_epoch += 1 return self._stop(timeout, force) def shutdown(self, timeout: float | None = None, force: bool = False) -> dict: @@ -336,6 +369,8 @@ def shutdown(self, timeout: float | None = None, force: bool = False) -> dict: If accounting/signalling fails, the daemon remains up and normal lifecycle calls reopen. """ with self._lifecycle: + # What: compute lifecycle epoch from 1; why: the enclosing return or state update later reads lifecycle epoch, so shutdown must retain the computed value under that name. + self._lifecycle_epoch += 1 with self._cond: self._shutdown_requested = True self._cond.notify_all() @@ -409,10 +444,132 @@ def switch( force: bool = False, ) -> dict: with self._lifecycle: + # What: compute lifecycle epoch from 1; why: the enclosing return or state update later reads lifecycle epoch, so switch must retain the computed value under that name. + self._lifecycle_epoch += 1 + # What: enter the cond managed context before previous self model self port list self args; why: switch releases this resource or lock after previous self model self port list self args on both success and failure paths. + with self._cond: + # What: compute previous from model and port and child and stopping; why: can restore self child is and previous is later reads previous, so switch must retain the computed value under that name. + previous = ((self._model, self._port, list(self._args)) + # What: apply the if self child is not and not portion of previous; why: switch uses this clause to evaluate previous as one grouped value. + if self._child is not None and not self._stopping else None) stopped = self._stop(force=force) - started = self._start(model, port, args) + # What: establish the handler boundary for the protected operation; why: ServeManager.switch routes failures to exception while preserving cleanup and success flow. + try: + # What: compute started from start and model and port and args; why: return started accounting stopped accounting later reads started, so switch must retain the computed value under that name. + started = self._start(model, port, args) + # What: handle exception by rollback attempted false launched false; why: ServeManager.switch converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: + # What: map the attempted field as false; why: ServeManager.switch carries attempted through rollback into rollback attempted true. + rollback = {"attempted": False, "launched": False} + # A post-spawn failure may leave an owned child. Never spawn a + # second engine or bypass accounting to remove that child. + # What: enter the cond managed context before can restore self child is and previous is; why: switch releases this resource or lock after can restore self child is and previous is on both success and failure paths. + with self._cond: + # What: compute can restore from child and previous; why: if can restore later reads can restore, so switch must retain the computed value under that name. + can_restore = self._child is None and previous is not None + # What: gate on can restore before rollback; why: switch admits rollback only for this predicate and excludes the opposite state. + if can_restore: + # What: compute rollback entry from true; why: rollback update launched pid restored pid later reads rollback entry, so switch must retain the computed value under that name. + rollback["attempted"] = True + # What: establish the handler boundary for the protected operation; why: ServeManager.switch routes failures to exception while preserving cleanup and success flow. + try: + # What: compute restored from start and previous; why: rollback update launched pid restored pid later reads restored, so switch must retain the computed value under that name. + restored = self._start(*previous) + # What: preserve the exact rollback update launched pid restored pid literal fragment; why: switch passes this fragment verbatim through rollback.update(launched=True, pid=restored["pid"]), because changing it would alter a protocol payload, serialized fixture, or public message. + rollback.update(launched=True, pid=restored["pid"]) + # What: preserve the exact self emit replacement launch failed previous engine literal fragment; why: switch passes this fragment verbatim through self._emit("replacement launch failed. + self._emit("replacement launch failed; previous engine relaunched") + # What: handle exception by rollback error str recovery exc; why: ServeManager.switch converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as recovery_exc: + # What: compute rollback entry from str and recovery exc; why: self emit f replacement launch rollback failed later reads rollback entry, so switch must retain the computed value under that name. + rollback["error"] = str(recovery_exc) + # What: preserve the exact self emit f replacement launch rollback failed literal fragment; why: switch passes this fragment verbatim through self._emit(f"replacement launch rollback failed: {recovery_exc}"), because changing it would alter a protocol payload, serialized fixture, or public mess. + self._emit(f"replacement launch rollback failed: {recovery_exc}") + # What: raise SwitchLaunchError for the caller; why: ServeManager.switch stops this rejected path before it can mutate state, dispatch work, or report success. + raise SwitchLaunchError(exc, rollback, stopped["accounting"]) from exc return {**started, "accounting": stopped["accounting"]} + # What: define switch_for_readiness around model and port and args and force; why: its direct callers call switch_for_readiness for switch for readiness and rely on this exact input and result contract. + def switch_for_readiness(self, model, port, args=None, force=False): + """Capture a recovery ticket atomically; never hold the lock during HTTP probes.""" + # What: document capture a recovery ticket atomically never in the switch_for_readiness docstring; why: introspection and maintainers read this exact docstring fragment to understand switch for readiness behavior without executing it. + # What: enter the lifecycle managed context before with self cond; why: switch_for_readiness releases this resource or lock after with self cond on both success and failure paths. + with self._lifecycle: + # What: enter the cond managed context before previous self model self port list self args; why: switch_for_readiness releases this resource or lock after previous self model self port list self args on both success and failure paths. + with self._cond: + # What: compute previous from model and port and child and stopping; why: ticket switch recovery self lifecycle epoch self child previous later reads previous, so switch_for_readiness must retain the computed value under that name. + previous = ((self._model, self._port, list(self._args)) + # What: apply the if self child is not and not portion of previous; why: switch_for_readiness uses this clause to evaluate previous as one grouped value. + if self._child is not None and not self._stopping else None) + # What: compute result from switch and model and port and args; why: return result ticket later reads result, so switch_for_readiness must retain the computed value under that name. + result = self.switch(model, port, args, force) + # What: enter the cond managed context before ticket switch recovery self lifecycle epoch self child previous; why: switch_for_readiness releases this resource or lock after ticket switch recovery self lifecycle epoch self child previous on both success and failure paths. + with self._cond: + # What: compute ticket from switch recovery and lifecycle epoch and child and previous; why: return result ticket later reads ticket, so switch_for_readiness must retain the computed value under that name. + ticket = SwitchRecovery(self._lifecycle_epoch, self._child, previous) + # What: return result and ticket from switch_for_readiness; why: switch_for_readiness exposes result and ticket so its caller can continue with the function\'s computed outcome. + return result, ticket + + # What: define recover_switch around ticket and force; why: its direct callers call recover_switch for recover switch and rely on this exact input and result contract. + def recover_switch(self, ticket: SwitchRecovery, force=False): + """Recover only this switch, without overriding newer lifecycle intent. + + All stop/accounting safeguards still apply. A failed readiness check is + not permission to force-kill an engine or discard its accounting. + """ + # What: document recover only this switch without overriding in the recover_switch docstring; why: introspection and maintainers read this exact docstring fragment to understand recover switch behavior without executing it. + # What: document all stop accounting safeguards still apply in the recover_switch docstring; why: introspection and maintainers read this exact docstring fragment to understand recover switch behavior without executing it. + # What: document not permission to force kill an engine in the recover_switch docstring; why: introspection and maintainers read this exact docstring fragment to understand recover switch behavior without executing it. + # What: preserve the paragraph boundary in the the recover_switch docstring; why: introspection and maintainers read this paragraph break to understand recover switch behavior without executing it. + # What: enter the lifecycle managed context before with self cond; why: recover_switch releases this resource or lock after with self cond on both success and failure paths. + with self._lifecycle: + # What: enter the cond managed context before superseded self lifecycle epoch ticket epoch; why: recover_switch releases this resource or lock after superseded self lifecycle epoch ticket epoch on both success and failure paths. + with self._cond: + # What: compute superseded from shutdown requested and lifecycle epoch and epoch and ticket; why: if superseded later reads superseded, so recover_switch must retain the computed value under that name. + superseded = (self._lifecycle_epoch != ticket.epoch + # What: apply the or self shutdown requested portion of superseded; why: recover_switch uses this clause to evaluate superseded as one grouped value. + or self._shutdown_requested + # What: apply the or self child is not and self child portion of superseded; why: recover_switch uses this clause to evaluate superseded as one grouped value. + or (self._child is not None and self._child is not ticket.child)) + # What: compute reaping from child and child and ticket; why: if reaping and not ticket child reaped wait self reap wait s later reads reaping, so recover_switch must retain the computed value under that name. + reaping = self._child is None and ticket.child is not None + # What: gate on superseded before the computed value; why: recover_switch admits the computed value only for this predicate and excludes the opposite state. + if superseded: + # What: map the attempted field as false; why: ServeManager.recover_switch carries attempted into return {"attempted": False, "launched": False, "reason": "superseded"}. + return {"attempted": False, "launched": False, "reason": "superseded"} + # What: gate on previous and ticket before the computed value; why: recover_switch admits the computed value only for this predicate and excludes the opposite state. + if ticket.previous is None: + # What: map the attempted field as false; why: ServeManager.recover_switch carries attempted into return {"attempted": False, "launched": False, "reason": "no-previous-en. + return {"attempted": False, "launched": False, "reason": "no-previous-engine"} + # What: compute lifecycle epoch from 1; why: the enclosing return or state update later reads lifecycle epoch, so recover_switch must retain the computed value under that name. + self._lifecycle_epoch += 1 # consume ticket before any fallible operation + # What: establish the handler boundary for the protected operation; why: ServeManager.recover_switch routes failures to exception while preserving cleanup and success flow. + try: + # The monitor clears _child before clearing its persisted state. + # Wait for that cleanup so it cannot erase the restored pidfile. + # What: gate on reaping and wait and reap wait s and reaped and child before runtime error; why: recover_switch admits runtime error only for this predicate and excludes the opposite state. + if reaping and not ticket.child.reaped.wait(self._reap_wait_s): + # What: raise RuntimeError for the caller; why: ServeManager.recover_switch stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("replacement exit cleanup has not completed") + # What: compute stopped from stop and force; why: port ticket previous accounting stopped accounting later reads stopped, so recover_switch must retain the computed value under that name. + stopped = self._stop(force=force) + # What: compute restored from start and previous and ticket; why: return attempted launched pid restored pid later reads restored, so recover_switch must retain the computed value under that name. + restored = self._start(*ticket.previous) + # What: handle exception by self emit f readiness rollback failed exc; why: ServeManager.recover_switch converts that failure into this concrete recovery, response, or cleanup behavior. + except Exception as exc: + # What: preserve the exact self emit f readiness rollback failed exc literal fragment; why: recover_switch passes this fragment verbatim through self._emit(f"readiness rollback failed: {exc}"), because changing it would alter a protocol payload, serialized fixture, or public message. + self._emit(f"readiness rollback failed: {exc}") + # What: map the attempted field as true; why: ServeManager.recover_switch carries attempted into return {"attempted": True, "launched": False, "error": str(exc). + return {"attempted": True, "launched": False, "error": str(exc), + # What: map the engine preserved field as current pid; why: ServeManager.recover_switch carries engine preserved into "enginePreserved": self.current_pid() is not None}. + "enginePreserved": self.current_pid() is not None} + # What: preserve the exact self emit replacement readiness failed previous engine literal fragment; why: recover_switch passes this fragment verbatim through self._emit("replacement readiness failed. + self._emit("replacement readiness failed; previous engine relaunched") + # What: map the attempted field as true; why: ServeManager.recover_switch carries attempted into return {"attempted": True, "launched": True, "pid": restored["pid"]. + return {"attempted": True, "launched": True, "pid": restored["pid"], + # What: map the port field as previous and ticket and 1; why: ServeManager.recover_switch carries port into "port": ticket.previous[1], "accounting": stopped["accounting"]}. + "port": ticket.previous[1], "accounting": stopped["accounting"]} + def pending_accounting(self) -> list[dict[str, Any]]: return self._accounting.pending() @@ -783,6 +940,18 @@ def _reap(self, child, info: ExitInfo) -> None: with self._cond: self._stop_requested = True + # What: enter the cond managed context before is current self child is child; why: _reap releases this resource or lock after is current self child is child on both success and failure paths. + with self._cond: + # What: compute is current from child and child; why: if is current later reads is current, so _reap must retain the computed value under that name. + is_current = self._child is child + # Clear durable adoption state before publishing the stopped state. Otherwise callers + # can observe running=false and still find a dead pidfile long enough to attempt an + # invalid re-adoption or a conflicting recovery. + # What: gate on is current before clear and store; why: _reap admits clear and store only for this predicate and excludes the opposite state. + if is_current: + # What: call self._store.clear with the declared inputs; why: _reap invokes self._store.clear while performing with self cond; the call advances that operation through its result or side effect. + self._store.clear() + with self._cond: is_current = self._child is child if is_current: @@ -793,11 +962,8 @@ def _reap(self, child, info: ExitInfo) -> None: info = ExitInfo(info.code, "stopped") self._last_exit = info self._cond.notify_all() - # Outside the lock. Clear the persisted state BEFORE waking stop() waiters, so a caller - # that sees stop() return also sees an empty pidfile — no window where a racing re-adopt - # could latch onto the just-killed pid. - if is_current: - self._store.clear() + # The pidfile was cleared before publishing stopped state, so a caller that sees either + # status.running=false or stop() return cannot re-adopt this dead generation. child.reaped.set() if getattr(child, "tailer", None) is not None: try: diff --git a/python/freetoken/daemon/server.py b/python/freetoken/daemon/server.py index d4e742b0b..459af33b7 100644 --- a/python/freetoken/daemon/server.py +++ b/python/freetoken/daemon/server.py @@ -50,6 +50,12 @@ def _build_parser(prog: str) -> argparse.ArgumentParser: p.add_argument("--state-dir", default=_default_state_dir(), help="Lock/pidfile/log directory") p.add_argument("--token", default=os.environ.get("FREETOKEN_DAEMON_TOKEN"), help="Optional X-FT-Token shared secret") p.add_argument("--default-serve-port", type=int, default=DEFAULT_SERVE_PORT, help="Port used when /engine/start omits one") + # What: preserve the exact p add argument catalog default os environ get freetoken swap catalog help literal frag; why: _build_parser passes this fragment verbatim through p.add_argument("--catalog", default=os.environ.get("FREETOKEN_SWAP_CATAL, because changing it would alter a protocol payload, serialized fixtur. + p.add_argument("--catalog", default=os.environ.get("FREETOKEN_SWAP_CATALOG"), help="TOML named-model catalog (or $FREETOKEN_SWAP_CATALOG)") + # What: preserve the exact p add argument catalog watch interval type float default literal fragment; why: _build_parser passes this fragment verbatim through p.add_argument("--catalog-watch-interval", type=float, default=1.0, because changing it would alter a protocol payload, serialized fixture, or public messag. + p.add_argument("--catalog-watch-interval", type=float, default=1.0, + # What: preserve the catalog-watch interval help text; why: _build_parser presents this wording for the delay separating safe catalog change checks. + help="Seconds between safe catalog change checks; 0 disables watching (default 1)") p.add_argument("--serve-python", default=sys.executable, help="Interpreter used to launch ft serve") p.add_argument("--grace", type=float, default=10.0, help="SIGTERM→SIGKILL grace seconds on stop") p.add_argument("--poll-interval", type=float, default=1.0, help="Adopted-serve liveness / OOM reapply interval") @@ -103,16 +109,26 @@ def _run() -> None: def main(argv: Sequence[str] | None = None, *, prog: str = "ft daemon") -> int: args = _build_parser(prog).parse_args(list(argv) if argv is not None else None) + # What: gate on catalog watch interval and args before print and stderr and sys; why: main admits print and stderr and sys only for this predicate and excludes the opposite state. + if args.catalog_watch_interval < 0: + # What: preserve the exact print ft daemon catalog watch interval must be literal fragment; why: main passes this fragment verbatim through print("ft daemon: --catalog-watch-interval must be non-negative", file=s, because changing it would alter a protocol payload, serialized fixture, or public message. + print("ft daemon: --catalog-watch-interval must be non-negative", file=sys.stderr) + # What: return 2 from main; why: main exposes 2 so its caller can continue with the function\'s computed outcome. + return 2 logging.basicConfig( level=getattr(logging, args.log_level.upper(), logging.INFO), format="%(asctime)s [ft-daemon] %(levelname)s %(message)s", ) from .checkpoint import CheckpointManager + # What: import catalog error and model catalog for main using catalog and catalog error and model catalog; why: main uses the catalog error annotation in main and model catalog load, making that imported dependency available to its named operation. + from .catalog import CatalogError, ModelCatalog from .logring import LogRing from .metrics import FootprintCache from .pidfile import AlreadyRunning, ServeStateStore, SingleInstance from .proxy import ServeProbe + # What: import routing coordinator for main using router and routing coordinator; why: main uses routing coordinator, making that imported dependency available to its named operation. + from .router import RoutingCoordinator from .serve_manager import ServeManager from .tailer import LogTailer @@ -120,6 +136,17 @@ def main(argv: Sequence[str] | None = None, *, prog: str = "ft daemon") -> int: log_dir = os.path.join(state_dir, "logs") os.makedirs(log_dir, exist_ok=True) + # What: establish the handler boundary for the protected operation; why: main routes failures to catalog error while preserving cleanup and success flow. + try: + # What: compute catalog from catalog and args and load and empty; why: print f ft daemon invalid model later reads catalog, so main must retain the computed value under that name. + catalog = ModelCatalog.load(args.catalog) if args.catalog else ModelCatalog.empty() + # What: handle catalog error by print f ft daemon invalid model catalog; why: main converts that failure into this concrete recovery, response, or cleanup behavior. + except CatalogError as exc: + # What: preserve the exact print f ft daemon invalid model literal fragment; why: main passes this fragment verbatim through print(f"ft daemon: invalid model catalog: {exc}", file=sys.stderr), because changing it would alter a protocol payload, serialized fixture, or public message. + print(f"ft daemon: invalid model catalog: {exc}", file=sys.stderr) + # What: return 2 from main; why: main exposes 2 so its caller can continue with the function\'s computed outcome. + return 2 + # The ONE hard refusal: two daemons cannot co-own one engine. Everything else degrades. lock = SingleInstance(os.path.join(state_dir, "daemon.pid")) try: @@ -166,6 +193,13 @@ def tailer_factory(child): except Exception as exc: # noqa: BLE001 logger.warning("re-adoption skipped: %s", exc) + # What: compute router from routing coordinator and manager and catalog and probe; why: router coordinated exit stop child later reads router, so main must retain the computed value under that name. + router = RoutingCoordinator( + # What: supply default port to RoutingCoordinator; why: main binds this default serve port and args value to RoutingCoordinator's default port input. + manager, catalog, probe, default_port=args.default_serve_port + # What: complete the RoutingCoordinator call with default port; why: main groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + stop_reaper = threading.Event() if not args.no_oom: _start_oom_reaper(manager, args.poll_interval, stop_reaper) @@ -177,11 +211,13 @@ def shutdown_hook() -> None: stop_reaper.set() if args.stop_serve_on_exit: logger.info("stopping serve on daemon exit (--stop-serve-on-exit)") - manager.stop() + # What: supply stop child to router.coordinated_exit; why: shutdown_hook binds this true value to router.coordinated_exit's stop child input. + router.coordinated_exit(stop_child=True) else: # Default: the engine outlives the daemon. Leave it running and # persisted so the next daemon re-adopts it; just stop following its log. - manager.detach() + # What: supply stop child to router.coordinated_exit; why: shutdown_hook binds this false value to router.coordinated_exit's stop child input. + router.coordinated_exit(stop_child=False) from .app import build_app @@ -197,6 +233,16 @@ def shutdown_hook() -> None: checkpoints=checkpoints, started_wall=time.time(), shutdown_hook=shutdown_hook, + # What: supply catalog to build_app; why: main binds this catalog value to build_app's catalog input. + catalog=catalog, + # What: supply router to build_app; why: main binds this router value to build_app's router input. + router=router, + # What: supply catalog path to build_app; why: main binds this catalog and args value to build_app's catalog path input. + catalog_path=args.catalog, + # What: supply catalog watch interval s to build_app; why: main binds this catalog and catalog watch interval and args and 0 value to build_app's catalog watch interval s input. + catalog_watch_interval_s=args.catalog_watch_interval if args.catalog else 0, + # What: supply activity path to os.path.join; why: main binds this join and state dir and path and os and activity value to os.path.join's activity path input. + activity_path=os.path.join(state_dir, "activity.jsonl"), ) import uvicorn diff --git a/python/freetoken/server/control_api.py b/python/freetoken/server/control_api.py index 7158e4e5f..6332ab97d 100644 --- a/python/freetoken/server/control_api.py +++ b/python/freetoken/server/control_api.py @@ -58,6 +58,22 @@ def register_control_routes( async def health(): return build_health(get_state(), app.version) + # What: register GET /ready on the application router; why: clients reach ready's handler only through this method-and-path binding. + @app.get("/ready") + # What: define ready around the current object state; why: the registered API client call ready for ready and rely on this exact input and result contract. + async def ready(): + """HTTP readiness for supervisors that cannot inspect health JSON.""" + # What: document http readiness for supervisors that cannot in the ready docstring; why: introspection and maintainers read this exact docstring fragment to understand ready behavior without executing it. + # What: import jsonresponse for ready using fastapi and responses and jsonresponse; why: ready uses jsonresponse, making that imported dependency available to its named operation. + from fastapi.responses import JSONResponse + + # What: compute doc from build health and version and get state and app; why: accepting doc get status ok and doc get later reads doc, so ready must retain the computed value under that name. + doc = build_health(get_state(), app.version) + # What: compute accepting from get and doc and ok and serving and status; why: return jsonresponse status code if accepting else later reads accepting, so ready must retain the computed value under that name. + accepting = doc.get("status") == "ok" and doc.get("maintenance") == "serving" + # What: return HTTP 200 when accepting and 503 otherwise; why: supervisors use this status and ready boolean to decide whether the daemon control plane may receive traffic. + return JSONResponse(status_code=200 if accepting else 503, content=doc) + from . import request_ring @app.get("/v1/requests") diff --git a/tests/daemon/test_activity.py b/tests/daemon/test_activity.py new file mode 100644 index 000000000..274dd6492 --- /dev/null +++ b/tests/daemon/test_activity.py @@ -0,0 +1,252 @@ +# What: enable postponed evaluation of annotations; why: type hints in test_activity can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import base64 for test capture is opt in redacted binary safe and evicted with row using base64; why: test_capture_is_opt_in_redacted_binary_safe_and_evicted_with_row uses base64 b64decode, making that imported dependency available to its named operation. +import base64 +# What: import time for record using time; why: _record uses time monotonic, making that imported dependency available to its named operation. +import time + +# What: import activity store for record using freetoken and daemon and activity and activity store; why: _record uses the activity store annotation in record, making that imported dependency available to its named operation. +from freetoken.daemon.activity import ActivityStore + + +# What: define the _record test helper around store and model and body and cancelled; why: the record scenario calls this helper to produce or observe the exact behavior checked by its assertions. +def _record(store: ActivityStore, *, model="a", body=b"ok", cancelled=False): + # What: return record and store and model and cancelled from the _record test helper; why: the record scenario uses this helper result in its subsequent act or assertion. + return store.record( + # What: arrange model to store.record; why: the record scenario binds this model value to store.record's model input. + model=model, + # What: arrange route to store.record; why: the record scenario binds this v1 and chat and completions value to store.record's route input. + route="/v1/chat/completions", + # What: arrange method to store.record; why: the record scenario binds this post value to store.record's method input. + method="POST", + # What: arrange status to store.record; why: the record scenario binds this 200 value to store.record's status input. + status=200, + # What: arrange started to time.monotonic; why: the record scenario binds this monotonic and time and 0 01 value to time.monotonic's started input. + started=time.monotonic() - 0.01, + # What: arrange ttft s to store.record; why: the record scenario binds this 0 002 value to store.record's ttft s input. + ttft_s=0.002, + # What: arrange response bytes to len; why: the record scenario binds this len and body value to len's response bytes input. + response_bytes=len(body), + # What: arrange cancelled to store.record; why: the record scenario binds this cancelled value to store.record's cancelled input. + cancelled=cancelled, + # What: arrange request headers to store.record; why: the record scenario binds this authorization and x auth token and x trace and bearer and secret value to store.record's request headers input. + request_headers={ + # What: arrange the authorization field as bearer and secret; why: _record carries authorization into "Authorization": "Bearer secret", "X-Auth-Token": "also-secret". + "Authorization": "Bearer secret", "X-Auth-Token": "also-secret", + # What: arrange the x trace field as visible; why: _record carries x trace into "X-Trace": "visible". + "X-Trace": "visible", + # What: arrange the enclosing predicate mapping with authorization and x auth token and x trace; why: _record groups the supplied clauses as one _record expression mapping before its value is consumed. + }, + # What: arrange request body to store.record; why: the record scenario binds no value to store.record's request body input. + request_body=b"\x00prompt", + # What: arrange the set cookie field as private; why: _record carries set cookie into response_headers={"Set-Cookie": "private", "Content-Type": "application/. + response_headers={"Set-Cookie": "private", "Content-Type": "application/octet-stream"}, + # What: arrange response body to store.record; why: the record scenario binds this body value to store.record's response body input. + response_body=body, + # What: arrange the store.record call with model and route and method and status and started; why: _record groups the supplied clauses as one store.record call before its value is consumed. + ) + + +# What: define the test_activity_rows_are_bounded_filterable_and_aggregated test around local fixtures; why: this test groups the arrange, act, and assertions that protect the activity rows are bounded filterable and aggregated outcome. +def test_activity_rows_are_bounded_filterable_and_aggregated(): + # What: act by calling ActivityStore and capture store; why: the activity rows are bounded filterable and aggregated test asserts the response, state, or failure produced by this call. + store = ActivityStore(max_entries=2, capture_budget_bytes=0) + # What: arrange the exact record store model a body b fixture fragment; why: the activity rows are bounded filterable and aggregated scenario feeds this byte-preserved fragment through _record(store, model="a", body=b"1") before asserting its protocol or parser result. + _record(store, model="a", body=b"1") + # What: arrange the exact record store model b body b fixture fragment; why: the activity rows are bounded filterable and aggregated scenario feeds this byte-preserved fragment through _record(store, model="b", body=b"22") before asserting its protocol or parser result. + _record(store, model="b", body=b"22") + # What: act by calling _record and capture latest; why: the activity rows are bounded filterable and aggregated test asserts the response, state, or failure produced by this call. + latest = _record(store, model="a", body=b"333", cancelled=True) + + # What: act by calling store.list and capture page; why: the activity rows are bounded filterable and aggregated test asserts the response, state, or failure produced by this call. + page = store.list(limit=1) + # What: assert that row id for row in page equals latest id; why: this assertion protects the activity rows are bounded filterable and aggregated regression after the test's arranged inputs and exercised call. + assert [row["id"] for row in page["data"]] == [latest["id"]] + # What: assert that page next before id equals latest id; why: this assertion protects the activity rows are bounded filterable and aggregated regression after the test's arranged inputs and exercised call. + assert page["nextBeforeId"] == latest["id"] + # What: assert that row model for row in store list equals a b; why: this assertion protects the activity rows are bounded filterable and aggregated regression after the test's arranged inputs and exercised call. + assert [row["model"] for row in store.list(limit=10)["data"]] == ["a", "b"] + # What: assert the expected store stats model a == outcome; why: test activity test activity rows are bounded filterable and aggregated protects its regression by requiring this observable result after the exercised behavior. + assert store.stats(model="a") == { + # What: arrange count 1 cancelled 1 errors 0 for the scenario; why: test activity test activity rows are bounded filterable and aggregated requires this concrete input or helper state before exercising the behavior under test. + "count": 1, "cancelled": 1, "errors": 0, + # What: arrange responseBytes 3 averageDurationS latest durationS for the scenario; why: test duration s requires this concrete input or helper state before exercising the behavior under test. + "responseBytes": 3, "averageDurationS": latest["durationS"], + # What: arrange persistence enabled False healthy True error None for the scenario; why: test activity test activity rows are bounded filterable and aggregated requires this concrete input or helper state before exercising the behavior under test. + "persistence": {"enabled": False, "healthy": True, "error": None}, + # What: arrange the grouped source fragment for the scenario; why: test activity test activity rows are bounded filterable and aggregated requires this concrete input or helper state before exercising the behavior under test. + } + + +# What: define the test_capture_is_opt_in_redacted_binary_safe_and_evicted_with_row test around local fixtures; why: this test groups the arrange, act, and assertions that protect the capture is opt in redacted binary safe and evicted with row outcome. +def test_capture_is_opt_in_redacted_binary_safe_and_evicted_with_row(): + # What: act by calling ActivityStore and capture store; why: the capture is opt in redacted binary safe and evicted with row test asserts the response, state, or failure produced by this call. + store = ActivityStore(max_entries=1, capture_budget_bytes=1024) + # What: act by calling _record and capture first; why: the capture is opt in redacted binary safe and evicted with row test asserts the response, state, or failure produced by this call. + first = _record(store, body=b"\xffresult") + # What: act by calling store.capture and capture capture; why: the capture is opt in redacted binary safe and evicted with row test asserts the response, state, or failure produced by this call. + capture = store.capture(first["id"]) + + # What: assert that first has capture is true; why: this assertion protects the capture is opt in redacted binary safe and evicted with row regression after the test's arranged inputs and exercised call. + assert first["hasCapture"] is True + # What: assert the expected capture requestHeaders == outcome; why: test activity test capture is opt in redacted binary safe and evicted with row protects its regression by requiring this observable result after the exercised behavior. + assert capture["requestHeaders"] == { + # What: arrange Authorization REDACTED X Auth Token REDACTED for the scenario; why: test activity test capture is opt in redacted binary safe and evicted with row requires this concrete input or helper state before exercising the behavior under test. + "Authorization": "[REDACTED]", "X-Auth-Token": "[REDACTED]", + # What: arrange X Trace visible for the scenario; why: test activity test capture is opt in redacted binary safe and evicted with row requires this concrete input or helper state before exercising the behavior under test. + "X-Trace": "visible", + # What: arrange the grouped source fragment for the scenario; why: test activity test capture is opt in redacted binary safe and evicted with row requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that capture response headers set cookie equals redacted; why: this assertion protects the capture is opt in redacted binary safe and evicted with row regression after the test's arranged inputs and exercised call. + assert capture["responseHeaders"]["Set-Cookie"] == "[REDACTED]" + # What: assert that base64 b64decode capture request body base64 equals b x00prompt; why: this assertion protects the capture is opt in redacted binary safe and evicted with row regression after the test's arranged inputs and exercised call. + assert base64.b64decode(capture["requestBodyBase64"]) == b"\x00prompt" + # What: assert that base64 b64decode capture response body base64 equals b xffresult; why: this assertion protects the capture is opt in redacted binary safe and evicted with row regression after the test's arranged inputs and exercised call. + assert base64.b64decode(capture["responseBodyBase64"]) == b"\xffresult" + + # What: act by calling _record and capture second; why: the capture is opt in redacted binary safe and evicted with row test asserts the response, state, or failure produced by this call. + second = _record(store, body=b"next") + # What: assert that the evicted first row has no retained capture; why: capture eviction must remove sensitive body data together with its activity row. + assert store.capture(first["id"]) is None + # What: assert that the retained second row still has a capture; why: eviction must not remove the newest body data while its activity row remains. + assert store.capture(second["id"]) is not None + + +# What: define the test_capture_skips_cancelled_and_over_budget_items_and_reconfigures test around local fixtures; why: this test groups the arrange, act, and assertions that protect the capture skips cancelled and over budget items and reconfigures outcome. +def test_capture_skips_cancelled_and_over_budget_items_and_reconfigures(): + # What: act by calling ActivityStore and capture store; why: the capture skips cancelled and over budget items and reconfigures test asserts the response, state, or failure produced by this call. + store = ActivityStore(max_entries=5, capture_budget_bytes=8) + # What: assert that record store body b too large has capture is false; why: this assertion protects the capture skips cancelled and over budget items and reconfigures regression after the test's arranged inputs and exercised call. + assert _record(store, body=b"too-large")["hasCapture"] is False + # What: assert that record store body b x cancelled is false; why: this assertion protects the capture skips cancelled and over budget items and reconfigures regression after the test's arranged inputs and exercised call. + assert _record(store, body=b"x", cancelled=True)["hasCapture"] is False + # What: act by calling store.reconfigure with 1 and 0; why: the capture skips cancelled and over budget items and reconfigures scenario observes the store.reconfigure return value during page store list limit. + store.reconfigure(1, 0) + # What: act by calling store.list and capture page; why: the capture skips cancelled and over budget items and reconfigures test asserts the response, state, or failure produced by this call. + page = store.list(limit=10) + # What: assert that page count equals 1; why: this assertion protects the capture skips cancelled and over budget items and reconfigures regression after the test's arranged inputs and exercised call. + assert page["count"] == 1 + # What: assert that page data 0 has capture is false; why: this assertion protects the capture skips cancelled and over budget items and reconfigures regression after the test's arranged inputs and exercised call. + assert page["data"][0]["hasCapture"] is False + # What: assert that store capture item limit equals 0; why: this assertion protects the capture skips cancelled and over budget items and reconfigures regression after the test's arranged inputs and exercised call. + assert store.capture_item_limit == 0 + + # What: act by calling ActivityStore and capture retained; why: the capture skips cancelled and over budget items and reconfigures test asserts the response, state, or failure produced by this call. + retained = ActivityStore(max_entries=2, capture_budget_bytes=1024) + # What: act by calling _record and capture row; why: the capture skips cancelled and over budget items and reconfigures test asserts the response, state, or failure produced by this call. + row = _record(retained, body=b"captured") + # What: assert that row has capture is true; why: this assertion protects the capture skips cancelled and over budget items and reconfigures regression after the test's arranged inputs and exercised call. + assert row["hasCapture"] is True + # What: act by calling retained.reconfigure with 2 and 0; why: the capture skips cancelled and over budget items and reconfigures scenario observes the retained.reconfigure return value during assert retained list limit data has capture is. + retained.reconfigure(2, 0) + # What: assert that retained list limit 2 data 0 has capture is false; why: this assertion protects the capture skips cancelled and over budget items and reconfigures regression after the test's arranged inputs and exercised call. + assert retained.list(limit=2)["data"][0]["hasCapture"] is False + # What: assert that retained capture row id is group delimiter; why: this assertion protects the capture skips cancelled and over budget items and reconfigures regression after the test's arranged inputs and exercised call. + assert retained.capture(row["id"]) is None + + +# What: define the test_body_free_activity_survives_restart_and_compacts_corrupt_history test around tmp path; why: this test groups the arrange, act, and assertions that protect the body free activity survives restart and compacts corrupt history outcome. +def test_body_free_activity_survives_restart_and_compacts_corrupt_history(tmp_path): + # What: arrange path as tmp path and activity and jsonl; why: the body free activity survives restart and compacts corrupt history test consumes this named precondition before exercising the behavior. + path = tmp_path / "activity.jsonl" + # What: act by calling ActivityStore and capture first; why: the body free activity survives restart and compacts corrupt history test asserts the response, state, or failure produced by this call. + first = ActivityStore(2, 1024, str(path)) + # What: arrange the exact record first model a body b fixture fragment; why: the body free activity survives restart and compacts corrupt history scenario feeds this byte-preserved fragment through _record(first, model="a", body=b"first") before asserting its protocol or parser result. + _record(first, model="a", body=b"first") + # What: arrange the exact record first model b body b fixture fragment; why: the body free activity survives restart and compacts corrupt history scenario feeds this byte-preserved fragment through _record(first, model="b", body=b"second") before asserting its protocol or parser result. + _record(first, model="b", body=b"second") + # What: act by calling _record and capture latest; why: the body free activity survives restart and compacts corrupt history test asserts the response, state, or failure produced by this call. + latest = _record(first, model="c", body=b"third") + # What: enter the path.open managed context before target write truncated n; why: test_body_free_activity_survives_restart_and_compacts_corrupt_history releases this resource or lock after target write truncated n on both success and failure paths. + with path.open("a", encoding="utf-8") as target: + # What: arrange the exact target write truncated n fixture fragment; why: the body free activity survives restart and compacts corrupt history scenario feeds this byte-preserved fragment through target.write("truncated{\n") before asserting its protocol or parser result. + target.write("truncated{\n") + # What: arrange the exact target write x n fixture fragment; why: the body free activity survives restart and compacts corrupt history scenario feeds this byte-preserved fragment through target.write("x" * 9000 + "\n") before asserting its protocol or parser result. + target.write("x" * 9000 + "\n") + + # What: act by calling ActivityStore and capture recovered; why: the body free activity survives restart and compacts corrupt history test asserts the response, state, or failure produced by this call. + recovered = ActivityStore(2, 1024, str(path)) + # What: act by calling recovered.list and capture page; why: the body free activity survives restart and compacts corrupt history test asserts the response, state, or failure produced by this call. + page = recovered.list(limit=10) + + # What: assert that row model for row in page equals c b; why: this assertion protects the body free activity survives restart and compacts corrupt history regression after the test's arranged inputs and exercised call. + assert [row["model"] for row in page["data"]] == ["c", "b"] + # What: assert that all row has capture is false for row in page data; why: this assertion protects the body free activity survives restart and compacts corrupt history regression after the test's arranged inputs and exercised call. + assert all(row["hasCapture"] is False for row in page["data"]) + # What: assert that page persistence equals enabled true healthy true error; why: this assertion protects the body free activity survives restart and compacts corrupt history regression after the test's arranged inputs and exercised call. + assert page["persistence"] == {"enabled": True, "healthy": True, "error": None} + # What: assert that recovered capture latest id is group delimiter; why: this assertion protects the body free activity survives restart and compacts corrupt history regression after the test's arranged inputs and exercised call. + assert recovered.capture(latest["id"]) is None + # What: act by calling _record and capture next row; why: the body free activity survives restart and compacts corrupt history test asserts the response, state, or failure produced by this call. + next_row = _record(recovered, model="d") + # What: assert that next row id equals latest id 1; why: this assertion protects the body free activity survives restart and compacts corrupt history regression after the test's arranged inputs and exercised call. + assert next_row["id"] == latest["id"] + 1 + # What: assert that len path read text encoding utf 8 splitlines is at most 4; why: this assertion protects the body free activity survives restart and compacts corrupt history regression after the test's arranged inputs and exercised call. + assert len(path.read_text(encoding="utf-8").splitlines()) <= 4 + + +# What: define the test_persistence_failure_never_breaks_in_memory_activity test around tmp path; why: this test groups the arrange, act, and assertions that protect the persistence failure never breaks in memory activity outcome. +def test_persistence_failure_never_breaks_in_memory_activity(tmp_path): + # What: arrange missing parent as tmp path and activity and jsonl and missing; why: the persistence failure never breaks in memory activity test consumes this named precondition before exercising the behavior. + missing_parent = tmp_path / "missing" / "activity.jsonl" + # What: act by calling ActivityStore and capture store; why: the persistence failure never breaks in memory activity test asserts the response, state, or failure produced by this call. + store = ActivityStore(2, 0, str(missing_parent)) + # What: act by calling _record and capture row; why: the persistence failure never breaks in memory activity test asserts the response, state, or failure produced by this call. + row = _record(store) + + # What: act by calling store.list and capture page; why: the persistence failure never breaks in memory activity test asserts the response, state, or failure produced by this call. + page = store.list(limit=10) + # What: assert that page data 0 id equals row id; why: this assertion protects the persistence failure never breaks in memory activity regression after the test's arranged inputs and exercised call. + assert page["data"][0]["id"] == row["id"] + # What: assert the expected page persistence == outcome; why: test activity test persistence failure never breaks in memory activity protects its regression by requiring this observable result after the exercised behavior. + assert page["persistence"] == { + # What: arrange enabled True healthy False error write failed for the scenario; why: test activity test persistence failure never breaks in memory activity requires this concrete input or helper state before exercising the behavior under test. + "enabled": True, "healthy": False, "error": "write_failed", + # What: arrange the grouped source fragment for the scenario; why: test activity test persistence failure never breaks in memory activity requires this concrete input or helper state before exercising the behavior under test. + } + + +# What: define the test_load_failure_requires_atomic_rewrite_before_health_recovers test around tmp path and monkeypatch; why: this test groups the arrange, act, and assertions that protect the load failure requires atomic rewrite before health recovers outcome. +def test_load_failure_requires_atomic_rewrite_before_health_recovers(tmp_path, monkeypatch): + # What: arrange path as tmp path and activity and jsonl; why: the load failure requires atomic rewrite before health recovers test consumes this named precondition before exercising the behavior. + path = tmp_path / "activity.jsonl" + # What: arrange the exact path write text unread history n encoding utf 8 fixture fragment; why: the load failure requires atomic rewrite before health recovers scenario feeds this byte-preserved fragment through path.write_text('{"unread":"history"}\n', encoding="utf-8") before asserting its protocol or parser re. + path.write_text('{"unread":"history"}\n', encoding="utf-8") + # What: arrange real open as open; why: the load failure requires atomic rewrite before health recovers test consumes this named precondition before exercising the behavior. + real_open = open + + # What: define the fail_initial_read test helper around name; why: the load failure requires atomic rewrite before health recovers scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def fail_initial_read(name, *args, **kwargs): + # What: act on args and str and name and path before oserror; why: the load failure requires atomic rewrite before health recovers scenario admits oserror only for this predicate and excludes the opposite state. + if str(name) == str(path) and not args: + # What: raise OSError for the caller; why: fail_initial_read stops this rejected path before it can mutate state, dispatch work, or report success. + raise OSError("private path detail") + # What: return real open and name and args and kwargs from the fail_initial_read test helper; why: the load failure requires atomic rewrite before health recovers scenario uses this helper result in its subsequent act or assertion. + return real_open(name, *args, **kwargs) + + # What: arrange the exact monkeypatch setattr builtins open fail initial read fixture fragment; why: the load failure requires atomic rewrite before health recovers scenario feeds this byte-preserved fragment through monkeypatch.setattr("builtins.open", fail_initial_read) before asserting its protocol or parser re. + monkeypatch.setattr("builtins.open", fail_initial_read) + # What: act by calling ActivityStore and capture store; why: the load failure requires atomic rewrite before health recovers test asserts the response, state, or failure produced by this call. + store = ActivityStore(2, 0, str(path)) + # What: arrange the exact monkeypatch setattr builtins open real open fixture fragment; why: the load failure requires atomic rewrite before health recovers scenario feeds this byte-preserved fragment through monkeypatch.setattr("builtins.open", real_open) before asserting its protocol or parser result. + monkeypatch.setattr("builtins.open", real_open) + # What: assert the expected store list persistence == outcome; why: test activity test load failure requires atomic rewrite before health recovers protects its regression by requiring this observable result after the exercised behavior. + assert store.list()["persistence"] == { + # What: arrange enabled True healthy False error load failed for the scenario; why: test activity test load failure requires atomic rewrite before health recovers requires this concrete input or helper state before exercising the behavior under test. + "enabled": True, "healthy": False, "error": "load_failed", + # What: arrange the grouped source fragment for the scenario; why: test activity test load failure requires atomic rewrite before health recovers requires this concrete input or helper state before exercising the behavior under test. + } + + # What: act by calling _record and capture row; why: the load failure requires atomic rewrite before health recovers test asserts the response, state, or failure produced by this call. + row = _record(store) + # What: assert the expected store list persistence == outcome; why: test activity test load failure requires atomic rewrite before health recovers protects its regression by requiring this observable result after the exercised behavior. + assert store.list()["persistence"] == { + # What: arrange enabled True healthy True error None for the scenario; why: test activity test load failure requires atomic rewrite before health recovers requires this concrete input or helper state before exercising the behavior under test. + "enabled": True, "healthy": True, "error": None, + # What: arrange the grouped source fragment for the scenario; why: test activity test load failure requires atomic rewrite before health recovers requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that path read text encoding utf 8 count n equals 1; why: this assertion protects the load failure requires atomic rewrite before health recovers regression after the test's arranged inputs and exercised call. + assert path.read_text(encoding="utf-8").count("\n") == 1 + # What: assert that activity store 2 0 str path list equals row id; why: this assertion protects the load failure requires atomic rewrite before health recovers regression after the test's arranged inputs and exercised call. + assert ActivityStore(2, 0, str(path)).list()["data"][0]["id"] == row["id"] diff --git a/tests/daemon/test_catalog.py b/tests/daemon/test_catalog.py new file mode 100644 index 000000000..87dcb0ce5 --- /dev/null +++ b/tests/daemon/test_catalog.py @@ -0,0 +1,1576 @@ +# What: enable postponed evaluation of annotations; why: type hints in test_catalog can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import pytest for module initialization using pytest; why: module initialization uses pytest mark parametrize, making that imported dependency available to its named operation. +import pytest +# What: arrange from concurrent futures import ThreadPoolExecutor for the scenario; why: test catalog requires this concrete input or helper state before exercising the behavior under test. +from concurrent.futures import ThreadPoolExecutor +# What: import test client for test profile api uses validated catalog and existing switch transaction using fastapi and testclient and test client; why: test_profile_api_uses_validated_catalog_and_existing_switch_transaction uses test client, making that imported dependency available to its named operation. +from fastapi.testclient import TestClient + +# What: arrange from freetoken daemon catalog import CatalogError ModelCapabilities ModelCatalog for the scenario; why: test catalog requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.catalog import CatalogError, ModelCapabilities, ModelCatalog +# What: import build app for test profile api uses validated catalog and existing switch transaction using freetoken and daemon and app and build app; why: test_profile_api_uses_validated_catalog_and_existing_switch_transaction uses build app, making that imported dependency available to its named operation. +from freetoken.daemon.app import build_app +# What: arrange from freetoken daemon import client as daemon client for the scenario; why: test catalog requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon import client as daemon_client +# What: import log ring for test profile api uses validated catalog and existing switch transaction using freetoken and daemon and logring and log ring; why: test_profile_api_uses_validated_catalog_and_existing_switch_transaction uses log ring, making that imported dependency available to its named operation. +from freetoken.daemon.logring import LogRing +# What: arrange from freetoken daemon readiness import wait for ready for the scenario; why: test catalog requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.readiness import wait_for_ready + + +# What: define the test_catalog_reads_named_profiles_without_shell_interpolation test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog reads named profiles without shell interpolation outcome. +def test_catalog_reads_named_profiles_without_shell_interpolation(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog reads named profiles without shell interpolation test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and qwen coder and model and models; why: the catalog reads named profiles without shell interpolation scenario observes the path.write_text return value during models qwen coder nmodel models qwen gguf nport nargs. + path.write_text( + # What: arrange the exact models qwen coder nmodel models qwen gguf nport nargs fixture fragment; why: the catalog reads named profiles without shell interpolation scenario feeds this byte-preserved fragment through """[models.qwen-coder]\nmodel = \"/models/qwen.gguf\"\nport = 1922\nargs before asserting its p. + """[models.qwen-coder]\nmodel = \"/models/qwen.gguf\"\nport = 1922\nargs = [\"--max-seq-len-override\", \"32768\"]\ndescription = \"coding profile\"\n""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog reads named profiles without shell interpolation scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_reads_named_profiles_without_shell_interpolation groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: act by calling ModelCatalog.load and capture catalog; why: the catalog reads named profiles without shell interpolation test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog.load(str(path)) + # What: assert the expected catalog get qwen coder request == outcome; why: test catalog test catalog reads named profiles without shell interpolation protects its regression by requiring this observable result after the exercised behavior. + assert catalog.get("qwen-coder").request() == { + # What: arrange model models qwen gguf port 1922 args max seq len override 32768 for the scenario; why: test catalog test catalog reads named profiles without shell interpolation requires this concrete input or helper state before exercising the behavior under test. + "model": "/models/qwen.gguf", "port": 1922, "args": ["--max-seq-len-override", "32768"] + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog reads named profiles without shell interpolation requires this. + } + # What: assert the expected catalog public == outcome; why: test catalog test catalog reads named profiles without shell interpolation protects its regression by requiring this observable result after the exercised behavior. + assert catalog.public() == [{ + # What: arrange name qwen coder model models qwen gguf port 1922 for the scenario; why: test catalog test catalog reads named profiles without shell interpolation requires this concrete input or helper state before exercising the behavior under test. + "name": "qwen-coder", "model": "/models/qwen.gguf", "port": 1922, + # What: arrange args max seq len override 32768 description coding profile readyTimeoutS 120.0 for the scenario; why: test catalog reads named profiles without shell requires this concrete input or helper state before exercising the behavior under test. + "args": ["--max-seq-len-override", "32768"], "description": "coding profile", "readyTimeoutS": 120.0, + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog reads named profiles without shell interpolation requires this concrete input or helper state before exercising the. + }] + + +# What: define the test_catalog_validates_safe_upstream_no_activation_suffixes test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog validates safe upstream no activation suffixes outcome. +def test_catalog_validates_safe_upstream_no_activation_suffixes(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog validates safe upstream no activation suffixes test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with router and upstream no activation suffixes and wasm and map; why: the catalog validates safe upstream no activation suffixes scenario observes the path.write_text return value during router. + path.write_text( + # What: arrange the exact router fixture fragment; why: the catalog validates safe upstream no activation suffixes scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact upstream no activation suffixes wasm map fixture fragment; why: the catalog validates safe upstream no activation suffixes scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact models local fixture fragment; why: the catalog validates safe upstream no activation suffixes scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact model local gguf fixture fragment; why: the catalog validates safe upstream no activation suffixes scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog validates safe upstream no activation suffixes scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + """[router] +upstream_no_activation_suffixes = [".wasm", ".map"] + +[models.local] +model = "local.gguf" +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog validates safe upstream no activation suffixes scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_validates_safe_upstream_no_activation_suffixes groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling ModelCatalog.load and capture settings; why: the catalog validates safe upstream no activation suffixes test asserts the response, state, or failure produced by this call. + settings = ModelCatalog.load(str(path)).settings + + # What: assert that settings upstream no activation suffixes equals wasm map; why: this assertion protects the catalog validates safe upstream no activation suffixes regression after the test's arranged inputs and exercised call. + assert settings.upstream_no_activation_suffixes == (".wasm", ".map") + + +# What: define the test_catalog_validates_bounded_activity_and_capture_settings test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog validates bounded activity and capture settings outcome. +def test_catalog_validates_bounded_activity_and_capture_settings(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog validates bounded activity and capture settings test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with router and activity max entries and capture buffer mb and activity session headers; why: the catalog validates bounded activity and capture settings scenario observes the path.write_text return value during router. + path.write_text( + # What: arrange the exact router fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact activity max entries fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact capture buffer mb fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact activity session headers x conversation id fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact performance disabled true fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact performance every s fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact models local fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact model local gguf fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + """[router] +activity_max_entries = 25 +capture_buffer_mb = 4 +activity_session_headers = ["X-Conversation-ID"] +performance_disabled = true +performance_every_s = 30 + +[models.local] +model = "local.gguf" +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog validates bounded activity and capture settings scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_validates_bounded_activity_and_capture_settings groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling ModelCatalog.load and capture settings; why: the catalog validates bounded activity and capture settings test asserts the response, state, or failure produced by this call. + settings = ModelCatalog.load(str(path)).settings + + # What: assert that settings activity max entries equals 25; why: this assertion protects the catalog validates bounded activity and capture settings regression after the test's arranged inputs and exercised call. + assert settings.activity_max_entries == 25 + # What: assert that settings capture buffer mb equals 4; why: this assertion protects the catalog validates bounded activity and capture settings regression after the test's arranged inputs and exercised call. + assert settings.capture_buffer_mb == 4 + # What: assert that settings activity session headers equals x conversation id; why: this assertion protects the catalog validates bounded activity and capture settings regression after the test's arranged inputs and exercised call. + assert settings.activity_session_headers == ("x-conversation-id",) + # What: assert that settings performance disabled is true; why: this assertion protects the catalog validates bounded activity and capture settings regression after the test's arranged inputs and exercised call. + assert settings.performance_disabled is True + # What: assert that settings performance every s equals 30; why: this assertion protects the catalog validates bounded activity and capture settings regression after the test's arranged inputs and exercised call. + assert settings.performance_every_s == 30 + + +# What: parameterize test_catalog_rejects_unbounded_activity_or_capture_settings with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects unbounded activity or capture settings. +@pytest.mark.parametrize("key,value", [ + # What: arrange activity max entries 0 for the scenario; why: test catalog test catalog rejects unbounded activity or capture settings requires this concrete input or helper state before exercising the behavior under test. + ("activity_max_entries", 0), + # What: arrange activity max entries 100001 for the scenario; why: test catalog test catalog rejects unbounded activity or capture settings requires this concrete input or helper state before exercising the behavior under test. + ("activity_max_entries", 100001), + # What: arrange capture buffer mb 1 for the scenario; why: test catalog test catalog rejects unbounded activity or capture settings requires this concrete input or helper state before exercising the behavior under test. + ("capture_buffer_mb", -1), + # What: arrange capture buffer mb 257 for the scenario; why: test catalog test catalog rejects unbounded activity or capture settings requires this concrete input or helper state before exercising the behavior under test. + ("capture_buffer_mb", 257), + # What: arrange performance every s 4 for the scenario; why: test catalog test catalog rejects unbounded activity or capture settings requires this concrete input or helper state before exercising the behavior under test. + ("performance_every_s", 4), + # What: arrange performance every s 3601 for the scenario; why: test catalog test catalog rejects unbounded activity or capture settings requires this concrete input or helper state before exercising the behavior under test. + ("performance_every_s", 3601), + # What: arrange the performance disabled portion of the enclosing predicate; why: this clause remains in the catalog rejects unbounded activity or capture settings scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("performance_disabled", 1), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_unbounded_activity_or_capture_settings groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_unbounded_activity_or_capture_settings test around tmp path and key and value; why: this test groups the arrange, act, and assertions that protect the catalog rejects unbounded activity or capture settings outcome. +def test_catalog_rejects_unbounded_activity_or_capture_settings(tmp_path, key, value): + # What: arrange path as tmp path and models and toml; why: the catalog rejects unbounded activity or capture settings test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with key and value and router and value and models; why: the catalog rejects unbounded activity or capture settings scenario observes the path.write_text return value during f router n key value n. + path.write_text( + # What: arrange the exact f router n key value n fixture fragment; why: the catalog rejects unbounded activity or capture settings scenario feeds this byte-preserved fragment through f'[router]\n{key} = {value}\n\n[models.local]\nmodel = "local.gguf"\n' before asserting its protocol or parser result. + f'[router]\n{key} = {value}\n\n[models.local]\nmodel = "local.gguf"\n', + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog rejects unbounded activity or capture settings scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_rejects_unbounded_activity_or_capture_settings groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: assert the pytest.raises failure context; why: the catalog rejects unbounded activity or capture settings scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=key): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects unbounded activity or capture settings scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: parameterize test_catalog_rejects_credential_or_invalid_activity_session_headers with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects credential or invalid activity session headers. +@pytest.mark.parametrize("headers", [ + # What: arrange the authorization portion of the enclosing predicate; why: this clause remains in the catalog rejects credential or invalid activity session headers scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '["Authorization"]', + # What: arrange the x auth token portion of the enclosing predicate; why: this clause remains in the catalog rejects credential or invalid activity session headers scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '["X-Auth-Token"]', + # What: arrange the x api key portion of the enclosing predicate; why: this clause remains in the catalog rejects credential or invalid activity session headers scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '["X-Api-Key"]', + # What: arrange the x session id x session id portion of the enclosing predicate; why: this clause remains in the catalog rejects credential or invalid activity session headers scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '["X-Session-ID", "x-session-id"]', + # What: arrange the bad header portion of the enclosing predicate; why: this clause remains in the catalog rejects credential or invalid activity session headers scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '["bad header"]', +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_credential_or_invalid_activity_session_headers groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_credential_or_invalid_activity_session_headers test around tmp path and headers; why: this test groups the arrange, act, and assertions that protect the catalog rejects credential or invalid activity session headers outcome. +def test_catalog_rejects_credential_or_invalid_activity_session_headers(tmp_path, headers): + # What: arrange path as tmp path and models and toml; why: the catalog rejects credential or invalid activity session headers test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with headers and router and activity session headers and models and local; why: the catalog rejects credential or invalid activity session headers scenario observes the path.write_text return value during f router nactivity session headers headers n n. + path.write_text( + # What: arrange the exact f router nactivity session headers headers n n fixture fragment; why: the catalog rejects credential or invalid activity session headers scenario feeds this byte-preserved fragment through f'[router]\nactivity_session_headers = {headers}\n\n[models.local]\nmode before asserting its pr. + f'[router]\nactivity_session_headers = {headers}\n\n[models.local]\nmodel = "local.gguf"\n', + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog rejects credential or invalid activity session headers scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_rejects_credential_or_invalid_activity_session_headers groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: assert the pytest.raises failure context; why: the catalog rejects credential or invalid activity session headers scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match="activity_session_headers"): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects credential or invalid activity session headers scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: parameterize test_catalog_rejects_unsafe_upstream_no_activation_suffixes with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects unsafe upstream no activation suffixes. +@pytest.mark.parametrize("value", [ + # What: arrange the js portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe upstream no activation suffixes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '".js"', + # What: arrange the js portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe upstream no activation suffixes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '["js"]', + # What: arrange the secret portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe upstream no activation suffixes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '["../secret"]', + # What: arrange the js js portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe upstream no activation suffixes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '[".js", ".js"]', +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_unsafe_upstream_no_activation_suffixes groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_unsafe_upstream_no_activation_suffixes test around tmp path and value; why: this test groups the arrange, act, and assertions that protect the catalog rejects unsafe upstream no activation suffixes outcome. +def test_catalog_rejects_unsafe_upstream_no_activation_suffixes(tmp_path, value): + # What: arrange path as tmp path and models and toml; why: the catalog rejects unsafe upstream no activation suffixes test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with value and router and upstream no activation suffixes and models and local; why: the catalog rejects unsafe upstream no activation suffixes scenario observes the path.write_text return value during f router. + path.write_text( + # What: arrange the exact f router fixture fragment; why: the catalog rejects unsafe upstream no activation suffixes scenario feeds this byte-preserved fragment through f"""[router] before asserting its protocol or parser result. + # What: arrange the exact upstream no activation suffixes value fixture fragment; why: the catalog rejects unsafe upstream no activation suffixes scenario feeds this byte-preserved fragment through f"""[router] before asserting its protocol or parser result. + # What: arrange the exact models local fixture fragment; why: the catalog rejects unsafe upstream no activation suffixes scenario feeds this byte-preserved fragment through f"""[router] before asserting its protocol or parser result. + # What: arrange the exact model local gguf fixture fragment; why: the catalog rejects unsafe upstream no activation suffixes scenario feeds this byte-preserved fragment through f"""[router] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog rejects unsafe upstream no activation suffixes scenario feeds this byte-preserved fragment through f"""[router] before asserting its protocol or parser result. + f"""[router] +upstream_no_activation_suffixes = {value} + +[models.local] +model = "local.gguf" +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog rejects unsafe upstream no activation suffixes scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_rejects_unsafe_upstream_no_activation_suffixes groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: assert the pytest.raises failure context; why: the catalog rejects unsafe upstream no activation suffixes scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match="upstream_no_activation_suffixes"): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects unsafe upstream no activation suffixes scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: define the test_catalog_validates_custom_readiness_and_owned_loopback_proxy_targets test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog validates custom readiness and owned loopback proxy targets outcome. +def test_catalog_validates_custom_readiness_and_owned_loopback_proxy_targets(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog validates custom readiness and owned loopback proxy targets test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and coding and model and coding; why: the catalog validates custom readiness and owned loopback proxy targets scenario observes the path.write_text return value during models coding. + path.write_text( + # What: arrange the exact models coding fixture fragment; why: the catalog validates custom readiness and owned loopback proxy targets scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact model coding gguf fixture fragment; why: the catalog validates custom readiness and owned loopback proxy targets scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact port fixture fragment; why: the catalog validates custom readiness and owned loopback proxy targets scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact check endpoint ready fixture fragment; why: the catalog validates custom readiness and owned loopback proxy targets scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact proxy http port gateway v1 fixture fragment; why: the catalog validates custom readiness and owned loopback proxy targets scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog validates custom readiness and owned loopback proxy targets scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + """[models.coding] +model = "coding.gguf" +port = 1922 +check_endpoint = "/ready" +proxy = "http://127.0.0.1:${PORT}/gateway/v1" +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog validates custom readiness and owned loopback proxy targets scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_validates_custom_readiness_and_owned_loopback_proxy_targets groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling operation.get and capture profile; why: the catalog validates custom readiness and owned loopback proxy targets test asserts the response, state, or failure produced by this call. + profile = ModelCatalog.load(str(path)).get("coding") + + # What: assert that profile check endpoint equals ready; why: this assertion protects the catalog validates custom readiness and owned loopback proxy targets regression after the test's arranged inputs and exercised call. + assert profile.check_endpoint == "/ready" + # What: assert that profile proxy base url 1922 equals http 127 0 0 1 1922 gateway v1; why: this assertion protects the catalog validates custom readiness and owned loopback proxy targets regression after the test's arranged inputs and exercised call. + assert profile.proxy_base_url(1922) == "http://127.0.0.1:1922/gateway/v1" + # What: assert that profile public check endpoint equals ready; why: this assertion protects the catalog validates custom readiness and owned loopback proxy targets regression after the test's arranged inputs and exercised call. + assert profile.public()["checkEndpoint"] == "/ready" + # What: assert that profile public proxy equals http 127 0 0 1 port gateway v1; why: this assertion protects the catalog validates custom readiness and owned loopback proxy targets regression after the test's arranged inputs and exercised call. + assert profile.public()["proxy"] == "http://127.0.0.1:${PORT}/gateway/v1" + + +# What: parameterize test_catalog_rejects_unsafe_readiness_or_proxy_targets with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects unsafe readiness or proxy targets. +@pytest.mark.parametrize("field,value,message", [ + # What: arrange the check endpoint ready absolute ascii path portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe readiness or proxy targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("check_endpoint", "ready", "absolute ASCII path"), + # What: arrange the check endpoint health absolute ascii path portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe readiness or proxy targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("check_endpoint", "/../health", "absolute ASCII path"), + # What: arrange the check endpoint health token x absolute ascii portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe readiness or proxy targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("check_endpoint", "/health?token=x", "absolute ASCII path"), + # What: arrange the proxy http portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe readiness or proxy targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("proxy", "http://127.0.0.1:1922", "127.0.0.1"), + # What: arrange the proxy http localhost port portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe readiness or proxy targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("proxy", "http://localhost:${PORT}", "127.0.0.1"), + # What: arrange the proxy https port portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe readiness or proxy targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("proxy", "https://127.0.0.1:${PORT}", "127.0.0.1"), + # What: arrange the proxy http port admin portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe readiness or proxy targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("proxy", "http://127.0.0.1:${PORT}/../admin", "127.0.0.1"), + # What: arrange the proxy http port api token x portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe readiness or proxy targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("proxy", "http://127.0.0.1:${PORT}/api?token=x", "127.0.0.1"), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_unsafe_readiness_or_proxy_targets groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_unsafe_readiness_or_proxy_targets test around tmp path and field and value and message; why: this test groups the arrange, act, and assertions that protect the catalog rejects unsafe readiness or proxy targets outcome. +def test_catalog_rejects_unsafe_readiness_or_proxy_targets( + # What: arrange the tmp path input for test_catalog_rejects_unsafe_readiness_or_proxy_targets; why: test_catalog_rejects_unsafe_readiness_or_proxy_targets consumes tmp path during path tmp path models toml, so callers must bind it with the other signature inputs. + tmp_path, field, value, message +# What: arrange the grouped source fragment for the scenario; why: test catalog rejects unsafe readiness or proxy targets requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange path as tmp path and models and toml; why: the catalog rejects unsafe readiness or proxy targets test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with field and value and models and bad and model; why: the catalog rejects unsafe readiness or proxy targets scenario observes the path.write_text return value during f models bad nmodel bad gguf n field. + path.write_text( + # What: arrange the exact f models bad nmodel bad gguf n field fixture fragment; why: the catalog rejects unsafe readiness or proxy targets scenario feeds this byte-preserved fragment through f'[models.bad]\nmodel = "bad.gguf"\n{field} = "{value}"\n' before asserting its protocol or parser result. + f'[models.bad]\nmodel = "bad.gguf"\n{field} = "{value}"\n', + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog rejects unsafe readiness or proxy targets scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_rejects_unsafe_readiness_or_proxy_targets groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: assert the pytest.raises failure context; why: the catalog rejects unsafe readiness or proxy targets scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=message): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects unsafe readiness or proxy targets scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: define the test_catalog_validates_and_exposes_supported_listing_capabilities test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog validates and exposes supported listing capabilities outcome. +def test_catalog_validates_and_exposes_supported_listing_capabilities(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog validates and exposes supported listing capabilities test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and coding and model and coding; why: the catalog validates and exposes supported listing capabilities scenario observes the path.write_text return value during models coding. + path.write_text( + # What: arrange the exact models coding fixture fragment; why: the catalog validates and exposes supported listing capabilities scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact model coding gguf fixture fragment; why: the catalog validates and exposes supported listing capabilities scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact models coding capabilities fixture fragment; why: the catalog validates and exposes supported listing capabilities scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact in text fixture fragment; why: the catalog validates and exposes supported listing capabilities scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact out text fixture fragment; why: the catalog validates and exposes supported listing capabilities scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact tools true fixture fragment; why: the catalog validates and exposes supported listing capabilities scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact context fixture fragment; why: the catalog validates and exposes supported listing capabilities scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog validates and exposes supported listing capabilities scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + """[models.coding] +model = "coding.gguf" + +[models.coding.capabilities] +in = ["text"] +out = ["text"] +tools = true +context = 32768 +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog validates and exposes supported listing capabilities scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_validates_and_exposes_supported_listing_capabilities groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling operation.get and capture profile; why: the catalog validates and exposes supported listing capabilities test asserts the response, state, or failure produced by this call. + profile = ModelCatalog.load(str(path)).get("coding") + + # What: assert that profile capabilities equals model capabilities text text true 32768; why: this assertion protects the catalog validates and exposes supported listing capabilities regression after the test's arranged inputs and exercised call. + assert profile.capabilities == ModelCapabilities(("text",), ("text",), True, 32768) + # What: assert the expected profile public capabilities == outcome; why: test catalog test catalog validates and exposes supported listing capabilities protects its regression by requiring this observable result after the exercised behavior. + assert profile.public()["capabilities"] == { + # What: arrange in text out text tools True context 32768 for the scenario; why: test catalog test catalog validates and exposes supported listing capabilities requires this concrete input or helper state before exercising the behavior under test. + "in": ["text"], "out": ["text"], "tools": True, "context": 32768, + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog validates and exposes supported listing capabilities requires this concrete input or helper state before exercising the behavior under test. + } + + +# What: define the test_catalog_validates_model_display_name_and_json_metadata test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog validates model display name and json metadata outcome. +def test_catalog_validates_model_display_name_and_json_metadata(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog validates model display name and json metadata test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and coding and model and coding; why: the catalog validates model display name and json metadata scenario observes the path.write_text return value during models coding. + path.write_text( + # What: arrange the exact models coding fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact model coding gguf fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact name coding model fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact description fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact upstream timeout s fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact models coding metadata fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact tier stable fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact tags local text fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact models coding metadata nested fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact enabled true fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + """[models.coding] +model = "coding.gguf" +name = " Coding Model " +description = " " +upstream_timeout_s = 45 + +[models.coding.metadata] +tier = "stable" +tags = ["local", "text"] + +[models.coding.metadata.nested] +enabled = true +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog validates model display name and json metadata scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_validates_model_display_name_and_json_metadata groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling operation.get and capture profile; why: the catalog validates model display name and json metadata test asserts the response, state, or failure produced by this call. + profile = ModelCatalog.load(str(path)).get("coding") + + # What: assert that profile display name equals coding model; why: this assertion protects the catalog validates model display name and json metadata regression after the test's arranged inputs and exercised call. + assert profile.display_name == "Coding Model" + # What: assert that profile description is group delimiter; why: this assertion protects the catalog validates model display name and json metadata regression after the test's arranged inputs and exercised call. + assert profile.description is None + # What: assert that profile upstream timeout s equals 45; why: this assertion protects the catalog validates model display name and json metadata regression after the test's arranged inputs and exercised call. + assert profile.upstream_timeout_s == 45 + # What: assert the expected profile metadata == outcome; why: test catalog test catalog validates model display name and json metadata protects its regression by requiring this observable result after the exercised behavior. + assert profile.metadata() == { + # What: arrange nested enabled True tags local text tier stable for the scenario; why: test catalog test catalog validates model display name and json metadata requires this concrete input or helper state before exercising the behavior under test. + "nested": {"enabled": True}, "tags": ["local", "text"], "tier": "stable", + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog validates model display name and json metadata requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that profile public display name equals coding model; why: this assertion protects the catalog validates model display name and json metadata regression after the test's arranged inputs and exercised call. + assert profile.public()["displayName"] == "Coding Model" + # What: assert that profile public metadata equals profile metadata; why: this assertion protects the catalog validates model display name and json metadata regression after the test's arranged inputs and exercised call. + assert profile.public()["metadata"] == profile.metadata() + # What: assert that profile public upstream timeout s equals 45; why: this assertion protects the catalog validates model display name and json metadata regression after the test's arranged inputs and exercised call. + assert profile.public()["upstreamTimeoutS"] == 45 + + +# What: define the test_catalog_validates_request_fields_and_creates_variant_aliases test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog validates request fields and creates variant aliases outcome. +def test_catalog_validates_request_fields_and_creates_variant_aliases(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog validates request fields and creates variant aliases test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and coding and model and coding; why: the catalog validates request fields and creates variant aliases scenario observes the path.write_text return value during models coding. + path.write_text( + # What: arrange the exact models coding fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact model coding gguf fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact use model name engine coding v1 fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact drop fields metadata private fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact models coding set fields fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange models coding for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + # What: arrange the exact max tokens fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact chat template kwargs enable thinking true fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact models coding set fields by id coding high fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange models coding for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + # What: arrange the exact chat template kwargs reasoning effort high fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + """[models.coding] +model = "coding.gguf" +use_model_name = "engine/coding-v1" +drop_fields = ["metadata.private"] + +[models.coding.set_fields] +temperature = 0.2 +"max_tokens?" = 4096 +"chat_template_kwargs.enable_thinking?" = true + +[models.coding.set_fields_by_id."coding:high"] +temperature = 0.1 +"chat_template_kwargs.reasoning_effort" = "high" +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog validates request fields and creates variant aliases scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_validates_request_fields_and_creates_variant_aliases groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling ModelCatalog.load and capture catalog; why: the catalog validates request fields and creates variant aliases test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog.load(str(path)) + # What: act by calling catalog.get and capture profile; why: the catalog validates request fields and creates variant aliases test asserts the response, state, or failure produced by this call. + profile = catalog.get("coding:high") + + # What: assert that profile is catalog get coding; why: this assertion protects the catalog validates request fields and creates variant aliases regression after the test's arranged inputs and exercised call. + assert profile is catalog.get("coding") + # What: assert that profile aliases equals coding high; why: this assertion protects the catalog validates request fields and creates variant aliases regression after the test's arranged inputs and exercised call. + assert profile.aliases == ("coding:high",) + # What: assert that profile use model name equals engine coding v1; why: this assertion protects the catalog validates request fields and creates variant aliases regression after the test's arranged inputs and exercised call. + assert profile.use_model_name == "engine/coding-v1" + # What: assert that profile drop fields equals metadata private; why: this assertion protects the catalog validates request fields and creates variant aliases regression after the test's arranged inputs and exercised call. + assert profile.drop_fields == ("metadata.private",) + # What: assert the expected profile public setFields == outcome; why: test catalog test catalog validates request fields and creates variant aliases protects its regression by requiring this observable result after the exercised behavior. + assert profile.public()["setFields"] == { + # What: arrange temperature 0.2 for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + "temperature": 0.2, + # What: arrange chat template kwargs enable thinking True for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + "chat_template_kwargs.enable_thinking?": True, + # What: arrange max tokens 4096 for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + "max_tokens?": 4096, + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert the expected profile public setFieldsById == outcome; why: test catalog test catalog validates request fields and creates variant aliases protects its regression by requiring this observable result after the exercised behavior. + assert profile.public()["setFieldsById"] == { + # What: arrange coding high for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + "coding:high": { + # What: arrange chat template kwargs reasoning effort high for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + "chat_template_kwargs.reasoning_effort": "high", + # What: arrange temperature 0.1 for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + "temperature": 0.1, + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + } + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog validates request fields and creates variant aliases requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that profile public use model name equals engine coding v1; why: this assertion protects the catalog validates request fields and creates variant aliases regression after the test's arranged inputs and exercised call. + assert profile.public()["useModelName"] == "engine/coding-v1" + + +# What: define the test_catalog_hard_request_field_wins_over_soft_spelling test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog hard request field wins over soft spelling outcome. +def test_catalog_hard_request_field_wins_over_soft_spelling(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog hard request field wins over soft spelling test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and coding and model and coding; why: the catalog hard request field wins over soft spelling scenario observes the path.write_text return value during models coding. + path.write_text( + # What: arrange the exact models coding fixture fragment; why: the catalog hard request field wins over soft spelling scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact model coding gguf fixture fragment; why: the catalog hard request field wins over soft spelling scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange the exact models coding set fields fixture fragment; why: the catalog hard request field wins over soft spelling scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + # What: arrange models coding for the scenario; why: test catalog test catalog hard request field wins over soft spelling requires this concrete input or helper state before exercising the behavior under test. + # What: arrange models coding for the scenario; why: test catalog test catalog hard request field wins over soft spelling requires this concrete input or helper state before exercising the behavior under test. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog hard request field wins over soft spelling scenario feeds this byte-preserved fragment through """[models.coding] before asserting its protocol or parser result. + """[models.coding] +model = "coding.gguf" +[models.coding.set_fields] +max_tokens = 1000 +"max_tokens?" = 2000 +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog hard request field wins over soft spelling scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_hard_request_field_wins_over_soft_spelling groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling operation.get and capture fields; why: the catalog hard request field wins over soft spelling test asserts the response, state, or failure produced by this call. + fields = ModelCatalog.load(str(path)).get("coding").set_fields + + # What: assert the expected field key field value field soft for field in fields == outcome; why: test catalog test catalog hard request field wins over soft spelling protects its regression by requiring this observable result after the exercised behavior. + assert [(field.key, field.value(), field.soft) for field in fields] == [ + # What: arrange max tokens 1000 False for the scenario; why: test catalog test catalog hard request field wins over soft spelling requires this concrete input or helper state before exercising the behavior under test. + ("max_tokens", 1000, False) + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog hard request field wins over soft spelling requires this concrete input or helper state before exercising the behavior under test. + ] + + +# What: parameterize test_catalog_rejects_unsupported_or_malformed_capabilities with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects unsupported or malformed capabilities. +@pytest.mark.parametrize("declaration,message", [ + # What: arrange the in image unsupported modalities image portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or malformed capabilities scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('in = ["image"]', "unsupported modalities: image"), + # What: arrange the out audio unsupported modalities audio portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or malformed capabilities scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('out = ["audio"]', "unsupported modalities: audio"), + # What: arrange the out video unsupported modalities video portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or malformed capabilities scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('out = ["video"]', "unsupported modalities: video"), + # What: arrange the in text text must not contain portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or malformed capabilities scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('in = ["text", "text"]', "must not contain duplicates"), + # What: arrange the tools tools must be a boolean portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or malformed capabilities scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("tools = 1", "tools must be a boolean"), + # What: arrange the context context must be a nonnegative portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or malformed capabilities scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("context = -1", "context must be a nonnegative integer"), + # What: arrange the context true context must be a portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or malformed capabilities scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("context = true", "context must be a nonnegative integer"), + # What: arrange the reranker true unsupported keys reranker portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or malformed capabilities scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("reranker = true", "unsupported keys: reranker"), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_unsupported_or_malformed_capabilities groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_unsupported_or_malformed_capabilities test around tmp path and declaration and message; why: this test groups the arrange, act, and assertions that protect the catalog rejects unsupported or malformed capabilities outcome. +def test_catalog_rejects_unsupported_or_malformed_capabilities( + # What: arrange the tmp path input for test_catalog_rejects_unsupported_or_malformed_capabilities; why: test_catalog_rejects_unsupported_or_malformed_capabilities consumes tmp path during path tmp path models toml, so callers must bind it with the other signature inputs. + tmp_path, declaration, message +# What: arrange the grouped source fragment for the scenario; why: test catalog rejects unsupported or malformed capabilities requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange path as tmp path and models and toml; why: the catalog rejects unsupported or malformed capabilities test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with declaration and models and coding and model and coding; why: the catalog rejects unsupported or malformed capabilities scenario observes the path.write_text return value during f models coding nmodel coding gguf n. + path.write_text( + # What: arrange the exact f models coding nmodel coding gguf n fixture fragment; why: the catalog rejects unsupported or malformed capabilities scenario feeds this byte-preserved fragment through f'[models.coding]\nmodel = "coding.gguf"\n' before asserting its protocol or parser result. + # What: arrange the exact f models coding capabilities n declaration n fixture fragment; why: the catalog rejects unsupported or malformed capabilities scenario feeds this byte-preserved fragment through f'[models.coding]\nmodel = "coding.gguf"\n' before asserting its protocol or parser result. + f'[models.coding]\nmodel = "coding.gguf"\n' + f'[models.coding.capabilities]\n{declaration}\n', + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog rejects unsupported or malformed capabilities scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_rejects_unsupported_or_malformed_capabilities groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: assert the pytest.raises failure context; why: the catalog rejects unsupported or malformed capabilities scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=message): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects unsupported or malformed capabilities scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: parameterize test_catalog_rejects_unsafe_request_field_configuration with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects unsafe request field configuration. +@pytest.mark.parametrize("declaration,message", [ + # What: arrange the models coding set fields nmodel other must not set portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe request field configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[models.coding.set_fields]\nmodel = "other"', "must not set model"), + # What: arrange the models coding set fields n model other must not portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe request field configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[models.coding.set_fields]\n"model?" = "other"', "must not set model"), + # What: arrange the models coding set fields n bad path safe dot delimited portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe request field configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[models.coding.set_fields]\n"bad..path" = 1', "safe dot-delimited"), + # What: arrange the models coding set fields nstarted json compatible portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe request field configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[models.coding.set_fields]\nstarted = 2026-09-14', "JSON-compatible"), + # What: arrange the enclosing predicate collection with models and coding and set fields by id and; why: test_catalog_rejects_unsafe_request_field_configuration groups the supplied clauses as one test_catalog_rejects_unsafe_request_field_configuration expression collection before its. + ( + # What: arrange the models coding set fields by id bad alias ntemperature portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe request field configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '[models.coding.set_fields_by_id."bad//alias"]\ntemperature = 1', + # What: arrange the slash separated portion of the enclosing predicate; why: this clause remains in the catalog rejects unsafe request field configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "slash-separated", + # What: arrange the enclosing predicate collection with models and coding and set fields by id and; why: test_catalog_rejects_unsafe_request_field_configuration groups the supplied clauses as one test_catalog_rejects_unsafe_request_field_configuration expression collection before its. + ), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_unsafe_request_field_configuration groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_unsafe_request_field_configuration test around tmp path and declaration and message; why: this test groups the arrange, act, and assertions that protect the catalog rejects unsafe request field configuration outcome. +def test_catalog_rejects_unsafe_request_field_configuration( + # What: arrange the tmp path input for test_catalog_rejects_unsafe_request_field_configuration; why: test_catalog_rejects_unsafe_request_field_configuration consumes tmp path during path tmp path models toml, so callers must bind it with the other signature inputs. + tmp_path, declaration, message +# What: arrange the grouped source fragment for the scenario; why: test catalog rejects unsafe request field configuration requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange path as tmp path and models and toml; why: the catalog rejects unsafe request field configuration test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with declaration and models and coding and model and coding; why: the catalog rejects unsafe request field configuration scenario observes the path.write_text return value during f models coding nmodel coding gguf n declaration. + path.write_text( + # What: arrange the exact f models coding nmodel coding gguf n declaration fixture fragment; why: the catalog rejects unsafe request field configuration scenario feeds this byte-preserved fragment through f'[models.coding]\nmodel = "coding.gguf"\n{declaration}\n' before asserting its protocol or parser result. + f'[models.coding]\nmodel = "coding.gguf"\n{declaration}\n', + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog rejects unsafe request field configuration scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_rejects_unsafe_request_field_configuration groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: assert the pytest.raises failure context; why: the catalog rejects unsafe request field configuration scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=message): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects unsafe request field configuration scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: define the test_filter_generated_alias_cannot_collide_with_another_profile test around tmp path; why: this test groups the arrange, act, and assertions that protect the filter generated alias cannot collide with another profile outcome. +def test_filter_generated_alias_cannot_collide_with_another_profile(tmp_path): + # What: arrange path as tmp path and models and toml; why: the filter generated alias cannot collide with another profile test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and one and model and one; why: the filter generated alias cannot collide with another profile scenario observes the path.write_text return value during models one. + path.write_text( + # What: arrange the exact models one fixture fragment; why: the filter generated alias cannot collide with another profile scenario feeds this byte-preserved fragment through """[models.one] before asserting its protocol or parser result. + # What: arrange the exact model one gguf fixture fragment; why: the filter generated alias cannot collide with another profile scenario feeds this byte-preserved fragment through """[models.one] before asserting its protocol or parser result. + # What: arrange the exact models one set fields by id two fixture fragment; why: the filter generated alias cannot collide with another profile scenario feeds this byte-preserved fragment through """[models.one] before asserting its protocol or parser result. + # What: arrange the exact temperature fixture fragment; why: the filter generated alias cannot collide with another profile scenario feeds this byte-preserved fragment through """[models.one] before asserting its protocol or parser result. + # What: arrange the exact models two fixture fragment; why: the filter generated alias cannot collide with another profile scenario feeds this byte-preserved fragment through """[models.one] before asserting its protocol or parser result. + # What: arrange the exact model two gguf fixture fragment; why: the filter generated alias cannot collide with another profile scenario feeds this byte-preserved fragment through """[models.one] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the filter generated alias cannot collide with another profile scenario feeds this byte-preserved fragment through """[models.one] before asserting its protocol or parser result. + """[models.one] +model = "one.gguf" +[models.one.set_fields_by_id.two] +temperature = 0.1 +[models.two] +model = "two.gguf" +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the filter generated alias cannot collide with another profile scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_filter_generated_alias_cannot_collide_with_another_profile groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: assert the pytest.raises failure context; why: the filter generated alias cannot collide with another profile scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match="conflicts with a configured profile"): + # What: act by calling ModelCatalog.load with str and path; why: the filter generated alias cannot collide with another profile scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: parameterize test_catalog_rejects_ambiguous_or_shell_style_profiles with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects ambiguous or shell style profiles. +@pytest.mark.parametrize("content, message", [ + # What: arrange the models bad nmodel m nargs port n portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or shell style profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[models.bad]\nmodel = 'm'\nargs = ['--port', '9']\n", "must not set --model or --port"), + # What: arrange the models bad nmodel m ncmd anything n portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or shell style profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[models.bad]\nmodel = 'm'\ncmd = 'anything'\n", "unsupported keys"), + # What: arrange the models bad nmodel n non empty string portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or shell style profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[models.bad]\nmodel = ''\n", "non-empty string"), + # What: arrange the models bad nmodel m nport n through portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or shell style profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[models.bad]\nmodel = 'm'\nport = -1\n", "0 through 65535"), + # What: arrange the models bad nmodel m nuse model name n non empty portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or shell style profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[models.bad]\nmodel = 'm'\nuse_model_name = ''\n", "non-empty trimmed"), + # What: arrange the models bad nmodel m nuse model name bad n portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or shell style profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[models.bad]\nmodel = 'm'\nuse_model_name = ' bad'\n", "non-empty trimmed"), + # What: arrange the models bad nmodel m nupstream timeout s n upstream timeout s portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or shell style profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[models.bad]\nmodel = 'm'\nupstream_timeout_s = 0\n", "upstream_timeout_s"), + # What: arrange the enclosing predicate collection with models and bad and model and m and json; why: test_catalog_rejects_ambiguous_or_shell_style_profiles groups the supplied clauses as one test_catalog_rejects_ambiguous_or_shell_style_profiles expression collection before its. + ( + # What: arrange the models bad nmodel m n models bad metadata ncreated portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or shell style profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "[models.bad]\nmodel = 'm'\n[models.bad.metadata]\ncreated = 2026-09-14\n", + # What: arrange the json compatible portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or shell style profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "JSON-compatible", + # What: arrange the enclosing predicate collection with models and bad and model and m and json; why: test_catalog_rejects_ambiguous_or_shell_style_profiles groups the supplied clauses as one test_catalog_rejects_ambiguous_or_shell_style_profiles expression collection before its. + ), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_ambiguous_or_shell_style_profiles groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_ambiguous_or_shell_style_profiles test around tmp path and content and message; why: this test groups the arrange, act, and assertions that protect the catalog rejects ambiguous or shell style profiles outcome. +def test_catalog_rejects_ambiguous_or_shell_style_profiles(tmp_path, content, message): + # What: arrange path as tmp path and models and toml; why: the catalog rejects ambiguous or shell style profiles test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text content encoding utf 8 fixture fragment; why: the catalog rejects ambiguous or shell style profiles scenario feeds this byte-preserved fragment through path.write_text(content, encoding="utf-8") before asserting its protocol or parser result. + path.write_text(content, encoding="utf-8") + # What: assert the pytest.raises failure context; why: the catalog rejects ambiguous or shell style profiles scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=message): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects ambiguous or shell style profiles scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: define the test_catalog_unknown_profile_has_operator_facing_error test around local fixtures; why: this test groups the arrange, act, and assertions that protect the catalog unknown profile has operator facing error outcome. +def test_catalog_unknown_profile_has_operator_facing_error(): + # What: assert the pytest.raises failure context; why: the catalog unknown profile has operator facing error scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match="unknown model profile 'missing'"): + # What: arrange the exact model catalog empty get missing fixture fragment; why: the catalog unknown profile has operator facing error scenario feeds this byte-preserved fragment through ModelCatalog.empty().get("missing") before asserting its protocol or parser result. + ModelCatalog.empty().get("missing") + + +# What: define the test_catalog_marks_port_zero_as_an_explicit_dynamic_port test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog marks port zero as an explicit dynamic port outcome. +def test_catalog_marks_port_zero_as_an_explicit_dynamic_port(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog marks port zero as an explicit dynamic port test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models dynamic nmodel m nport n fixture fragment; why: the catalog marks port zero as an explicit dynamic port scenario feeds this byte-preserved fragment through path.write_text("[models.dynamic]\nmodel = 'm'\nport = 0\n", encoding="u before asserting its protocol or pars. + path.write_text("[models.dynamic]\nmodel = 'm'\nport = 0\n", encoding="utf-8") + # What: act by calling operation.get and capture profile; why: the catalog marks port zero as an explicit dynamic port test asserts the response, state, or failure produced by this call. + profile = ModelCatalog.load(str(path)).get("dynamic") + # What: assert that profile port equals 0; why: this assertion protects the catalog marks port zero as an explicit dynamic port regression after the test's arranged inputs and exercised call. + assert profile.port == 0 + # What: assert that profile public dynamic port is true; why: this assertion protects the catalog marks port zero as an explicit dynamic port regression after the test's arranged inputs and exercised call. + assert profile.public()["dynamicPort"] is True + + +# What: define the test_catalog_resolves_collision_safe_aliases_and_hides_unlisted_models test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog resolves collision safe aliases and hides unlisted models outcome. +def test_catalog_resolves_collision_safe_aliases_and_hides_unlisted_models(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog resolves collision safe aliases and hides unlisted models test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with router and include aliases in list and true and models; why: the catalog resolves collision safe aliases and hides unlisted models scenario observes the path.write_text return value during router. + path.write_text( + # What: arrange the exact router fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact include aliases in list true fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact models visible fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact model visible gguf fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact aliases nickname compat id fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact models hidden fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact model hidden gguf fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact aliases private name fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact unlisted true fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + """[router] +include_aliases_in_list = true + +[models.visible] +model = "visible.gguf" +aliases = ["nickname", "compat-id"] + +[models.hidden] +model = "hidden.gguf" +aliases = ["private-name"] +unlisted = true +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog resolves collision safe aliases and hides unlisted models scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_resolves_collision_safe_aliases_and_hides_unlisted_models groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling ModelCatalog.load and capture catalog; why: the catalog resolves collision safe aliases and hides unlisted models test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog.load(str(path)) + + # What: assert that catalog get nickname is catalog get visible; why: this assertion protects the catalog resolves collision safe aliases and hides unlisted models regression after the test's arranged inputs and exercised call. + assert catalog.get("nickname") is catalog.get("visible") + # What: assert that catalog get private name is catalog get hidden; why: this assertion protects the catalog resolves collision safe aliases and hides unlisted models regression after the test's arranged inputs and exercised call. + assert catalog.get("private-name") is catalog.get("hidden") + # What: assert that catalog listed model ids equals visible nickname compat id; why: this assertion protects the catalog resolves collision safe aliases and hides unlisted models regression after the test's arranged inputs and exercised call. + assert catalog.listed_model_ids() == ("visible", "nickname", "compat-id") + # What: act by calling ModelCatalog and capture default listing; why: the catalog resolves collision safe aliases and hides unlisted models test asserts the response, state, or failure produced by this call. + default_listing = ModelCatalog({ + # What: arrange the visible field as get and catalog and visible; why: test_catalog_resolves_collision_safe_aliases_and_hides_unlisted_models carries visible through default listing into assert default listing listed model ids equals visible. + "visible": catalog.get("visible"), + # What: arrange the hidden field as get and catalog and hidden; why: test_catalog_resolves_collision_safe_aliases_and_hides_unlisted_models carries hidden through default listing into assert default listing listed model ids equals visible. + "hidden": catalog.get("hidden"), + # What: arrange the ModelCatalog call with get; why: test_catalog_resolves_collision_safe_aliases_and_hides_unlisted_models groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: assert that default listing listed model ids equals visible; why: this assertion protects the catalog resolves collision safe aliases and hides unlisted models regression after the test's arranged inputs and exercised call. + assert default_listing.listed_model_ids() == ("visible",) + # What: act by calling catalog.public and capture public; why: the catalog resolves collision safe aliases and hides unlisted models test asserts the response, state, or failure produced by this call. + public = {profile["name"]: profile for profile in catalog.public()} + # What: assert that public visible aliases equals nickname compat id; why: this assertion protects the catalog resolves collision safe aliases and hides unlisted models regression after the test's arranged inputs and exercised call. + assert public["visible"]["aliases"] == ["nickname", "compat-id"] + # What: assert that public hidden unlisted is true; why: this assertion protects the catalog resolves collision safe aliases and hides unlisted models regression after the test's arranged inputs and exercised call. + assert public["hidden"]["unlisted"] is True + + +# What: parameterize test_catalog_rejects_ambiguous_or_invalid_model_aliases with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects ambiguous or invalid model aliases. +@pytest.mark.parametrize( + # What: arrange the models message portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or invalid model aliases scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "models,message", + # What: arrange the grouped source fragment for the scenario; why: test catalog rejects ambiguous or invalid model aliases requires this concrete input or helper state before exercising the behavior under test. + [ + # What: arrange the enclosing predicate collection with models and one and model and one and conflicts; why: test_catalog_rejects_ambiguous_or_invalid_model_aliases groups the supplied clauses as one test_catalog_rejects_ambiguous_or_invalid_model_aliases expression collection before its. + ( + # What: arrange the exact models one nmodel one gguf naliases two n fixture fragment; why: the catalog rejects ambiguous or invalid model aliases scenario feeds this byte-preserved fragment through "[models.one]\nmodel='one.gguf'\naliases=['two']\n" before asserting its protocol or parser result. + # What: arrange the exact models two nmodel two gguf n fixture fragment; why: the catalog rejects ambiguous or invalid model aliases scenario feeds this byte-preserved fragment through "[models.one]\nmodel='one.gguf'\naliases=['two']\n" before asserting its protocol or parser result. + "[models.one]\nmodel='one.gguf'\naliases=['two']\n" + "[models.two]\nmodel='two.gguf'\n", + # What: arrange the conflicts with a configured profile portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or invalid model aliases scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "conflicts with a configured profile", + # What: arrange the enclosing predicate collection with models and one and model and one and conflicts; why: test_catalog_rejects_ambiguous_or_invalid_model_aliases groups the supplied clauses as one test_catalog_rejects_ambiguous_or_invalid_model_aliases expression collection before its. + ), + # What: arrange the enclosing predicate collection with models and one and model and one and assigned; why: test_catalog_rejects_ambiguous_or_invalid_model_aliases groups the supplied clauses as one test_catalog_rejects_ambiguous_or_invalid_model_aliases expression collection before its. + ( + # What: arrange the exact models one nmodel one gguf naliases shared n fixture fragment; why: the catalog rejects ambiguous or invalid model aliases scenario feeds this byte-preserved fragment through "[models.one]\nmodel='one.gguf'\naliases=['shared']\n" before asserting its protocol or parser result. + # What: arrange the exact models two nmodel two gguf naliases shared n fixture fragment; why: the catalog rejects ambiguous or invalid model aliases scenario feeds this byte-preserved fragment through "[models.one]\nmodel='one.gguf'\naliases=['shared']\n" before asserting its protocol or parser result. + "[models.one]\nmodel='one.gguf'\naliases=['shared']\n" + "[models.two]\nmodel='two.gguf'\naliases=['shared']\n", + # What: arrange the assigned to both portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or invalid model aliases scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "assigned to both", + # What: arrange the enclosing predicate collection with models and one and model and one and assigned; why: test_catalog_rejects_ambiguous_or_invalid_model_aliases groups the supplied clauses as one test_catalog_rejects_ambiguous_or_invalid_model_aliases expression collection before its. + ), + # What: arrange the models one nmodel one gguf naliases bad name portion of the enclosing predicate; why: this clause remains in the catalog rejects ambiguous or invalid model aliases scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[models.one]\nmodel='one.gguf'\naliases=['bad//name']\n", "distinct valid"), + # What: arrange the grouped source fragment for the scenario; why: test catalog rejects ambiguous or invalid model aliases requires this concrete input or helper state before exercising the behavior under test. + ], +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_ambiguous_or_invalid_model_aliases groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +) +# What: define the test_catalog_rejects_ambiguous_or_invalid_model_aliases test around tmp path and models and message; why: this test groups the arrange, act, and assertions that protect the catalog rejects ambiguous or invalid model aliases outcome. +def test_catalog_rejects_ambiguous_or_invalid_model_aliases(tmp_path, models, message): + # What: arrange path as tmp path and models and toml; why: the catalog rejects ambiguous or invalid model aliases test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models encoding utf 8 fixture fragment; why: the catalog rejects ambiguous or invalid model aliases scenario feeds this byte-preserved fragment through path.write_text(models, encoding="utf-8") before asserting its protocol or parser result. + path.write_text(models, encoding="utf-8") + # What: assert the pytest.raises failure context; why: the catalog rejects ambiguous or invalid model aliases scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=message): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects ambiguous or invalid model aliases scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: parameterize test_catalog_rejects_unsafe_namespaced_model_ids with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects unsafe namespaced model ids. +@pytest.mark.parametrize("model_id", ["bad//name", "bad/../name", "/bad"]) +# What: define the test_catalog_rejects_unsafe_namespaced_model_ids test around tmp path and model id; why: this test groups the arrange, act, and assertions that protect the catalog rejects unsafe namespaced model ids outcome. +def test_catalog_rejects_unsafe_namespaced_model_ids(tmp_path, model_id): + # What: arrange path as tmp path and models and toml; why: the catalog rejects unsafe namespaced model ids test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with model id and models and model and model and gguf; why: the catalog rejects unsafe namespaced model ids scenario observes the path.write_text return value during f models model id nmodel model gguf n. + path.write_text( + # What: arrange the exact f models model id nmodel model gguf n fixture fragment; why: the catalog rejects unsafe namespaced model ids scenario feeds this byte-preserved fragment through f'[models."{model_id}"]\nmodel = "model.gguf"\n', encoding="utf-8" before asserting its protocol or parser result. + f'[models."{model_id}"]\nmodel = "model.gguf"\n', encoding="utf-8" + # What: arrange the path.write_text call with encoding; why: test_catalog_rejects_unsafe_namespaced_model_ids groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: assert the pytest.raises failure context; why: the catalog rejects unsafe namespaced model ids scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match="slash-separated"): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects unsafe namespaced model ids scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: define the test_catalog_accepts_colon_variant_model_ids test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog accepts colon variant model ids outcome. +def test_catalog_accepts_colon_variant_model_ids(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog accepts colon variant model ids test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and coding and high and model; why: the catalog accepts colon variant model ids scenario observes the path.write_text return value during models coding high nmodel coding gguf n. + path.write_text( + # What: arrange the exact models coding high nmodel coding gguf n fixture fragment; why: the catalog accepts colon variant model ids scenario feeds this byte-preserved fragment through '[models."coding:high"]\nmodel = "coding.gguf"\n', encoding="utf-8" before asserting its protocol or parser result. + '[models."coding:high"]\nmodel = "coding.gguf"\n', encoding="utf-8" + # What: arrange the path.write_text call with encoding; why: test_catalog_accepts_colon_variant_model_ids groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: assert that model catalog load str path get coding high equals coding high; why: this assertion protects the catalog accepts colon variant model ids regression after the test's arranged inputs and exercised call. + assert ModelCatalog.load(str(path)).get("coding:high").name == "coding:high" + + +# What: define the test_catalog_validates_pin_and_warm_selectors test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog validates pin and warm selectors outcome. +def test_catalog_validates_pin_and_warm_selectors(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog validates pin and warm selectors test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and a and model and a; why: the catalog validates pin and warm selectors scenario observes the path.write_text return value during models a. + path.write_text( + # What: arrange the exact models a fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact model a gguf fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact aliases a variant fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact models b fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact model b gguf fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact selectors public fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact strategy pin fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact targets a variant b fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact name public model fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact description stable local model fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact selectors public metadata fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact tier stable fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact type operator value fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact selectors available fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact strategy warm fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact targets a b fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact selectors hidden fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact strategy pin fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact targets a fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact unlisted true fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through """[models.a] before asserting its protocol or parser result. + """[models.a] +model = "a.gguf" +aliases = ["a:variant"] +[models.b] +model = "b.gguf" + +[selectors.public] +strategy = "pin" +targets = ["a:variant", "b"] +name = "Public Model" +description = "Stable local model" +[selectors.public.metadata] +tier = "stable" +type = "operator-value" + +[selectors.available] +strategy = "warm" +targets = ["a", "b"] + +[selectors.hidden] +strategy = "pin" +targets = ["a"] +unlisted = true +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog validates pin and warm selectors scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_validates_pin_and_warm_selectors groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling ModelCatalog.load and capture catalog; why: the catalog validates pin and warm selectors test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog.load(str(path)) + + # What: assert that catalog selector public targets equals a variant b; why: this assertion protects the catalog validates pin and warm selectors regression after the test's arranged inputs and exercised call. + assert catalog.selector("public").targets == ("a:variant", "b") + # What: assert that catalog selector available strategy equals warm; why: this assertion protects the catalog validates pin and warm selectors regression after the test's arranged inputs and exercised call. + assert catalog.selector("available").strategy == "warm" + # What: assert the expected catalog public selectors == outcome; why: test catalog test catalog validates pin and warm selectors protects its regression by requiring this observable result after the exercised behavior. + assert catalog.public_selectors() == [ + # What: arrange name available strategy warm targets a b for the scenario; why: test catalog test catalog validates pin and warm selectors requires this concrete input or helper state before exercising the behavior under test. + {"name": "available", "strategy": "warm", "targets": ["a", "b"]}, + # What: arrange name hidden strategy pin targets a unlisted True for the scenario; why: test catalog test catalog validates pin and warm selectors requires this concrete input or helper state before exercising the behavior under test. + {"name": "hidden", "strategy": "pin", "targets": ["a"], "unlisted": True}, + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog validates pin and warm selectors requires this concrete input or helper state before exercising the behavior under test. + { + # What: arrange name public strategy pin targets a variant b for the scenario; why: test catalog test catalog validates pin and warm selectors requires this concrete input or helper state before exercising the behavior under test. + "name": "public", "strategy": "pin", "targets": ["a:variant", "b"], + # What: arrange displayName Public Model description Stable local model for the scenario; why: test catalog test catalog validates pin and warm selectors requires this concrete input or helper state before exercising the behavior under test. + "displayName": "Public Model", "description": "Stable local model", + # What: arrange metadata tier stable type operator value for the scenario; why: test catalog test catalog validates pin and warm selectors requires this concrete input or helper state before exercising the behavior under test. + "metadata": {"tier": "stable", "type": "operator-value"}, + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog validates pin and warm selectors requires this concrete input or helper state before exercising the behavior under test. + }, + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog validates pin and warm selectors requires this concrete input or helper state before exercising the behavior under test. + ] + # What: assert that catalog listed model ids equals a b available public; why: this assertion protects the catalog validates pin and warm selectors regression after the test's arranged inputs and exercised call. + assert catalog.listed_model_ids() == ("a", "b", "available", "public") + + +# What: define the test_catalog_validates_runtime_routing_profiles_and_selector_targets test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog validates runtime routing profiles and selector targets outcome. +def test_catalog_validates_runtime_routing_profiles_and_selector_targets(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog validates runtime routing profiles and selector targets test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with router and preload model and a and variant; why: the catalog validates runtime routing profiles and selector targets scenario observes the path.write_text return value during router. + path.write_text( + # What: arrange the exact router fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact preload model a variant fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact startup routing profile coding fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact models a fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact model a gguf fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact aliases a variant fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact selectors available fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact strategy warm fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact targets a fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact profiles coding fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact description coding mode fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact profiles coding pins fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact public available fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact direct a variant fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact disabled fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + """[router] +preload_model = "a:variant" +startup_routing_profile = "coding" + +[models.a] +model = "a.gguf" +aliases = ["a:variant"] + +[selectors.available] +strategy = "warm" +targets = ["a"] + +[profiles.coding] +description = "Coding mode" +[profiles.coding.pins] +public = "available" +direct = "a:variant" +disabled = "" +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog validates runtime routing profiles and selector targets scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_validates_runtime_routing_profiles_and_selector_targets groups the supplied clauses as one path.write_text call before its value is consumed. + ) + + # What: act by calling ModelCatalog.load and capture catalog; why: the catalog validates runtime routing profiles and selector targets test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog.load(str(path)) + + # What: act by calling catalog.routing_profile and capture profile; why: the catalog validates runtime routing profiles and selector targets test asserts the response, state, or failure produced by this call. + profile = catalog.routing_profile("coding") + # What: assert that profile replacement public equals true available; why: this assertion protects the catalog validates runtime routing profiles and selector targets regression after the test's arranged inputs and exercised call. + assert profile.replacement("public") == (True, "available") + # What: assert that profile replacement disabled equals true; why: this assertion protects the catalog validates runtime routing profiles and selector targets regression after the test's arranged inputs and exercised call. + assert profile.replacement("disabled") == (True, None) + # What: assert that profile replacement other equals false; why: this assertion protects the catalog validates runtime routing profiles and selector targets regression after the test's arranged inputs and exercised call. + assert profile.replacement("other") == (False, None) + # What: assert that catalog settings preload model equals a; why: this assertion protects the catalog validates runtime routing profiles and selector targets regression after the test's arranged inputs and exercised call. + assert catalog.settings.preload_model == "a" + # What: assert that catalog settings startup routing profile equals coding; why: this assertion protects the catalog validates runtime routing profiles and selector targets regression after the test's arranged inputs and exercised call. + assert catalog.settings.startup_routing_profile == "coding" + # What: assert the expected catalog public routing profiles == outcome; why: test catalog test catalog validates runtime routing profiles and selector targets protects its regression by requiring this observable result after the exercised behavior. + assert catalog.public_routing_profiles() == [{ + # What: arrange name coding for the scenario; why: test catalog test catalog validates runtime routing profiles and selector targets requires this concrete input or helper state before exercising the behavior under test. + "name": "coding", + # What: arrange description Coding mode for the scenario; why: test catalog test catalog validates runtime routing profiles and selector targets requires this concrete input or helper state before exercising the behavior under test. + "description": "Coding mode", + # What: arrange pins direct a variant disabled None public available for the scenario; why: test catalog test catalog validates runtime routing profiles and selector targets requires this concrete input or helper state before exercising the behavior under test. + "pins": {"direct": "a:variant", "disabled": None, "public": "available"}, + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog validates runtime routing profiles and selector targets requires this concrete input or helper state before exercising the behavior under test. + }] + + +# What: parameterize test_catalog_rejects_invalid_runtime_routing_profiles with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects invalid runtime routing profiles. +@pytest.mark.parametrize("content,message", [ + # What: arrange the profiles empty npins n must contain at portion of the enclosing predicate; why: this clause remains in the catalog rejects invalid runtime routing profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[profiles.empty]\npins = {}\n', "must contain at least one"), + # What: arrange the enclosing predicate collection with profiles and bad and pins and public and references; why: test_catalog_rejects_invalid_runtime_routing_profiles groups the supplied clauses as one test_catalog_rejects_invalid_runtime_routing_profiles expression collection before its. + ( + # What: arrange the profiles bad pins npublic missing n portion of the enclosing predicate; why: this clause remains in the catalog rejects invalid runtime routing profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '[profiles.bad.pins]\npublic = "missing"\n', + # What: arrange the references unknown model portion of the enclosing predicate; why: this clause remains in the catalog rejects invalid runtime routing profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "references unknown model", + # What: arrange the enclosing predicate collection with profiles and bad and pins and public and references; why: test_catalog_rejects_invalid_runtime_routing_profiles groups the supplied clauses as one test_catalog_rejects_invalid_runtime_routing_profiles expression collection before its. + ), + # What: arrange the profiles bad pins npublic n model id or portion of the enclosing predicate; why: this clause remains in the catalog rejects invalid runtime routing profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[profiles.bad.pins]\npublic = 7\n', "model ID or empty string"), + # What: arrange the profiles bad name pins npublic a portion of the enclosing predicate; why: this clause remains in the catalog rejects invalid runtime routing profiles scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[profiles."bad/name".pins]\npublic = "a"\n', "profile name"), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_invalid_runtime_routing_profiles groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_invalid_runtime_routing_profiles test around tmp path and content and message; why: this test groups the arrange, act, and assertions that protect the catalog rejects invalid runtime routing profiles outcome. +def test_catalog_rejects_invalid_runtime_routing_profiles(tmp_path, content, message): + # What: arrange path as tmp path and models and toml; why: the catalog rejects invalid runtime routing profiles test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models a nmodel a gguf n content fixture fragment; why: the catalog rejects invalid runtime routing profiles scenario feeds this byte-preserved fragment through path.write_text('[models.a]\nmodel = "a.gguf"\n' + content, encoding="ut before asserting its protocol or parser. + path.write_text('[models.a]\nmodel = "a.gguf"\n' + content, encoding="utf-8") + # What: assert the pytest.raises failure context; why: the catalog rejects invalid runtime routing profiles scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=message): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects invalid runtime routing profiles scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: parameterize test_catalog_rejects_unknown_startup_targets with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects unknown startup targets. +@pytest.mark.parametrize("setting,message", [ + # What: arrange the preload model missing unknown model profile portion of the enclosing predicate; why: this clause remains in the catalog rejects unknown startup targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('preload_model = "missing"', "unknown model profile"), + # What: arrange the startup routing profile missing unknown profile portion of the enclosing predicate; why: this clause remains in the catalog rejects unknown startup targets scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('startup_routing_profile = "missing"', "unknown profile"), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_unknown_startup_targets groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_unknown_startup_targets test around tmp path and setting and message; why: this test groups the arrange, act, and assertions that protect the catalog rejects unknown startup targets outcome. +def test_catalog_rejects_unknown_startup_targets(tmp_path, setting, message): + # What: arrange path as tmp path and models and toml; why: the catalog rejects unknown startup targets test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text f router n setting n fixture fragment; why: the catalog rejects unknown startup targets scenario feeds this byte-preserved fragment through path.write_text(f"[router]\n{setting}\n[models.a]\nmodel='a.gguf'\n", en before asserting its protocol or parser result. + path.write_text(f"[router]\n{setting}\n[models.a]\nmodel='a.gguf'\n", encoding="utf-8") + # What: assert the pytest.raises failure context; why: the catalog rejects unknown startup targets scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=message): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects unknown startup targets scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: parameterize test_catalog_rejects_unsupported_or_ambiguous_selectors with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects unsupported or ambiguous selectors. +@pytest.mark.parametrize("content,message", [ + # What: arrange the grouped source fragment for the scenario; why: test catalog rejects unsupported or ambiguous selectors requires this concrete input or helper state before exercising the behavior under test. + ( + # What: arrange the selectors bad nstrategy spillover ntargets a n portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or ambiguous selectors scenario\'s enclosing expression so its grouping and evaluation order stay intact. + '[selectors.bad]\nstrategy = "spillover"\ntargets = ["a"]\n', + # What: arrange the requires multi resident or peer capacity portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or ambiguous selectors scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "requires multi-resident or peer capacity", + # What: arrange the grouped source fragment for the scenario; why: test catalog rejects unsupported or ambiguous selectors requires this concrete input or helper state before exercising the behavior under test. + ), + # What: arrange the selectors bad nstrategy random ntargets a n portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or ambiguous selectors scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[selectors.bad]\nstrategy = "random"\ntargets = ["a"]\n', "pin or warm"), + # What: arrange the selectors bad nstrategy pin ntargets n to portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or ambiguous selectors scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[selectors.bad]\nstrategy = "pin"\ntargets = []\n', "1 to 64"), + # What: arrange the enclosing predicate collection with selectors and bad and strategy and pin and json; why: test_catalog_rejects_unsupported_or_ambiguous_selectors groups the supplied clauses as one test_catalog_rejects_unsupported_or_ambiguous_selectors expression collection before its. + ( + # What: arrange the exact selectors bad nstrategy pin ntargets a n fixture fragment; why: the catalog rejects unsupported or ambiguous selectors scenario feeds this byte-preserved fragment through '[selectors.bad]\nstrategy = "pin"\ntargets = ["a"]\n' before asserting its protocol or parser result. + # What: arrange the exact selectors bad metadata ncreated n fixture fragment; why: the catalog rejects unsupported or ambiguous selectors scenario feeds this byte-preserved fragment through '[selectors.bad]\nstrategy = "pin"\ntargets = ["a"]\n' before asserting its protocol or parser result. + '[selectors.bad]\nstrategy = "pin"\ntargets = ["a"]\n' + '[selectors.bad.metadata]\ncreated = 2026-09-14\n', + # What: arrange the json compatible portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or ambiguous selectors scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "JSON-compatible", + # What: arrange the enclosing predicate collection with selectors and bad and strategy and pin and json; why: test_catalog_rejects_unsupported_or_ambiguous_selectors groups the supplied clauses as one test_catalog_rejects_unsupported_or_ambiguous_selectors expression collection before its. + ), + # What: arrange the selectors bad nstrategy pin ntargets missing n portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or ambiguous selectors scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[selectors.bad]\nstrategy = "pin"\ntargets = ["missing"]\n', "not a configured"), + # What: arrange the selectors a nstrategy pin ntargets a n portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or ambiguous selectors scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[selectors.a]\nstrategy = "pin"\ntargets = ["a"]\n', "conflicts"), + # What: arrange the grouped source fragment for the scenario; why: test catalog rejects unsupported or ambiguous selectors requires this concrete input or helper state before exercising the behavior under test. + ( + # What: arrange the exact selectors first nstrategy pin ntargets second n fixture fragment; why: the catalog rejects unsupported or ambiguous selectors scenario feeds this byte-preserved fragment through '[selectors.first]\nstrategy = "pin"\ntargets = ["second"]\n' before asserting its protocol or parser resul. + # What: arrange the exact selectors second nstrategy warm ntargets a n fixture fragment; why: the catalog rejects unsupported or ambiguous selectors scenario feeds this byte-preserved fragment through '[selectors.first]\nstrategy = "pin"\ntargets = ["second"]\n' before asserting its protocol or parser result. + '[selectors.first]\nstrategy = "pin"\ntargets = ["second"]\n' + '[selectors.second]\nstrategy = "warm"\ntargets = ["a"]\n', + # What: arrange the cannot reference another selector portion of the enclosing predicate; why: this clause remains in the catalog rejects unsupported or ambiguous selectors scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "cannot reference another selector", + # What: arrange the grouped source fragment for the scenario; why: test catalog rejects unsupported or ambiguous selectors requires this concrete input or helper state before exercising the behavior under test. + ), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_catalog_rejects_unsupported_or_ambiguous_selectors groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_catalog_rejects_unsupported_or_ambiguous_selectors test around tmp path and content and message; why: this test groups the arrange, act, and assertions that protect the catalog rejects unsupported or ambiguous selectors outcome. +def test_catalog_rejects_unsupported_or_ambiguous_selectors( + # What: arrange the tmp path input for test_catalog_rejects_unsupported_or_ambiguous_selectors; why: test_catalog_rejects_unsupported_or_ambiguous_selectors consumes tmp path during path tmp path models toml, so callers must bind it with the other signature inputs. + tmp_path, content, message +# What: arrange the grouped source fragment for the scenario; why: test catalog rejects unsupported or ambiguous selectors requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange path as tmp path and models and toml; why: the catalog rejects unsupported or ambiguous selectors test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models a nmodel a gguf n content fixture fragment; why: the catalog rejects unsupported or ambiguous selectors scenario feeds this byte-preserved fragment through path.write_text('[models.a]\nmodel = "a.gguf"\n' + content, encoding="ut before asserting its protocol or pars. + path.write_text('[models.a]\nmodel = "a.gguf"\n' + content, encoding="utf-8") + # What: assert the pytest.raises failure context; why: the catalog rejects unsupported or ambiguous selectors scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=message): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects unsupported or ambiguous selectors scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: define the test_catalog_supports_namespaced_model_ids_and_longest_upstream_prefix test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog supports namespaced model ids and longest upstream prefix outcome. +def test_catalog_supports_namespaced_model_ids_and_longest_upstream_prefix(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog supports namespaced model ids and longest upstream prefix test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with router and include aliases in list and true and models; why: the catalog supports namespaced model ids and longest upstream prefix scenario observes the path.write_text return value during router. + path.write_text( + # What: arrange the exact router fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact include aliases in list true fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact models author fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact model parent gguf fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact models author model fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact model exact gguf fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact aliases org compat fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through """[router] before asserting its protocol or parser result. + """[router] +include_aliases_in_list = true + +[models.author] +model = "parent.gguf" + +[models."author/model"] +model = "exact.gguf" +aliases = ["org/compat"] +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_supports_namespaced_model_ids_and_longest_upstream_prefix groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: act by calling ModelCatalog.load and capture catalog; why: the catalog supports namespaced model ids and longest upstream prefix test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog.load(str(path)) + + # What: assert that catalog get org compat name equals author model; why: this assertion protects the catalog supports namespaced model ids and longest upstream prefix regression after the test's arranged inputs and exercised call. + assert catalog.get("org/compat").name == "author/model" + # What: assert that catalog listed model ids equals author author model org compat; why: this assertion protects the catalog supports namespaced model ids and longest upstream prefix regression after the test's arranged inputs and exercised call. + assert catalog.listed_model_ids() == ("author", "author/model", "org/compat") + # What: act by evaluating requested profile remaining catalog resolve upstream path author model api x y; why: test catalog test captures the behavior or response that its following assertions inspect. + requested, profile, remaining = catalog.resolve_upstream_path("author/model/api/x/y") + # What: assert the expected requested profile name remaining == outcome; why: test catalog test catalog supports namespaced model ids and longest upstream prefix protects its regression by requiring this observable result after the exercised behavior. + assert (requested, profile.name, remaining) == ( + # What: arrange author model author model api x y for the scenario; why: test catalog test catalog supports namespaced model ids and longest upstream prefix requires this concrete input or helper state before exercising the behavior under test. + "author/model", "author/model", "/api/x/y", + # What: arrange the grouped source fragment for the scenario; why: test catalog test catalog supports namespaced model ids and longest upstream prefix requires this concrete input or helper state before exercising the behavior under test. + ) + # What: act by evaluating requested profile remaining catalog resolve upstream path org compat; why: test catalog test catalog supports namespaced model ids and longest upstream prefix captures the behavior or response that its following assertions inspect. + requested, profile, remaining = catalog.resolve_upstream_path("org/compat") + # What: assert that requested profile name remaining equals org compat author model; why: this assertion protects the catalog supports namespaced model ids and longest upstream prefix regression after the test's arranged inputs and exercised call. + assert (requested, profile.name, remaining) == ("org/compat", "author/model", "/") + # What: assert the pytest.raises failure context; why: the catalog supports namespaced model ids and longest upstream prefix scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match="does not begin"): + # What: arrange the exact catalog resolve upstream path missing model v1 chat fixture fragment; why: the catalog supports namespaced model ids and longest upstream prefix scenario feeds this byte-preserved fragment through catalog.resolve_upstream_path("missing/model/v1/chat") before asserting its protocol or. + catalog.resolve_upstream_path("missing/model/v1/chat") + + +# What: define the test_readiness_waits_for_engine_health_not_just_a_listening_process test around local fixtures; why: this test groups the arrange, act, and assertions that protect the readiness waits for engine health not just a listening process outcome. +def test_readiness_waits_for_engine_health_not_just_a_listening_process(): + # What: define Manager as the owner of status; why: daemon callers use this class boundary so those methods share one manager state invariant. + class Manager: + # What: define the status test helper around captured fixture state; why: the readiness waits for engine health not just a listening process scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def status(self): + # What: arrange the running field as true; why: Manager.status carries running into return {"running": True, "pid": 44}. + return {"running": True, "pid": 44} + + # What: define Probe as the owner of __init__ and fresh_health; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: define the __init__ test helper around captured fixture state; why: the readiness waits for engine health not just a listening process scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def __init__(self): + # What: act by calling iter and capture docs; why: the readiness waits for engine health not just a listening process test asserts the response, state, or failure produced by this call. + self.docs = iter([ + # What: arrange the reachable field as true; why: Probe.__init__ carries reachable through docs into the enclosing return or state update. + {"reachable": True, "status": "loading"}, + # What: arrange the reachable field as true; why: Probe.__init__ carries reachable through docs into the enclosing return or state update. + {"reachable": True, "status": "ok", "model": "m"}, + # What: arrange the iter call with ordered positional inputs; why: Probe.__init__ groups the supplied clauses as one iter call before its value is consumed. + ]) + + # What: arrange the def fresh health self port test helper boundary; why: test catalog test readiness waits for engine health not just a listening process uses this local double to isolate the behavior checked by its assertions. + def fresh_health(self, port): + # What: assert that port equals 1922; why: this assertion protects the readiness waits for engine health not just a listening process regression after the test's arranged inputs and exercised call. + assert port == 1922 + # What: return next and docs from the fresh_health test helper; why: the readiness waits for engine health not just a listening process scenario uses this helper result in its subsequent act or assertion. + return next(self.docs) + + # What: act by calling iter and capture clock; why: the readiness waits for engine health not just a listening process test asserts the response, state, or failure produced by this call. + clock = iter([0.0, 0.0, 0.1, 0.1]) + # What: act by calling wait_for_ready and capture result; why: the readiness waits for engine health not just a listening process test asserts the response, state, or failure produced by this call. + result = wait_for_ready(Manager(), Probe(), pid=44, port=1922, timeout_s=1, now=lambda: next(clock), sleep=lambda _: None) + # What: assert that result equals ready true health reachable true status; why: this assertion protects the readiness waits for engine health not just a listening process regression after the test's arranged inputs and exercised call. + assert result == {"ready": True, "health": {"reachable": True, "status": "ok", "model": "m"}} + + +# What: define the test_readiness_timeout_leaves_the_existing_engine_under_manager_control test around local fixtures; why: this test groups the arrange, act, and assertions that protect the readiness timeout leaves the existing engine under manager control outcome. +def test_readiness_timeout_leaves_the_existing_engine_under_manager_control(): + # What: define Manager as the owner of status; why: daemon callers use this class boundary so those methods share one manager state invariant. + class Manager: + # What: define the status test helper around captured fixture state; why: the readiness timeout leaves the existing engine under manager control scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def status(self): + # What: arrange the running field as true; why: Manager.status carries running into return {"running": True, "pid": 44}. + return {"running": True, "pid": 44} + + # What: define Probe as the owner of fresh_health; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: arrange the def fresh health self port test helper boundary; why: test catalog test readiness timeout leaves the existing engine under manager control uses this local double to isolate the behavior checked by its assertions. + def fresh_health(self, port): + # What: arrange the helper response as reachable True status loading; why: test catalog test readiness timeout leaves the existing engine under manager control feeds this result into the behavior whose outcome is asserted. + return {"reachable": True, "status": "loading"} + + # What: act by calling iter and capture clock; why: the readiness timeout leaves the existing engine under manager control test asserts the response, state, or failure produced by this call. + clock = iter([0.0, 0.0, 1.0]) + # What: act by calling wait_for_ready and capture result; why: the readiness timeout leaves the existing engine under manager control test asserts the response, state, or failure produced by this call. + result = wait_for_ready(Manager(), Probe(), pid=44, port=1922, timeout_s=1, now=lambda: next(clock), sleep=lambda _: None) + # What: assert the expected result == outcome; why: test catalog test readiness timeout leaves the existing engine under manager control protects its regression by requiring this observable result after the exercised behavior. + assert result == { + # What: arrange ready False for the scenario; why: test catalog test readiness timeout leaves the existing engine under manager control requires this concrete input or helper state before exercising the behavior under test. + "ready": False, + # What: arrange reason timeout for the scenario; why: test catalog test readiness timeout leaves the existing engine under manager control requires this concrete input or helper state before exercising the behavior under test. + "reason": "timeout", + # What: arrange health reachable True status loading for the scenario; why: test catalog test readiness timeout leaves the existing engine under manager control requires this concrete input or helper state before exercising the behavior under test. + "health": {"reachable": True, "status": "loading"}, + # What: arrange the grouped source fragment for the scenario; why: test catalog test readiness timeout leaves the existing engine under manager control requires this concrete input or helper state before exercising the behavior under test. + } + + +# What: define the test_profile_api_uses_validated_catalog_and_existing_switch_transaction test around tmp path; why: this test groups the arrange, act, and assertions that protect the profile api uses validated catalog and existing switch transaction outcome. +def test_profile_api_uses_validated_catalog_and_existing_switch_transaction(tmp_path): + # What: arrange path as tmp path and models and toml; why: the profile api uses validated catalog and existing switch transaction test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models coding nmodel models coding gguf nport fixture fragment; why: the profile api uses validated catalog and existing switch transaction scenario feeds this byte-preserved fragment through path.write_text("[models.coding]\nmodel = '/models/coding.gguf'\nport = before as. + path.write_text("[models.coding]\nmodel = '/models/coding.gguf'\nport = 1922\ncheck_endpoint = '/ready'\nargs = ['--max-seq-len-override', '32768']\n", encoding="utf-8") + + # What: define Manager as the owner of __init__ and status and start and switch and switch_for_readiness; why: daemon callers use this class boundary so those methods share one manager state invariant. + class Manager: + # What: define the __init__ test helper around captured fixture state; why: the profile api uses validated catalog and existing switch transaction scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def __init__(self): + # What: arrange calls as the fixture input; why: the profile api uses validated catalog and existing switch transaction test consumes this named precondition before exercising the behavior. + self.calls = [] + # What: arrange running as false; why: the profile api uses validated catalog and existing switch transaction test consumes this named precondition before exercising the behavior. + self.running = False + + # What: define the status test helper around captured fixture state; why: the profile api uses validated catalog and existing switch transaction scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def status(self): + # What: arrange the running field as running; why: Manager.status carries running into return {"running": self.running, "pid": 101 if self.running else None, ". + return {"running": self.running, "pid": 101 if self.running else None, "port": 1922 if self.running else None} + + # What: define the start test helper around model and port and args; why: the profile api uses validated catalog and existing switch transaction scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def start(self, model, port, args): + # What: arrange the exact self calls append start model port args fixture fragment; why: the profile api uses validated catalog and existing switch transaction scenario feeds this byte-preserved fragment through self.calls.append(("start", model, port, args)) before asserting its protocol or parser result. + self.calls.append(("start", model, port, args)) + # What: arrange self running True for the scenario; why: test catalog test profile api uses validated catalog and existing switch transaction requires this concrete input or helper. + self.running = True + # What: arrange the started field as true; why: Manager.start carries started into return {"started": True, "model": model, "port": port}. + return {"started": True, "model": model, "port": port} + + # What: define the switch test helper around model and port and args and force; why: the profile api uses validated catalog and existing switch transaction scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def switch(self, model, port, args, force): + # What: arrange the exact self calls append switch model port args force fixture fragment; why: the profile api uses validated catalog and existing switch transaction scenario feeds this byte-preserved fragment through self.calls.append(("switch", model, port, args, force)) before asserting its protocol or. + self.calls.append(("switch", model, port, args, force)) + # What: arrange self running True for the scenario; why: test catalog test profile api uses validated catalog and existing switch transaction requires this concrete input or helper. + self.running = True + # What: arrange the switched field as true; why: Manager.switch carries switched into return {"switched": True, "model": model, "port": port}. + return {"switched": True, "model": model, "port": port} + + # What: define the switch_for_readiness test helper around captured fixture state; why: the profile api uses validated catalog and existing switch transaction scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def switch_for_readiness(self, *args): + # What: return switch and args from the switch_for_readiness test helper; why: the profile api uses validated catalog and existing switch transaction scenario uses this helper result in its subsequent act or assertion. + return self.switch(*args), None + + # What: define Probe as the owner of fresh_readiness; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: define the fresh_readiness test helper around port and target; why: the profile api uses validated catalog and existing switch transaction scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def fresh_readiness(self, port, target): + # What: assert that target equals ready; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert target == "/ready" + # What: arrange the helper response as reachable True ready True port port; why: test catalog test profile api uses validated catalog and existing switch transaction feeds this result into the behavior whose outcome is asserted. + return {"reachable": True, "ready": True, "port": port} + + # What: act by calling Manager and capture manager; why: the profile api uses validated catalog and existing switch transaction test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_profile_api_uses_validated_catalog_and_existing_switch_transaction releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the profile api uses validated catalog and existing switch transaction test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_profile_api_uses_validated_catalog_and_existing_switch_transaction; why: test_profile_api_uses_validated_catalog_and_existing_switch_transaction consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=Probe(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to ModelCatalog.load; why: the profile api uses validated catalog and existing switch transaction scenario binds this lifecycle value to ModelCatalog.load's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=ModelCatalog.load(str(path)), token="secret", + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_profile_api_uses_validated_catalog_and_existing_switch_transaction groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the profile api uses validated catalog and existing switch transaction test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: assert that client get router profiles status code equals 401; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert client.get("/router/profiles").status_code == 401 + # What: act by calling client.get and capture listing; why: the profile api uses validated catalog and existing switch transaction test asserts the response, state, or failure produced by this call. + listing = client.get("/router/profiles", headers={"X-FT-Token": "secret"}) + # What: assert that listing status code equals 200; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert listing.status_code == 200 + # What: assert that listing json data 0 name equals coding; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert listing.json()["data"][0]["name"] == "coding" + # What: act by calling client.get and capture public listing; why: the profile api uses validated catalog and existing switch transaction test asserts the response, state, or failure produced by this call. + public_listing = client.get("/models") + # What: assert that public listing status code equals 200; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert public_listing.status_code == 200 + # What: assert that public listing json data 0 id equals coding; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert public_listing.json()["data"][0]["id"] == "coding" + # What: assert that models coding gguf is absent from public listing text; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert "/models/coding.gguf" not in public_listing.text + # What: act by calling client.post and capture started; why: the profile api uses validated catalog and existing switch transaction test asserts the response, state, or failure produced by this call. + started = client.post("/engine/start-profile", json={"name": "coding"}, headers={"X-FT-Token": "secret"}) + # What: assert that started status code equals 200; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert started.status_code == 200 + # What: assert that started json profile equals coding; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert started.json()["profile"] == "coding" + # What: assert that started json readiness ready is true; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert started.json()["readiness"]["ready"] is True + # What: act by calling client.post and capture switched; why: the profile api uses validated catalog and existing switch transaction test asserts the response, state, or failure produced by this call. + switched = client.post("/engine/switch-profile", json={"name": "coding", "force": True}, headers={"X-FT-Token": "secret"}) + # What: assert that switched status code equals 200; why: this assertion protects the profile api uses validated catalog and existing switch transaction regression after the test's arranged inputs and exercised call. + assert switched.status_code == 200 + # What: assert the expected manager calls == outcome; why: test catalog test profile api uses validated catalog and existing switch transaction protects its regression by requiring this observable result after the exercised behavior. + assert manager.calls == [ + # What: arrange start models coding gguf 1922 max seq len override 32768 for the scenario; why: test catalog test profile api uses validated catalog and existing switch transaction requires this concrete input or helper state before exercising the behavior under test. + ("start", "/models/coding.gguf", 1922, ["--max-seq-len-override", "32768"]), + # What: arrange switch models coding gguf 1922 max seq len override 32768 True for the scenario; why: test catalog test profile api uses validated catalog and existing switch transaction requires this concrete input or helper state before exercising the behavior under test. + ("switch", "/models/coding.gguf", 1922, ["--max-seq-len-override", "32768"], True), + # What: arrange the grouped source fragment for the scenario; why: test catalog test profile api uses validated catalog and existing switch transaction requires this concrete input or helper state before exercising the behavior under test. + ] + + +# What: define the test_client_shutdown_uses_the_daemon_shutdown_transaction test around monkeypatch and capsys; why: this test groups the arrange, act, and assertions that protect the client shutdown uses the daemon shutdown transaction outcome. +def test_client_shutdown_uses_the_daemon_shutdown_transaction(monkeypatch, capsys): + # What: arrange seen as the fixture input; why: the client shutdown uses the daemon shutdown transaction test consumes this named precondition before exercising the behavior. + seen = {} + + # What: define the request test helper around method and url and path; why: the client shutdown uses the daemon shutdown transaction scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request(method, url, path, **kwargs): + # What: arrange method to seen.update; why: the client shutdown uses the daemon shutdown transaction scenario binds this method value to seen.update's method input. + seen.update(method=method, url=url, path=path, **kwargs) + # What: arrange the stopping field as true; why: request carries stopping into return {"stopping": True}. + return {"stopping": True} + + # What: arrange the exact monkeypatch setattr daemon client request json request fixture fragment; why: the client shutdown uses the daemon shutdown transaction scenario feeds this byte-preserved fragment through monkeypatch.setattr(daemon_client, "_request_json", request) before asserting its protocol or parser r. + monkeypatch.setattr(daemon_client, "_request_json", request) + # What: assert that daemon client main shutdown url http daemon 1900 equals 0; why: this assertion protects the client shutdown uses the daemon shutdown transaction regression after the test's arranged inputs and exercised call. + assert daemon_client.main(["shutdown", "--url", "http://daemon:1900", "--force"]) == 0 + # What: assert the expected seen == outcome; why: test catalog test client shutdown uses the daemon shutdown transaction protects its regression by requiring this observable result after the exercised behavior. + assert seen == { + # What: arrange method POST url http daemon 1900 path shutdown for the scenario; why: test catalog test client shutdown uses the daemon shutdown transaction requires this concrete input or helper state before exercising the behavior under test. + "method": "POST", "url": "http://daemon:1900", "path": "/shutdown", + # What: arrange body force True token None timeout daemon client DEFAULT LIFECYCLE TIMEOUT for the scenario; why: test catalog test client shutdown uses the daemon shutdown transaction requires this concrete input or helper state before exercising the behavior under test. + "body": {"force": True}, "token": None, "timeout": daemon_client.DEFAULT_LIFECYCLE_TIMEOUT, + # What: arrange the grouped source fragment for the scenario; why: test catalog test client shutdown uses the daemon shutdown transaction requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that stopping true is present in capsys readouterr out; why: this assertion protects the client shutdown uses the daemon shutdown transaction regression after the test's arranged inputs and exercised call. + assert '"stopping": true' in capsys.readouterr().out + + +# What: define the test_client_models_uses_authenticated_profile_control_route test around monkeypatch and capsys; why: this test groups the arrange, act, and assertions that protect the client models uses authenticated profile control route outcome. +def test_client_models_uses_authenticated_profile_control_route(monkeypatch, capsys): + # What: arrange seen as the fixture input; why: the client models uses authenticated profile control route test consumes this named precondition before exercising the behavior. + seen = {} + + # What: define the request test helper around method and url and path; why: the client models uses authenticated profile control route scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request(method, url, path, **kwargs): + # What: arrange method to seen.update; why: the client models uses authenticated profile control route scenario binds this method value to seen.update's method input. + seen.update(method=method, url=url, path=path, **kwargs) + # What: arrange the data field as name and coding; why: request carries data into return {"data": [{"name": "coding"}]}. + return {"data": [{"name": "coding"}]} + + # What: arrange the exact monkeypatch setattr daemon client request json request fixture fragment; why: the client models uses authenticated profile control route scenario feeds this byte-preserved fragment through monkeypatch.setattr(daemon_client, "_request_json", request) before asserting its protocol or parser. + monkeypatch.setattr(daemon_client, "_request_json", request) + # What: assert the expected daemon client main outcome; why: test catalog test client models uses authenticated profile control route protects its regression by requiring this observable result after the exercised behavior. + assert daemon_client.main([ + # What: arrange models url http daemon 1900 token control secret for the scenario; why: test catalog test client models uses authenticated profile control route requires this concrete input or helper state before exercising the behavior under test. + "models", "--url", "http://daemon:1900", "--token", "control-secret" + # What: arrange == 0 for the scenario; why: test catalog test client models uses authenticated profile control route requires this concrete input or helper state before exercising the behavior under test. + ]) == 0 + # What: assert the expected seen == outcome; why: test catalog test client models uses authenticated profile control route protects its regression by requiring this observable result after the exercised behavior. + assert seen == { + # What: arrange method GET url http daemon 1900 path router profiles for the scenario; why: test catalog test client models uses authenticated profile control route requires this concrete input or helper state before exercising the behavior under test. + "method": "GET", "url": "http://daemon:1900", "path": "/router/profiles", + # What: arrange body None token control secret timeout daemon client DEFAULT TIMEOUT for the scenario; why: test catalog test client models uses authenticated profile control route requires this concrete input or helper state before exercising the behavior under test. + "body": None, "token": "control-secret", "timeout": daemon_client.DEFAULT_TIMEOUT, + # What: arrange the grouped source fragment for the scenario; why: test catalog test client models uses authenticated profile control route requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that name coding is present in capsys readouterr out; why: this assertion protects the client models uses authenticated profile control route regression after the test's arranged inputs and exercised call. + assert '"name": "coding"' in capsys.readouterr().out + + +# What: define the test_readiness_supports_a_validated_non_health_endpoint test around local fixtures; why: this test groups the arrange, act, and assertions that protect the readiness supports a validated non health endpoint outcome. +def test_readiness_supports_a_validated_non_health_endpoint(): + # What: define Manager as the owner of status; why: daemon callers use this class boundary so those methods share one manager state invariant. + class Manager: + # What: define the status test helper around captured fixture state; why: the readiness supports a validated non health endpoint scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def status(self): + # What: arrange the running field as true; why: Manager.status carries running into return {"running": True, "pid": 44}. + return {"running": True, "pid": 44} + + # What: define Probe as the owner of fresh_readiness; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: define the fresh_readiness test helper around port and path; why: the readiness supports a validated non health endpoint scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def fresh_readiness(self, port, path): + # What: assert that port path equals 1922 ready; why: this assertion protects the readiness supports a validated non health endpoint regression after the test's arranged inputs and exercised call. + assert (port, path) == (1922, "/ready") + # What: arrange the reachable field as true; why: Probe.fresh_readiness carries reachable into return {"reachable": True, "ready": True}. + return {"reachable": True, "ready": True} + + # What: act by calling wait_for_ready and capture result; why: the readiness supports a validated non health endpoint test asserts the response, state, or failure produced by this call. + result = wait_for_ready( + # What: arrange pid to Manager; why: the readiness supports a validated non health endpoint scenario binds this 44 value to Manager's pid input. + Manager(), Probe(), pid=44, port=1922, timeout_s=1, path="/ready" + # What: arrange the wait_for_ready call with pid and port and timeout s and path; why: test_readiness_supports_a_validated_non_health_endpoint groups the supplied clauses as one wait_for_ready call before its value is consumed. + ) + # What: assert that result equals ready true health reachable true ready; why: this assertion protects the readiness supports a validated non health endpoint regression after the test's arranged inputs and exercised call. + assert result == {"ready": True, "health": {"reachable": True, "ready": True}} + + +# What: define the test_router_policy_is_strict_and_public_model_fields_are_safe test around tmp path; why: this test groups the arrange, act, and assertions that protect the router policy is strict and public model fields are safe outcome. +def test_router_policy_is_strict_and_public_model_fields_are_safe(tmp_path): + # What: arrange path as tmp path and models and toml; why: the router policy is strict and public model fields are safe test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact router fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact api keys one two fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact default ttl s fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange path write text for the scenario; why: test catalog test router policy is strict and public model fields are safe requires this concrete input or helper state before exercising the behavior under test. + # What: arrange the exact upstream timeout s fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact scheduler fifo fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact global concurrency limit fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact send loading state true fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact router groups interactive fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact members coding chat fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact swap true fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact exclusive true fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact models coding fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact model coding gguf fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact ttl s fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange path write text for the scenario; why: test catalog test router policy is strict and public model fields are safe requires this concrete input or helper state before exercising the behavior under test. + # What: arrange path write text for the scenario; why: test catalog test router policy is strict and public model fields are safe requires this concrete input or helper state before exercising the behavior under test. + # What: arrange the exact concurrency limit fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact send loading state false fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact group interactive fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact models chat fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact model chat gguf fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange path write text for the scenario; why: test catalog test router policy is strict and public model fields are safe requires this concrete input or helper state before exercising the behavior under test. + # What: arrange the exact encoding utf 8 fixture fragment; why: the router policy is strict and public model fields are safe scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + path.write_text(""" +[router] +api_keys = ["one", "two"] +default_ttl_s = 300 +unload_timeout_s = 45 +upstream_timeout_s = 42 +scheduler = "fifo" +global_concurrency_limit = 4 +send_loading_state = true + +[router.groups.interactive] +members = ["coding", "chat"] +swap = true +exclusive = true + +[models.coding] +model = "coding.gguf" +ttl_s = 0 +unload_timeout_s = 60 +priority = 10 +concurrency_limit = 2 +send_loading_state = false +group = "interactive" + +[models.chat] +model = "chat.gguf" +priority = -5 +""", encoding="utf-8") + # What: act by calling ModelCatalog.load and capture catalog; why: the router policy is strict and public model fields are safe test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog.load(str(path)) + # What: assert that catalog settings api keys equals one two; why: this assertion protects the router policy is strict and public model fields are safe regression after the test's arranged inputs and exercised call. + assert catalog.settings.api_keys == ("one", "two") + # What: assert that catalog settings default ttl s equals 300; why: this assertion protects the router policy is strict and public model fields are safe regression after the test's arranged inputs and exercised call. + assert catalog.settings.default_ttl_s == 300 + # What: assert that catalog settings upstream timeout s equals 42; why: this assertion protects the router policy is strict and public model fields are safe regression after the test's arranged inputs and exercised call. + assert catalog.settings.upstream_timeout_s == 42 + # What: assert that catalog settings global concurrency limit equals 4; why: this assertion protects the router policy is strict and public model fields are safe regression after the test's arranged inputs and exercised call. + assert catalog.settings.global_concurrency_limit == 4 + # What: assert that catalog settings send loading state is true; why: this assertion protects the router policy is strict and public model fields are safe regression after the test's arranged inputs and exercised call. + assert catalog.settings.send_loading_state is True + # What: assert that catalog settings groups 0 members equals coding chat; why: this assertion protects the router policy is strict and public model fields are safe regression after the test's arranged inputs and exercised call. + assert catalog.settings.groups[0].members == ("coding", "chat") + # What: act by calling catalog.public and capture public; why: the router policy is strict and public model fields are safe test asserts the response, state, or failure produced by this call. + public = {item["name"]: item for item in catalog.public()} + # What: assert the expected public coding == outcome; why: test catalog test router policy is strict and public model fields are safe protects its regression by requiring this observable result after the exercised behavior. + assert public["coding"] == { + # What: arrange name coding model coding gguf args readyTimeoutS 120.0 for the scenario; why: test catalog test router policy is strict and public model fields are safe requires this concrete input or helper state before exercising the behavior under test. + "name": "coding", "model": "coding.gguf", "args": [], "readyTimeoutS": 120.0, + # What: arrange ttlS 0.0 unloadTimeoutS 60.0 priority 10 for the scenario; why: test catalog test router policy is strict and public model fields are safe requires this concrete input or helper state before exercising the behavior under test. + "ttlS": 0.0, "unloadTimeoutS": 60.0, "priority": 10, + # What: arrange group interactive concurrencyLimit 2 sendLoadingState False for the scenario; why: test catalog test router policy is strict and public model fields are safe requires this concrete input or helper state before exercising the behavior under test. + "group": "interactive", "concurrencyLimit": 2, "sendLoadingState": False, + # What: arrange the grouped source fragment for the scenario; why: test catalog test router policy is strict and public model fields are safe requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that api keys is absent from str public; why: this assertion protects the router policy is strict and public model fields are safe regression after the test's arranged inputs and exercised call. + assert "api_keys" not in str(public) + + +# What: parameterize test_router_policy_rejects_ambiguous_or_unsafe_configuration with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test router policy rejects ambiguous or unsafe configuration. +@pytest.mark.parametrize("router, message", [ + # What: arrange the router nscheduler lifo scheduler portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router]\nscheduler = 'lifo'", "scheduler"), + # What: arrange the router nupstream timeout s upstream timeout s portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router]\nupstream_timeout_s = 0", "upstream_timeout_s"), + # What: arrange the drop fields model drop fields portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("drop_fields = ['model']", "drop_fields"), + # What: arrange the router napi keys same same duplicates portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router]\napi_keys = ['same', 'same']", "duplicates"), + # What: arrange the router ninclude aliases in list yes include aliases in list portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router]\ninclude_aliases_in_list = 'yes'", "include_aliases_in_list"), + # What: arrange the router nglobal concurrency limit global concurrency limit portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router]\nglobal_concurrency_limit = -1", "global_concurrency_limit"), + # What: arrange the router nsend loading state yes send loading state portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router]\nsend_loading_state = 'yes'", "send_loading_state"), + # What: arrange the concurrency limit true concurrency limit portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("concurrency_limit = true", "concurrency_limit"), + # What: arrange the send loading state send loading state portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("send_loading_state = 1", "send_loading_state"), + # What: arrange the router groups bad name nmembers a router portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ('[router.groups."bad/name"]\nmembers = ["a"]', "router group names"), + # What: arrange the router groups g nmembers missing configured models portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router.groups.g]\nmembers = ['missing']", "configured models"), + # What: arrange the router groups g nmembers a npersistent true persistent portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router.groups.g]\nmembers = ['a']\npersistent = true", "persistent"), + # What: arrange the router groups g nmembers a nexclusive false exclusive portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router.groups.g]\nmembers = ['a']\nexclusive = false", "exclusive"), + # What: arrange the router groups g nmembers a nswap false multi resident portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[router.groups.g]\nmembers = ['a']\nswap = false", "multi-resident"), + # What: arrange the models b nmodel b gguf n router groups g nmembers portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("[models.b]\nmodel = 'b.gguf'\n[router.groups.g]\nmembers = ['a', 'b']\npersistent = true\nswap = false", "exactly one"), + # What: arrange the group other must match portion of the enclosing predicate; why: this clause remains in the router policy rejects ambiguous or unsafe configuration scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("group = 'other'", "must match"), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_router_policy_rejects_ambiguous_or_unsafe_configuration groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_router_policy_rejects_ambiguous_or_unsafe_configuration test around tmp path and router and message; why: this test groups the arrange, act, and assertions that protect the router policy rejects ambiguous or unsafe configuration outcome. +def test_router_policy_rejects_ambiguous_or_unsafe_configuration(tmp_path, router, message): + # What: arrange path as tmp path and models and toml; why: the router policy rejects ambiguous or unsafe configuration test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models a nmodel a gguf n router fixture fragment; why: the router policy rejects ambiguous or unsafe configuration scenario feeds this byte-preserved fragment through path.write_text("[models.a]\nmodel = 'a.gguf'\n" + router, encoding="utf before asserting its protocol or. + path.write_text("[models.a]\nmodel = 'a.gguf'\n" + router, encoding="utf-8") + # What: assert the pytest.raises failure context; why: the router policy rejects ambiguous or unsafe configuration scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match=message): + # What: act by calling ModelCatalog.load with str and path; why: the router policy rejects ambiguous or unsafe configuration scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: define the test_router_policy_accepts_a_singleton_persistent_protected_slot test around tmp path; why: this test groups the arrange, act, and assertions that protect the router policy accepts a singleton persistent protected slot outcome. +def test_router_policy_accepts_a_singleton_persistent_protected_slot(tmp_path): + # What: arrange path as tmp path and models and toml; why: the router policy accepts a singleton persistent protected slot test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact router groups resident fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact members a fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact swap false fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact exclusive true fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact persistent true fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact models a fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact model a gguf fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact group resident fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + # What: arrange the exact encoding utf 8 fixture fragment; why: the router policy accepts a singleton persistent protected slot scenario feeds this byte-preserved fragment through path.write_text(""" before asserting its protocol or parser result. + path.write_text(""" +[router.groups.resident] +members = ["a"] +swap = false +exclusive = true +persistent = true + +[models.a] +model = "a.gguf" +group = "resident" +""", encoding="utf-8") + # What: act by calling ModelCatalog.load and capture group; why: the router policy accepts a singleton persistent protected slot test asserts the response, state, or failure produced by this call. + group = ModelCatalog.load(str(path)).settings.groups[0] + # What: assert that group members group swap group exclusive group persistent equals a false true true; why: this assertion protects the router policy accepts a singleton persistent protected slot regression after the test's arranged inputs and exercised call. + assert (group.members, group.swap, group.exclusive, group.persistent) == (("a",), False, True, True) diff --git a/tests/daemon/test_daemon_import_safety.py b/tests/daemon/test_daemon_import_safety.py index 277103431..8a0c6f6bc 100644 --- a/tests/daemon/test_daemon_import_safety.py +++ b/tests/daemon/test_daemon_import_safety.py @@ -20,6 +20,10 @@ "freetoken.daemon", "freetoken.daemon.version", "freetoken.daemon.accounting", + # What: arrange the freetoken daemon catalog portion of daemon modules; why: the daemon import safety scenario uses this clause to evaluate daemon modules as one grouped value. + "freetoken.daemon.catalog", + # What: arrange the freetoken daemon readiness portion of daemon modules; why: the daemon import safety scenario uses this clause to evaluate daemon modules as one grouped value. + "freetoken.daemon.readiness", "freetoken.daemon.logfmt", "freetoken.daemon.logring", "freetoken.daemon.osproc", diff --git a/tests/daemon/test_daemon_serve_manager.py b/tests/daemon/test_daemon_serve_manager.py index fd9956ebb..3cbb47869 100644 --- a/tests/daemon/test_daemon_serve_manager.py +++ b/tests/daemon/test_daemon_serve_manager.py @@ -13,7 +13,8 @@ ) from freetoken.daemon.logring import LogRing from freetoken.daemon.pidfile import ServeState, ServeStateStore -from freetoken.daemon.serve_manager import Conflict, ExitInfo, ServeManager +# What: arrange from freetoken daemon serve manager import Conflict ExitInfo ServeManager SwitchLaunchError for the scenario; why: test daemon serve manager requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.serve_manager import Conflict, ExitInfo, ServeManager, SwitchLaunchError # --------------------------------------------------------------------------- test doubles @@ -117,6 +118,246 @@ def make_manager( # --------------------------------------------------------------------------- start / idempotency +# What: parameterize test_switch_spawn_failure_restores_exact_previous_launch with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test switch spawn failure restores exact previous launch. +@pytest.mark.parametrize("recovery_fails", [False, True]) +# What: define the test_switch_spawn_failure_restores_exact_previous_launch test around tmp path and recovery fails; why: this test groups the arrange, act, and assertions that protect the switch spawn failure restores exact previous launch outcome. +def test_switch_spawn_failure_restores_exact_previous_launch(tmp_path, recovery_fails): + # What: act by calling Spawner and capture sp; why: the switch spawn failure restores exact previous launch test asserts the response, state, or failure produced by this call. + sp = Spawner() + # What: arrange calls as the fixture input; why: the switch spawn failure restores exact previous launch test consumes this named precondition before exercising the behavior. + calls = [] + + # What: define the spawn test helper around model and port and args; why: the switch spawn failure restores exact previous launch scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def spawn(model, port, args): + # What: act by calling calls.append with model and port and list and args; why: the switch spawn failure restores exact previous launch scenario observes the calls.append return value during if model bad or recovery fails and. + calls.append((model, port, list(args))) + # What: act on model and recovery fails and len and calls before oserror; why: the switch spawn failure restores exact previous launch scenario admits oserror only for this predicate and excludes the opposite state. + if model == "bad" or (recovery_fails and len(calls) == 3): + # What: raise OSError for the caller; why: spawn stops this rejected path before it can mutate state, dispatch work, or report success. + raise OSError("injected launch failure") + # What: return sp and model and port and args from the spawn test helper; why: the switch spawn failure restores exact previous launch scenario uses this helper result in its subsequent act or assertion. + return sp(model, port, args) + + # What: act by calling make_manager and capture mgr and store and; why: the switch spawn failure restores exact previous launch test asserts the response, state, or failure produced by this call. + mgr, store, _ = make_manager( + # What: arrange the pid input for test_switch_spawn_failure_restores_exact_previous_launch; why: test_switch_spawn_failure_restores_exact_previous_launch consumes pid during signature binding, so callers must bind it with the other signature inputs. + tmp_path, spawn, signal_fn=lambda pid, sig: sp.by_pid(pid).die() + # What: arrange the make_manager call with signal fn; why: test_switch_spawn_failure_restores_exact_previous_launch groups the supplied clauses as one make_manager call before its value is consumed. + ) + # What: arrange the exact mgr start previous example fixture fragment; why: the switch spawn failure restores exact previous launch scenario feeds this byte-preserved fragment through mgr.start("previous", 1922, ["--example"]) before asserting its protocol or parser result. + mgr.start("previous", 1922, ["--example"]) + # What: assert the pytest.raises failure context; why: the switch spawn failure restores exact previous launch scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(SwitchLaunchError) as failed: + # What: arrange the exact mgr switch bad fixture fragment; why: the switch spawn failure restores exact previous launch scenario feeds this byte-preserved fragment through mgr.switch("bad", 1923, []) before asserting its protocol or parser result. + mgr.switch("bad", 1923, []) + # What: assert the expected calls == previous 1922 example outcome; why: test daemon serve manager test switch spawn failure restores exact previous launch protects its regression by requiring this observable result after the exercised behavior. + assert calls == [("previous", 1922, ["--example"]), + # What: arrange bad 1923 previous 1922 example for the scenario; why: test daemon serve manager test switch spawn failure restores exact previous launch requires this concrete input or helper state before exercising the behavior under test. + ("bad", 1923, []), ("previous", 1922, ["--example"])] + # What: assert that failed value rollback attempted is true; why: this assertion protects the switch spawn failure restores exact previous launch regression after the test's arranged inputs and exercised call. + assert failed.value.rollback["attempted"] is True + # What: assert that failed value rollback launched is not recovery fails; why: this assertion protects the switch spawn failure restores exact previous launch regression after the test's arranged inputs and exercised call. + assert failed.value.rollback["launched"] is (not recovery_fails) + # What: assert that failed value accounting is not group delimiter; why: this assertion protects the switch spawn failure restores exact previous launch regression after the test's arranged inputs and exercised call. + assert failed.value.accounting is not None + # What: assert that mgr status running is not recovery fails; why: this assertion protects the switch spawn failure restores exact previous launch regression after the test's arranged inputs and exercised call. + assert mgr.status()["running"] is (not recovery_fails) + # What: act on recovery fails before model and load and store; why: the switch spawn failure restores exact previous launch scenario admits model and load and store only for this predicate and excludes the opposite state. + if not recovery_fails: + # What: assert that store load model equals previous; why: this assertion protects the switch spawn failure restores exact previous launch regression after the test's arranged inputs and exercised call. + assert store.load().model == "previous" + # What: act by calling mgr.stop with the declared inputs; why: the switch spawn failure restores exact previous launch scenario observes the mgr.stop return value during the enclosing return. + mgr.stop() + + +# What: define the test_switch_spawn_failure_without_previous_does_not_retry test around tmp path; why: this test groups the arrange, act, and assertions that protect the switch spawn failure without previous does not retry outcome. +def test_switch_spawn_failure_without_previous_does_not_retry(tmp_path): + # What: define the spawn test helper around captured fixture state; why: the switch spawn failure without previous does not retry scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def spawn(*args): + # What: raise OSError for the caller; why: spawn stops this rejected path before it can mutate state, dispatch work, or report success. + raise OSError("injected launch failure") + + # What: act by calling make_manager and capture mgr and and; why: the switch spawn failure without previous does not retry test asserts the response, state, or failure produced by this call. + mgr, _, _ = make_manager(tmp_path, spawn) + # What: assert the pytest.raises failure context; why: the switch spawn failure without previous does not retry scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(SwitchLaunchError) as failed: + # What: arrange the exact mgr switch bad fixture fragment; why: the switch spawn failure without previous does not retry scenario feeds this byte-preserved fragment through mgr.switch("bad", 1922) before asserting its protocol or parser result. + mgr.switch("bad", 1922) + # What: assert that failed value rollback equals attempted false launched false; why: this assertion protects the switch spawn failure without previous does not retry regression after the test's arranged inputs and exercised call. + assert failed.value.rollback == {"attempted": False, "launched": False} + # What: assert that mgr status running is false; why: this assertion protects the switch spawn failure without previous does not retry regression after the test's arranged inputs and exercised call. + assert not mgr.status()["running"] + + +# What: parameterize test_readiness_recovery_never_overrides_newer_lifecycle with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test readiness recovery never overrides newer lifecycle. +@pytest.mark.parametrize("newer_action", [None, "stop", "switch", "shutdown", "start"]) +# What: define the test_readiness_recovery_never_overrides_newer_lifecycle test around tmp path and newer action; why: this test groups the arrange, act, and assertions that protect the readiness recovery never overrides newer lifecycle outcome. +def test_readiness_recovery_never_overrides_newer_lifecycle(tmp_path, newer_action): + # What: act by calling Spawner and capture sp; why: the readiness recovery never overrides newer lifecycle test asserts the response, state, or failure produced by this call. + sp = Spawner() + # What: act by calling make_manager and capture mgr and and; why: the readiness recovery never overrides newer lifecycle test asserts the response, state, or failure produced by this call. + mgr, _, _ = make_manager(tmp_path, sp, + # What: arrange the pid input for test_readiness_recovery_never_overrides_newer_lifecycle; why: test_readiness_recovery_never_overrides_newer_lifecycle consumes pid during signature binding, so callers must bind it with the other signature inputs. + signal_fn=lambda pid, sig: sp.by_pid(pid).die()) + # What: arrange the exact mgr start previous original fixture fragment; why: the readiness recovery never overrides newer lifecycle scenario feeds this byte-preserved fragment through mgr.start("previous", 1922, ["--original"]) before asserting its protocol or parser result. + mgr.start("previous", 1922, ["--original"]) + # What: act by calling mgr.switch_for_readiness and capture and ticket; why: the readiness recovery never overrides newer lifecycle test asserts the response, state, or failure produced by this call. + _, ticket = mgr.switch_for_readiness("replacement", 1923) + # What: act on newer action before stop and mgr; why: the readiness recovery never overrides newer lifecycle scenario admits stop and mgr only for this predicate and excludes the opposite state. + if newer_action == "stop": + # What: act by calling mgr.stop with the declared inputs; why: the readiness recovery never overrides newer lifecycle scenario observes the mgr.stop return value during elif newer action shutdown. + mgr.stop() + # What: act on newer action before shutdown and mgr; why: the readiness recovery never overrides newer lifecycle scenario admits shutdown and mgr only for this predicate and excludes the opposite state. + elif newer_action == "shutdown": + # What: act by calling mgr.shutdown with the declared inputs; why: the readiness recovery never overrides newer lifecycle scenario observes the mgr.shutdown return value during elif newer action switch. + mgr.shutdown() + # What: act on newer action before switch and mgr; why: the readiness recovery never overrides newer lifecycle scenario admits switch and mgr only for this predicate and excludes the opposite state. + elif newer_action == "switch": + # What: arrange the exact mgr switch newer fixture fragment; why: the readiness recovery never overrides newer lifecycle scenario feeds this byte-preserved fragment through mgr.switch("newer", 1924) before asserting its protocol or parser result. + mgr.switch("newer", 1924) + # What: act on newer action before start and mgr; why: the readiness recovery never overrides newer lifecycle scenario admits start and mgr only for this predicate and excludes the opposite state. + elif newer_action == "start": + # What: arrange the exact mgr start replacement even explicit idempotent intent fixture fragment; why: the readiness recovery never overrides newer lifecycle scenario feeds this byte-preserved fragment through mgr.start("replacement", 1923) # even explicit idempotent intent wins before asserting its protocol o. + mgr.start("replacement", 1923) # even explicit idempotent intent wins + # What: act by calling mgr.recover_switch and capture result; why: the readiness recovery never overrides newer lifecycle test asserts the response, state, or failure produced by this call. + result = mgr.recover_switch(ticket) + # What: assert that result launched is newer action is; why: this assertion protects the readiness recovery never overrides newer lifecycle regression after the test's arranged inputs and exercised call. + assert result["launched"] is (newer_action is None) + # What: act on newer action before result; why: the readiness recovery never overrides newer lifecycle scenario admits result only for this predicate and excludes the opposite state. + if newer_action is not None: + # What: assert that result reason equals superseded; why: this assertion protects the readiness recovery never overrides newer lifecycle regression after the test's arranged inputs and exercised call. + assert result["reason"] == "superseded" + # What: select the remaining branch that performs assert sp calls previous original; why: test_readiness_recovery_never_overrides_newer_lifecycle covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: assert that sp calls 1 equals previous 1922 original; why: this assertion protects the readiness recovery never overrides newer lifecycle regression after the test's arranged inputs and exercised call. + assert sp.calls[-1] == ("previous", 1922, ["--original"]) + # Recovery is single-use even when a delayed caller repeats the request. + # What: assert that mgr recover switch ticket reason equals superseded; why: this assertion protects the readiness recovery never overrides newer lifecycle regression after the test's arranged inputs and exercised call. + assert mgr.recover_switch(ticket)["reason"] == "superseded" + # What: act by calling mgr.stop with the declared inputs; why: the readiness recovery never overrides newer lifecycle scenario observes the mgr.stop return value during the enclosing return. + mgr.stop() + + +# What: define the test_readiness_recovery_preserves_engine_when_accounting_fails test around tmp path; why: this test groups the arrange, act, and assertions that protect the readiness recovery preserves engine when accounting fails outcome. +def test_readiness_recovery_preserves_engine_when_accounting_fails(tmp_path): + # What: act by calling Spawner and capture sp; why: the readiness recovery preserves engine when accounting fails test asserts the response, state, or failure produced by this call. + sp = Spawner() + # What: act by calling make_manager and capture mgr and and; why: the readiness recovery preserves engine when accounting fails test asserts the response, state, or failure produced by this call. + mgr, _, _ = make_manager(tmp_path, sp, + # What: arrange the pid input for test_readiness_recovery_preserves_engine_when_accounting_fails; why: test_readiness_recovery_preserves_engine_when_accounting_fails consumes pid during signature binding, so callers must bind it with the other signature inputs. + signal_fn=lambda pid, sig: sp.by_pid(pid).die()) + # What: arrange the exact mgr start previous fixture fragment; why: the readiness recovery preserves engine when accounting fails scenario feeds this byte-preserved fragment through mgr.start("previous", 1922) before asserting its protocol or parser result. + mgr.start("previous", 1922) + # What: act by calling mgr.switch_for_readiness and capture and ticket; why: the readiness recovery preserves engine when accounting fails test asserts the response, state, or failure produced by this call. + _, ticket = mgr.switch_for_readiness("replacement", 1923) + + # What: define the unavailable test helper around port; why: the readiness recovery preserves engine when accounting fails scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def unavailable(port): + # What: raise AccountingPrepareError for the caller; why: unavailable stops this rejected path before it can mutate state, dispatch work, or report success. + raise AccountingPrepareError("injected unavailable accounting") + + # What: arrange prepare stop as unavailable; why: the readiness recovery preserves engine when accounting fails test consumes this named precondition before exercising the behavior. + mgr._prepare_stop = unavailable + # What: act by calling mgr.recover_switch and capture result; why: the readiness recovery preserves engine when accounting fails test asserts the response, state, or failure produced by this call. + result = mgr.recover_switch(ticket) + # What: assert that result attempted and not result launched; why: this assertion protects the readiness recovery preserves engine when accounting fails regression after the test's arranged inputs and exercised call. + assert result["attempted"] and not result["launched"] + # What: assert that result engine preserved; why: this assertion protects the readiness recovery preserves engine when accounting fails regression after the test's arranged inputs and exercised call. + assert result["enginePreserved"] + # What: assert that mgr status model equals replacement; why: this assertion protects the readiness recovery preserves engine when accounting fails regression after the test's arranged inputs and exercised call. + assert mgr.status()["model"] == "replacement" + # What: arrange prepare stop as the fixture input; why: the readiness recovery preserves engine when accounting fails test consumes this named precondition before exercising the behavior. + mgr._prepare_stop = None + # What: act by calling mgr.stop with the declared inputs; why: the readiness recovery preserves engine when accounting fails scenario observes the mgr.stop return value during the enclosing return. + mgr.stop() + + +# What: define the test_readiness_recovery_can_restore_after_replacement_exits test around tmp path; why: this test groups the arrange, act, and assertions that protect the readiness recovery can restore after replacement exits outcome. +def test_readiness_recovery_can_restore_after_replacement_exits(tmp_path): + # What: act by calling Spawner and capture sp; why: the readiness recovery can restore after replacement exits test asserts the response, state, or failure produced by this call. + sp = Spawner() + # What: act by calling make_manager and capture mgr and and; why: the readiness recovery can restore after replacement exits test asserts the response, state, or failure produced by this call. + mgr, _, _ = make_manager(tmp_path, sp, + # What: arrange the pid input for test_readiness_recovery_can_restore_after_replacement_exits; why: test_readiness_recovery_can_restore_after_replacement_exits consumes pid during signature binding, so callers must bind it with the other signature inputs. + signal_fn=lambda pid, sig: sp.by_pid(pid).die()) + # What: arrange the exact mgr start previous fixture fragment; why: the readiness recovery can restore after replacement exits scenario feeds this byte-preserved fragment through mgr.start("previous", 1922) before asserting its protocol or parser result. + mgr.start("previous", 1922) + # What: act by calling mgr.switch_for_readiness and capture replacement and ticket; why: the readiness recovery can restore after replacement exits test asserts the response, state, or failure produced by this call. + replacement, ticket = mgr.switch_for_readiness("replacement", 1923) + # What: act by calling sp.by_pid and capture child; why: the readiness recovery can restore after replacement exits test asserts the response, state, or failure produced by this call. + child = sp.by_pid(replacement["pid"]) + # What: act by calling child.die with 1; why: the readiness recovery can restore after replacement exits scenario observes the child.die return value during assert child reaped wait. + child.die(1) + # What: assert that child reaped wait 3; why: this assertion protects the readiness recovery can restore after replacement exits regression after the test's arranged inputs and exercised call. + assert child.reaped.wait(3) + # What: assert that mgr recover switch ticket launched; why: this assertion protects the readiness recovery can restore after replacement exits regression after the test's arranged inputs and exercised call. + assert mgr.recover_switch(ticket)["launched"] + # What: assert that mgr status model equals previous; why: this assertion protects the readiness recovery can restore after replacement exits regression after the test's arranged inputs and exercised call. + assert mgr.status()["model"] == "previous" + # What: act by calling mgr.stop with the declared inputs; why: the readiness recovery can restore after replacement exits scenario observes the mgr.stop return value during the enclosing return. + mgr.stop() + + +# What: define the test_recovery_waits_for_old_pidfile_cleanup test around tmp path; why: this test groups the arrange, act, and assertions that protect the recovery waits for old pidfile cleanup outcome. +def test_recovery_waits_for_old_pidfile_cleanup(tmp_path): + # What: act by calling Spawner and capture sp; why: the recovery waits for old pidfile cleanup test asserts the response, state, or failure produced by this call. + sp = Spawner() + # What: act by calling make_manager and capture mgr and store and; why: the recovery waits for old pidfile cleanup test asserts the response, state, or failure produced by this call. + mgr, store, _ = make_manager(tmp_path, sp, + # What: arrange the pid input for test_recovery_waits_for_old_pidfile_cleanup; why: test_recovery_waits_for_old_pidfile_cleanup consumes pid during signature binding, so callers must bind it with the other signature inputs. + signal_fn=lambda pid, sig: sp.by_pid(pid).die()) + # What: arrange the exact mgr start previous fixture fragment; why: the recovery waits for old pidfile cleanup scenario feeds this byte-preserved fragment through mgr.start("previous", 1922) before asserting its protocol or parser result. + mgr.start("previous", 1922) + # What: act by calling mgr.switch_for_readiness and capture replacement and ticket; why: the recovery waits for old pidfile cleanup test asserts the response, state, or failure produced by this call. + replacement, ticket = mgr.switch_for_readiness("replacement", 1923) + # What: act by calling threading.Event and capture entered and release; why: the recovery waits for old pidfile cleanup test asserts the response, state, or failure produced by this call. + entered, release = threading.Event(), threading.Event() + # What: arrange clear as clear and store; why: the recovery waits for old pidfile cleanup test consumes this named precondition before exercising the behavior. + clear = store.clear + + # What: define the delayed_clear test helper around captured fixture state; why: the recovery waits for old pidfile cleanup scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def delayed_clear(): + # What: act by calling entered.set with the declared inputs; why: the recovery waits for old pidfile cleanup scenario observes the entered.set return value during assert release wait. + entered.set() + # What: assert that release wait 5; why: this assertion protects the recovery waits for old pidfile cleanup regression after the test's arranged inputs and exercised call. + assert release.wait(5) + # What: act by calling clear with the declared inputs; why: the recovery waits for old pidfile cleanup scenario observes the clear return value during the enclosing return. + clear() + + # What: arrange clear as delayed clear; why: the recovery waits for old pidfile cleanup test consumes this named precondition before exercising the behavior. + store.clear = delayed_clear + # What: arrange the exact sp by pid replacement pid die fixture fragment; why: the recovery waits for old pidfile cleanup scenario feeds this byte-preserved fragment through sp.by_pid(replacement["pid"]).die(1) before asserting its protocol or parser result. + sp.by_pid(replacement["pid"]).die(1) + # What: assert that entered wait 3; why: this assertion protects the recovery waits for old pidfile cleanup regression after the test's arranged inputs and exercised call. + assert entered.wait(3) + # What: arrange result as the fixture input; why: the recovery waits for old pidfile cleanup test consumes this named precondition before exercising the behavior. + result = {} + # What: act by calling threading.Thread and capture recovery; why: the recovery waits for old pidfile cleanup test asserts the response, state, or failure produced by this call. + recovery = threading.Thread(target=lambda: result.update(mgr.recover_switch(ticket))) + # What: act by calling recovery.start with the declared inputs; why: the recovery waits for old pidfile cleanup scenario observes the recovery.start return value during try. + recovery.start() + # What: establish the handler boundary for the protected operation; why: test_recovery_waits_for_old_pidfile_cleanup routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: assert that len sp calls equals 2; why: this assertion protects the recovery waits for old pidfile cleanup regression after the test's arranged inputs and exercised call. + assert len(sp.calls) == 2 + # What: run release set on every exit path; why: test_recovery_waits_for_old_pidfile_cleanup performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act by calling release.set with the declared inputs; why: the recovery waits for old pidfile cleanup scenario observes the release.set return value during recovery join. + release.set() + # What: act by calling recovery.join with 3; why: the recovery waits for old pidfile cleanup scenario observes the recovery.join return value during assert not recovery is alive. + recovery.join(3) + # What: assert that recovery is alive is false; why: this assertion protects the recovery waits for old pidfile cleanup regression after the test's arranged inputs and exercised call. + assert not recovery.is_alive() + # What: assert that result launched; why: this assertion protects the recovery waits for old pidfile cleanup regression after the test's arranged inputs and exercised call. + assert result["launched"] + # What: assert that store load model equals previous; why: this assertion protects the recovery waits for old pidfile cleanup regression after the test's arranged inputs and exercised call. + assert store.load().model == "previous" + # What: arrange clear as clear; why: the recovery waits for old pidfile cleanup test consumes this named precondition before exercising the behavior. + store.clear = clear + # What: act by calling mgr.stop with the declared inputs; why: the recovery waits for old pidfile cleanup scenario observes the mgr.stop return value during the enclosing return. + mgr.stop() + + def test_start_reports_running(tmp_path): sp = Spawner() mgr, store, _ = make_manager(tmp_path, sp) diff --git a/tests/daemon/test_metrics.py b/tests/daemon/test_metrics.py new file mode 100644 index 000000000..430235054 --- /dev/null +++ b/tests/daemon/test_metrics.py @@ -0,0 +1,147 @@ +# What: import json for test amd smi process vram parses multi gpu json using json; why: test_amd_smi_process_vram_parses_multi_gpu_json uses json dumps, making that imported dependency available to its named operation. +import json +# What: import simple namespace for test amd smi process vram parses multi gpu json using types and simple namespace; why: test_amd_smi_process_vram_parses_multi_gpu_json uses simple namespace, making that imported dependency available to its named operation. +from types import SimpleNamespace + +# What: import metrics for test vram measurement falls through to amd smi using freetoken and daemon and metrics; why: test_vram_measurement_falls_through_to_amd_smi uses the metrics annotation in test vram measurement falls through to amd smi, making that imported dependency available to its named operation. +from freetoken.daemon import metrics + + +# What: define the test_amd_smi_process_vram_parses_multi_gpu_json test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the amd smi process vram parses multi gpu json outcome. +def test_amd_smi_process_vram_parses_multi_gpu_json(monkeypatch): + # What: arrange doc as gpu and process list and 0 and gpu and process list; why: the amd smi process vram parses multi gpu json test consumes this named precondition before exercising the behavior. + doc = [ + # What: arrange the gpu field as 0; why: test_amd_smi_process_vram_parses_multi_gpu_json carries gpu through doc into returncode 0 stdout json dumps doc. + {"gpu": 0, "process_list": [{"process_info": { + # What: arrange the pid portion of doc; why: the amd smi process vram parses multi gpu json scenario uses this clause to evaluate doc as one grouped value. + "pid": 41, + # What: arrange the vram mem field as value and unit and 2 and gi b; why: test_amd_smi_process_vram_parses_multi_gpu_json carries vram mem through doc into returncode 0 stdout json dumps doc. + "memory_usage": {"vram_mem": {"value": 2, "unit": "GiB"}}, + # What: arrange the doc mapping with gpu and process list; why: test_amd_smi_process_vram_parses_multi_gpu_json groups the supplied clauses as one doc mapping before its value is consumed. + }}]}, + # What: arrange the process info field as pid and memory usage and 41 and vram mem and value; why: test_amd_smi_process_vram_parses_multi_gpu_json carries process info through doc into returncode 0 stdout json dumps doc. + {"gpu": 1, "process_list": [{"process_info": { + # What: arrange the pid portion of doc; why: the amd smi process vram parses multi gpu json scenario uses this clause to evaluate doc as one grouped value. + "pid": 41, + # What: arrange the vram mem field as value and unit and 512 and mi b; why: test_amd_smi_process_vram_parses_multi_gpu_json carries vram mem through doc into returncode 0 stdout json dumps doc. + "memory_usage": {"vram_mem": {"value": 512, "unit": "MiB"}}, + # What: arrange the process info field as pid and memory usage and 42 and vram mem and gb; why: test_amd_smi_process_vram_parses_multi_gpu_json carries process info through doc into returncode 0 stdout json dumps doc. + }}, {"process_info": { + # What: arrange the pid portion of doc; why: the amd smi process vram parses multi gpu json scenario uses this clause to evaluate doc as one grouped value. + "pid": 42, + # What: arrange the vram mem field as gb; why: test_amd_smi_process_vram_parses_multi_gpu_json carries vram mem through doc into returncode 0 stdout json dumps doc. + "memory_usage": {"vram_mem": "1.5 GB"}, + # What: arrange the doc mapping with process info; why: test_amd_smi_process_vram_parses_multi_gpu_json groups the supplied clauses as one doc mapping before its value is consumed. + }}]}, + # What: arrange the doc collection with gpu and process list and 0 and process info and pid and gpu and process list and 1 and process info and process info; why: test_amd_smi_process_vram_parses_multi_gpu_json groups the supplied clauses as one doc collection before its value is consumed. + ] + # What: arrange the exact monkeypatch setattr metrics subprocess run lambda args kwargs fixture fragment; why: the amd smi process vram parses multi gpu json scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics.subprocess, "run", lambda *args, **kwargs: S before asserting its protocol or. + monkeypatch.setattr(metrics.subprocess, "run", lambda *args, **kwargs: SimpleNamespace( + # What: arrange returncode to json.dumps; why: the amd smi process vram parses multi gpu json scenario binds this 0 value to json.dumps's returncode input. + returncode=0, stdout=json.dumps(doc) + # What: arrange the monkeypatch.setattr call with subprocess and simple namespace; why: test_amd_smi_process_vram_parses_multi_gpu_json groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + )) + + # What: assert the expected metrics amd smi process vram == outcome; why: test metrics test amd smi process vram parses multi gpu json protects its regression by requiring this observable result after the exercised behavior. + assert metrics._amd_smi_process_vram() == { + # What: arrange 41 2 1024 3 512 1024 2 for the scenario; why: test metrics test amd smi process vram parses multi gpu json requires this concrete input or helper state before exercising the behavior under test. + 41: 2 * 1024**3 + 512 * 1024**2, + # What: assert that metrics amd smi process vram equals 41 2 1024 3 512 1024; why: this assertion protects the amd smi process vram parses multi gpu json regression after the test's arranged inputs and exercised call. + 42: 1_500_000_000, + # What: arrange the grouped source fragment for the scenario; why: test metrics test amd smi process vram parses multi gpu json requires this concrete input or helper state before exercising the behavior under test. + } + + +# What: define the test_amd_smi_process_vram_distinguishes_empty_from_unavailable test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the amd smi process vram distinguishes empty from unavailable outcome. +def test_amd_smi_process_vram_distinguishes_empty_from_unavailable(monkeypatch): + # What: arrange the exact monkeypatch setattr metrics subprocess run lambda args kwargs fixture fragment; why: the amd smi process vram distinguishes empty from unavailable scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics.subprocess, "run", lambda *args, **kwargs: S before asserting. + monkeypatch.setattr(metrics.subprocess, "run", lambda *args, **kwargs: SimpleNamespace( + # What: arrange returncode 0 stdout for the scenario; why: test metrics test amd smi process vram distinguishes empty from unavailable requires this concrete input or helper state before exercising the behavior under test. + returncode=0, stdout="[]" + # What: arrange the monkeypatch.setattr call with subprocess and simple namespace; why: test_amd_smi_process_vram_distinguishes_empty_from_unavailable groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + )) + # What: assert that metrics amd smi process vram equals group delimiter; why: this assertion protects the amd smi process vram distinguishes empty from unavailable regression after the test's arranged inputs and exercised call. + assert metrics._amd_smi_process_vram() == {} + + # What: arrange the exact monkeypatch setattr metrics subprocess run lambda args kwargs fixture fragment; why: the amd smi process vram distinguishes empty from unavailable scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics.subprocess, "run", lambda *args, **kwargs: S before asserting. + monkeypatch.setattr(metrics.subprocess, "run", lambda *args, **kwargs: SimpleNamespace( + # What: arrange returncode 1 stdout for the scenario; why: test metrics test amd smi process vram distinguishes empty from unavailable requires this concrete input or helper state before exercising the behavior under test. + returncode=1, stdout="" + # What: arrange the monkeypatch.setattr call with subprocess and simple namespace; why: test_amd_smi_process_vram_distinguishes_empty_from_unavailable groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + )) + # What: assert that metrics amd smi process vram is group delimiter; why: this assertion protects the amd smi process vram distinguishes empty from unavailable regression after the test's arranged inputs and exercised call. + assert metrics._amd_smi_process_vram() is None + + # What: arrange the exact monkeypatch setattr metrics subprocess run lambda args kwargs fixture fragment; why: the amd smi process vram distinguishes empty from unavailable scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics.subprocess, "run", lambda *args, **kwargs: S before asserting. + monkeypatch.setattr(metrics.subprocess, "run", lambda *args, **kwargs: SimpleNamespace( + # What: arrange the exact returncode stdout process info pid memory usage fixture fragment; why: the amd smi process vram distinguishes empty from unavailable scenario feeds this byte-preserved fragment through returncode=0, stdout='[{"process_info":{"pid":41,"memory_usage":{}}}]' before asserting its protocol. + returncode=0, stdout='[{"process_info":{"pid":41,"memory_usage":{}}}]' + # What: arrange the monkeypatch.setattr call with subprocess and simple namespace; why: test_amd_smi_process_vram_distinguishes_empty_from_unavailable groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + )) + # What: assert that metrics amd smi process vram is group delimiter; why: this assertion protects the amd smi process vram distinguishes empty from unavailable regression after the test's arranged inputs and exercised call. + assert metrics._amd_smi_process_vram() is None + + +# What: define the test_vram_measurement_falls_through_to_amd_smi test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the vram measurement falls through to amd smi outcome. +def test_vram_measurement_falls_through_to_amd_smi(monkeypatch): + # What: arrange the exact monkeypatch setattr metrics nvml process vram lambda fixture fragment; why: the vram measurement falls through to amd smi scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics, "_nvml_process_vram", lambda: None) before asserting its protocol or parser result. + monkeypatch.setattr(metrics, "_nvml_process_vram", lambda: None) + # What: arrange the exact monkeypatch setattr metrics smi process vram lambda fixture fragment; why: the vram measurement falls through to amd smi scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics, "_smi_process_vram", lambda: {}) before asserting its protocol or parser result. + monkeypatch.setattr(metrics, "_smi_process_vram", lambda: {}) + # What: arrange the 41 field as 123; why: test_vram_measurement_falls_through_to_amd_smi carries 41 into monkeypatch.setattr(metrics, "_amd_smi_process_vram", lambda: {41: 123,. + monkeypatch.setattr(metrics, "_amd_smi_process_vram", lambda: {41: 123, 42: 456}) + + # What: assert that metrics vram measurement for pids 41 equals 123 true amd smi; why: this assertion protects the vram measurement falls through to amd smi regression after the test's arranged inputs and exercised call. + assert metrics._vram_measurement_for_pids([41]) == (123, True, "amd-smi") + + +# What: define the test_engine_footprint_reports_sources_and_availability test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the engine footprint reports sources and availability outcome. +def test_engine_footprint_reports_sources_and_availability(monkeypatch): + # What: arrange the exact monkeypatch setattr metrics osproc tree pids lambda pid pid fixture fragment; why: the engine footprint reports sources and availability scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics.osproc, "tree_pids", lambda pid: [pid, pid + before asserting its protoc. + monkeypatch.setattr(metrics.osproc, "tree_pids", lambda pid: [pid, pid + 1]) + # What: arrange the exact monkeypatch setattr metrics osproc read pss bytes if available lambda pid pid fixture f; why: the engine footprint reports sources and availability scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics.osproc, "read_pss_bytes_if_available", lambd before asserting. + monkeypatch.setattr(metrics.osproc, "read_pss_bytes_if_available", lambda pid: pid * 10) + # What: act by calling monkeypatch.setattr with metrics and vram measurement for pids and 1234 and true and amd smi; why: the engine footprint reports sources and availability scenario observes the monkeypatch.setattr return value during metrics vram measurement for pids lambda pids amd smi. + monkeypatch.setattr( + # What: arrange the exact metrics vram measurement for pids lambda pids amd smi fixture fragment; why: the engine footprint reports sources and availability scenario feeds this byte-preserved fragment through metrics, "_vram_measurement_for_pids", lambda pids: (1234, True, "amd-sm before asserting its protocol. + metrics, "_vram_measurement_for_pids", lambda pids: (1234, True, "amd-smi") + # What: arrange the monkeypatch.setattr call with metrics; why: test_engine_footprint_reports_sources_and_availability groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: assert the expected metrics engine footprint 10 == outcome; why: test metrics test engine footprint reports sources and availability protects its regression by requiring this observable result after the exercised behavior. + assert metrics.engine_footprint(10) == { + # What: arrange ramBytes 210 for the scenario; why: test metrics test engine footprint reports sources and availability requires this concrete input or helper state before exercising the behavior under test. + "ramBytes": 210, + # What: arrange vramBytes 1234 for the scenario; why: test metrics test engine footprint reports sources and availability requires this concrete input or helper state before exercising the behavior under test. + "vramBytes": 1234, + # What: arrange pids 10 11 for the scenario; why: test metrics test engine footprint reports sources and availability requires this concrete input or helper state before exercising the behavior under test. + "pids": [10, 11], + # What: arrange ramAvailable True for the scenario; why: test metrics test engine footprint reports sources and availability requires this concrete input or helper state before exercising the behavior under test. + "ramAvailable": True, + # What: arrange vramAvailable True for the scenario; why: test metrics test engine footprint reports sources and availability requires this concrete input or helper state before exercising the behavior under test. + "vramAvailable": True, + # What: arrange ramSource proc smaps rollup pss for the scenario; why: test metrics test engine footprint reports sources and availability requires this concrete input or helper state before exercising the behavior under test. + "ramSource": "proc-smaps-rollup-pss", + # What: arrange vramSource amd smi for the scenario; why: test metrics test engine footprint reports sources and availability requires this concrete input or helper state before exercising the behavior under test. + "vramSource": "amd-smi", + # What: arrange the grouped source fragment for the scenario; why: test metrics test engine footprint reports sources and availability requires this concrete input or helper state before exercising the behavior under test. + } + + +# What: define the test_engine_footprint_does_not_label_fallback_zero_as_measured test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the engine footprint does not label fallback zero as measured outcome. +def test_engine_footprint_does_not_label_fallback_zero_as_measured(monkeypatch): + # What: arrange the exact monkeypatch setattr metrics osproc tree pids lambda pid pid fixture fragment; why: the engine footprint does not label fallback zero as measured scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics.osproc, "tree_pids", lambda pid: [pid]) before asserting its pro. + monkeypatch.setattr(metrics.osproc, "tree_pids", lambda pid: [pid]) + # What: arrange the exact monkeypatch setattr metrics osproc read pss bytes if available lambda pid fixture fragm; why: the engine footprint does not label fallback zero as measured scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics.osproc, "read_pss_bytes_if_available", lambd before a. + monkeypatch.setattr(metrics.osproc, "read_pss_bytes_if_available", lambda pid: None) + # What: arrange the exact monkeypatch setattr metrics vram measurement for pids lambda pids fixture fragment; why: the engine footprint does not label fallback zero as measured scenario feeds this byte-preserved fragment through monkeypatch.setattr(metrics, "_vram_measurement_for_pids", lambda pids: before asserti. + monkeypatch.setattr(metrics, "_vram_measurement_for_pids", lambda pids: (0, False, None)) + + # What: act by calling metrics.engine_footprint and capture footprint; why: the engine footprint does not label fallback zero as measured test asserts the response, state, or failure produced by this call. + footprint = metrics.engine_footprint(10) + # What: assert that footprint ram bytes equals footprint vram bytes equals 0; why: this assertion protects the engine footprint does not label fallback zero as measured regression after the test's arranged inputs and exercised call. + assert footprint["ramBytes"] == footprint["vramBytes"] == 0 + # What: assert that footprint ram available is footprint vram available is false; why: this assertion protects the engine footprint does not label fallback zero as measured regression after the test's arranged inputs and exercised call. + assert footprint["ramAvailable"] is footprint["vramAvailable"] is False + # What: assert that footprint ram source is footprint vram source is group delimiter; why: this assertion protects the engine footprint does not label fallback zero as measured regression after the test's arranged inputs and exercised call. + assert footprint["ramSource"] is footprint["vramSource"] is None diff --git a/tests/daemon/test_performance.py b/tests/daemon/test_performance.py new file mode 100644 index 000000000..b7bd54c84 --- /dev/null +++ b/tests/daemon/test_performance.py @@ -0,0 +1,132 @@ +# What: enable postponed evaluation of annotations; why: type hints in test_performance can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: arrange from datetime import datetime timezone for the scenario; why: test performance requires this concrete input or helper state before exercising the behavior under test. +from datetime import datetime, timezone +# What: import threading for test performance sampler stops and reconfigures without old generation resuming using threading; why: test_performance_sampler_stops_and_reconfigures_without_old_generation_resuming uses threading event, making that imported dependency available to its named operation. +import threading +# What: import time for test performance sampler stops and reconfigures without old generation resuming using time; why: test_performance_sampler_stops_and_reconfigures_without_old_generation_resuming uses time sleep, making that imported dependency available to its named operation. +import time + +# What: arrange from freetoken daemon performance import PerformanceMonitor for the scenario; why: test performance requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.performance import PerformanceMonitor + + +# What: define the test_performance_history_is_one_hour_bounded_and_filterable test around local fixtures; why: this test groups the arrange, act, and assertions that protect the performance history is one hour bounded and filterable outcome. +def test_performance_history_is_one_hour_bounded_and_filterable(): + # What: arrange clock as 1700000000 0; why: the performance history is one hour bounded and filterable test consumes this named precondition before exercising the behavior. + clock = [1_700_000_000.0] + # What: arrange values as 10 and 20 and 30; why: the performance history is one hour bounded and filterable test consumes this named precondition before exercising the behavior. + values = [10, 20, 30] + # What: act by calling PerformanceMonitor and capture monitor; why: the performance history is one hour bounded and filterable test asserts the response, state, or failure produced by this call. + monitor = PerformanceMonitor( + # What: arrange the lambda portion of monitor; why: the performance history is one hour bounded and filterable scenario uses this clause to evaluate monitor as one grouped value. + lambda: { + # What: arrange the ram bytes field as pop and values and 0; why: test_performance_history_is_one_hour_bounded_and_filterable carries ram bytes through monitor into monitor sample once. + "ramBytes": values.pop(0), "vramBytes": 7, + # What: arrange the ram available field as true; why: test_performance_history_is_one_hour_bounded_and_filterable carries ram available through monitor into monitor sample once. + "ramAvailable": True, "vramAvailable": False, + # What: arrange the ram source field as test pss; why: test_performance_history_is_one_hour_bounded_and_filterable carries ram source through monitor into monitor sample once. + "ramSource": "test-pss", "vramSource": None, + # What: arrange the pids field as 123; why: test_performance_history_is_one_hour_bounded_and_filterable carries pids through monitor into monitor sample once. + "pids": [123], + # What: arrange the monitor mapping with ram bytes and vram bytes and ram available and vram available and ram source; why: test_performance_history_is_one_hour_bounded_and_filterable groups the supplied clauses as one monitor mapping before its value is consumed. + }, + # What: arrange every s to PerformanceMonitor; why: the performance history is one hour bounded and filterable scenario binds this 1800 value to PerformanceMonitor's every s input. + every_s=1800, + # What: arrange wall now to PerformanceMonitor; why: the performance history is one hour bounded and filterable scenario binds this clock and 0 value to PerformanceMonitor's wall now input. + wall_now=lambda: clock[0], + # What: arrange the PerformanceMonitor call with every s and wall now; why: test_performance_history_is_one_hour_bounded_and_filterable groups the supplied clauses as one PerformanceMonitor call before its value is consumed. + ) + # What: act across the computed value to perform timestamp and clock; why: the performance history is one hour bounded and filterable scenario repeats the body only while or for the loop header admits an iteration. + for timestamp in (1_700_000_000.0, 1_700_001_800.0, 1_700_003_600.0): + # What: arrange clock entry as timestamp; why: the performance history is one hour bounded and filterable test consumes this named precondition before exercising the behavior. + clock[0] = timestamp + # What: act by calling monitor.sample_once with the declared inputs; why: the performance history is one hour bounded and filterable scenario observes the monitor.sample_once return value during result monitor current. + monitor.sample_once() + + # What: act by calling monitor.current and capture result; why: the performance history is one hour bounded and filterable test asserts the response, state, or failure produced by this call. + result = monitor.current() + # What: assert that row ram bytes for row in result equals 20 30; why: this assertion protects the performance history is one hour bounded and filterable regression after the test's arranged inputs and exercised call. + assert [row["ram_bytes"] for row in result["sys_stats"]] == [20, 30] + # What: assert that result gpu stats equals group delimiter; why: this assertion protects the performance history is one hour bounded and filterable regression after the test's arranged inputs and exercised call. + assert result["gpu_stats"] == [] + # What: assert that result retention s equals 3600; why: this assertion protects the performance history is one hour bounded and filterable regression after the test's arranged inputs and exercised call. + assert result["retentionS"] == 3600 + # What: assert that pids is absent from result sys stats 0; why: this assertion protects the performance history is one hour bounded and filterable regression after the test's arranged inputs and exercised call. + assert "pids" not in result["sys_stats"][0] + # What: assert that result sys stats 0 scope equals engine process tree; why: this assertion protects the performance history is one hour bounded and filterable regression after the test's arranged inputs and exercised call. + assert result["sys_stats"][0]["scope"] == "engine-process-tree" + + # What: act by calling datetime.fromtimestamp and capture after; why: the performance history is one hour bounded and filterable test asserts the response, state, or failure produced by this call. + after = datetime.fromtimestamp(1_700_001_800.0, timezone.utc) + # What: assert that row ram bytes for row in monitor current equals 30; why: this assertion protects the performance history is one hour bounded and filterable regression after the test's arranged inputs and exercised call. + assert [row["ram_bytes"] for row in monitor.current(after=after)["sys_stats"]] == [30] + + +# What: define the test_performance_probe_failure_is_generic_and_recovers test around local fixtures; why: this test groups the arrange, act, and assertions that protect the performance probe failure is generic and recovers outcome. +def test_performance_probe_failure_is_generic_and_recovers(): + # What: arrange fail as true; why: the performance probe failure is generic and recovers test consumes this named precondition before exercising the behavior. + fail = [True] + + # What: define the sample test helper around captured fixture state; why: the performance probe failure is generic and recovers scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def sample(): + # What: act on fail before runtime error; why: the performance probe failure is generic and recovers scenario admits runtime error only for this predicate and excludes the opposite state. + if fail[0]: + # What: raise RuntimeError for the caller; why: sample stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("private probe detail") + # What: return no value from the sample test helper; why: the performance probe failure is generic and recovers scenario uses this helper result in its subsequent act or assertion. + return {} + + # What: act by calling PerformanceMonitor and capture monitor; why: the performance probe failure is generic and recovers test asserts the response, state, or failure produced by this call. + monitor = PerformanceMonitor(sample) + # What: act by calling monitor.sample_once with the declared inputs; why: the performance probe failure is generic and recovers scenario observes the monitor.sample_once return value during assert monitor current error sample failed. + monitor.sample_once() + # What: assert that monitor current error equals sample failed; why: this assertion protects the performance probe failure is generic and recovers regression after the test's arranged inputs and exercised call. + assert monitor.current()["error"] == "sample_failed" + # What: arrange fail entry as false; why: the performance probe failure is generic and recovers test consumes this named precondition before exercising the behavior. + fail[0] = False + # What: act by calling monitor.sample_once with the declared inputs; why: the performance probe failure is generic and recovers scenario observes the monitor.sample_once return value during assert monitor current healthy is. + monitor.sample_once() + # What: assert that monitor current healthy is true; why: this assertion protects the performance probe failure is generic and recovers regression after the test's arranged inputs and exercised call. + assert monitor.current()["healthy"] is True + # What: assert that monitor current error is group delimiter; why: this assertion protects the performance probe failure is generic and recovers regression after the test's arranged inputs and exercised call. + assert monitor.current()["error"] is None + + +# What: define the test_performance_sampler_stops_and_reconfigures_without_old_generation_resuming test around local fixtures; why: this test groups the arrange, act, and assertions that protect the performance sampler stops and reconfigures without old generation resuming outcome. +def test_performance_sampler_stops_and_reconfigures_without_old_generation_resuming(): + # What: act by calling threading.Event and capture sampled; why: the performance sampler stops and reconfigures without old generation resuming test asserts the response, state, or failure produced by this call. + sampled = threading.Event() + # What: arrange calls as the fixture input; why: the performance sampler stops and reconfigures without old generation resuming test consumes this named precondition before exercising the behavior. + calls = [] + + # What: define the sample test helper around captured fixture state; why: the performance sampler stops and reconfigures without old generation resuming scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def sample(): + # What: act by calling calls.append with len and calls; why: the performance sampler stops and reconfigures without old generation resuming scenario observes the calls.append return value during sampled set. + calls.append(len(calls)) + # What: act by calling sampled.set with the declared inputs; why: the performance sampler stops and reconfigures without old generation resuming scenario observes the sampled.set return value during return. + sampled.set() + # What: return no value from the sample test helper; why: the performance sampler stops and reconfigures without old generation resuming scenario uses this helper result in its subsequent act or assertion. + return {} + + # What: act by calling PerformanceMonitor and capture monitor; why: the performance sampler stops and reconfigures without old generation resuming test asserts the response, state, or failure produced by this call. + monitor = PerformanceMonitor(sample, every_s=0.01) + # What: act by calling monitor.start with the declared inputs; why: the performance sampler stops and reconfigures without old generation resuming scenario observes the monitor.start return value during assert sampled wait. + monitor.start() + # What: assert that sampled wait 1; why: this assertion protects the performance sampler stops and reconfigures without old generation resuming regression after the test's arranged inputs and exercised call. + assert sampled.wait(1) + # What: act by calling monitor.reconfigure with 0 02 and false; why: the performance sampler stops and reconfigures without old generation resuming scenario observes the monitor.reconfigure return value during sampled clear. + monitor.reconfigure(0.02, False) + # What: act by calling sampled.clear with the declared inputs; why: the performance sampler stops and reconfigures without old generation resuming scenario observes the sampled.clear return value during assert sampled wait. + sampled.clear() + # What: assert that sampled wait 1; why: this assertion protects the performance sampler stops and reconfigures without old generation resuming regression after the test's arranged inputs and exercised call. + assert sampled.wait(1) + # What: act by calling monitor.stop with the declared inputs; why: the performance sampler stops and reconfigures without old generation resuming scenario observes the monitor.stop return value during stopped at len calls. + monitor.stop() + # What: act by calling len and capture stopped at; why: the performance sampler stops and reconfigures without old generation resuming test asserts the response, state, or failure produced by this call. + stopped_at = len(calls) + # What: act by calling time.sleep with 0 05; why: the performance sampler stops and reconfigures without old generation resuming scenario observes the time.sleep return value during assert len calls stopped at. + time.sleep(0.05) + # What: assert that len calls equals stopped at; why: this assertion protects the performance sampler stops and reconfigures without old generation resuming regression after the test's arranged inputs and exercised call. + assert len(calls) == stopped_at diff --git a/tests/daemon/test_real_process_recovery.py b/tests/daemon/test_real_process_recovery.py new file mode 100644 index 000000000..777b383bb --- /dev/null +++ b/tests/daemon/test_real_process_recovery.py @@ -0,0 +1,599 @@ +"""Linux CPU integration: real process groups, HTTP probes, and durable recovery. + +No model runtime or production endpoint is used. The subprocess below is a small +test HTTP server, not a substitute for the separate GPU qualification gates. +""" +# What: document linux cpu integration real process groups in the test_real_process_recovery docstring; why: introspection and maintainers read this exact docstring fragment to understand test real process recovery behavior without executing it. +# What: document no model runtime or production endpoint in the test_real_process_recovery docstring; why: introspection and maintainers read this exact docstring fragment to understand test real process recovery behavior without executing it. +# What: document test http server not a substitute in the test_real_process_recovery docstring; why: introspection and maintainers read this exact docstring fragment to understand test real process recovery behavior without executing it. +# What: preserve the paragraph boundary in the the test_real_process_recovery docstring; why: introspection and maintainers read this paragraph break to understand test real process recovery behavior without executing it. + +# What: import json for json get using json; why: json_get uses json load, making that imported dependency available to its named operation. +import json +# What: import os for test real readiness rollback and process group cleanup using os; why: test_real_readiness_rollback_and_process_group_cleanup uses os killpg, making that imported dependency available to its named operation. +import os +# What: import socket for test real readiness rollback and process group cleanup using socket; why: test_real_readiness_rollback_and_process_group_cleanup uses socket socket, making that imported dependency available to its named operation. +import socket +# What: import subprocess for test native router binds and routes a real readopted child using subprocess; why: test_native_router_binds_and_routes_a_real_readopted_child uses subprocess popen, making that imported dependency available to its named operation. +import subprocess +# What: import sys for module initialization using sys; why: module initialization uses sys platform, making that imported dependency available to its named operation. +import sys +# What: import time for test native router binds and routes a real readopted child using time; why: test_native_router_binds_and_routes_a_real_readopted_child uses time monotonic, making that imported dependency available to its named operation. +import time +# What: import urllib request for json get using urllib and request; why: json_get uses urllib request urlopen, making that imported dependency available to its named operation. +import urllib.request +# What: import thread pool executor for test native router supervises a real child and relays sse using concurrent and futures and thread pool executor; why: test_native_router_supervises_a_real_child_and_relays_sse uses thread pool executor, making that imported dependency available to its named operation. +from concurrent.futures import ThreadPoolExecutor + +# What: import pytest for module initialization using pytest; why: module initialization uses pytest mark skipif, making that imported dependency available to its named operation. +import pytest +# What: import test client for test native router uses fresh dynamic ports for real child reactivation using fastapi and testclient and test client; why: test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation uses test client, making that imported dependency available to its named operation. +from fastapi.testclient import TestClient + +# What: import build app for test native router supervises a real child and relays sse using freetoken and daemon and app and build app; why: test_native_router_supervises_a_real_child_and_relays_sse uses build app, making that imported dependency available to its named operation. +from freetoken.daemon.app import build_app +# What: arrange from freetoken daemon catalog import ModelCatalog ModelProfile RouterSettings for the scenario; why: test real process recovery requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.catalog import ModelCatalog, ModelProfile, RouterSettings +# What: import log ring for test real readiness rollback and process group cleanup using freetoken and daemon and logring and log ring; why: test_real_readiness_rollback_and_process_group_cleanup uses log ring, making that imported dependency available to its named operation. +from freetoken.daemon.logring import LogRing +# What: arrange from freetoken daemon pidfile import ServeState ServeStateStore for the scenario; why: test real process recovery requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.pidfile import ServeState, ServeStateStore +# What: import serve probe for test real readiness rollback and process group cleanup using freetoken and daemon and proxy and serve probe; why: test_real_readiness_rollback_and_process_group_cleanup uses serve probe, making that imported dependency available to its named operation. +from freetoken.daemon.proxy import ServeProbe +# What: import wait for ready for test real readiness rollback and process group cleanup using freetoken and daemon and readiness and wait for ready; why: test_real_readiness_rollback_and_process_group_cleanup uses wait for ready, making that imported dependency available to its named operation. +from freetoken.daemon.readiness import wait_for_ready +# What: arrange from freetoken daemon router import RoutingCoordinator for the scenario; why: test real process recovery requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.router import RoutingCoordinator +# What: arrange from freetoken daemon serve manager import AdoptedChild PopenChild ServeManager for the scenario; why: test real process recovery requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.serve_manager import AdoptedChild, PopenChild, ServeManager + + +# What: act by calling pytest.mark.skipif and capture pytestmark; why: the real process recovery test asserts the response, state, or failure produced by this call. +pytestmark = pytest.mark.skipif(sys.platform != "linux", reason="Linux process-group integration") + +# What: arrange server as import and json and os and signal; why: the real process recovery test consumes this named precondition before exercising the behavior. +# What: arrange the exact import json os signal subprocess sys fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact from http server import base httprequest handler httpserver fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact model port resistant sys argv fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact worker subprocess popen sys executable c import time fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact if resistant yes fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact signal signal signal sigterm signal sig ign fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact class handler base httprequest handler fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact def log message args pass fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact def do get fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact if self path health fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact body status error if model bad fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact maintenance serving worker pid worker pid fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact else fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact body requests prompt tokens total completion tokens total fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact uptime s reachable fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact data json dumps body encode fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self send response fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self send header content length str len data fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self end headers fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self wfile write data fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact def do post fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact if self path v1 chat completions fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact length int self headers get content length fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact request json loads self rfile read length or b fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact body data model s echo s fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact data done n n model json dumps fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact data body encode fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self send response fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self send header content type text event stream fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self send header content length str len data fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self end headers fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self wfile write data fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact return fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact self send error fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact httpserver int port handler serve forever fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +# What: arrange the exact the grouped expression fixture fragment; why: the real process recovery scenario feeds this byte-preserved fragment through server before asserting its protocol or parser result. +SERVER = r''' +import json, os, signal, subprocess, sys +from http.server import BaseHTTPRequestHandler, HTTPServer +model, port, resistant = sys.argv[1:] +worker = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(120)"]) +if resistant == "yes": + signal.signal(signal.SIGTERM, signal.SIG_IGN) +class Handler(BaseHTTPRequestHandler): + def log_message(self, *args): pass + def do_GET(self): + if self.path == "/health": + body = {"status": "error" if model == "bad" else "ok", + "maintenance": "serving", "worker_pid": worker.pid} + else: + body = {"requests": {"promptTokensTotal": 0, "completionTokensTotal": 0}, + "uptimeS": 0, "reachable": True} + data = json.dumps(body).encode() + self.send_response(200) + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + def do_POST(self): + if self.path == "/v1/chat/completions": + length = int(self.headers.get("Content-Length", "0")) + request = json.loads(self.rfile.read(length) or b"{}") + body = ('data: {"model":"%s","echo":%s}\n\n' + 'data: [DONE]\n\n') % (model, json.dumps(request.get("model"))) + data = body.encode() + self.send_response(200) + self.send_header("Content-Type", "text/event-stream") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + return + self.send_error(404) +HTTPServer(("127.0.0.1", int(port)), Handler).serve_forever() +''' + + +# What: define the json_get test helper around port and path; why: the json get scenario calls this helper to produce or observe the exact behavior checked by its assertions. +def json_get(port, path): + # What: enter the urllib.request.urlopen managed context before return json load response; why: json_get releases this resource or lock after return json load response on both success and failure paths. + with urllib.request.urlopen(f"http://127.0.0.1:{port}{path}", timeout=2) as response: + # What: return load and response and json from the json_get test helper; why: the json get scenario uses this helper result in its subsequent act or assertion. + return json.load(response) + + +# What: define the running test helper around pid; why: the running scenario calls this helper to produce or observe the exact behavior checked by its assertions. +def running(pid): + # What: establish the handler boundary for the protected operation; why: running routes failures to file not found error and process lookup error while preserving cleanup and success flow. + try: + # Zombies have exited even if the host's init has not reaped them yet. + # What: enter the open managed context before return source read rsplit split z; why: running releases this resource or lock after return source read rsplit split z on both success and failure paths. + with open(f"/proc/{pid}/stat") as source: + # What: return split and rsplit and read and source and z from the running test helper; why: the running scenario uses this helper result in its subsequent act or assertion. + return source.read().rsplit(")", 1)[1].split()[0] != "Z" + # What: handle file not found error and process lookup error by return false; why: running converts that failure into this concrete recovery, response, or cleanup behavior. + except (FileNotFoundError, ProcessLookupError): + # What: return false from the running test helper; why: the running scenario uses this helper result in its subsequent act or assertion. + return False + + +# What: parameterize test_real_readiness_rollback_and_process_group_cleanup with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test real readiness rollback and process group cleanup. +@pytest.mark.parametrize("resistant", [False, True]) +# What: define the test_real_readiness_rollback_and_process_group_cleanup test around tmp path and resistant; why: this test groups the arrange, act, and assertions that protect the real readiness rollback and process group cleanup outcome. +def test_real_readiness_rollback_and_process_group_cleanup(tmp_path, resistant): + # What: enter the socket.socket managed context before reservation bind; why: test_real_readiness_rollback_and_process_group_cleanup releases this resource or lock after reservation bind on both success and failure paths. + with socket.socket() as reservation: + # What: arrange the exact reservation bind fixture fragment; why: the real readiness rollback and process group cleanup scenario feeds this byte-preserved fragment through reservation.bind(("127.0.0.1", 0)) before asserting its protocol or parser result. + reservation.bind(("127.0.0.1", 0)) + # What: act by calling reservation.getsockname and capture port; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + port = reservation.getsockname()[1] + # What: arrange children as the fixture input; why: the real readiness rollback and process group cleanup test consumes this named precondition before exercising the behavior. + children = [] + # What: arrange workers as the fixture input; why: the real readiness rollback and process group cleanup test consumes this named precondition before exercising the behavior. + workers = [] + + # What: define the spawn test helper around model and actual port and args; why: the real readiness rollback and process group cleanup scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def spawn(model, actual_port, args): + # What: act by calling subprocess.Popen and capture proc; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + proc = subprocess.Popen([sys.executable, "-u", "-c", SERVER, model, str(actual_port), + # What: arrange start new session to subprocess.Popen; why: the real readiness rollback and process group cleanup scenario binds this true value to subprocess.Popen's start new session input. + "yes" if resistant else "no"], start_new_session=True, + # What: arrange stdin to subprocess.Popen; why: the real readiness rollback and process group cleanup scenario binds this devnull and subprocess value to subprocess.Popen's stdin input. + stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + # What: arrange stderr to subprocess.Popen; why: the real readiness rollback and process group cleanup scenario binds this devnull and subprocess value to subprocess.Popen's stderr input. + stderr=subprocess.DEVNULL) + # What: act by calling PopenChild and capture child; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + child = PopenChild(proc, None) + # What: act by calling children.append with child; why: the real readiness rollback and process group cleanup scenario observes the children.append return value during return child. + children.append(child) + # What: return child from the spawn test helper; why: the real readiness rollback and process group cleanup scenario uses this helper result in its subsequent act or assertion. + return child + + # What: act by calling ServeStateStore and capture store; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + store = ServeStateStore(str(tmp_path / "serve.json")) + # What: act by calling ServeManager and capture manager; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + manager = ServeManager(LogRing(), store, spawn_fn=spawn, apply_oom=False, + # What: arrange grace s to ServeManager; why: the real readiness rollback and process group cleanup scenario binds this 0 2 value to ServeManager's grace s input. + grace_s=0.2, reap_wait_s=3, + # What: arrange the p input for test_real_readiness_rollback_and_process_group_cleanup; why: test_real_readiness_rollback_and_process_group_cleanup consumes p during signature binding, so callers must bind it with the other signature inputs. + read_stats=lambda p: json_get(p, "/v1/stats")) + # What: act by calling ServeProbe and capture probe; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + probe = ServeProbe() + # What: establish the handler boundary for the protected operation; why: test_real_readiness_rollback_and_process_group_cleanup routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: act by calling manager.start and capture first; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + first = manager.start("good", port, ["original-argument"]) + # What: assert that wait for ready manager probe pid first pid port port timeout s 5; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert wait_for_ready(manager, probe, pid=first["pid"], port=port, timeout_s=5)["ready"] + # What: arrange the exact workers append json get port health worker pid fixture fragment; why: the real readiness rollback and process group cleanup scenario feeds this byte-preserved fragment through workers.append(json_get(port, "/health")["worker_pid"]) before asserting its protocol or parser result. + workers.append(json_get(port, "/health")["worker_pid"]) + # What: act by calling manager.switch_for_readiness and capture replacement and ticket; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + replacement, ticket = manager.switch_for_readiness("bad", port) + # What: act by calling wait_for_ready and capture failed; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + failed = wait_for_ready(manager, probe, pid=replacement["pid"], port=port, timeout_s=5) + # What: assert that failed reason equals engine error; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert failed["reason"] == "engine-error" + # What: arrange the exact workers append json get port health worker pid fixture fragment; why: the real readiness rollback and process group cleanup scenario feeds this byte-preserved fragment through workers.append(json_get(port, "/health")["worker_pid"]) before asserting its protocol or parser result. + workers.append(json_get(port, "/health")["worker_pid"]) + # What: act by calling manager.recover_switch and capture recovery; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + recovery = manager.recover_switch(ticket) + # What: assert that recovery launched; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert recovery["launched"] + # What: assert that wait for ready manager probe pid recovery pid port port timeout s 5; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert wait_for_ready(manager, probe, pid=recovery["pid"], port=port, timeout_s=5)["ready"] + # What: arrange the exact workers append json get port health worker pid fixture fragment; why: the real readiness rollback and process group cleanup scenario feeds this byte-preserved fragment through workers.append(json_get(port, "/health")["worker_pid"]) before asserting its protocol or parser result. + workers.append(json_get(port, "/health")["worker_pid"]) + # What: act by calling store.load and capture saved; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + saved = store.load() + # What: assert that saved pid equals recovery pid and saved model equals good; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert saved.pid == recovery["pid"] and saved.model == "good" + # What: assert that saved args equals original argument; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert saved.args == ["original-argument"] + # What: assert that len manager pending accounting equals 2; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert len(manager.pending_accounting()) == 2 + # What: act by calling manager.stop with the declared inputs; why: the real readiness rollback and process group cleanup scenario observes the manager.stop return value during assert store load is. + manager.stop() + # What: assert that store load is group delimiter; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert store.load() is None + # What: assert that all child reaped is set and child proc poll is not for child in children; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert all(child.reaped.is_set() and child.proc.poll() is not None for child in children) + # What: assert that all child proc returncode equals 9 if resistant else 15 for child; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert all(child.proc.returncode == (-9 if resistant else -15) for child in children) + # What: act by calling time.monotonic and capture deadline; why: the real readiness rollback and process group cleanup test asserts the response, state, or failure produced by this call. + deadline = time.monotonic() + 3 + # What: act across any and deadline and monotonic and running and pid to perform sleep and time; why: the real readiness rollback and process group cleanup scenario repeats the body only while or for the loop header admits an iteration. + while any(running(pid) for pid in workers) and time.monotonic() < deadline: + # What: act by calling time.sleep with 0 05; why: the real readiness rollback and process group cleanup scenario observes the time.sleep return value during assert not any running pid for. + time.sleep(0.05) + # What: assert that any running pid for pid in workers is false; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert not any(running(pid) for pid in workers) + # What: enter the socket.socket managed context before assert connection connect ex port; why: test_real_readiness_rollback_and_process_group_cleanup releases this resource or lock after assert connection connect ex port on both success and failure paths. + with socket.socket() as connection: + # What: assert that connection connect ex 127 0 0 1 port differs from 0; why: this assertion protects the real readiness rollback and process group cleanup regression after the test's arranged inputs and exercised call. + assert connection.connect_ex(("127.0.0.1", port)) != 0 + # What: run test owned sessions only always clean up on every exit path; why: test_real_readiness_rollback_and_process_group_cleanup performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # Test-owned sessions only. Always clean up even if an assertion fails. + # What: act across children to perform process lookup error and killpg and pid and os and child; why: the real readiness rollback and process group cleanup scenario repeats the body only while or for the loop header admits an iteration. + for child in children: + # What: establish the handler boundary for the protected operation; why: test_real_readiness_rollback_and_process_group_cleanup routes failures to process lookup error while preserving cleanup and success flow. + try: + # What: act by calling os.killpg with pid and child and 9; why: the real readiness rollback and process group cleanup scenario observes the os.killpg return value during except process lookup error. + os.killpg(child.pid, 9) + # What: handle process lookup error by pass; why: test_real_readiness_rollback_and_process_group_cleanup converts that failure into this concrete recovery, response, or cleanup behavior. + except ProcessLookupError: + # What: ignore the anticipated exception handled by this branch; why: spawn continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: act on poll and proc and child before wait and proc and child; why: the real readiness rollback and process group cleanup scenario admits wait and proc and child only for this predicate and excludes the opposite state. + if child.proc.poll() is None: + # What: arrange timeout to child.proc.wait; why: the real readiness rollback and process group cleanup scenario binds this 3 value to child.proc.wait's timeout input. + child.proc.wait(timeout=3) + + +# What: define the test_native_router_supervises_a_real_child_and_relays_sse test around tmp path; why: this test groups the arrange, act, and assertions that protect the native router supervises a real child and relays sse outcome. +def test_native_router_supervises_a_real_child_and_relays_sse(tmp_path): + # What: enter the socket.socket managed context before reservation bind; why: test_native_router_supervises_a_real_child_and_relays_sse releases this resource or lock after reservation bind on both success and failure paths. + with socket.socket() as reservation: + # What: arrange the exact reservation bind fixture fragment; why: the native router supervises a real child and relays sse scenario feeds this byte-preserved fragment through reservation.bind(("127.0.0.1", 0)) before asserting its protocol or parser result. + reservation.bind(("127.0.0.1", 0)) + # What: act by calling reservation.getsockname and capture port; why: the native router supervises a real child and relays sse test asserts the response, state, or failure produced by this call. + port = reservation.getsockname()[1] + # What: arrange children as the fixture input; why: the native router supervises a real child and relays sse test consumes this named precondition before exercising the behavior. + children = [] + + # What: define the spawn test helper around model and actual port and args; why: the native router supervises a real child and relays sse scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def spawn(model, actual_port, args): + # What: act by calling subprocess.Popen and capture proc; why: the native router supervises a real child and relays sse test asserts the response, state, or failure produced by this call. + proc = subprocess.Popen( + # What: act by calling str with actual port; why: the native router supervises a real child and relays sse scenario observes the str return value during start new session stdin subprocess devnull stdout subprocess devnull. + [sys.executable, "-u", "-c", SERVER, model, str(actual_port), "no"], + # What: arrange start new session to subprocess.Popen; why: the native router supervises a real child and relays sse scenario binds this true value to subprocess.Popen's start new session input. + start_new_session=True, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + # What: arrange stderr to subprocess.Popen; why: the native router supervises a real child and relays sse scenario binds this devnull and subprocess value to subprocess.Popen's stderr input. + stderr=subprocess.DEVNULL, + # What: arrange the grouped source fragment for the scenario; why: test real process recovery test native router supervises a real child and relays sse requires this concrete input or helper state before exercising the behavior under test. + ) + # What: act by calling PopenChild and capture child; why: the native router supervises a real child and relays sse test asserts the response, state, or failure produced by this call. + child = PopenChild(proc, None) + # What: act by calling children.append with child; why: the native router supervises a real child and relays sse scenario observes the children.append return value during return child. + children.append(child) + # What: return child from the spawn test helper; why: the native router supervises a real child and relays sse scenario uses this helper result in its subsequent act or assertion. + return child + + # What: act by calling ServeStateStore and capture store; why: the native router supervises a real child and relays sse test asserts the response, state, or failure produced by this call. + store = ServeStateStore(str(tmp_path / "serve.json")) + # What: act by calling ServeManager and capture manager; why: the native router supervises a real child and relays sse test asserts the response, state, or failure produced by this call. + manager = ServeManager(LogRing(), store, spawn_fn=spawn, apply_oom=False, + # What: arrange grace s to ServeManager; why: the native router supervises a real child and relays sse scenario binds this 0 2 value to ServeManager's grace s input. + grace_s=0.2, reap_wait_s=3, + # What: arrange the p input for test_native_router_supervises_a_real_child_and_relays_sse; why: test_native_router_supervises_a_real_child_and_relays_sse consumes p during signature binding, so callers must bind it with the other signature inputs. + read_stats=lambda p: json_get(p, "/v1/stats")) + # What: act by calling ServeProbe and capture probe; why: the native router supervises a real child and relays sse test asserts the response, state, or failure produced by this call. + probe = ServeProbe() + # What: act by calling ModelCatalog and capture catalog; why: the native router supervises a real child and relays sse test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog( + # What: arrange the good field as model profile and port and good and good; why: test_native_router_supervises_a_real_child_and_relays_sse carries good through catalog into lifecycle pool lifecycle proxy pool proxy catalog catalog. + {"good": ModelProfile("good", "good", (), port=port)}, + # What: arrange settings to RouterSettings; why: the native router supervises a real child and relays sse scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(send_loading_state=True), + # What: arrange the ModelCatalog call with settings; why: test_native_router_supervises_a_real_child_and_relays_sse groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: establish the handler boundary for the protected operation; why: test_native_router_supervises_a_real_child_and_relays_sse routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_native_router_supervises_a_real_child_and_relays_sse releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(2) as proxy: + # What: act by calling build_app and capture app; why: the native router supervises a real child and relays sse test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_native_router_supervises_a_real_child_and_relays_sse; why: test_native_router_supervises_a_real_child_and_relays_sse consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=probe, footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the native router supervises a real child and relays sse scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_native_router_supervises_a_real_child_and_relays_sse groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the native router supervises a real child and relays sse test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange the model field as good; why: test_native_router_supervises_a_real_child_and_relays_sse sends this field through response so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "good", "stream": True}, + # What: arrange the operation.post call with json; why: test_native_router_supervises_a_real_child_and_relays_sse groups the supplied clauses as one operation.post call before its value is consumed. + ) + # What: assert that response status code equals 200; why: this assertion protects the native router supervises a real child and relays sse regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that b reasoning content freetoken swap loading model good is present in response content; why: this assertion protects the native router supervises a real child and relays sse regression after the test's arranged inputs and exercised call. + assert b'"reasoning_content":"freetoken-swap loading model: good\\n"' in response.content + # What: assert that b model good is present in response content; why: this assertion protects the native router supervises a real child and relays sse regression after the test's arranged inputs and exercised call. + assert b'"model":"good"' in response.content + # What: assert that response content endswith b data done n n; why: this assertion protects the native router supervises a real child and relays sse regression after the test's arranged inputs and exercised call. + assert response.content.endswith(b"data: [DONE]\n\n") + # What: assert that manager status running is true; why: this assertion protects the native router supervises a real child and relays sse regression after the test's arranged inputs and exercised call. + assert manager.status()["running"] is True + # What: assert that store load is not group delimiter; why: this assertion protects the native router supervises a real child and relays sse regression after the test's arranged inputs and exercised call. + assert store.load() is not None + # What: act by calling manager.stop with the declared inputs; why: the native router supervises a real child and relays sse scenario observes the manager.stop return value during assert store load is. + manager.stop() + # What: assert that store load is group delimiter; why: this assertion protects the native router supervises a real child and relays sse regression after the test's arranged inputs and exercised call. + assert store.load() is None + # What: assert that children 0 reaped is set; why: this assertion protects the native router supervises a real child and relays sse regression after the test's arranged inputs and exercised call. + assert children[0].reaped.is_set() + # What: run for child in children on every exit path; why: test_native_router_supervises_a_real_child_and_relays_sse performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act across children to perform process lookup error and killpg and pid and os and child; why: the native router supervises a real child and relays sse scenario repeats the body only while or for the loop header admits an iteration. + for child in children: + # What: establish the handler boundary for the protected operation; why: test_native_router_supervises_a_real_child_and_relays_sse routes failures to process lookup error while preserving cleanup and success flow. + try: + # What: act by calling os.killpg with pid and child and 9; why: the native router supervises a real child and relays sse scenario observes the os.killpg return value during except process lookup error. + os.killpg(child.pid, 9) + # What: handle process lookup error by pass; why: test_native_router_supervises_a_real_child_and_relays_sse converts that failure into this concrete recovery, response, or cleanup behavior. + except ProcessLookupError: + # What: ignore the anticipated exception handled by this branch; why: spawn continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: act on poll and proc and child before wait and proc and child; why: the native router supervises a real child and relays sse scenario admits wait and proc and child only for this predicate and excludes the opposite state. + if child.proc.poll() is None: + # What: arrange timeout to child.proc.wait; why: the native router supervises a real child and relays sse scenario binds this 3 value to child.proc.wait's timeout input. + child.proc.wait(timeout=3) + + +# What: define the test_native_router_binds_and_routes_a_real_readopted_child test around tmp path; why: this test groups the arrange, act, and assertions that protect the native router binds and routes a real readopted child outcome. +def test_native_router_binds_and_routes_a_real_readopted_child(tmp_path): + # What: enter the socket.socket managed context before reservation bind; why: test_native_router_binds_and_routes_a_real_readopted_child releases this resource or lock after reservation bind on both success and failure paths. + with socket.socket() as reservation: + # What: arrange the exact reservation bind fixture fragment; why: the native router binds and routes a real readopted child scenario feeds this byte-preserved fragment through reservation.bind(("127.0.0.1", 0)) before asserting its protocol or parser result. + reservation.bind(("127.0.0.1", 0)) + # What: act by calling reservation.getsockname and capture port; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + port = reservation.getsockname()[1] + # What: act by calling subprocess.Popen and capture proc; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + proc = subprocess.Popen( + # What: act by calling str with port; why: the native router binds and routes a real readopted child scenario observes the str return value during start new session stdin subprocess devnull stdout subprocess devnull. + [sys.executable, "-u", "-c", SERVER, "good", str(port), "no"], + # What: arrange start new session to subprocess.Popen; why: the native router binds and routes a real readopted child scenario binds this true value to subprocess.Popen's start new session input. + start_new_session=True, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + # What: arrange stderr to subprocess.Popen; why: the native router binds and routes a real readopted child scenario binds this devnull and subprocess value to subprocess.Popen's stderr input. + stderr=subprocess.DEVNULL, + # What: arrange the subprocess.Popen call with start new session and stdin and stdout and stderr; why: test_native_router_binds_and_routes_a_real_readopted_child groups the supplied clauses as one subprocess.Popen call before its value is consumed. + ) + # What: act by calling ServeStateStore and capture store; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + store = ServeStateStore(str(tmp_path / "serve.json")) + # What: arrange the exact store save serve state model good port port fixture fragment; why: the native router binds and routes a real readopted child scenario feeds this byte-preserved fragment through store.save(ServeState(model="good", port=port, pid=proc.pid, args=["--ad before asserting its protocol or parser. + store.save(ServeState(model="good", port=port, pid=proc.pid, args=["--adopted"])) + # What: act by calling ServeProbe and capture probe; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + probe = ServeProbe() + # What: act by calling time.monotonic and capture deadline; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + deadline = time.monotonic() + 5 + # What: act across deadline and monotonic and time to perform oserror and sleep and json get and port and time; why: the native router binds and routes a real readopted child scenario repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: establish the handler boundary for the protected operation; why: test_native_router_binds_and_routes_a_real_readopted_child routes failures to oserror while preserving cleanup and success flow. + try: + # What: act on json get and port before the computed value; why: the native router binds and routes a real readopted child scenario admits the computed value only for this predicate and excludes the opposite state. + if json_get(port, "/health")["status"] == "ok": + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the native router binds and routes a real readopted child scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: handle oserror by time sleep 0 05; why: test_native_router_binds_and_routes_a_real_readopted_child converts that failure into this concrete recovery, response, or cleanup behavior. + except OSError: + # What: act by calling time.sleep with 0 05; why: the native router binds and routes a real readopted child scenario observes the time.sleep return value during else. + time.sleep(0.05) + # What: select the remaining branch that performs raise assertion error test child did not; why: test_native_router_binds_and_routes_a_real_readopted_child covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: raise AssertionError for the caller; why: test_native_router_binds_and_routes_a_real_readopted_child stops this rejected path before it can mutate state, dispatch work, or report success. + raise AssertionError("test child did not become ready") + + # What: act by calling AdoptedChild and capture adopted; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + adopted = AdoptedChild( + # What: arrange alive check to running; why: the native router binds and routes a real readopted child scenario binds this running and pid and proc value to running's alive check input. + proc.pid, port, None, None, alive_check=lambda: running(proc.pid), + # What: arrange sleep to AdoptedChild; why: the native router binds and routes a real readopted child scenario binds this sleep and time value to AdoptedChild's sleep input. + sleep=time.sleep, poll_interval=0.05, + # What: arrange the AdoptedChild call with alive check and sleep and poll interval; why: test_native_router_binds_and_routes_a_real_readopted_child groups the supplied clauses as one AdoptedChild call before its value is consumed. + ) + # What: act by calling ServeManager and capture manager; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + manager = ServeManager( + # What: act by calling LogRing with the declared inputs; why: the native router binds and routes a real readopted child scenario observes the LogRing return value during spawn fn lambda args value for value. + LogRing(), store, + # What: arrange the args input for test_native_router_binds_and_routes_a_real_readopted_child; why: test_native_router_binds_and_routes_a_real_readopted_child consumes args during signature binding, so callers must bind it with the other signature inputs. + spawn_fn=lambda *args: (_ for _ in ()).throw(AssertionError("unexpected second spawn")), + # What: arrange the state input for test_native_router_binds_and_routes_a_real_readopted_child; why: test_native_router_binds_and_routes_a_real_readopted_child consumes state during signature binding, so callers must bind it with the other signature inputs. + adopt_fn=lambda state: adopted, + # What: arrange apply oom to ServeManager; why: the native router binds and routes a real readopted child scenario binds this false value to ServeManager's apply oom input. + apply_oom=False, grace_s=0.2, reap_wait_s=3, + # What: arrange the p input for test_native_router_binds_and_routes_a_real_readopted_child; why: test_native_router_binds_and_routes_a_real_readopted_child consumes p during signature binding, so callers must bind it with the other signature inputs. + read_stats=lambda p: json_get(p, "/v1/stats"), + # What: arrange the ServeManager call with spawn fn and adopt fn and apply oom and grace s and reap wait s; why: test_native_router_binds_and_routes_a_real_readopted_child groups the supplied clauses as one ServeManager call before its value is consumed. + ) + # What: act by calling ModelCatalog and capture catalog; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog({ + # What: arrange the good field as model profile and port and good and good and adopted; why: test_native_router_binds_and_routes_a_real_readopted_child carries good through catalog into router routing coordinator manager catalog probe. + "good": ModelProfile("good", "good", ("--adopted",), port=port), + # What: arrange the ModelCatalog call with model profile; why: test_native_router_binds_and_routes_a_real_readopted_child groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: establish the handler boundary for the protected operation; why: test_native_router_binds_and_routes_a_real_readopted_child routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: assert that manager readopt is true; why: this assertion protects the native router binds and routes a real readopted child regression after the test's arranged inputs and exercised call. + assert manager.readopt() is True + # What: act by calling RoutingCoordinator and capture router; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog, probe) + # What: assert that router status active profile equals good; why: this assertion protects the native router binds and routes a real readopted child regression after the test's arranged inputs and exercised call. + assert router.status()["activeProfile"] == "good" + # What: assert that router status active identity matches engine is true; why: this assertion protects the native router binds and routes a real readopted child regression after the test's arranged inputs and exercised call. + assert router.status()["activeIdentityMatchesEngine"] is True + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_native_router_binds_and_routes_a_real_readopted_child releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(2) as proxy: + # What: act by calling build_app and capture app; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_native_router_binds_and_routes_a_real_readopted_child; why: test_native_router_binds_and_routes_a_real_readopted_child consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=probe, footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the native router binds and routes a real readopted child scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_native_router_binds_and_routes_a_real_readopted_child groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the native router binds and routes a real readopted child test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange the model field as good; why: test_native_router_binds_and_routes_a_real_readopted_child sends this field through response so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "good", "stream": True}, + # What: arrange the operation.post call with json; why: test_native_router_binds_and_routes_a_real_readopted_child groups the supplied clauses as one operation.post call before its value is consumed. + ) + # What: assert that response status code equals 200; why: this assertion protects the native router binds and routes a real readopted child regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that response content endswith b data done n n; why: this assertion protects the native router binds and routes a real readopted child regression after the test's arranged inputs and exercised call. + assert response.content.endswith(b"data: [DONE]\n\n") + # What: assert that manager status pid equals proc pid and manager status adopted is true; why: this assertion protects the native router binds and routes a real readopted child regression after the test's arranged inputs and exercised call. + assert manager.status()["pid"] == proc.pid and manager.status()["adopted"] is True + # What: assert that router status activations equals 0; why: this assertion protects the native router binds and routes a real readopted child regression after the test's arranged inputs and exercised call. + assert router.status()["activations"] == 0 + # What: act by calling manager.stop with the declared inputs; why: the native router binds and routes a real readopted child scenario observes the manager.stop return value during proc wait timeout. + manager.stop() + # What: arrange timeout to proc.wait; why: the native router binds and routes a real readopted child scenario binds this 3 value to proc.wait's timeout input. + proc.wait(timeout=3) + # What: assert that store load is group delimiter; why: this assertion protects the native router binds and routes a real readopted child regression after the test's arranged inputs and exercised call. + assert store.load() is None + # What: run if proc poll is on every exit path; why: test_native_router_binds_and_routes_a_real_readopted_child performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act on poll and proc before process lookup error and killpg and pid and os and proc; why: the native router binds and routes a real readopted child scenario admits process lookup error and killpg and pid and os and proc only for this predicate and excludes the opposite state. + if proc.poll() is None: + # What: establish the handler boundary for the protected operation; why: test_native_router_binds_and_routes_a_real_readopted_child routes failures to process lookup error while preserving cleanup and success flow. + try: + # What: act by calling os.killpg with pid and proc and 9; why: the native router binds and routes a real readopted child scenario observes the os.killpg return value during except process lookup error. + os.killpg(proc.pid, 9) + # What: handle process lookup error by pass; why: test_native_router_binds_and_routes_a_real_readopted_child converts that failure into this concrete recovery, response, or cleanup behavior. + except ProcessLookupError: + # What: ignore the anticipated exception handled by this branch; why: test_native_router_binds_and_routes_a_real_readopted_child continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: arrange timeout to proc.wait; why: the native router binds and routes a real readopted child scenario binds this 3 value to proc.wait's timeout input. + proc.wait(timeout=3) + + +# What: define the test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation test around tmp path; why: this test groups the arrange, act, and assertions that protect the native router uses fresh dynamic ports for real child reactivation outcome. +def test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation(tmp_path): + # What: define the free_port test helper around captured fixture state; why: the native router uses fresh dynamic ports for real child reactivation scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def free_port(): + # What: enter the socket.socket managed context before reservation bind; why: free_port releases this resource or lock after reservation bind on both success and failure paths. + with socket.socket() as reservation: + # What: arrange the exact reservation bind fixture fragment; why: the native router uses fresh dynamic ports for real child reactivation scenario feeds this byte-preserved fragment through reservation.bind(("127.0.0.1", 0)) before asserting its protocol or parser result. + reservation.bind(("127.0.0.1", 0)) + # What: return getsockname and reservation and 1 from the free_port test helper; why: the native router uses fresh dynamic ports for real child reactivation scenario uses this helper result in its subsequent act or assertion. + return reservation.getsockname()[1] + + # What: act by calling free_port and capture first port and second port; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + first_port, second_port = free_port(), free_port() + # What: assert that first port differs from second port; why: this assertion protects the native router uses fresh dynamic ports for real child reactivation regression after the test's arranged inputs and exercised call. + assert first_port != second_port + # What: arrange children as the fixture input; why: the native router uses fresh dynamic ports for real child reactivation test consumes this named precondition before exercising the behavior. + children = [] + + # What: define the spawn test helper around model and actual port and args; why: the native router uses fresh dynamic ports for real child reactivation scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def spawn(model, actual_port, args): + # What: act by calling subprocess.Popen and capture proc; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + proc = subprocess.Popen( + # What: act by calling str with actual port; why: the native router uses fresh dynamic ports for real child reactivation scenario observes the str return value during start new session stdin subprocess devnull stdout subprocess devnull. + [sys.executable, "-u", "-c", SERVER, model, str(actual_port), "no"], + # What: arrange start new session to subprocess.Popen; why: the native router uses fresh dynamic ports for real child reactivation scenario binds this true value to subprocess.Popen's start new session input. + start_new_session=True, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + # What: arrange stderr to subprocess.Popen; why: the native router uses fresh dynamic ports for real child reactivation scenario binds this devnull and subprocess value to subprocess.Popen's stderr input. + stderr=subprocess.DEVNULL, + # What: arrange the grouped source fragment for the scenario; why: test real process recovery test native router uses fresh dynamic ports for real child reactivation requires this concrete input or helper state before exercising the behavior under test. + ) + # What: act by calling PopenChild and capture child; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + child = PopenChild(proc, None) + # What: act by calling children.append with child; why: the native router uses fresh dynamic ports for real child reactivation scenario observes the children.append return value during return child. + children.append(child) + # What: return child from the spawn test helper; why: the native router uses fresh dynamic ports for real child reactivation scenario uses this helper result in its subsequent act or assertion. + return child + + # What: act by calling ServeStateStore and capture store; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + store = ServeStateStore(str(tmp_path / "serve.json")) + # What: act by calling ServeManager and capture manager; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + manager = ServeManager(LogRing(), store, spawn_fn=spawn, apply_oom=False, + # What: arrange grace s to ServeManager; why: the native router uses fresh dynamic ports for real child reactivation scenario binds this 0 2 value to ServeManager's grace s input. + grace_s=0.2, reap_wait_s=3, + # What: arrange the p input for test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation; why: test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation consumes p during signature binding, so callers must bind it with the other signature inputs. + read_stats=lambda p: json_get(p, "/v1/stats")) + # What: act by calling ServeProbe and capture probe; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + probe = ServeProbe() + # What: act by calling ModelCatalog and capture catalog; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog({"good": ModelProfile("good", "good", (), port=0)}) + # What: act by calling iter and capture ports; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + ports = iter((first_port, second_port)) + # What: act by calling RoutingCoordinator and capture router; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog, probe, port_allocator=lambda: next(ports)) + # What: establish the handler boundary for the protected operation; why: test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(2) as proxy: + # What: act by calling build_app and capture app; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation; why: test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=probe, footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the native router uses fresh dynamic ports for real child reactivation scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.post and capture first; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + first = client.post("/v1/chat/completions", json={"model": "good", "stream": True}) + # What: assert that first status code equals 200; why: this assertion protects the native router uses fresh dynamic ports for real child reactivation regression after the test's arranged inputs and exercised call. + assert first.status_code == 200 + # What: assert that manager status port equals first port; why: this assertion protects the native router uses fresh dynamic ports for real child reactivation regression after the test's arranged inputs and exercised call. + assert manager.status()["port"] == first_port + # What: assert that router evict idle good is true; why: this assertion protects the native router uses fresh dynamic ports for real child reactivation regression after the test's arranged inputs and exercised call. + assert router.evict_idle("good") is True + # What: act by calling client.post and capture second; why: the native router uses fresh dynamic ports for real child reactivation test asserts the response, state, or failure produced by this call. + second = client.post("/v1/chat/completions", json={"model": "good", "stream": True}) + # What: assert that second status code equals 200; why: this assertion protects the native router uses fresh dynamic ports for real child reactivation regression after the test's arranged inputs and exercised call. + assert second.status_code == 200 + # What: assert that manager status port equals second port; why: this assertion protects the native router uses fresh dynamic ports for real child reactivation regression after the test's arranged inputs and exercised call. + assert manager.status()["port"] == second_port + # What: assert that router status activations equals 2; why: this assertion protects the native router uses fresh dynamic ports for real child reactivation regression after the test's arranged inputs and exercised call. + assert router.status()["activations"] == 2 + # What: assert that len children equals 2 and children 0 reaped is set; why: this assertion protects the native router uses fresh dynamic ports for real child reactivation regression after the test's arranged inputs and exercised call. + assert len(children) == 2 and children[0].reaped.is_set() + # What: act by calling manager.stop with the declared inputs; why: the native router uses fresh dynamic ports for real child reactivation scenario observes the manager.stop return value during assert children reaped is set and store load is. + manager.stop() + # What: assert that children 1 reaped is set and store load is; why: this assertion protects the native router uses fresh dynamic ports for real child reactivation regression after the test's arranged inputs and exercised call. + assert children[1].reaped.is_set() and store.load() is None + # What: run for child in children on every exit path; why: test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act across children to perform process lookup error and killpg and pid and os and child; why: the native router uses fresh dynamic ports for real child reactivation scenario repeats the body only while or for the loop header admits an iteration. + for child in children: + # What: establish the handler boundary for the protected operation; why: test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation routes failures to process lookup error while preserving cleanup and success flow. + try: + # What: act by calling os.killpg with pid and child and 9; why: the native router uses fresh dynamic ports for real child reactivation scenario observes the os.killpg return value during except process lookup error. + os.killpg(child.pid, 9) + # What: handle process lookup error by pass; why: test_native_router_uses_fresh_dynamic_ports_for_real_child_reactivation converts that failure into this concrete recovery, response, or cleanup behavior. + except ProcessLookupError: + # What: ignore the anticipated exception handled by this branch; why: spawn continues its retry or cleanup path instead of re-raising that transient failure. + pass + # What: act on poll and proc and child before wait and proc and child; why: the native router uses fresh dynamic ports for real child reactivation scenario admits wait and proc and child only for this predicate and excludes the opposite state. + if child.proc.poll() is None: + # What: arrange timeout to child.proc.wait; why: the native router uses fresh dynamic ports for real child reactivation scenario binds this 3 value to child.proc.wait's timeout input. + child.proc.wait(timeout=3) diff --git a/tests/daemon/test_router.py b/tests/daemon/test_router.py new file mode 100644 index 000000000..c8b555aae --- /dev/null +++ b/tests/daemon/test_router.py @@ -0,0 +1,7062 @@ +# What: enable postponed evaluation of annotations; why: type hints in test_router can reference runtime types without eager imports or forward-reference failures. +from __future__ import annotations + +# What: import asyncio for test cancelled queued http request cannot trigger a later swap using asyncio; why: test_cancelled_queued_http_request_cannot_trigger_a_later_swap uses asyncio run, making that imported dependency available to its named operation. +import asyncio +# What: import base64 for test activity and opt in capture apis are authenticated redacted and durable using base64; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable uses base64 b64decode, making that imported dependency available to its named operation. +import base64 +# What: import threading for test switch waits until an active lease finishes using threading; why: test_switch_waits_until_an_active_lease_finishes uses threading event, making that imported dependency available to its named operation. +import threading +# What: import json for test request filter applies nested drop global and requested id fields in order using json; why: test_request_filter_applies_nested_drop_global_and_requested_id_fields_in_order uses json loads, making that imported dependency available to its named operation. +import json +# What: import time for test router reload cannot race atomic profile lookup and dynamic port binding using time; why: test_router_reload_cannot_race_atomic_profile_lookup_and_dynamic_port_binding uses time sleep, making that imported dependency available to its named operation. +import time +# What: import bytes io for test explicit cancel while upstream connects closes result and releases lease using io and bytes io; why: test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease uses bytes io, making that imported dependency available to its named operation. +from io import BytesIO +# What: import thread pool executor for test cancelled queued http request cannot trigger a later swap using concurrent and futures and thread pool executor; why: test_cancelled_queued_http_request_cannot_trigger_a_later_swap uses thread pool executor, making that imported dependency available to its named operation. +from concurrent.futures import ThreadPoolExecutor +# What: arrange from http server import BaseHTTPRequestHandler ThreadingHTTPServer for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +# What: import pytest for module initialization using pytest; why: module initialization uses pytest mark parametrize, making that imported dependency available to its named operation. +import pytest +# What: import httpx for scenario using httpx; why: scenario uses httpx asgitransport, making that imported dependency available to its named operation. +import httpx +# What: import test client for test http concurrency rejection returns retry after and releases request id using fastapi and testclient and test client; why: test_http_concurrency_rejection_returns_retry_after_and_releases_request_id uses test client, making that imported dependency available to its named operation. +from fastapi.testclient import TestClient + +# What: arrange from freetoken daemon catalog import for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.catalog import ( + # What: arrange CatalogError for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + CatalogError, + # What: arrange ModelCapabilities for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + ModelCapabilities, + # What: arrange ModelCatalog for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + ModelCatalog, + # What: arrange ModelProfile for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + ModelProfile, + # What: arrange ModelSelector for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + ModelSelector, + # What: arrange RequestField for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + RequestField, + # What: arrange RouterSettings for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + RouterSettings, + # What: arrange RoutingGroup for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + RoutingGroup, + # What: arrange RoutingProfile for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + RoutingProfile, +# What: arrange the enclosing predicate with from freetoken daemon catalog import catalog error model capabilities model catalog model profile model selector request; why: test_router groups the supplied clauses as one test_router expression before its value is consumed. +) +# What: import build app for test cancelled queued http request cannot trigger a later swap using freetoken and daemon and app and build app; why: test_cancelled_queued_http_request_cannot_trigger_a_later_swap uses build app, making that imported dependency available to its named operation. +from freetoken.daemon.app import build_app +# What: arrange from freetoken daemon inference proxy import for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.inference_proxy import ( + # What: arrange UpstreamResponse for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + UpstreamResponse, + # What: arrange filter request body for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + filter_request_body, + # What: arrange forward headers for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + forward_headers, + # What: arrange open upstream for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + open_upstream, + # What: arrange response headers for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. + response_headers, +# What: arrange the enclosing predicate with from freetoken daemon inference proxy import upstream response filter request body forward headers open upstream respons; why: test_router groups the supplied clauses as one test_router expression before its value is consumed. +) +# What: arrange from freetoken daemon logring import LogRing for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.logring import LogRing +# What: import serve probe for test custom readiness path accepts real http success without json using freetoken and daemon and proxy and serve probe; why: test_custom_readiness_path_accepts_real_http_success_without_json uses serve probe, making that imported dependency available to its named operation. +from freetoken.daemon.proxy import ServeProbe +# What: import wait for ready for test custom readiness path accepts real http success without json using freetoken and daemon and readiness and wait for ready; why: test_custom_readiness_path_accepts_real_http_success_without_json uses wait for ready, making that imported dependency available to its named operation. +from freetoken.daemon.readiness import wait_for_ready +# What: arrange from freetoken daemon router import RoutingCoordinator RoutingError for the scenario; why: test router requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.router import RoutingCoordinator, RoutingError + + +# What: define Manager as the owner of __init__ and status and serve_args and start and switch_for_readiness; why: daemon callers use this class boundary so those methods share one manager state invariant. +class Manager: + # What: define the __init__ test helper around captured fixture state; why: the init scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def __init__(self): + # What: arrange model as the fixture input; why: the router test consumes this named precondition before exercising the behavior. + self.model = None + # What: arrange port as the fixture input; why: the router test consumes this named precondition before exercising the behavior. + self.port = None + # What: arrange args as the fixture input; why: the router test consumes this named precondition before exercising the behavior. + self.args = [] + # What: arrange pid as 100; why: the router test consumes this named precondition before exercising the behavior. + self.pid = 100 + # What: arrange calls as the fixture input; why: the router test consumes this named precondition before exercising the behavior. + self.calls = [] + + # What: define the status test helper around captured fixture state; why: the status scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def status(self): + # What: arrange the running field as model; why: Manager.status carries running into return {"running": self.model is not None, "model": self.model, "port":. + return {"running": self.model is not None, "model": self.model, "port": self.port, "pid": self.pid} + + # What: define the serve_args test helper around captured fixture state; why: the serve args scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def serve_args(self): + # What: return list and args from the serve_args test helper; why: the serve args scenario uses this helper result in its subsequent act or assertion. + return list(self.args) + + # What: define the start test helper around model and port and args; why: the start scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def start(self, model, port, args): + # What: arrange the exact self calls append start model fixture fragment; why: the start scenario feeds this byte-preserved fragment through self.calls.append(("start", model)) before asserting its protocol or parser result. + self.calls.append(("start", model)) + # What: act by calling list and capture model and port and args; why: the router test asserts the response, state, or failure produced by this call. + self.model, self.port, self.args = model, port, list(args) + # What: arrange pid from 1; why: the start scenario uses pid during return pid self pid before checking the protected result. + self.pid += 1 + # What: arrange the pid field as pid; why: Manager.start carries pid into return {"pid": self.pid}. + return {"pid": self.pid} + + # What: define the switch_for_readiness test helper around model and port and args; why: the switch for readiness scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def switch_for_readiness(self, model, port, args): + # What: arrange the exact self calls append switch model fixture fragment; why: the switch for readiness scenario feeds this byte-preserved fragment through self.calls.append(("switch", model)) before asserting its protocol or parser result. + self.calls.append(("switch", model)) + # What: act by calling list and capture previous; why: the router test asserts the response, state, or failure produced by this call. + previous = self.model, self.port, list(self.args) + # What: act by calling list and capture model and port and args; why: the router test asserts the response, state, or failure produced by this call. + self.model, self.port, self.args = model, port, list(args) + # What: arrange pid from 1; why: the switch for readiness scenario uses pid during return pid self pid previous before checking the protected result. + self.pid += 1 + # What: arrange the pid field as pid; why: Manager.switch_for_readiness carries pid into return {"pid": self.pid}, previous. + return {"pid": self.pid}, previous + + # What: define the recover_switch test helper around ticket; why: the recover switch scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def recover_switch(self, ticket): + # What: arrange model and port and args as ticket; why: the router test consumes this named precondition before exercising the behavior. + self.model, self.port, self.args = ticket + # What: arrange pid from 1; why: the recover switch scenario uses pid during return launched pid self pid before checking the protected result. + self.pid += 1 + # What: arrange the launched field as true; why: Manager.recover_switch carries launched into return {"launched": True, "pid": self.pid}. + return {"launched": True, "pid": self.pid} + + # What: define the stop test helper around timeout; why: the stop scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def stop(self, timeout): + # What: arrange the exact self calls append stop timeout fixture fragment; why: the stop scenario feeds this byte-preserved fragment through self.calls.append(("stop", timeout)) before asserting its protocol or parser result. + self.calls.append(("stop", timeout)) + # What: arrange model as the fixture input; why: the router test consumes this named precondition before exercising the behavior. + self.model = None + # What: arrange the stopped field as true; why: Manager.stop carries stopped into return {"stopped": True}. + return {"stopped": True} + + +# What: define the catalog test helper around captured fixture state; why: the catalog scenario calls this helper to produce or observe the exact behavior checked by its assertions. +def catalog(): + # What: return model catalog and model profile and low and high and low from the catalog test helper; why: the catalog scenario uses this helper result in its subsequent act or assertion. + return ModelCatalog({ + # What: arrange the low field as model profile and low and low and gguf and 0; why: catalog carries low into "low": ModelProfile("low", "low.gguf", (), priority=0). + "low": ModelProfile("low", "low.gguf", (), priority=0), + # What: arrange the high field as model profile and high and high and gguf and 10; why: catalog carries high into "high": ModelProfile("high", "high.gguf", (), priority=10). + "high": ModelProfile("high", "high.gguf", (), priority=10), + # What: arrange the ModelCatalog call with model profile; why: catalog groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + + +# What: define the ready test helper around manager and probe and pid and port and timeout s; why: the ready scenario calls this helper to produce or observe the exact behavior checked by its assertions. +def ready(manager, probe, *, pid, port, timeout_s): + # What: return HTTP 200 when accepting and 503 otherwise; why: supervisors use this status and ready boolean to decide whether the daemon control plane may receive traffic. + return {"ready": True, "health": {"status": "ok"}} + + +# What: define the test_routes_to_ready_engine_then_shares_its_lease test around local fixtures; why: this test groups the arrange, act, and assertions that protect the routes to ready engine then shares its lease outcome. +def test_routes_to_ready_engine_then_shares_its_lease(): + # What: act by calling Manager and capture manager; why: the routes to ready engine then shares its lease test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the routes to ready engine then shares its lease test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: act by calling router.acquire and capture first; why: the routes to ready engine then shares its lease test asserts the response, state, or failure produced by this call. + first = router.acquire("low") + # What: act by calling router.acquire and capture second; why: the routes to ready engine then shares its lease test asserts the response, state, or failure produced by this call. + second = router.acquire("low") + # What: assert that manager calls equals start low gguf; why: this assertion protects the routes to ready engine then shares its lease regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + # What: assert that router status active requests equals 2; why: this assertion protects the routes to ready engine then shares its lease regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 2 + # What: act by calling second.release with the declared inputs; why: the routes to ready engine then shares its lease scenario observes the second.release return value during first release. + second.release() + # What: act by calling first.release with the declared inputs; why: the routes to ready engine then shares its lease scenario observes the first.release return value during assert router status active requests. + first.release() + # What: assert that router status active requests equals 0; why: this assertion protects the routes to ready engine then shares its lease regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + + +# What: define the test_unknown_model_is_a_stable_404_router_error test around local fixtures; why: this test groups the arrange, act, and assertions that protect the unknown model is a stable 404 router error outcome. +def test_unknown_model_is_a_stable_404_router_error(): + # What: assert the pytest.raises failure context; why: the unknown model is a stable 404 router error scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RoutingError, match="unknown model") as exc: + # What: arrange the exact routing coordinator manager catalog object ready fn ready fixture fragment; why: the unknown model is a stable 404 router error scenario feeds this byte-preserved fragment through RoutingCoordinator(Manager(), catalog(), object(), ready_fn=ready).acqui before asserting its protocol or. + RoutingCoordinator(Manager(), catalog(), object(), ready_fn=ready).acquire("missing") + # What: assert that exc value code equals unknown model; why: this assertion protects the unknown model is a stable 404 router error regression after the test's arranged inputs and exercised call. + assert exc.value.code == "unknown_model" + # What: assert that exc value status code equals 404; why: this assertion protects the unknown model is a stable 404 router error regression after the test's arranged inputs and exercised call. + assert exc.value.status_code == 404 + + +# What: define the test_dynamic_profile_port_is_stable_while_resident_and_fresh_after_a_swap test around local fixtures; why: this test groups the arrange, act, and assertions that protect the dynamic profile port is stable while resident and fresh after a swap outcome. +def test_dynamic_profile_port_is_stable_while_resident_and_fresh_after_a_swap(): + # What: act by calling Manager and capture manager; why: the dynamic profile port is stable while resident and fresh after a swap test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the dynamic profile port is stable while resident and fresh after a swap test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({ + # What: arrange the dynamic field as model profile and dynamic and dynamic and gguf and 0; why: test_dynamic_profile_port_is_stable_while_resident_and_fresh_after_a_swap carries dynamic through catalog doc into manager catalog doc object ready fn ready port allocator lambda. + "dynamic": ModelProfile("dynamic", "dynamic.gguf", (), port=0), + # What: arrange the other field as model profile and other and other and gguf and 19555; why: test_dynamic_profile_port_is_stable_while_resident_and_fresh_after_a_swap carries other through catalog doc into manager catalog doc object ready fn ready port allocator lambda. + "other": ModelProfile("other", "other.gguf", (), port=19555), + # What: arrange the ModelCatalog call with model profile; why: test_dynamic_profile_port_is_stable_while_resident_and_fresh_after_a_swap groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: act by calling iter and capture allocated; why: the dynamic profile port is stable while resident and fresh after a swap test asserts the response, state, or failure produced by this call. + allocated = iter([20101, 20102]) + # What: act by calling RoutingCoordinator and capture router; why: the dynamic profile port is stable while resident and fresh after a swap test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator( + # What: arrange ready fn to object; why: the dynamic profile port is stable while resident and fresh after a swap scenario binds this ready value to object's ready fn input. + manager, catalog_doc, object(), ready_fn=ready, port_allocator=lambda: next(allocated), + # What: arrange the RoutingCoordinator call with ready fn and port allocator; why: test_dynamic_profile_port_is_stable_while_resident_and_fresh_after_a_swap groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + + # What: act by calling router.acquire and capture first; why: the dynamic profile port is stable while resident and fresh after a swap test asserts the response, state, or failure produced by this call. + first = router.acquire("dynamic") + # What: act by calling first.release with the declared inputs; why: the dynamic profile port is stable while resident and fresh after a swap scenario observes the first.release return value during warm router acquire dynamic. + first.release() + # What: act by calling router.acquire and capture warm; why: the dynamic profile port is stable while resident and fresh after a swap test asserts the response, state, or failure produced by this call. + warm = router.acquire("dynamic") + # What: assert that first port warm port equals 20101 20101; why: this assertion protects the dynamic profile port is stable while resident and fresh after a swap regression after the test's arranged inputs and exercised call. + assert (first.port, warm.port) == (20101, 20101) + # What: act by calling warm.release with the declared inputs; why: the dynamic profile port is stable while resident and fresh after a swap scenario observes the warm.release return value during router acquire other release. + warm.release() + # What: arrange the exact router acquire other release fixture fragment; why: the dynamic profile port is stable while resident and fresh after a swap scenario feeds this byte-preserved fragment through router.acquire("other").release() before asserting its protocol or parser result. + router.acquire("other").release() + # What: act by calling router.acquire and capture cold again; why: the dynamic profile port is stable while resident and fresh after a swap test asserts the response, state, or failure produced by this call. + cold_again = router.acquire("dynamic") + # What: assert that cold again port equals 20102; why: this assertion protects the dynamic profile port is stable while resident and fresh after a swap regression after the test's arranged inputs and exercised call. + assert cold_again.port == 20102 + # What: act by calling cold_again.release with the declared inputs; why: the dynamic profile port is stable while resident and fresh after a swap scenario observes the cold_again.release return value during assert manager calls. + cold_again.release() + # What: assert the expected manager calls == outcome; why: test router test dynamic profile port is stable while resident and fresh after a swap protects its regression by requiring this observable result after the exercised behavior. + assert manager.calls == [ + # What: arrange start dynamic gguf for the scenario; why: test router test dynamic profile port is stable while resident and fresh after a swap requires this concrete input or helper state before exercising the behavior under test. + ("start", "dynamic.gguf"), + # What: arrange switch other gguf for the scenario; why: test router test dynamic profile port is stable while resident and fresh after a swap requires this concrete input or helper state before exercising the behavior under test. + ("switch", "other.gguf"), + # What: arrange switch dynamic gguf for the scenario; why: test router test dynamic profile port is stable while resident and fresh after a swap requires this concrete input or helper state before exercising the behavior under test. + ("switch", "dynamic.gguf"), + # What: arrange the grouped source fragment for the scenario; why: test router test dynamic profile port is stable while resident and fresh after a swap requires this concrete input or helper state before exercising the behavior under test. + ] + + +# What: parameterize test_router_binds_unambiguous_exact_manager_re_adoption with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test router binds unambiguous exact manager re adoption. +@pytest.mark.parametrize("configured_port", [1919, 0, None]) +# What: define the test_router_binds_unambiguous_exact_manager_re_adoption test around configured port; why: this test groups the arrange, act, and assertions that protect the router binds unambiguous exact manager re adoption outcome. +def test_router_binds_unambiguous_exact_manager_re_adoption(configured_port): + # What: act by calling Manager and capture manager; why: the router binds unambiguous exact manager re adoption test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: arrange model as adopted and gguf; why: the router binds unambiguous exact manager re adoption test consumes this named precondition before exercising the behavior. + manager.model = "adopted.gguf" + # What: arrange port as 1919; why: the router binds unambiguous exact manager re adoption test consumes this named precondition before exercising the behavior. + manager.port = 1919 + # What: arrange args as served model name and adopted; why: the router binds unambiguous exact manager re adoption test consumes this named precondition before exercising the behavior. + manager.args = ["--served-model-name", "adopted"] + # What: act by calling ModelCatalog and capture catalog doc; why: the router binds unambiguous exact manager re adoption test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({ + # What: arrange the adopted field as model profile and tuple and args and configured port; why: test_router_binds_unambiguous_exact_manager_re_adoption carries adopted through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "adopted": ModelProfile( + # What: arrange port to tuple; why: the router binds unambiguous exact manager re adoption scenario binds this configured port value to tuple's port input. + "adopted", "adopted.gguf", tuple(manager.args), port=configured_port + # What: arrange the ModelProfile call with port; why: test_router_binds_unambiguous_exact_manager_re_adoption groups the supplied clauses as one ModelProfile call before its value is consumed. + ), + # What: arrange the ModelCatalog call with model profile; why: test_router_binds_unambiguous_exact_manager_re_adoption groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + + # What: act by calling RoutingCoordinator and capture router; why: the router binds unambiguous exact manager re adoption test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: assert that router status active profile equals adopted; why: this assertion protects the router binds unambiguous exact manager re adoption regression after the test's arranged inputs and exercised call. + assert router.status()["activeProfile"] == "adopted" + # What: assert that router status active identity matches engine is true; why: this assertion protects the router binds unambiguous exact manager re adoption regression after the test's arranged inputs and exercised call. + assert router.status()["activeIdentityMatchesEngine"] is True + # What: act by calling router.acquire and capture lease; why: the router binds unambiguous exact manager re adoption test asserts the response, state, or failure produced by this call. + lease = router.acquire("adopted") + # What: act by calling lease.release with the declared inputs; why: the router binds unambiguous exact manager re adoption scenario observes the lease.release return value during assert manager calls. + lease.release() + # What: assert that manager calls equals group delimiter; why: this assertion protects the router binds unambiguous exact manager re adoption regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + # What: assert that router status activations equals 0; why: this assertion protects the router binds unambiguous exact manager re adoption regression after the test's arranged inputs and exercised call. + assert router.status()["activations"] == 0 + + +# What: define the test_router_refuses_ambiguous_or_argument_mismatched_re_adoption test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router refuses ambiguous or argument mismatched re adoption outcome. +def test_router_refuses_ambiguous_or_argument_mismatched_re_adoption(): + # What: act by calling Manager and capture manager; why: the router refuses ambiguous or argument mismatched re adoption test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: arrange model as shared and gguf; why: the router refuses ambiguous or argument mismatched re adoption test consumes this named precondition before exercising the behavior. + manager.model = "shared.gguf" + # What: arrange port as 1919; why: the router refuses ambiguous or argument mismatched re adoption test consumes this named precondition before exercising the behavior. + manager.port = 1919 + # What: arrange args as actual; why: the router refuses ambiguous or argument mismatched re adoption test consumes this named precondition before exercising the behavior. + manager.args = ["--actual"] + # What: act by calling ModelCatalog and capture ambiguous; why: the router refuses ambiguous or argument mismatched re adoption test asserts the response, state, or failure produced by this call. + ambiguous = ModelCatalog({ + # What: arrange the one field as model profile and tuple and args and manager and one; why: test_router_refuses_ambiguous_or_argument_mismatched_re_adoption carries one through ambiguous into assert routing coordinator manager ambiguous object ready fn ready. + "one": ModelProfile("one", "shared.gguf", tuple(manager.args), port=0), + # What: arrange the two field as model profile and tuple and args and manager and two; why: test_router_refuses_ambiguous_or_argument_mismatched_re_adoption carries two through ambiguous into assert routing coordinator manager ambiguous object ready fn ready. + "two": ModelProfile("two", "shared.gguf", tuple(manager.args), port=0), + # What: arrange the ModelCatalog call with model profile; why: test_router_refuses_ambiguous_or_argument_mismatched_re_adoption groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: act by calling ModelCatalog and capture mismatched; why: the router refuses ambiguous or argument mismatched re adoption test asserts the response, state, or failure produced by this call. + mismatched = ModelCatalog({ + # What: arrange the one field as model profile and one and shared and gguf and different; why: test_router_refuses_ambiguous_or_argument_mismatched_re_adoption carries one through mismatched into assert routing coordinator manager mismatched object ready fn ready. + "one": ModelProfile("one", "shared.gguf", ("--different",), port=1919), + # What: arrange the ModelCatalog call with model profile; why: test_router_refuses_ambiguous_or_argument_mismatched_re_adoption groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + + # What: assert that routing coordinator manager ambiguous object ready fn ready is group delimiter; why: this assertion protects the router refuses ambiguous or argument mismatched re adoption regression after the test's arranged inputs and exercised call. + assert RoutingCoordinator(manager, ambiguous, object(), ready_fn=ready).status()["activeProfile"] is None + # What: assert that routing coordinator manager mismatched object ready fn ready is group delimiter; why: this assertion protects the router refuses ambiguous or argument mismatched re adoption regression after the test's arranged inputs and exercised call. + assert RoutingCoordinator(manager, mismatched, object(), ready_fn=ready).status()["activeProfile"] is None + + +# What: define the test_switch_waits_until_an_active_lease_finishes test around local fixtures; why: this test groups the arrange, act, and assertions that protect the switch waits until an active lease finishes outcome. +def test_switch_waits_until_an_active_lease_finishes(): + # What: act by calling Manager and capture manager; why: the switch waits until an active lease finishes test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the switch waits until an active lease finishes test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: act by calling router.acquire and capture lease; why: the switch waits until an active lease finishes test asserts the response, state, or failure produced by this call. + lease = router.acquire("low") + # What: act by calling threading.Event and capture entered; why: the switch waits until an active lease finishes test asserts the response, state, or failure produced by this call. + entered = threading.Event() + # What: act by calling threading.Event and capture released; why: the switch waits until an active lease finishes test asserts the response, state, or failure produced by this call. + released = threading.Event() + # What: arrange result as the fixture input; why: the switch waits until an active lease finishes test consumes this named precondition before exercising the behavior. + result = [] + + # What: define the acquire_high test helper around captured fixture state; why: the switch waits until an active lease finishes scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def acquire_high(): + # What: act by calling entered.set with the declared inputs; why: the switch waits until an active lease finishes scenario observes the entered.set return value during held router acquire high. + entered.set() + # What: act by calling router.acquire and capture held; why: the switch waits until an active lease finishes test asserts the response, state, or failure produced by this call. + held = router.acquire("high") + # What: act by calling result.append with held; why: the switch waits until an active lease finishes scenario observes the result.append return value during released set. + result.append(held) + # What: act by calling released.set with the declared inputs; why: the switch waits until an active lease finishes scenario observes the released.set return value during the enclosing return. + released.set() + + # What: act by calling threading.Thread and capture thread; why: the switch waits until an active lease finishes test asserts the response, state, or failure produced by this call. + thread = threading.Thread(target=acquire_high) + # What: act by calling thread.start with the declared inputs; why: the switch waits until an active lease finishes scenario observes the thread.start return value during assert entered wait. + thread.start() + # What: assert that entered wait 1; why: this assertion protects the switch waits until an active lease finishes regression after the test's arranged inputs and exercised call. + assert entered.wait(1) + # What: assert that released wait 0 05 is false; why: this assertion protects the switch waits until an active lease finishes regression after the test's arranged inputs and exercised call. + assert not released.wait(0.05) + # What: act by calling lease.release with the declared inputs; why: the switch waits until an active lease finishes scenario observes the lease.release return value during assert released wait. + lease.release() + # What: assert that released wait 1; why: this assertion protects the switch waits until an active lease finishes regression after the test's arranged inputs and exercised call. + assert released.wait(1) + # What: act by calling operation.release with the declared inputs; why: the switch waits until an active lease finishes scenario observes the operation.release return value during thread join. + result.pop().release() + # What: act by calling thread.join with 1; why: the switch waits until an active lease finishes scenario observes the thread.join return value during assert manager calls start low gguf switch high gguf. + thread.join(1) + # What: assert that manager calls equals start low gguf switch high gguf; why: this assertion protects the switch waits until an active lease finishes regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf"), ("switch", "high.gguf")] + + +# What: define the test_cancelled_queued_http_request_cannot_trigger_a_later_swap test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the cancelled queued http request cannot trigger a later swap outcome. +def test_cancelled_queued_http_request_cannot_trigger_a_later_swap(monkeypatch): + # What: act by calling Manager and capture manager; why: the cancelled queued http request cannot trigger a later swap test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the cancelled queued http request cannot trigger a later swap test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the cancelled queued http request cannot trigger a later swap test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: arrange monkeypatch setattr for the scenario; why: test cancelled queued http request cannot trigger a later swap requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the cancelled queued http request cannot trigger a later swap scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs pytest fail cancelled queued request fixture fragment; why: the cancelled queued http request cannot trigger a later swap scenario feeds this byte-preserved fragment through lambda **kwargs: pytest.fail("cancelled queued request reached upstream" before asserting its pro. + lambda **kwargs: pytest.fail("cancelled queued request reached upstream"), + # What: arrange the monkeypatch.setattr call with fail; why: test_cancelled_queued_http_request_cannot_trigger_a_later_swap groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: define the scenario test helper around app; why: the cancelled queued http request cannot trigger a later swap scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def scenario(app): + # What: act by calling httpx.ASGITransport and capture transport; why: the cancelled queued http request cannot trigger a later swap test asserts the response, state, or failure produced by this call. + transport = httpx.ASGITransport(app=app) + # What: arrange async with httpx AsyncClient transport transport base url http test as client for the scenario; why: test cancelled queued http request cannot trigger requires this concrete input or helper state before exercising the behavior under test. + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: + # What: act by calling asyncio.create_task and capture request; why: the cancelled queued http request cannot trigger a later swap test asserts the response, state, or failure produced by this call. + request = asyncio.create_task(client.post( + # What: arrange the model field as high; why: scenario sends this field through request so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "high"}, + # What: arrange the x ft request id field as cancelled while queued; why: scenario carries x ft request id through request into request cancel. + headers={"X-FT-Request-ID": "cancelled-while-queued"}, + # What: arrange the asyncio.create_task call with post; why: scenario groups the supplied clauses as one asyncio.create_task call before its value is consumed. + )) + # What: act across range to perform status and router; why: the cancelled queued http request cannot trigger a later swap scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: arrange if router status queuedRequests == 1 for the scenario; why: test router test cancelled queued http request cannot trigger a later swap requires this concrete input or helper state before exercising the behavior under test. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the cancelled queued http request cannot trigger a later swap scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the cancelled queued http request cannot trigger a later swap scenario observes the asyncio.sleep return value during assert router status queued requests. + await asyncio.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the cancelled queued http request cannot trigger a later swap regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: act by calling request.cancel with the declared inputs; why: the cancelled queued http request cannot trigger a later swap scenario observes the request.cancel return value during with pytest raises asyncio cancelled error. + request.cancel() + # What: assert the pytest.raises failure context; why: the cancelled queued http request cannot trigger a later swap scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(asyncio.CancelledError): + # What: arrange the await request portion of the enclosing predicate; why: this clause remains in the cancelled queued http request cannot trigger a later swap scenario\'s enclosing expression so its grouping and evaluation order stay intact. + await request + # What: act across range to perform status and router; why: the cancelled queued http request cannot trigger a later swap scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: arrange if router status queuedRequests == 0 for the scenario; why: test router test cancelled queued http request cannot trigger a later swap requires this concrete input or helper state before exercising the behavior under test. + if router.status()["queuedRequests"] == 0: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the cancelled queued http request cannot trigger a later swap scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the cancelled queued http request cannot trigger a later swap scenario observes the asyncio.sleep return value during assert router status queued requests. + await asyncio.sleep(0.01) + # What: assert that router status queued requests equals 0; why: this assertion protects the cancelled queued http request cannot trigger a later swap regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 0 + # What: assert that await client get router requests json data equals group delimiter; why: this assertion protects the cancelled queued http request cannot trigger a later swap regression after the test's arranged inputs and exercised call. + assert (await client.get("/router/requests")).json()["data"] == [] + # What: act by calling client.post and capture retry; why: the cancelled queued http request cannot trigger a later swap test asserts the response, state, or failure produced by this call. + retry = await client.post( + # What: arrange the model field as missing; why: scenario sends this field through retry so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "missing"}, + # What: arrange the x ft request id field as cancelled while queued; why: scenario carries x ft request id through retry into assert retry status code equals 404. + headers={"X-FT-Request-ID": "cancelled-while-queued"}, + # What: arrange the client.post call with json and headers; why: scenario groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: assert that retry status code equals 404; why: this assertion protects the cancelled queued http request cannot trigger a later swap regression after the test's arranged inputs and exercised call. + assert retry.status_code == 404 + # What: assert that retry json error type equals unknown model; why: this assertion protects the cancelled queued http request cannot trigger a later swap regression after the test's arranged inputs and exercised call. + assert retry.json()["error"]["type"] == "unknown_model" + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_cancelled_queued_http_request_cannot_trigger_a_later_swap releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(2) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the cancelled queued http request cannot trigger a later swap test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_cancelled_queued_http_request_cannot_trigger_a_later_swap; why: test_cancelled_queued_http_request_cannot_trigger_a_later_swap consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to catalog; why: the cancelled queued http request cannot trigger a later swap scenario binds this lifecycle value to catalog's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog(), router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_cancelled_queued_http_request_cannot_trigger_a_later_swap groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling asyncio.run with scenario and app; why: the cancelled queued http request cannot trigger a later swap scenario observes the asyncio.run return value during active release. + asyncio.run(scenario(app)) + + # What: act by calling active.release with the declared inputs; why: the cancelled queued http request cannot trigger a later swap scenario observes the active.release return value during assert manager calls start low gguf. + active.release() + # What: assert that manager calls equals start low gguf; why: this assertion protects the cancelled queued http request cannot trigger a later swap regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + # What: assert that router status cancellations equals 1; why: this assertion protects the cancelled queued http request cannot trigger a later swap regression after the test's arranged inputs and exercised call. + assert router.status()["cancellations"] == 1 + # What: assert that router status active requests equals 0; why: this assertion protects the cancelled queued http request cannot trigger a later swap regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + + +# What: define the test_default_profile_concurrency_limit_is_shared_by_alternate_ids test around local fixtures; why: this test groups the arrange, act, and assertions that protect the default profile concurrency limit is shared by alternate ids outcome. +def test_default_profile_concurrency_limit_is_shared_by_alternate_ids(): + # What: act by calling Manager and capture manager; why: the default profile concurrency limit is shared by alternate ids test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelProfile and capture profile; why: the default profile concurrency limit is shared by alternate ids test asserts the response, state, or failure produced by this call. + profile = ModelProfile("low", "low.gguf", (), aliases=("alternate",)) + # What: act by calling RoutingCoordinator and capture router; why: the default profile concurrency limit is shared by alternate ids test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator( + # What: arrange the low field as profile; why: test_default_profile_concurrency_limit_is_shared_by_alternate_ids carries low through router into leases router acquire low for value in range. + manager, ModelCatalog({"low": profile}), object(), ready_fn=ready + # What: arrange the RoutingCoordinator call with ready fn; why: test_default_profile_concurrency_limit_is_shared_by_alternate_ids groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + # What: act by calling router.acquire and capture leases; why: the default profile concurrency limit is shared by alternate ids test asserts the response, state, or failure produced by this call. + leases = [router.acquire("low") for _ in range(10)] + + # What: arrange with pytest raises RoutingError match concurrency limit as exc for the scenario; why: test raises routing error match concurrency limit as exc requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(RoutingError, match="concurrency limit") as exc: + # What: arrange the exact router acquire alternate fixture fragment; why: the default profile concurrency limit is shared by alternate ids scenario feeds this byte-preserved fragment through router.acquire("alternate") before asserting its protocol or parser result. + router.acquire("alternate") + # What: assert that exc value status code equals 429; why: this assertion protects the default profile concurrency limit is shared by alternate ids regression after the test's arranged inputs and exercised call. + assert exc.value.status_code == 429 + # What: assert that exc value code equals concurrency limit; why: this assertion protects the default profile concurrency limit is shared by alternate ids regression after the test's arranged inputs and exercised call. + assert exc.value.code == "concurrency_limit" + # What: assert that router status reserved requests equals 10; why: this assertion protects the default profile concurrency limit is shared by alternate ids regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 10 + # What: assert that router status queued requests equals 0; why: this assertion protects the default profile concurrency limit is shared by alternate ids regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 0 + + # What: act by calling leases.release with the declared inputs; why: the default profile concurrency limit is shared by alternate ids scenario observes the leases.release return value during replacement router acquire alternate. + leases[0].release() + # What: act by calling router.acquire and capture replacement; why: the default profile concurrency limit is shared by alternate ids test asserts the response, state, or failure produced by this call. + replacement = router.acquire("alternate") + # What: arrange with pytest raises ValueError match already released for the scenario; why: test raises value error match already released requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(ValueError, match="already released"): + # What: act by calling leases.release with the declared inputs; why: the default profile concurrency limit is shared by alternate ids scenario observes the leases.release return value during assert router status reserved requests. + leases[0].release() + # What: assert that router status reserved requests equals 10; why: this assertion protects the default profile concurrency limit is shared by alternate ids regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 10 + # What: act by calling replacement.release with the declared inputs; why: the default profile concurrency limit is shared by alternate ids scenario observes the replacement.release return value during for lease in leases. + replacement.release() + # What: act across leases to perform release and lease; why: the default profile concurrency limit is shared by alternate ids scenario repeats the body only while or for the loop header admits an iteration. + for lease in leases[1:]: + # What: act by calling lease.release with the declared inputs; why: the default profile concurrency limit is shared by alternate ids scenario observes the lease.release return value during assert router status reserved requests. + lease.release() + # What: assert that router status reserved requests equals 0; why: this assertion protects the default profile concurrency limit is shared by alternate ids regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 0 + # What: assert that manager calls equals start low gguf; why: this assertion protects the default profile concurrency limit is shared by alternate ids regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + + +# What: define the test_global_concurrency_limit_rejects_conflicting_model_before_it_queues test around local fixtures; why: this test groups the arrange, act, and assertions that protect the global concurrency limit rejects conflicting model before it queues outcome. +def test_global_concurrency_limit_rejects_conflicting_model_before_it_queues(): + # What: act by calling Manager and capture manager; why: the global concurrency limit rejects conflicting model before it queues test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the global concurrency limit rejects conflicting model before it queues test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with low and high; why: test_global_concurrency_limit_rejects_conflicting_model_before_it_queues groups the supplied clauses as one catalog_doc mapping before its value. + { + # What: arrange the low field as model profile and low and low and gguf; why: test_global_concurrency_limit_rejects_conflicting_model_before_it_queues carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "low": ModelProfile("low", "low.gguf", ()), + # What: arrange the high field as model profile and high and high and gguf; why: test_global_concurrency_limit_rejects_conflicting_model_before_it_queues carries high through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "high": ModelProfile("high", "high.gguf", ()), + # What: arrange the catalog_doc mapping with low and high; why: test_global_concurrency_limit_rejects_conflicting_model_before_it_queues groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange settings to RouterSettings; why: the global concurrency limit rejects conflicting model before it queues scenario binds this router settings and 1 value to RouterSettings's settings input. + settings=RouterSettings(global_concurrency_limit=1), + # What: arrange the ModelCatalog call with settings; why: test_global_concurrency_limit_rejects_conflicting_model_before_it_queues groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the global concurrency limit rejects conflicting model before it queues test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling router.acquire and capture lease; why: the global concurrency limit rejects conflicting model before it queues test asserts the response, state, or failure produced by this call. + lease = router.acquire("low") + + # What: assert the pytest.raises failure context; why: the global concurrency limit rejects conflicting model before it queues scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RoutingError) as exc: + # What: arrange the exact router acquire high fixture fragment; why: the global concurrency limit rejects conflicting model before it queues scenario feeds this byte-preserved fragment through router.acquire("high") before asserting its protocol or parser result. + router.acquire("high") + # What: assert that exc value code exc value status code equals concurrency limit 429; why: this assertion protects the global concurrency limit rejects conflicting model before it queues regression after the test's arranged inputs and exercised call. + assert (exc.value.code, exc.value.status_code) == ("concurrency_limit", 429) + # What: assert that router status queued requests equals 0; why: this assertion protects the global concurrency limit rejects conflicting model before it queues regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 0 + # What: assert that router status reserved requests equals 1; why: this assertion protects the global concurrency limit rejects conflicting model before it queues regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 1 + # What: act by calling lease.release with the declared inputs; why: the global concurrency limit rejects conflicting model before it queues scenario observes the lease.release return value during the enclosing return. + lease.release() + + +# What: define the test_admission_reservation_reports_cold_queue_position_and_cleans_up_on_cancel test around local fixtures; why: this test groups the arrange, act, and assertions that protect the admission reservation reports cold queue position and cleans up on cancel outcome. +def test_admission_reservation_reports_cold_queue_position_and_cleans_up_on_cancel(): + # What: act by calling Manager and capture manager; why: the admission reservation reports cold queue position and cleans up on cancel test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the admission reservation reports cold queue position and cleans up on cancel test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the admission reservation reports cold queue position and cleans up on cancel test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: act by calling threading.Event and capture cancellation; why: the admission reservation reports cold queue position and cleans up on cancel test asserts the response, state, or failure produced by this call. + cancellation = threading.Event() + # What: act by calling threading.Event and capture reserved; why: the admission reservation reports cold queue position and cleans up on cancel test asserts the response, state, or failure produced by this call. + reserved = threading.Event() + # What: arrange cold as the fixture input; why: the admission reservation reports cold queue position and cleans up on cancel test consumes this named precondition before exercising the behavior. + cold = [] + # What: arrange errors as the fixture input; why: the admission reservation reports cold queue position and cleans up on cancel test consumes this named precondition before exercising the behavior. + errors = [] + + # What: define the acquire_high test helper around captured fixture state; why: the admission reservation reports cold queue position and cleans up on cancel scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def acquire_high(): + # What: establish the handler boundary for the protected operation; why: acquire_high routes failures to routing error while preserving cleanup and success flow. + try: + # What: act by calling router.acquire with high and cancellation and append and set and cold and loading required; why: the admission reservation reports cold queue position and cleans up on cancel scenario observes the router.acquire return value during high cancellation lambda loading required position. + router.acquire( + # What: arrange the exact high cancellation lambda loading required position fixture fragment; why: the admission reservation reports cold queue position and cleans up on cancel scenario feeds this byte-preserved fragment through "high", cancellation, lambda loading_required, position: ( before asserti. + "high", cancellation, lambda loading_required, position: ( + # What: act by calling cold.append with loading required and position; why: the admission reservation reports cold queue position and cleans up on cancel scenario observes the cold.append return value while evaluating cold.append((loading_required, position)), reserved.set(). + cold.append((loading_required, position)), reserved.set() + # What: arrange the enclosing predicate collection with append and cold and loading required and position and set and reserved; why: acquire_high groups the supplied clauses as one acquire_high expression collection before its value is consumed. + ), + # What: arrange the router.acquire call with cancellation and append; why: acquire_high groups the supplied clauses as one router.acquire call before its value is consumed. + ) + # What: handle routing error by errors append exc; why: acquire_high converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: act by calling errors.append with exc; why: the admission reservation reports cold queue position and cleans up on cancel scenario observes the errors.append return value during the enclosing return. + errors.append(exc) + + # What: act by calling threading.Thread and capture thread; why: the admission reservation reports cold queue position and cleans up on cancel test asserts the response, state, or failure produced by this call. + thread = threading.Thread(target=acquire_high) + # What: act by calling thread.start with the declared inputs; why: the admission reservation reports cold queue position and cleans up on cancel scenario observes the thread.start return value during assert reserved wait. + thread.start() + # What: assert that reserved wait 1; why: this assertion protects the admission reservation reports cold queue position and cleans up on cancel regression after the test's arranged inputs and exercised call. + assert reserved.wait(1) + # What: assert that cold equals true 1; why: this assertion protects the admission reservation reports cold queue position and cleans up on cancel regression after the test's arranged inputs and exercised call. + assert cold == [(True, 1)] + # What: assert that router queue position cancellation equals 1; why: this assertion protects the admission reservation reports cold queue position and cleans up on cancel regression after the test's arranged inputs and exercised call. + assert router.queue_position(cancellation) == 1 + # What: act by calling router.cancel_acquire with cancellation; why: the admission reservation reports cold queue position and cleans up on cancel scenario observes the router.cancel_acquire return value during assert router queue position cancellation is. + router.cancel_acquire(cancellation) + # What: assert that router queue position cancellation is group delimiter; why: this assertion protects the admission reservation reports cold queue position and cleans up on cancel regression after the test's arranged inputs and exercised call. + assert router.queue_position(cancellation) is None + # What: assert that router status queued requests equals 0; why: this assertion protects the admission reservation reports cold queue position and cleans up on cancel regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 0 + # What: assert the expected router status reservedRequests == 1 only the active low lease remains outcome; why: test admission reservation reports cold protects its regression by requiring this observable result after the exercised behavior. + assert router.status()["reservedRequests"] == 1 # only the active low lease remains + # What: act by calling thread.join with 1; why: the admission reservation reports cold queue position and cleans up on cancel scenario observes the thread.join return value during assert not thread is alive. + thread.join(1) + # What: assert that thread is alive is false; why: this assertion protects the admission reservation reports cold queue position and cleans up on cancel regression after the test's arranged inputs and exercised call. + assert not thread.is_alive() + # What: assert that error code error status code for error in errors equals request cancelled 409; why: this assertion protects the admission reservation reports cold queue position and cleans up on cancel regression after the test's arranged inputs and exercised call. + assert [(error.code, error.status_code) for error in errors] == [("request_cancelled", 409)] + # What: assert the expected router status reservedRequests == 1 outcome; why: test router test admission reservation reports cold queue position and cleans up on cancel protects its regression by requiring this observable result after the exercised behavior. + assert router.status()["reservedRequests"] == 1 + # What: act by calling active.release with the declared inputs; why: the admission reservation reports cold queue position and cleans up on cancel scenario observes the active.release return value during the enclosing return. + active.release() + + +# What: define the test_admission_reservation_reports_warm_and_callback_failure_releases_capacity test around local fixtures; why: this test groups the arrange, act, and assertions that protect the admission reservation reports warm and callback failure releases capacity outcome. +def test_admission_reservation_reports_warm_and_callback_failure_releases_capacity(): + # What: act by calling Manager and capture manager; why: the admission reservation reports warm and callback failure releases capacity test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the admission reservation reports warm and callback failure releases capacity test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: arrange the exact router acquire low release fixture fragment; why: the admission reservation reports warm and callback failure releases capacity scenario feeds this byte-preserved fragment through router.acquire("low").release() before asserting its protocol or parser result. + router.acquire("low").release() + # What: arrange observed as the fixture input; why: the admission reservation reports warm and callback failure releases capacity test consumes this named precondition before exercising the behavior. + observed = [] + # What: act by calling router.acquire and capture warm; why: the admission reservation reports warm and callback failure releases capacity test asserts the response, state, or failure produced by this call. + warm = router.acquire( + # What: arrange low threading Event lambda loading required position observed append for the scenario; why: test admission reservation reports warm and callback failure releases capacity requires this concrete input or helper state before exercising the behavior under test. + "low", threading.Event(), lambda loading_required, position: observed.append( + # What: arrange the loading required position portion of warm; why: the admission reservation reports warm and callback failure releases capacity scenario uses this clause to evaluate warm as one grouped value. + (loading_required, position) + # What: arrange the observed.append call with loading required; why: test_admission_reservation_reports_warm_and_callback_failure_releases_capacity groups the supplied clauses as one observed.append call before its value is consumed. + ) + # What: arrange the router.acquire call with event and append; why: test_admission_reservation_reports_warm_and_callback_failure_releases_capacity groups the supplied clauses as one router.acquire call before its value is consumed. + ) + # What: assert that observed equals false 1; why: this assertion protects the admission reservation reports warm and callback failure releases capacity regression after the test's arranged inputs and exercised call. + assert observed == [(False, 1)] + # What: act by calling warm.release with the declared inputs; why: the admission reservation reports warm and callback failure releases capacity scenario observes the warm.release return value during def fail loading required position. + warm.release() + + # What: define the fail test helper around loading required and position; why: the admission reservation reports warm and callback failure releases capacity scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def fail(_loading_required, _position): + # What: raise RuntimeError for the caller; why: fail stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("observer failed") + + # What: assert the pytest.raises failure context; why: the admission reservation reports warm and callback failure releases capacity scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError, match="observer failed"): + # What: arrange the exact router acquire low threading event fail fixture fragment; why: the admission reservation reports warm and callback failure releases capacity scenario feeds this byte-preserved fragment through router.acquire("low", threading.Event(), fail) before asserting its protocol or parser resul. + router.acquire("low", threading.Event(), fail) + # What: assert that router status reserved requests equals 0; why: this assertion protects the admission reservation reports warm and callback failure releases capacity regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 0 + # What: assert that router status queued requests equals 0; why: this assertion protects the admission reservation reports warm and callback failure releases capacity regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 0 + + +# What: define the test_http_concurrency_rejection_returns_retry_after_and_releases_request_id test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the http concurrency rejection returns retry after and releases request id outcome. +def test_http_concurrency_rejection_returns_retry_after_and_releases_request_id(monkeypatch): + # What: act by calling Manager and capture manager; why: the http concurrency rejection returns retry after and releases request id test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelProfile and capture profile; why: the http concurrency rejection returns retry after and releases request id test asserts the response, state, or failure produced by this call. + profile = ModelProfile("low", "low.gguf", (), concurrency_limit=1) + # What: act by calling ModelCatalog and capture catalog doc; why: the http concurrency rejection returns retry after and releases request id test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"low": profile}) + # What: act by calling RoutingCoordinator and capture router; why: the http concurrency rejection returns retry after and releases request id test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling router.acquire and capture lease; why: the http concurrency rejection returns retry after and releases request id test asserts the response, state, or failure produced by this call. + lease = router.acquire("low") + # What: arrange monkeypatch setattr for the scenario; why: test http concurrency rejection returns retry after and releases request id requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the http concurrency rejection returns retry after and releases request id scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs pytest fail over limit request reached fixture fragment; why: the http concurrency rejection returns retry after and releases request id scenario feeds this byte-preserved fragment through lambda **kwargs: pytest.fail("over-limit request reached upstream") before asserti. + lambda **kwargs: pytest.fail("over-limit request reached upstream"), + # What: arrange the monkeypatch.setattr call with fail; why: test_http_concurrency_rejection_returns_retry_after_and_releases_request_id groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_http_concurrency_rejection_returns_retry_after_and_releases_request_id releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the http concurrency rejection returns retry after and releases request id test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_http_concurrency_rejection_returns_retry_after_and_releases_request_id; why: test_http_concurrency_rejection_returns_retry_after_and_releases_request_id consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the http concurrency rejection returns retry after and releases request id scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_http_concurrency_rejection_returns_retry_after_and_releases_request_id groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the http concurrency rejection returns retry after and releases request id test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.post and capture rejected; why: the http concurrency rejection returns retry after and releases request id test asserts the response, state, or failure produced by this call. + rejected = client.post( + # What: arrange the v1 messages portion of rejected; why: the http concurrency rejection returns retry after and releases request id scenario uses this clause to evaluate rejected as one grouped value. + "/v1/messages", + # What: arrange the model field as low; why: test_http_concurrency_rejection_returns_retry_after_and_releases_request_id sends this field through rejected so the router selects the canonical model or alias for upstream dispatch. + json={"model": "low", "messages": []}, + # What: arrange the x ft request id field as over limit; why: test_http_concurrency_rejection_returns_retry_after_and_releases_request_id carries x ft request id through rejected into assert rejected status code equals 429. + headers={"X-FT-Request-ID": "over-limit"}, + # What: arrange the client.post call with json and headers; why: test_http_concurrency_rejection_returns_retry_after_and_releases_request_id groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: assert that client get router requests json data equals group delimiter; why: this assertion protects the http concurrency rejection returns retry after and releases request id regression after the test's arranged inputs and exercised call. + assert client.get("/router/requests").json()["data"] == [] + + # What: assert that rejected status code equals 429; why: this assertion protects the http concurrency rejection returns retry after and releases request id regression after the test's arranged inputs and exercised call. + assert rejected.status_code == 429 + # What: assert that rejected headers retry after equals 1; why: this assertion protects the http concurrency rejection returns retry after and releases request id regression after the test's arranged inputs and exercised call. + assert rejected.headers["retry-after"] == "1" + # What: assert that rejected json error type equals concurrency limit; why: this assertion protects the http concurrency rejection returns retry after and releases request id regression after the test's arranged inputs and exercised call. + assert rejected.json()["error"]["type"] == "concurrency_limit" + # What: assert that router status reserved requests equals 1; why: this assertion protects the http concurrency rejection returns retry after and releases request id regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 1 + # What: act by calling lease.release with the declared inputs; why: the http concurrency rejection returns retry after and releases request id scenario observes the lease.release return value during the enclosing return. + lease.release() + + +# What: define the test_dynamic_port_failure_releases_concurrency_reservation test around local fixtures; why: this test groups the arrange, act, and assertions that protect the dynamic port failure releases concurrency reservation outcome. +def test_dynamic_port_failure_releases_concurrency_reservation(): + # What: act by calling RoutingCoordinator and capture router; why: the dynamic port failure releases concurrency reservation test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator( + # What: act by calling Manager with the declared inputs; why: the dynamic port failure releases concurrency reservation scenario observes the Manager return value during model catalog dynamic model profile dynamic dynamic gguf port. + Manager(), + # What: arrange the dynamic field as model profile and dynamic and dynamic and gguf and 0; why: test_dynamic_port_failure_releases_concurrency_reservation carries dynamic through router into router acquire dynamic. + ModelCatalog({"dynamic": ModelProfile("dynamic", "dynamic.gguf", (), port=0)}), + # What: act by calling object with the declared inputs; why: the dynamic port failure releases concurrency reservation scenario observes the object return value during ready fn ready. + object(), + # What: arrange ready fn to RoutingCoordinator; why: the dynamic port failure releases concurrency reservation scenario binds this ready value to RoutingCoordinator's ready fn input. + ready_fn=ready, + # What: arrange port allocator to operation.throw; why: the dynamic port failure releases concurrency reservation scenario binds this throw and oserror and value and no and port value to operation.throw's port allocator input. + port_allocator=lambda: (_ for _ in ()).throw(OSError("no port")), + # What: arrange the RoutingCoordinator call with ready fn and port allocator; why: test_dynamic_port_failure_releases_concurrency_reservation groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + + # What: assert the pytest.raises failure context; why: the dynamic port failure releases concurrency reservation scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(OSError, match="no port"): + # What: arrange the exact router acquire dynamic fixture fragment; why: the dynamic port failure releases concurrency reservation scenario feeds this byte-preserved fragment through router.acquire("dynamic") before asserting its protocol or parser result. + router.acquire("dynamic") + # What: assert that router status reserved requests equals 0; why: this assertion protects the dynamic port failure releases concurrency reservation regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 0 + # What: assert that router status queued requests equals 0; why: this assertion protects the dynamic port failure releases concurrency reservation regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 0 + + +# What: define the test_concurrent_cold_dynamic_requests_share_one_head_ticket_port test around local fixtures; why: this test groups the arrange, act, and assertions that protect the concurrent cold dynamic requests share one head ticket port outcome. +def test_concurrent_cold_dynamic_requests_share_one_head_ticket_port(): + # What: act by calling Manager and capture manager; why: the concurrent cold dynamic requests share one head ticket port test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling threading.Event and capture activation started; why: the concurrent cold dynamic requests share one head ticket port test asserts the response, state, or failure produced by this call. + activation_started = threading.Event() + # What: act by calling threading.Event and capture finish activation; why: the concurrent cold dynamic requests share one head ticket port test asserts the response, state, or failure produced by this call. + finish_activation = threading.Event() + # What: arrange allocated as the fixture input; why: the concurrent cold dynamic requests share one head ticket port test consumes this named precondition before exercising the behavior. + allocated = [] + + # What: define the allocate test helper around captured fixture state; why: the concurrent cold dynamic requests share one head ticket port scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def allocate(): + # What: act by calling len and capture port; why: the concurrent cold dynamic requests share one head ticket port test asserts the response, state, or failure produced by this call. + port = 21000 + len(allocated) + # What: act by calling allocated.append with port; why: the concurrent cold dynamic requests share one head ticket port scenario observes the allocated.append return value during return port. + allocated.append(port) + # What: return port from the allocate test helper; why: the concurrent cold dynamic requests share one head ticket port scenario uses this helper result in its subsequent act or assertion. + return port + + # What: define the blocking_ready test helper around manager and probe and pid and port and timeout s; why: the concurrent cold dynamic requests share one head ticket port scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def blocking_ready(manager, probe, *, pid, port, timeout_s): + # What: act by calling activation_started.set with the declared inputs; why: the concurrent cold dynamic requests share one head ticket port scenario observes the activation_started.set return value during assert finish activation wait. + activation_started.set() + # What: assert that finish activation wait 2; why: this assertion protects the concurrent cold dynamic requests share one head ticket port regression after the test's arranged inputs and exercised call. + assert finish_activation.wait(2) + # What: arrange the ready field as true; why: blocking_ready carries ready into return {"ready": True}. + return {"ready": True} + + # What: act by calling RoutingCoordinator and capture router; why: the concurrent cold dynamic requests share one head ticket port test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator( + # What: arrange the manager portion of router; why: the concurrent cold dynamic requests share one head ticket port scenario uses this clause to evaluate router as one grouped value. + manager, + # What: arrange the dynamic field as model profile and dynamic and dynamic and gguf and 0; why: test_concurrent_cold_dynamic_requests_share_one_head_ticket_port carries dynamic through router into first threading thread target lambda leases append router acquire dynamic. + ModelCatalog({"dynamic": ModelProfile("dynamic", "dynamic.gguf", (), port=0)}), + # What: act by calling object with the declared inputs; why: the concurrent cold dynamic requests share one head ticket port scenario observes the object return value during ready fn blocking ready. + object(), + # What: arrange ready fn to RoutingCoordinator; why: the concurrent cold dynamic requests share one head ticket port scenario binds this blocking ready value to RoutingCoordinator's ready fn input. + ready_fn=blocking_ready, + # What: arrange port allocator to RoutingCoordinator; why: the concurrent cold dynamic requests share one head ticket port scenario binds this allocate value to RoutingCoordinator's port allocator input. + port_allocator=allocate, + # What: arrange the RoutingCoordinator call with ready fn and port allocator; why: test_concurrent_cold_dynamic_requests_share_one_head_ticket_port groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + # What: arrange leases as the fixture input; why: the concurrent cold dynamic requests share one head ticket port test consumes this named precondition before exercising the behavior. + leases = [] + # What: act by calling threading.Thread and capture first; why: the concurrent cold dynamic requests share one head ticket port test asserts the response, state, or failure produced by this call. + first = threading.Thread(target=lambda: leases.append(router.acquire("dynamic"))) + # What: act by calling threading.Thread and capture second; why: the concurrent cold dynamic requests share one head ticket port test asserts the response, state, or failure produced by this call. + second = threading.Thread(target=lambda: leases.append(router.acquire("dynamic"))) + # What: act by calling first.start with the declared inputs; why: the concurrent cold dynamic requests share one head ticket port scenario observes the first.start return value during assert activation started wait. + first.start() + # What: assert that activation started wait 1; why: this assertion protects the concurrent cold dynamic requests share one head ticket port regression after the test's arranged inputs and exercised call. + assert activation_started.wait(1) + # What: act by calling second.start with the declared inputs; why: the concurrent cold dynamic requests share one head ticket port scenario observes the second.start return value during for value in range. + second.start() + # What: act across range to perform status and router; why: the concurrent cold dynamic requests share one head ticket port scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the concurrent cold dynamic requests share one head ticket port scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the concurrent cold dynamic requests share one head ticket port scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the concurrent cold dynamic requests share one head ticket port scenario observes the time.sleep return value during assert router status queued requests. + time.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the concurrent cold dynamic requests share one head ticket port regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: act by calling finish_activation.set with the declared inputs; why: the concurrent cold dynamic requests share one head ticket port scenario observes the finish_activation.set return value during first join. + finish_activation.set() + # What: act by calling first.join with 2; why: the concurrent cold dynamic requests share one head ticket port scenario observes the first.join return value during second join. + first.join(2) + # What: act by calling second.join with 2; why: the concurrent cold dynamic requests share one head ticket port scenario observes the second.join return value during assert not first is alive and not second is alive. + second.join(2) + + # What: assert that not first is alive and not second is alive; why: this assertion protects the concurrent cold dynamic requests share one head ticket port regression after the test's arranged inputs and exercised call. + assert not first.is_alive() and not second.is_alive() + # What: assert that allocated equals 21000; why: this assertion protects the concurrent cold dynamic requests share one head ticket port regression after the test's arranged inputs and exercised call. + assert allocated == [21000] + # What: assert that lease port for lease in leases equals 21000 21000; why: this assertion protects the concurrent cold dynamic requests share one head ticket port regression after the test's arranged inputs and exercised call. + assert [lease.port for lease in leases] == [21000, 21000] + # What: assert that manager calls equals start dynamic gguf; why: this assertion protects the concurrent cold dynamic requests share one head ticket port regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "dynamic.gguf")] + # What: act across leases to perform release and lease; why: the concurrent cold dynamic requests share one head ticket port scenario repeats the body only while or for the loop header admits an iteration. + for lease in leases: + # What: act by calling lease.release with the declared inputs; why: the concurrent cold dynamic requests share one head ticket port scenario observes the lease.release return value during the enclosing return. + lease.release() + + +# What: define the test_explicit_cancel_removes_a_queued_request_before_it_can_swap test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the explicit cancel removes a queued request before it can swap outcome. +def test_explicit_cancel_removes_a_queued_request_before_it_can_swap(monkeypatch): + # What: act by calling Manager and capture manager; why: the explicit cancel removes a queued request before it can swap test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the explicit cancel removes a queued request before it can swap test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the explicit cancel removes a queued request before it can swap test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: arrange monkeypatch setattr for the scenario; why: test explicit cancel removes a queued request before it can swap requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the explicit cancel removes a queued request before it can swap scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs pytest fail cancelled queued request fixture fragment; why: the explicit cancel removes a queued request before it can swap scenario feeds this byte-preserved fragment through lambda **kwargs: pytest.fail("cancelled queued request reached upstream" before asserting its p. + lambda **kwargs: pytest.fail("cancelled queued request reached upstream"), + # What: arrange the monkeypatch.setattr call with fail; why: test_explicit_cancel_removes_a_queued_request_before_it_can_swap groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: define the scenario test helper around app; why: the explicit cancel removes a queued request before it can swap scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def scenario(app): + # What: act by calling httpx.ASGITransport and capture transport; why: the explicit cancel removes a queued request before it can swap test asserts the response, state, or failure produced by this call. + transport = httpx.ASGITransport(app=app) + # What: arrange async with httpx AsyncClient transport transport base url http test as client for the scenario; why: test explicit cancel removes a queued request requires this concrete input or helper state before exercising the behavior under test. + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: + # What: act by calling asyncio.create_task and capture request; why: the explicit cancel removes a queued request before it can swap test asserts the response, state, or failure produced by this call. + request = asyncio.create_task(client.post( + # What: arrange the model field as high; why: scenario sends this field through request so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "high"}, + # What: arrange the x ft request id field as operator cancelled queue; why: scenario carries x ft request id through request into response await asyncio wait for request 1. + headers={"X-FT-Request-ID": "operator-cancelled-queue"}, + # What: arrange the grouped source fragment for the scenario; why: test router test explicit cancel removes a queued request before it can swap requires this concrete input. + )) + # What: act across range to perform status and router; why: the explicit cancel removes a queued request before it can swap scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the explicit cancel removes a queued request before it can swap scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the explicit cancel removes a queued request before it can swap scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the explicit cancel removes a queued request before it can swap scenario observes the asyncio.sleep return value during assert router status queued requests. + await asyncio.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the explicit cancel removes a queued request before it can swap regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: assert the expected await client get router requests json data == outcome; why: test router test explicit cancel removes a queued request before it can swap protects its regression by requiring this observable result after the exercised behavior. + assert (await client.get("/router/requests")).json()["data"] == [ + # What: arrange id operator cancelled queue profile high for the scenario; why: test router test explicit cancel removes a queued request before it can swap requires this concrete input or helper state before exercising the behavior under test. + {"id": "operator-cancelled-queue", "profile": "high"} + # What: arrange the grouped source fragment for the scenario; why: test router test explicit cancel removes a queued request before it can swap requires this concrete. + ] + # What: act by calling client.post and capture cancelled; why: the explicit cancel removes a queued request before it can swap test asserts the response, state, or failure produced by this call. + cancelled = await client.post( + # What: arrange the router requests operator cancelled queue cancel portion of cancelled; why: the explicit cancel removes a queued request before it can swap scenario uses this clause to evaluate cancelled as one grouped value. + "/router/requests/operator-cancelled-queue/cancel" + # What: arrange the client.post call with ordered positional inputs; why: scenario groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: assert the expected cancelled json == outcome; why: test router test explicit cancel removes a queued request before it can swap protects its regression by requiring this observable result after the exercised behavior. + assert cancelled.json() == { + # What: arrange cancelled True id operator cancelled queue for the scenario; why: test router test explicit cancel removes a queued request before it can swap requires this concrete input or helper state before exercising the behavior under test. + "cancelled": True, "id": "operator-cancelled-queue" + # What: arrange the grouped source fragment for the scenario; why: test router test explicit cancel removes a queued request before it can swap requires this concrete input. + } + # What: act by calling client.post and capture repeated; why: the explicit cancel removes a queued request before it can swap test asserts the response, state, or failure produced by this call. + repeated = await client.post( + # What: arrange the router requests operator cancelled queue cancel portion of repeated; why: the explicit cancel removes a queued request before it can swap scenario uses this clause to evaluate repeated as one grouped value. + "/router/requests/operator-cancelled-queue/cancel" + # What: arrange the client.post call with ordered positional inputs; why: scenario groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: assert that repeated json equals cancelled false reason not found; why: this assertion protects the explicit cancel removes a queued request before it can swap regression after the test's arranged inputs and exercised call. + assert repeated.json() == {"cancelled": False, "reason": "not_found"} + # What: act by calling asyncio.wait_for and capture response; why: the explicit cancel removes a queued request before it can swap test asserts the response, state, or failure produced by this call. + response = await asyncio.wait_for(request, 1) + # What: assert that response status code equals 409; why: this assertion protects the explicit cancel removes a queued request before it can swap regression after the test's arranged inputs and exercised call. + assert response.status_code == 409 + # What: assert that response json error type equals request cancelled; why: this assertion protects the explicit cancel removes a queued request before it can swap regression after the test's arranged inputs and exercised call. + assert response.json()["error"]["type"] == "request_cancelled" + # What: assert that await client get router requests json data equals group delimiter; why: this assertion protects the explicit cancel removes a queued request before it can swap regression after the test's arranged inputs and exercised call. + assert (await client.get("/router/requests")).json()["data"] == [] + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_explicit_cancel_removes_a_queued_request_before_it_can_swap releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(2) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the explicit cancel removes a queued request before it can swap test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_explicit_cancel_removes_a_queued_request_before_it_can_swap; why: test_explicit_cancel_removes_a_queued_request_before_it_can_swap consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to catalog; why: the explicit cancel removes a queued request before it can swap scenario binds this lifecycle value to catalog's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog(), router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_explicit_cancel_removes_a_queued_request_before_it_can_swap groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling asyncio.run with scenario and app; why: the explicit cancel removes a queued request before it can swap scenario observes the asyncio.run return value during active release. + asyncio.run(scenario(app)) + + # What: act by calling active.release with the declared inputs; why: the explicit cancel removes a queued request before it can swap scenario observes the active.release return value during assert manager calls start low gguf. + active.release() + # What: assert that manager calls equals start low gguf; why: this assertion protects the explicit cancel removes a queued request before it can swap regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + # What: assert that router status queued requests equals 0; why: this assertion protects the explicit cancel removes a queued request before it can swap regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 0 + # What: assert that router status active requests equals 0; why: this assertion protects the explicit cancel removes a queued request before it can swap regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + # What: assert that router status cancellations equals 1; why: this assertion protects the explicit cancel removes a queued request before it can swap regression after the test's arranged inputs and exercised call. + assert router.status()["cancellations"] == 1 + + +# What: define the test_queued_higher_priority_profile_runs_before_an_earlier_lower_priority_request test around local fixtures; why: this test groups the arrange, act, and assertions that protect the queued higher priority profile runs before an earlier lower priority request outcome. +def test_queued_higher_priority_profile_runs_before_an_earlier_lower_priority_request(): + # What: act by calling Manager and capture manager; why: the queued higher priority profile runs before an earlier lower priority request test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the queued higher priority profile runs before an earlier lower priority request test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({ + # What: arrange the active field as model profile and active and active and gguf and 0; why: test_queued_higher_priority_profile_runs_before_an_earlier_lower_priority_request carries active through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "active": ModelProfile("active", "active.gguf", (), priority=0), + # What: arrange the low field as model profile and low and low and gguf and 0; why: test_queued_higher_priority_profile_runs_before_an_earlier_lower_priority_request carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "low": ModelProfile("low", "low.gguf", (), priority=0), + # What: arrange the high field as model profile and high and high and gguf and 10; why: test_queued_higher_priority_profile_runs_before_an_earlier_lower_priority_request carries high through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "high": ModelProfile("high", "high.gguf", (), priority=10), + # What: arrange the ModelCatalog call with model profile; why: test_queued_higher_priority_profile_runs_before_an_earlier_lower_priority_request groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: act by calling RoutingCoordinator and capture router; why: the queued higher priority profile runs before an earlier lower priority request test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the queued higher priority profile runs before an earlier lower priority request test asserts the response, state, or failure produced by this call. + active = router.acquire("active") + # What: arrange completed as the fixture input; why: the queued higher priority profile runs before an earlier lower priority request test consumes this named precondition before exercising the behavior. + completed = [] + + # What: define the acquire_then_release test helper around name; why: the queued higher priority profile runs before an earlier lower priority request scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def acquire_then_release(name): + # What: act by calling router.acquire and capture lease; why: the queued higher priority profile runs before an earlier lower priority request test asserts the response, state, or failure produced by this call. + lease = router.acquire(name) + # What: act by calling completed.append with name; why: the queued higher priority profile runs before an earlier lower priority request scenario observes the completed.append return value during lease release. + completed.append(name) + # What: act by calling lease.release with the declared inputs; why: the queued higher priority profile runs before an earlier lower priority request scenario observes the lease.release return value during the enclosing return. + lease.release() + + # What: act by calling threading.Thread and capture low thread; why: the queued higher priority profile runs before an earlier lower priority request test asserts the response, state, or failure produced by this call. + low_thread = threading.Thread(target=acquire_then_release, args=("low",)) + # What: act by calling threading.Thread and capture high thread; why: the queued higher priority profile runs before an earlier lower priority request test asserts the response, state, or failure produced by this call. + high_thread = threading.Thread(target=acquire_then_release, args=("high",)) + # What: act by calling low_thread.start with the declared inputs; why: the queued higher priority profile runs before an earlier lower priority request scenario observes the low_thread.start return value during for value in range. + low_thread.start() + # What: act across range to perform status and router; why: the queued higher priority profile runs before an earlier lower priority request scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: arrange if router status queuedRequests == 1 for the scenario; why: test router test queued higher priority profile runs before an earlier lower priority request requires this concrete input or helper state before exercising the behavior under test. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the queued higher priority profile runs before an earlier lower priority request scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling operation.wait with 0 01; why: the queued higher priority profile runs before an earlier lower priority request scenario observes the operation.wait return value during assert router status queued requests. + threading.Event().wait(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the queued higher priority profile runs before an earlier lower priority request regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: act by calling high_thread.start with the declared inputs; why: the queued higher priority profile runs before an earlier lower priority request scenario observes the high_thread.start return value during for value in range. + high_thread.start() + # What: act across range to perform status and router; why: the queued higher priority profile runs before an earlier lower priority request scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: arrange if router status queuedRequests == 2 for the scenario; why: test router test queued higher priority profile runs before an earlier lower priority request requires this concrete input or helper state before exercising the behavior under test. + if router.status()["queuedRequests"] == 2: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the queued higher priority profile runs before an earlier lower priority request scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling operation.wait with 0 01; why: the queued higher priority profile runs before an earlier lower priority request scenario observes the operation.wait return value during assert router status queued requests. + threading.Event().wait(0.01) + # What: assert that router status queued requests equals 2; why: this assertion protects the queued higher priority profile runs before an earlier lower priority request regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 2 + # What: act by calling active.release with the declared inputs; why: the queued higher priority profile runs before an earlier lower priority request scenario observes the active.release return value during low thread join. + active.release() + # What: act by calling low_thread.join with 1; why: the queued higher priority profile runs before an earlier lower priority request scenario observes the low_thread.join return value during high thread join. + low_thread.join(1) + # What: act by calling high_thread.join with 1; why: the queued higher priority profile runs before an earlier lower priority request scenario observes the high_thread.join return value during assert not low thread is alive and not high thread is alive. + high_thread.join(1) + # What: assert that not low thread is alive and not high thread is alive; why: this assertion protects the queued higher priority profile runs before an earlier lower priority request regression after the test's arranged inputs and exercised call. + assert not low_thread.is_alive() and not high_thread.is_alive() + # What: assert the expected manager calls == outcome; why: test router test queued higher priority profile runs before an earlier lower priority request protects its regression by requiring this observable result after the exercised behavior. + assert manager.calls == [ + # What: arrange start active gguf for the scenario; why: test router test queued higher priority profile runs before an earlier lower priority request requires this concrete input or helper state before exercising the behavior under test. + ("start", "active.gguf"), + # What: arrange switch high gguf for the scenario; why: test router test queued higher priority profile runs before an earlier lower priority request requires this concrete input or helper state before exercising the behavior under test. + ("switch", "high.gguf"), + # What: arrange switch low gguf for the scenario; why: test router test queued higher priority profile runs before an earlier lower priority request requires this concrete input or helper state before exercising the behavior under test. + ("switch", "low.gguf"), + # What: arrange the grouped source fragment for the scenario; why: test router test queued higher priority profile runs before an earlier lower priority request requires this concrete input or helper state before exercising the behavior under test. + ] + # What: assert that completed equals high low; why: this assertion protects the queued higher priority profile runs before an earlier lower priority request regression after the test's arranged inputs and exercised call. + assert completed == ["high", "low"] + + +# What: define the test_failed_readiness_restores_previous_engine_before_reporting_error test around local fixtures; why: this test groups the arrange, act, and assertions that protect the failed readiness restores previous engine before reporting error outcome. +def test_failed_readiness_restores_previous_engine_before_reporting_error(): + # What: act by calling Manager and capture manager; why: the failed readiness restores previous engine before reporting error test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by evaluating router RoutingCoordinator manager catalog object ready fn ready; why: test router test failed readiness restores previous engine before reporting error captures the behavior or response that its following assertions inspect. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: arrange the exact router acquire low release fixture fragment; why: the failed readiness restores previous engine before reporting error scenario feeds this byte-preserved fragment through router.acquire("low").release() before asserting its protocol or parser result. + router.acquire("low").release() + + # What: define the not_ready test helper around manager and probe and pid and port and timeout s; why: the failed readiness restores previous engine before reporting error scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def not_ready(manager, probe, *, pid, port, timeout_s): + # What: arrange the ready field as false; why: not_ready carries ready into return {"ready": False, "reason": "engine-error"}. + return {"ready": False, "reason": "engine-error"} + + # What: act by evaluating router RoutingCoordinator manager catalog object ready fn not ready; why: test router test failed readiness restores previous engine before reporting error captures the behavior or response that its following assertions inspect. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=not_ready) + # What: assert the pytest.raises failure context; why: the failed readiness restores previous engine before reporting error scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RoutingError, match="not ready") as exc: + # What: arrange the exact router acquire high fixture fragment; why: the failed readiness restores previous engine before reporting error scenario feeds this byte-preserved fragment through router.acquire("high") before asserting its protocol or parser result. + router.acquire("high") + # What: assert that exc value code equals engine not ready; why: this assertion protects the failed readiness restores previous engine before reporting error regression after the test's arranged inputs and exercised call. + assert exc.value.code == "engine_not_ready" + # What: assert that exc value recovery launched is true; why: this assertion protects the failed readiness restores previous engine before reporting error regression after the test's arranged inputs and exercised call. + assert exc.value.recovery["launched"] is True + # What: assert that router status activating profile is group delimiter; why: this assertion protects the failed readiness restores previous engine before reporting error regression after the test's arranged inputs and exercised call. + assert router.status()["activatingProfile"] is None + # What: act by calling router.model_listing_snapshot and capture and loaded profiles; why: the failed readiness restores previous engine before reporting error test asserts the response, state, or failure produced by this call. + _, loaded_profiles = router.model_listing_snapshot() + # What: assert that loaded profiles equals frozenset low; why: this assertion protects the failed readiness restores previous engine before reporting error regression after the test's arranged inputs and exercised call. + assert loaded_profiles == frozenset({"low"}) + # What: assert that manager model equals low gguf; why: this assertion protects the failed readiness restores previous engine before reporting error regression after the test's arranged inputs and exercised call. + assert manager.model == "low.gguf" + + +# What: define the test_ttl_evicts_only_after_final_lease_and_uses_profile_timeout test around local fixtures; why: this test groups the arrange, act, and assertions that protect the ttl evicts only after final lease and uses profile timeout outcome. +def test_ttl_evicts_only_after_final_lease_and_uses_profile_timeout(): + # What: define Timer as the owner of __init__ and start and cancel; why: daemon callers use this class boundary so those methods share one timer state invariant. + class Timer: + # What: define the __init__ test helper around delay and callback; why: the ttl evicts only after final lease and uses profile timeout scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def __init__(self, delay, callback): + # What: arrange delay as delay; why: the ttl evicts only after final lease and uses profile timeout test consumes this named precondition before exercising the behavior. + self.delay = delay + # What: arrange callback as callback; why: the ttl evicts only after final lease and uses profile timeout test consumes this named precondition before exercising the behavior. + self.callback = callback + # What: arrange started as false; why: the ttl evicts only after final lease and uses profile timeout test consumes this named precondition before exercising the behavior. + self.started = False + # What: arrange cancelled as false; why: the ttl evicts only after final lease and uses profile timeout test consumes this named precondition before exercising the behavior. + self.cancelled = False + + # What: define the start test helper around captured fixture state; why: the ttl evicts only after final lease and uses profile timeout scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def start(self): + # What: arrange started as true; why: the ttl evicts only after final lease and uses profile timeout test consumes this named precondition before exercising the behavior. + self.started = True + + # What: define the cancel test helper around captured fixture state; why: the ttl evicts only after final lease and uses profile timeout scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def cancel(self): + # What: arrange cancelled as true; why: the ttl evicts only after final lease and uses profile timeout test consumes this named precondition before exercising the behavior. + self.cancelled = True + + # What: act by calling Manager and capture manager; why: the ttl evicts only after final lease and uses profile timeout test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the ttl evicts only after final lease and uses profile timeout test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({ + # What: arrange the low field as model profile and low and low and gguf and 12; why: test_ttl_evicts_only_after_final_lease_and_uses_profile_timeout carries low through catalog doc into manager catalog doc object ready fn ready. + "low": ModelProfile("low", "low.gguf", (), ttl_s=12, unload_timeout_s=7), + # What: arrange the ModelCatalog call with model profile; why: test_ttl_evicts_only_after_final_lease_and_uses_profile_timeout groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: arrange timers as the fixture input; why: the ttl evicts only after final lease and uses profile timeout test consumes this named precondition before exercising the behavior. + timers = [] + # What: act by calling RoutingCoordinator and capture router; why: the ttl evicts only after final lease and uses profile timeout test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator( + # What: arrange ready fn to object; why: the ttl evicts only after final lease and uses profile timeout scenario binds this ready value to object's ready fn input. + manager, catalog_doc, object(), ready_fn=ready, + # What: arrange the delay input for test_ttl_evicts_only_after_final_lease_and_uses_profile_timeout; why: test_ttl_evicts_only_after_final_lease_and_uses_profile_timeout consumes delay during signature binding, so callers must bind it with the other signature inputs. + timer_factory=lambda delay, callback: timers.append(Timer(delay, callback)) or timers[-1], + # What: arrange the RoutingCoordinator call with ready fn and timer factory; why: test_ttl_evicts_only_after_final_lease_and_uses_profile_timeout groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + # What: act by calling router.acquire and capture first; why: the ttl evicts only after final lease and uses profile timeout test asserts the response, state, or failure produced by this call. + first = router.acquire("low") + # What: act by calling router.acquire and capture second; why: the ttl evicts only after final lease and uses profile timeout test asserts the response, state, or failure produced by this call. + second = router.acquire("low") + # What: act by calling first.release with the declared inputs; why: the ttl evicts only after final lease and uses profile timeout scenario observes the first.release return value during assert timers. + first.release() + # What: assert that timers equals group delimiter; why: this assertion protects the ttl evicts only after final lease and uses profile timeout regression after the test's arranged inputs and exercised call. + assert timers == [] + # What: act by calling second.release with the declared inputs; why: the ttl evicts only after final lease and uses profile timeout scenario observes the second.release return value during assert len timers. + second.release() + # What: assert that len timers equals 1; why: this assertion protects the ttl evicts only after final lease and uses profile timeout regression after the test's arranged inputs and exercised call. + assert len(timers) == 1 + # What: assert that timers 0 delay equals 12; why: this assertion protects the ttl evicts only after final lease and uses profile timeout regression after the test's arranged inputs and exercised call. + assert timers[0].delay == 12 + # What: assert that timers 0 started is true; why: this assertion protects the ttl evicts only after final lease and uses profile timeout regression after the test's arranged inputs and exercised call. + assert timers[0].started is True + # What: assert that router evict idle low is true; why: this assertion protects the ttl evicts only after final lease and uses profile timeout regression after the test's arranged inputs and exercised call. + assert router.evict_idle("low") is True + # What: assert that manager calls equals start low gguf stop 7; why: this assertion protects the ttl evicts only after final lease and uses profile timeout regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf"), ("stop", 7)] + # What: assert that router status evictions equals 1; why: this assertion protects the ttl evicts only after final lease and uses profile timeout regression after the test's arranged inputs and exercised call. + assert router.status()["evictions"] == 1 + + +# What: define the test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the all supported openai and anthropic requests use native router and preserve sse outcome. +def test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse(monkeypatch): + # What: act by calling Manager and capture manager; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({ + # What: arrange the low field as model profile and low and low and gguf; why: test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "low": ModelProfile("low", "low.gguf", ()), + # What: arrange the ModelCatalog call with model profile; why: test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: act by calling RoutingCoordinator and capture router; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange calls as the fixture input; why: the all supported openai and anthropic requests use native router and preserve sse test consumes this named precondition before exercising the behavior. + calls = [] + + # What: define the upstream test helper around captured fixture state; why: the all supported openai and anthropic requests use native router and preserve sse scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling calls.append with kwargs; why: the all supported openai and anthropic requests use native router and preserve sse scenario observes the calls.append return value during return upstream response. + calls.append(kwargs) + # What: return upstream response and bytes io and 200 and content type and x upstream from the upstream test helper; why: the all supported openai and anthropic requests use native router and preserve sse scenario uses this helper result in its subsequent act or assertion. + return UpstreamResponse( + # What: arrange status to UpstreamResponse; why: the all supported openai and anthropic requests use native router and preserve sse scenario binds this 200 value to UpstreamResponse's status input. + status=200, + # What: arrange headers to UpstreamResponse; why: the all supported openai and anthropic requests use native router and preserve sse scenario binds this content type and x upstream and connection and keep alive and transfer encoding value to UpstreamResponse's headers input. + headers={ + # What: arrange Content Type text event stream X Upstream yes for the scenario; why: test router test all supported openai and anthropic requests use native router and preserve sse requires this concrete input or helper state before exercising the behavior under test. + "Content-Type": "text/event-stream", "X-Upstream": "yes", + # What: arrange the connection field as keep alive; why: upstream carries connection into "Connection": "keep-alive", "Keep-Alive": "timeout=5". + "Connection": "keep-alive", "Keep-Alive": "timeout=5", + # What: arrange the transfer encoding field as chunked; why: upstream carries transfer encoding into "Transfer-Encoding": "chunked", "Content-Length": "999". + "Transfer-Encoding": "chunked", "Content-Length": "999", + # What: arrange the enclosing predicate mapping with content type and x upstream and connection and keep alive and transfer encoding; why: upstream groups the supplied clauses as one upstream expression mapping before its value is consumed. + }, + # What: arrange raw to BytesIO; why: the all supported openai and anthropic requests use native router and preserve sse scenario binds this bytes io value to BytesIO's raw input. + raw=BytesIO(b"data: first\\n\\ndata: [DONE]\\n\\n"), + # What: arrange the grouped source fragment for the scenario; why: test router test all supported openai and anthropic requests use native router and preserve sse. + ) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the all supported openai and anthropic requests use native router and preserve sse scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream). + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse; why: test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the all supported openai and anthropic requests use native router and preserve sse scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act across the computed value to perform response and post and path and client; why: the all supported openai and anthropic requests use native router and preserve sse scenario repeats the body only while or for the loop header admits an iteration. + for path in ( + # What: arrange the v1 chat completions portion of the enclosing predicate; why: this clause remains in the all supported openai and anthropic requests use native router and preserve sse scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/chat/completions", + # What: arrange the v1 completions portion of the enclosing predicate; why: this clause remains in the all supported openai and anthropic requests use native router and preserve sse scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/completions", + # What: arrange the v1 responses portion of the enclosing predicate; why: this clause remains in the all supported openai and anthropic requests use native router and preserve sse scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/responses", + # What: arrange the v1 messages portion of the enclosing predicate; why: this clause remains in the all supported openai and anthropic requests use native router and preserve sse scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/messages", + # What: arrange the v1 messages count tokens portion of the enclosing predicate; why: this clause remains in the all supported openai and anthropic requests use native router and preserve sse scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/messages/count_tokens", + # What: arrange the grouped source fragment for the scenario; why: test all supported openai and anthropic requests use native router and preserve sse requires this concrete input or helper state before exercising the behavior under test. + ): + # What: act by calling client.post and capture response; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + response = client.post(path, json={"model": "low", "stream": True}) + # What: assert that response status code equals 200; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that response content equals b data first n ndata done; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert response.content == b"data: first\\n\\ndata: [DONE]\\n\\n" + # What: assert that response headers x upstream equals yes; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert response.headers["x-upstream"] == "yes" + # What: assert that connection is absent from response headers; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert "connection" not in response.headers + # What: assert that keep alive is absent from response headers; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert "keep-alive" not in response.headers + # What: assert that transfer encoding is absent from response headers; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert "transfer-encoding" not in response.headers + # What: assert that content length is absent from response headers; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert "content-length" not in response.headers + # What: act by calling client.get and capture status; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + status = client.get("/router/status") + # What: assert that status status code equals 200; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert status.status_code == 200 + # What: assert that status json active requests equals 0; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert status.json()["activeRequests"] == 0 + # What: act by calling operation.json and capture routed models; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + routed_models = client.get("/router/models").json() + # What: assert that routed models data 0 resident is true; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert routed_models["data"][0]["resident"] is True + # What: assert that routed models capacity equals max resident models 1 available resident slots 0; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert routed_models["capacity"] == {"maxResidentModels": 1, "availableResidentSlots": 0} + # What: assert that client get router profiles json active profile equals low; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert client.get("/router/profiles").json()["activeProfile"] == "low" + # What: act by calling client.get and capture metrics; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + metrics = client.get("/metrics") + # What: assert that metrics status code equals 200; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert metrics.status_code == 200 + # What: assert that freetoken swap admissions total 5 is present in metrics text; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert "freetoken_swap_admissions_total 5" in metrics.text + # What: act by calling client.get and capture passthrough; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + passthrough = client.get("/upstream/low/v1/models?limit=3") + # What: assert that passthrough status code equals 200; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert passthrough.status_code == 200 + # What: arrange legacy body as the fixture input; why: the all supported openai and anthropic requests use native router and preserve sse test consumes this named precondition before exercising the behavior. + legacy_body = b'{"prompt":"fixture","max_tokens":2}' + # What: assert that client post generate content legacy body status code equals 404; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert client.post("/generate", content=legacy_body).status_code == 404 + # What: act by calling client.post and capture legacy; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + legacy = client.post( + # What: arrange content to client.post; why: the all supported openai and anthropic requests use native router and preserve sse scenario binds this legacy body value to client.post's content input. + "/upstream/low/generate", content=legacy_body, + # What: arrange the content type field as application and json; why: test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse carries content type through legacy into assert legacy status code equals 200. + headers={"Content-Type": "application/json"}, + # What: arrange the client.post call with content and headers; why: test_all_supported_openai_and_anthropic_requests_use_native_router_and_preserve_sse groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: assert that legacy status code equals 200; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert legacy.status_code == 200 + # What: assert that legacy content equals response content; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert legacy.content == response.content + # What: act by calling client.post and capture blocked; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + blocked = client.post("/upstream/low/v1/admin/prepare-stop") + # What: assert that blocked status code equals 403; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert blocked.status_code == 403 + # What: act by calling operation.json and capture activity; why: the all supported openai and anthropic requests use native router and preserve sse test asserts the response, state, or failure produced by this call. + activity = client.get("/router/activity").json() + # What: assert that activity count equals 7; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert activity["count"] == 7 + # What: assert that all row has capture is false for row in activity data; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert all(row["hasCapture"] is False for row in activity["data"]) + # What: assert that client get router activity stats json count equals 7; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert client.get("/router/activity/stats").json()["count"] == 7 + # What: assert that client get f router captures activity data equals 404; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert client.get(f'/router/captures/{activity["data"][0]["id"]}').status_code == 404 + # What: assert that manager calls equals start low gguf; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + # What: assert the expected item path and query for item in calls == outcome; why: test router test all supported openai and anthropic requests use native router and preserve sse protects its regression by requiring this observable result after the exercised behavior. + assert [item["path_and_query"] for item in calls] == [ + # What: arrange v1 chat completions v1 completions v1 responses for the scenario; why: test router test all supported openai and anthropic requests use native router and preserve sse requires this concrete input or helper state before exercising the behavior under test. + "/v1/chat/completions", "/v1/completions", "/v1/responses", + # What: arrange v1 messages v1 messages count tokens v1 models limit 3 generate for the scenario; why: test router test all supported openai and anthropic requests use native router and preserve sse requires this concrete input or helper state before exercising the behavior under test. + "/v1/messages", "/v1/messages/count_tokens", "/v1/models?limit=3", "/generate", + # What: arrange the grouped source fragment for the scenario; why: test router test all supported openai and anthropic requests use native router and preserve sse requires. + ] + # What: assert that calls 2 method equals get; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert calls[-2]["method"] == "GET" + # What: assert that calls 1 method equals post; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert calls[-1]["method"] == "POST" + # What: assert that calls 1 body equals legacy body; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert calls[-1]["body"] == legacy_body + # What: assert that calls 1 timeout s equals 900 0; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert calls[-1]["timeout_s"] == 900.0 + # What: assert that router status active requests equals 0; why: this assertion protects the all supported openai and anthropic requests use native router and preserve sse regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + + +# What: define the test_profile_readiness_path_and_proxy_prefix_target_the_owned_child test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the profile readiness path and proxy prefix target the owned child outcome. +def test_profile_readiness_path_and_proxy_prefix_target_the_owned_child(monkeypatch): + # What: act by calling Manager and capture manager; why: the profile readiness path and proxy prefix target the owned child test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelProfile and capture profile; why: the profile readiness path and proxy prefix target the owned child test asserts the response, state, or failure produced by this call. + profile = ModelProfile( + # What: arrange the low portion of profile; why: the profile readiness path and proxy prefix target the owned child scenario uses this clause to evaluate profile as one grouped value. + "low", + # What: arrange the low gguf portion of profile; why: the profile readiness path and proxy prefix target the owned child scenario uses this clause to evaluate profile as one grouped value. + "low.gguf", + # What: arrange the profile collection with ordered entries; why: test_profile_readiness_path_and_proxy_prefix_target_the_owned_child groups the supplied clauses as one profile collection before its value is consumed. + (), + # What: arrange port to ModelProfile; why: the profile readiness path and proxy prefix target the owned child scenario binds this 1922 value to ModelProfile's port input. + port=1922, + # What: arrange check endpoint to ModelProfile; why: the profile readiness path and proxy prefix target the owned child scenario binds this ready value to ModelProfile's check endpoint input. + check_endpoint="/ready", + # What: arrange proxy to ModelProfile; why: the profile readiness path and proxy prefix target the owned child scenario binds this http and port and gateway value to ModelProfile's proxy input. + proxy="http://127.0.0.1:${PORT}/gateway", + # What: arrange upstream timeout s to ModelProfile; why: the profile readiness path and proxy prefix target the owned child scenario binds this 37 value to ModelProfile's upstream timeout s input. + upstream_timeout_s=37, + # What: arrange the ModelProfile call with port and check endpoint and proxy and upstream timeout s; why: test_profile_readiness_path_and_proxy_prefix_target_the_owned_child groups the supplied clauses as one ModelProfile call before its value is consumed. + ) + # What: act by calling ModelCatalog and capture catalog doc; why: the profile readiness path and proxy prefix target the owned child test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"low": profile}) + # What: arrange readiness calls as the fixture input; why: the profile readiness path and proxy prefix target the owned child test consumes this named precondition before exercising the behavior. + readiness_calls = [] + # What: arrange upstream calls as the fixture input; why: the profile readiness path and proxy prefix target the owned child test consumes this named precondition before exercising the behavior. + upstream_calls = [] + + # What: define the custom_ready test helper around manager and probe and pid and port and timeout s and path; why: the profile readiness path and proxy prefix target the owned child scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def custom_ready(manager, probe, *, pid, port, timeout_s, path): + # What: act by calling readiness_calls.append with pid and port and timeout s and path; why: the profile readiness path and proxy prefix target the owned child scenario observes the readiness_calls.append return value during return ready health reachable. + readiness_calls.append((pid, port, timeout_s, path)) + # What: arrange the ready field as true; why: custom_ready carries ready into return {"ready": True, "health": {"reachable": True}}. + return {"ready": True, "health": {"reachable": True}} + + # What: define the upstream test helper around captured fixture state; why: the profile readiness path and proxy prefix target the owned child scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling upstream_calls.append with kwargs; why: the profile readiness path and proxy prefix target the owned child scenario observes the upstream_calls.append return value during return upstream response content type application json bytes io. + upstream_calls.append(kwargs) + # What: arrange the helper response as UpstreamResponse 200 Content Type application json BytesIO b; why: test profile readiness path and proxy prefix feeds this result into the behavior whose outcome is asserted. + return UpstreamResponse(200, {"Content-Type": "application/json"}, BytesIO(b'{}')) + + # What: act by calling RoutingCoordinator and capture router; why: the profile readiness path and proxy prefix target the owned child test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=custom_ready) + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the profile readiness path and proxy prefix target the owned child scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before assertin. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_profile_readiness_path_and_proxy_prefix_target_the_owned_child releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the profile readiness path and proxy prefix target the owned child test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_profile_readiness_path_and_proxy_prefix_target_the_owned_child; why: test_profile_readiness_path_and_proxy_prefix_target_the_owned_child consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the profile readiness path and proxy prefix target the owned child scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_profile_readiness_path_and_proxy_prefix_target_the_owned_child groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the profile readiness path and proxy prefix target the owned child test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange the model field as low; why: test_profile_readiness_path_and_proxy_prefix_target_the_owned_child sends this field through response so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "low", "messages": []} + # What: arrange the operation.post call with json; why: test_profile_readiness_path_and_proxy_prefix_target_the_owned_child groups the supplied clauses as one operation.post call before its value is consumed. + ) + + # What: assert that response status code equals 200; why: this assertion protects the profile readiness path and proxy prefix target the owned child regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that readiness calls equals 101 1922 120 0 ready; why: this assertion protects the profile readiness path and proxy prefix target the owned child regression after the test's arranged inputs and exercised call. + assert readiness_calls == [(101, 1922, 120.0, "/ready")] + # What: assert that upstream calls 0 base url equals http 127 0 0 1 1922 gateway; why: this assertion protects the profile readiness path and proxy prefix target the owned child regression after the test's arranged inputs and exercised call. + assert upstream_calls[0]["base_url"] == "http://127.0.0.1:1922/gateway" + # What: assert that upstream calls 0 path and query equals v1 chat completions; why: this assertion protects the profile readiness path and proxy prefix target the owned child regression after the test's arranged inputs and exercised call. + assert upstream_calls[0]["path_and_query"] == "/v1/chat/completions" + # What: assert that upstream calls 0 timeout s equals 37; why: this assertion protects the profile readiness path and proxy prefix target the owned child regression after the test's arranged inputs and exercised call. + assert upstream_calls[0]["timeout_s"] == 37 + + # What: define Probe as the owner of fresh_readiness; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: define the fresh_readiness test helper around port and path; why: the profile readiness path and proxy prefix target the owned child scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def fresh_readiness(self, port, path): + # What: assert that port path equals 1922 ready; why: this assertion protects the profile readiness path and proxy prefix target the owned child regression after the test's arranged inputs and exercised call. + assert (port, path) == (1922, "/ready") + # What: arrange the reachable field as true; why: Probe.fresh_readiness carries reachable into return {"reachable": True, "ready": True}. + return {"reachable": True, "ready": True} + + # What: assert that router is ready probe is true; why: this assertion protects the profile readiness path and proxy prefix target the owned child regression after the test's arranged inputs and exercised call. + assert router.is_ready(Probe()) is True + + +# What: define the test_custom_readiness_path_accepts_real_http_success_without_json test around local fixtures; why: this test groups the arrange, act, and assertions that protect the custom readiness path accepts real http success without json outcome. +def test_custom_readiness_path_accepts_real_http_success_without_json(): + # What: define Handler as the owner of do_GET and log_message; why: daemon callers use this class boundary so those methods share one handler state invariant. + class Handler(BaseHTTPRequestHandler): + # What: define the do_GET test helper around captured fixture state; why: the custom readiness path accepts real http success without json scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def do_GET(self): + # What: assert that self path equals ready; why: this assertion protects the custom readiness path accepts real http success without json regression after the test's arranged inputs and exercised call. + assert self.path == "/ready" + # What: act by calling self.send_response with 204; why: the custom readiness path accepts real http success without json scenario observes the self.send_response return value during self end headers. + self.send_response(204) + # What: act by calling self.end_headers with the declared inputs; why: the custom readiness path accepts real http success without json scenario observes the self.end_headers return value during the enclosing return. + self.end_headers() + + # What: define the log_message test helper around format; why: the custom readiness path accepts real http success without json scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def log_message(self, format, *args): + # What: ignore the anticipated exception handled by this branch; why: log_message continues its retry or cleanup path instead of re-raising that transient failure. + pass + + # What: define RunningManager as the owner of status; why: daemon callers use this class boundary so those methods share one running manager state invariant. + class RunningManager: + # What: define the status test helper around captured fixture state; why: the custom readiness path accepts real http success without json scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def status(self): + # What: arrange the running field as true; why: RunningManager.status carries running into return {"running": True, "pid": 44}. + return {"running": True, "pid": 44} + + # What: act by calling ThreadingHTTPServer and capture server; why: the custom readiness path accepts real http success without json test asserts the response, state, or failure produced by this call. + server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + # What: act by calling threading.Thread and capture worker; why: the custom readiness path accepts real http success without json test asserts the response, state, or failure produced by this call. + worker = threading.Thread(target=server.serve_forever, daemon=True) + # What: act by calling worker.start with the declared inputs; why: the custom readiness path accepts real http success without json scenario observes the worker.start return value during try. + worker.start() + # What: establish the handler boundary for the protected operation; why: test_custom_readiness_path_accepts_real_http_success_without_json routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: act by calling wait_for_ready and capture result; why: the custom readiness path accepts real http success without json test asserts the response, state, or failure produced by this call. + result = wait_for_ready( + # What: act by calling RunningManager with the declared inputs; why: the custom readiness path accepts real http success without json scenario observes the RunningManager return value during serve probe. + RunningManager(), + # What: act by calling ServeProbe with the declared inputs; why: the custom readiness path accepts real http success without json scenario observes the ServeProbe return value during pid. + ServeProbe(), + # What: arrange pid to wait_for_ready; why: the custom readiness path accepts real http success without json scenario binds this 44 value to wait_for_ready's pid input. + pid=44, + # What: arrange port to wait_for_ready; why: the custom readiness path accepts real http success without json scenario binds this server port and server value to wait_for_ready's port input. + port=server.server_port, + # What: arrange timeout s to wait_for_ready; why: the custom readiness path accepts real http success without json scenario binds this 1 value to wait_for_ready's timeout s input. + timeout_s=1, + # What: arrange path to wait_for_ready; why: the custom readiness path accepts real http success without json scenario binds this ready value to wait_for_ready's path input. + path="/ready", + # What: arrange the wait_for_ready call with pid and port and timeout s and path; why: test_custom_readiness_path_accepts_real_http_success_without_json groups the supplied clauses as one wait_for_ready call before its value is consumed. + ) + # What: run server shutdown on every exit path; why: test_custom_readiness_path_accepts_real_http_success_without_json performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act by calling server.shutdown with the declared inputs; why: the custom readiness path accepts real http success without json scenario observes the server.shutdown return value during server server close. + server.shutdown() + # What: act by calling server.server_close with the declared inputs; why: the custom readiness path accepts real http success without json scenario observes the server.server_close return value during worker join. + server.server_close() + # What: act by calling worker.join with 2; why: the custom readiness path accepts real http success without json scenario observes the worker.join return value during assert result ready health reachable. + worker.join(2) + + # What: assert that result equals ready true health reachable true; why: this assertion protects the custom readiness path accepts real http success without json regression after the test's arranged inputs and exercised call. + assert result == {"ready": True, "health": {"reachable": True}} + + +# What: parameterize test_upstream_connector_rejects_non_owned_or_unsafe_base_before_network with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test upstream connector rejects non owned or unsafe base before network. +@pytest.mark.parametrize("base_url", [ + # What: arrange the http portion of the enclosing predicate; why: this clause remains in the upstream connector rejects non owned or unsafe base before network scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "http://127.0.0.1:1923", + # What: arrange the http localhost portion of the enclosing predicate; why: this clause remains in the upstream connector rejects non owned or unsafe base before network scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "http://localhost:1922", + # What: arrange the http admin portion of the enclosing predicate; why: this clause remains in the upstream connector rejects non owned or unsafe base before network scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "http://127.0.0.1:1922/../admin", + # What: arrange the http api token x portion of the enclosing predicate; why: this clause remains in the upstream connector rejects non owned or unsafe base before network scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "http://127.0.0.1:1922/api?token=x", +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_upstream_connector_rejects_non_owned_or_unsafe_base_before_network groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_upstream_connector_rejects_non_owned_or_unsafe_base_before_network test around base url; why: this test groups the arrange, act, and assertions that protect the upstream connector rejects non owned or unsafe base before network outcome. +def test_upstream_connector_rejects_non_owned_or_unsafe_base_before_network(base_url): + # What: assert the pytest.raises failure context; why: the upstream connector rejects non owned or unsafe base before network scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(ValueError, match="manager-owned loopback port"): + # What: act by calling open_upstream with the declared inputs; why: the upstream connector rejects non owned or unsafe base before network scenario observes the open_upstream return value during port. + open_upstream( + # What: arrange port to open_upstream; why: the upstream connector rejects non owned or unsafe base before network scenario binds this 1922 value to open_upstream's port input. + port=1922, + # What: arrange base url to open_upstream; why: the upstream connector rejects non owned or unsafe base before network scenario binds this base url value to open_upstream's base url input. + base_url=base_url, + # What: arrange the exact path and query v1 models fixture fragment; why: the upstream connector rejects non owned or unsafe base before network scenario feeds this byte-preserved fragment through path_and_query="/v1/models" before asserting its protocol or parser result. + path_and_query="/v1/models", + # What: arrange headers to open_upstream; why: the upstream connector rejects non owned or unsafe base before network scenario binds this the named fixture input value to open_upstream's headers input. + headers={}, + # What: arrange body to open_upstream; why: the upstream connector rejects non owned or unsafe base before network scenario binds this the named fixture input value to open_upstream's body input. + body=b"", + # What: arrange the exact method get fixture fragment; why: the upstream connector rejects non owned or unsafe base before network scenario feeds this byte-preserved fragment through method="GET" before asserting its protocol or parser result. + method="GET", + # What: arrange the open_upstream call with port and base url and path and query and headers and body; why: test_upstream_connector_rejects_non_owned_or_unsafe_base_before_network groups the supplied clauses as one open_upstream call before its value is consumed. + ) + + +# What: define the test_stateless_response_resource_routes_preserve_engine_error_without_activation test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the stateless response resource routes preserve engine error without activation outcome. +def test_stateless_response_resource_routes_preserve_engine_error_without_activation(monkeypatch): + # What: act by calling Manager and capture manager; why: the stateless response resource routes preserve engine error without activation test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the stateless response resource routes preserve engine error without activation test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the stateless response resource routes preserve engine error without activation scenario binds this router settings and router test key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("router-test-key",)), + # What: arrange the ModelCatalog call with settings; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the stateless response resource routes preserve engine error without activation test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange monkeypatch setattr for the scenario; why: test stateless response resource routes preserve engine error without activation requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the stateless response resource routes preserve engine error without activation scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs pytest fail stateless response lookup fixture fragment; why: the stateless response resource routes preserve engine error without activation scenario feeds this byte-preserved fragment through lambda **kwargs: pytest.fail("stateless response lookup reached upstream befor. + lambda **kwargs: pytest.fail("stateless response lookup reached upstream"), + # What: arrange the monkeypatch.setattr call with fail; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the stateless response resource routes preserve engine error without activation test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_stateless_response_resource_routes_preserve_engine_error_without_activation; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the stateless response resource routes preserve engine error without activation scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the stateless response resource routes preserve engine error without activation test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: assert that client get v1 responses resp abc status code equals 401; why: this assertion protects the stateless response resource routes preserve engine error without activation regression after the test's arranged inputs and exercised call. + assert client.get("/v1/responses/resp_abc").status_code == 401 + # What: assert that client post v1 responses resp abc cancel status code equals 401; why: this assertion protects the stateless response resource routes preserve engine error without activation regression after the test's arranged inputs and exercised call. + assert client.post("/v1/responses/resp_abc/cancel").status_code == 401 + # What: arrange headers as authorization and bearer and router test key; why: the stateless response resource routes preserve engine error without activation test consumes this named precondition before exercising the behavior. + headers = {"Authorization": "Bearer router-test-key"} + # What: act by calling client.get and capture lookup; why: the stateless response resource routes preserve engine error without activation test asserts the response, state, or failure produced by this call. + lookup = client.get("/v1/responses/resp_abc", headers=headers) + # What: act by calling client.post and capture cancel; why: the stateless response resource routes preserve engine error without activation test asserts the response, state, or failure produced by this call. + cancel = client.post("/v1/responses/resp_abc/cancel", headers=headers) + + # What: arrange expected as error and message and type and code and response; why: the stateless response resource routes preserve engine error without activation test consumes this named precondition before exercising the behavior. + expected = { + # What: arrange the error portion of expected; why: the stateless response resource routes preserve engine error without activation scenario uses this clause to evaluate expected as one grouped value. + "error": { + # What: arrange the message field as response and resp abc and not and found; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation carries message through expected into assert lookup json equals cancel json equals expected. + "message": "response 'resp_abc' not found (stateless server)", + # What: arrange the type field as invalid request error; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation carries type through expected into assert lookup json equals cancel json equals expected. + "type": "invalid_request_error", + # What: arrange the code field as the fixture input; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation carries code through expected into assert lookup json equals cancel json equals expected. + "code": None, + # What: arrange the expected mapping with message and type and code; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation groups the supplied clauses as one expected mapping before its value is consumed. + } + # What: arrange the expected mapping with error; why: test_stateless_response_resource_routes_preserve_engine_error_without_activation groups the supplied clauses as one expected mapping before its value is consumed. + } + # What: assert that lookup status code equals cancel status code equals 404; why: this assertion protects the stateless response resource routes preserve engine error without activation regression after the test's arranged inputs and exercised call. + assert lookup.status_code == cancel.status_code == 404 + # What: assert that lookup json equals cancel json equals expected; why: this assertion protects the stateless response resource routes preserve engine error without activation regression after the test's arranged inputs and exercised call. + assert lookup.json() == cancel.json() == expected + # What: assert that manager calls equals group delimiter; why: this assertion protects the stateless response resource routes preserve engine error without activation regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + # What: assert that router status admissions equals 0; why: this assertion protects the stateless response resource routes preserve engine error without activation regression after the test's arranged inputs and exercised call. + assert router.status()["admissions"] == 0 + # What: assert that router status reserved requests equals 0; why: this assertion protects the stateless response resource routes preserve engine error without activation regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 0 + + +# What: define the test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the namespaced upstream uses longest model prefix and preserves escaped suffix outcome. +def test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix(monkeypatch): + # What: act by calling Manager and capture manager; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({ + # What: arrange the author field as model profile and author and parent and gguf; why: test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix carries author through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "author": ModelProfile("author", "parent.gguf", ()), + # What: arrange the author model field as model profile and author and model and exact and gguf; why: test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix carries author model through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "author/model": ModelProfile( + # What: arrange aliases to ModelProfile; why: the namespaced upstream uses longest model prefix and preserves escaped suffix scenario binds this org and compat value to ModelProfile's aliases input. + "author/model", "exact.gguf", (), aliases=("org/compat",) + # What: arrange the ModelProfile call with aliases; why: test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix groups the supplied clauses as one ModelProfile call before its value is consumed. + ), + # What: arrange the ModelCatalog call with model profile; why: test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: act by calling RoutingCoordinator and capture router; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange calls as the fixture input; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test consumes this named precondition before exercising the behavior. + calls = [] + + # What: define the upstream test helper around captured fixture state; why: the namespaced upstream uses longest model prefix and preserves escaped suffix scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling calls.append with kwargs; why: the namespaced upstream uses longest model prefix and preserves escaped suffix scenario observes the calls.append return value during return upstream response content type application json bytes io. + calls.append(kwargs) + # What: arrange the helper response as UpstreamResponse 200 Content Type application json BytesIO b; why: test router test feeds this result into the. + return UpstreamResponse(200, {"Content-Type": "application/json"}, BytesIO(b'{}')) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the namespaced upstream uses longest model prefix and preserves escaped suffix scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) bef. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix; why: test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the namespaced upstream uses longest model prefix and preserves escaped suffix scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.post and capture exact; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + exact = client.post( + # What: arrange content to client.post; why: the namespaced upstream uses longest model prefix and preserves escaped suffix scenario binds this the named fixture input value to client.post's content input. + "/upstream/author/model/api/x%2Fy?preview=a%2Fb", content=b"exact" + # What: arrange the client.post call with content; why: test_namespaced_upstream_uses_longest_model_prefix_and_preserves_escaped_suffix groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.get and capture encoded alias; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + encoded_alias = client.get("/upstream/org%2Fcompat/v1/chat") + # What: act by calling client.post and capture automatic; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + automatic = client.post("/v1/chat/completions", json={"model": "org/compat"}) + # What: act by calling client.get and capture bare; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + bare = client.get("/upstream/org/compat") + # What: act by calling client.post and capture blocked; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + blocked = client.post("/upstream/author/model/v1/admin/prepare-stop") + # What: act by calling client.get and capture unknown; why: the namespaced upstream uses longest model prefix and preserves escaped suffix test asserts the response, state, or failure produced by this call. + unknown = client.get("/upstream/missing/model/v1/chat") + + # What: assert that exact status code equals encoded alias status code equals automatic status code equals 200; why: this assertion protects the namespaced upstream uses longest model prefix and preserves escaped suffix regression after the test's arranged inputs and exercised call. + assert exact.status_code == encoded_alias.status_code == automatic.status_code == 200 + # What: assert that bare status code equals 200; why: this assertion protects the namespaced upstream uses longest model prefix and preserves escaped suffix regression after the test's arranged inputs and exercised call. + assert bare.status_code == 200 + # What: assert that blocked status code equals 403; why: this assertion protects the namespaced upstream uses longest model prefix and preserves escaped suffix regression after the test's arranged inputs and exercised call. + assert blocked.status_code == 403 + # What: assert that unknown status code equals 404; why: this assertion protects the namespaced upstream uses longest model prefix and preserves escaped suffix regression after the test's arranged inputs and exercised call. + assert unknown.status_code == 404 + # What: assert that unknown json error type equals unknown model; why: this assertion protects the namespaced upstream uses longest model prefix and preserves escaped suffix regression after the test's arranged inputs and exercised call. + assert unknown.json()["error"]["type"] == "unknown_model" + # What: assert that manager calls equals start exact gguf; why: this assertion protects the namespaced upstream uses longest model prefix and preserves escaped suffix regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "exact.gguf")] + # What: assert the expected call path and query for call in calls == outcome; why: test router test namespaced upstream uses longest model prefix and preserves escaped suffix protects its regression by requiring this observable result after the exercised behavior. + assert [call["path_and_query"] for call in calls] == [ + # What: arrange api x 2 Fy preview a 2 Fb v1 chat v1 chat completions for the scenario; why: test router test namespaced upstream uses longest model prefix and preserves escaped suffix requires this concrete input or helper state before exercising the behavior under test. + "/api/x%2Fy?preview=a%2Fb", "/v1/chat", "/v1/chat/completions", "/", + # What: arrange the grouped source fragment for the scenario; why: test router test namespaced upstream uses longest model prefix and preserves escaped suffix requires this concrete input or helper state before exercising the behavior under test. + ] + # What: assert that calls 0 body equals b exact; why: this assertion protects the namespaced upstream uses longest model prefix and preserves escaped suffix regression after the test's arranged inputs and exercised call. + assert calls[0]["body"] == b"exact" + + +# What: define the test_upstream_static_suffix_refuses_cold_activation_and_allows_exact_resident test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the upstream static suffix refuses cold activation and allows exact resident outcome. +def test_upstream_static_suffix_refuses_cold_activation_and_allows_exact_resident(monkeypatch): + # What: act by calling Manager and capture manager; why: the upstream static suffix refuses cold activation and allows exact resident test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the upstream static suffix refuses cold activation and allows exact resident test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({ + # What: arrange the author model field as model profile and author and model and exact and gguf; why: test_upstream_static_suffix_refuses_cold_activation_and_allows_exact_resident carries author model through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "author/model": ModelProfile( + # What: arrange aliases to ModelProfile; why: the upstream static suffix refuses cold activation and allows exact resident scenario binds this org and compat value to ModelProfile's aliases input. + "author/model", "exact.gguf", (), aliases=("org/compat",) + # What: arrange the ModelProfile call with aliases; why: test_upstream_static_suffix_refuses_cold_activation_and_allows_exact_resident groups the supplied clauses as one ModelProfile call before its value is consumed. + ), + # What: arrange the ModelCatalog call with model profile; why: test_upstream_static_suffix_refuses_cold_activation_and_allows_exact_resident groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: act by calling RoutingCoordinator and capture router; why: the upstream static suffix refuses cold activation and allows exact resident test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange calls as the fixture input; why: the upstream static suffix refuses cold activation and allows exact resident test consumes this named precondition before exercising the behavior. + calls = [] + + # What: define the upstream test helper around captured fixture state; why: the upstream static suffix refuses cold activation and allows exact resident scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling calls.append with kwargs; why: the upstream static suffix refuses cold activation and allows exact resident scenario observes the calls.append return value during return upstream response content type text plain bytes io. + calls.append(kwargs) + # What: arrange the content type field as text and plain; why: upstream carries content type into return UpstreamResponse(200, {"Content-Type": "text/plain"}, BytesIO(b"a. + return UpstreamResponse(200, {"Content-Type": "text/plain"}, BytesIO(b"asset")) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the upstream static suffix refuses cold activation and allows exact resident scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) befor. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_upstream_static_suffix_refuses_cold_activation_and_allows_exact_resident releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the upstream static suffix refuses cold activation and allows exact resident test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_upstream_static_suffix_refuses_cold_activation_and_allows_exact_resident; why: test_upstream_static_suffix_refuses_cold_activation_and_allows_exact_resident consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the upstream static suffix refuses cold activation and allows exact resident scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_upstream_static_suffix_refuses_cold_activation_and_allows_exact_resident groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the upstream static suffix refuses cold activation and allows exact resident test asserts the response, state, or failure produced by this call. + client = TestClient(app) + + # What: act by calling client.get and capture cold asset; why: the upstream static suffix refuses cold activation and allows exact resident test asserts the response, state, or failure produced by this call. + cold_asset = client.get("/upstream/org/compat/ui/app.js") + # What: assert that cold asset status code equals 409; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert cold_asset.status_code == 409 + # What: assert that cold asset json error type equals model not loaded; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert cold_asset.json()["error"]["type"] == "model_not_loaded" + # What: assert that manager calls equals group delimiter; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + # What: assert that calls equals group delimiter; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert calls == [] + # What: assert that router status reserved requests equals 0; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 0 + + # What: act by calling client.get and capture cold api; why: the upstream static suffix refuses cold activation and allows exact resident test asserts the response, state, or failure produced by this call. + cold_api = client.get("/upstream/org/compat/api/status") + # What: assert that cold api status code equals 200; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert cold_api.status_code == 200 + # What: assert that manager calls equals start exact gguf; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "exact.gguf")] + + # What: act by calling client.get and capture warm asset; why: the upstream static suffix refuses cold activation and allows exact resident test asserts the response, state, or failure produced by this call. + warm_asset = client.get("/upstream/org/compat/ui/app.js") + # What: assert that warm asset status code equals 200; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert warm_asset.status_code == 200 + # What: assert that warm asset content equals b asset; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert warm_asset.content == b"asset" + + # What: assert that call path and query for call in calls equals api status ui app js; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert [call["path_and_query"] for call in calls] == ["/api/status", "/ui/app.js"] + # What: assert that manager calls equals start exact gguf; why: this assertion protects the upstream static suffix refuses cold activation and allows exact resident regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "exact.gguf")] + + +# What: define the test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable test around monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the activity and opt in capture apis are authenticated redacted and durable outcome. +def test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable( + # What: arrange monkeypatch tmp path for the scenario; why: test activity and opt in capture apis are authenticated redacted and durable requires this concrete input or helper state before exercising the behavior under test. + monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test activity and opt in capture apis are authenticated redacted and durable requires this concrete input or helper state before exercising the behavior under test. +): + # What: act by calling Manager and capture manager; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange api keys to RouterSettings; why: the activity and opt in capture apis are authenticated redacted and durable scenario binds this secret value to RouterSettings's api keys input. + RouterSettings(api_keys=("secret",), activity_max_entries=2, capture_buffer_mb=1), + # What: arrange the ModelCatalog call with model profile and router settings; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange activity path as tmp path and activity and jsonl; why: the activity and opt in capture apis are authenticated redacted and durable test consumes this named precondition before exercising the behavior. + activity_path = tmp_path / "activity.jsonl" + + # What: define the upstream test helper around captured fixture state; why: the activity and opt in capture apis are authenticated redacted and durable scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: return upstream response and bytes io and 200 and content type and set cookie from the upstream test helper; why: the activity and opt in capture apis are authenticated redacted and durable scenario uses this helper result in its subsequent act or assertion. + return UpstreamResponse( + # What: arrange the grouped expression portion of the enclosing predicate; why: this clause remains in the activity and opt in capture apis are authenticated redacted and durable scenario\'s enclosing expression so its grouping and evaluation order stay intact. + 200, + # What: arrange the content type field as application and octet stream; why: upstream carries content type into {"Content-Type": "application/octet-stream", "Set-Cookie": "private"}. + {"Content-Type": "application/octet-stream", "Set-Cookie": "private"}, + # What: act by calling BytesIO with the named fixture input; why: the activity and opt in capture apis are authenticated redacted and durable scenario observes the BytesIO return value while evaluating BytesIO(b"\xffresult"). + BytesIO(b"\xffresult"), + # What: arrange the grouped source fragment for the scenario; why: test router test activity and opt in capture apis are authenticated redacted and durable requires this concrete input or helper state before exercising the behavior under test. + ) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the activity and opt in capture apis are authenticated redacted and durable scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + app = build_app( + # What: act by evaluating manager manager ring LogRing probe object footprint fn lambda pid; why: test router test activity and opt in capture apis are authenticated redacted and durable captures the behavior or response that its following assertions inspect. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the activity and opt in capture apis are authenticated redacted and durable scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange activity path to str; why: the activity and opt in capture apis are authenticated redacted and durable scenario binds this str and activity path value to str's activity path input. + activity_path=str(activity_path), + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: assert that client get router activity status code equals 401; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert client.get("/router/activity").status_code == 401 + # What: arrange headers as authorization and x trace and x session id and bearer and secret; why: the activity and opt in capture apis are authenticated redacted and durable test consumes this named precondition before exercising the behavior. + headers = { + # What: arrange the authorization field as bearer and secret; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable carries authorization through headers into headers headers content type application json. + "Authorization": "Bearer secret", "X-Trace": "visible", + # What: arrange the x session id field as private session value; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable carries x session id through headers into headers headers content type application json. + "X-Session-ID": "private-session-value", + # What: arrange the headers mapping with authorization and x trace and x session id; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable groups the supplied clauses as one headers mapping before its value is consumed. + } + # What: act by calling client.post and capture response; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + response = client.post( + # What: arrange content to client.post; why: the activity and opt in capture apis are authenticated redacted and durable scenario binds this the named fixture input value to client.post's content input. + "/v1/chat/completions", content=b'{"model":"low","prompt":"private"}', + # What: arrange the content type field as application and json; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable carries content type through response into assert response content equals b xffresult. + headers={**headers, "Content-Type": "application/json"}, + # What: arrange the client.post call with content and headers; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: assert that response content equals b xffresult; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert response.content == b"\xffresult" + + # What: act by evaluating page client get router activity headers headers json; why: test router test activity and opt in capture apis are authenticated redacted and durable captures the behavior or response that its following assertions inspect. + page = client.get("/router/activity", headers=headers).json() + # What: assert that page count equals 1; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert page["count"] == 1 + # What: arrange row as page and 0 and data; why: the activity and opt in capture apis are authenticated redacted and durable test consumes this named precondition before exercising the behavior. + row = page["data"][0] + # What: assert that row model equals low; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert row["model"] == "low" + # What: assert that row route equals v1 chat completions; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert row["route"] == "/v1/chat/completions" + # What: assert that row has capture is true; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert row["hasCapture"] is True + # What: assert that len row session id equals 16; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert len(row["sessionId"]) == 16 + # What: assert that row session id differs from private session value; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert row["sessionId"] != "private-session-value" + # What: assert that client get router activity stats headers headers equals 1; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert client.get("/router/activity/stats", headers=headers).json()["count"] == 1 + # What: act by calling operation.json and capture capture; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + capture = client.get(f'/router/captures/{row["id"]}', headers=headers).json() + # What: assert that capture request headers authorization equals redacted; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert capture["requestHeaders"]["authorization"] == "[REDACTED]" + # What: assert that capture response headers set cookie equals redacted; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert capture["responseHeaders"]["Set-Cookie"] == "[REDACTED]" + # What: assert that base64 b64decode capture request body base64 equals b model low prompt private; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert base64.b64decode(capture["requestBodyBase64"]) == b'{"model":"low","prompt":"private"}' + # What: assert that base64 b64decode capture response body base64 equals b xffresult; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert base64.b64decode(capture["responseBodyBase64"]) == b"\xffresult" + + # What: act by calling Manager and capture restarted manager; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + restarted_manager = Manager() + # What: act by calling RoutingCoordinator and capture restarted router; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + restarted_router = RoutingCoordinator( + # What: arrange ready fn to object; why: the activity and opt in capture apis are authenticated redacted and durable scenario binds this ready value to object's ready fn input. + restarted_manager, catalog_doc, object(), ready_fn=ready + # What: arrange the RoutingCoordinator call with ready fn; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before restarted app build app; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable releases this resource or lock after restarted app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture restarted app; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + restarted_app = build_app( + # What: arrange manager to LogRing; why: the activity and opt in capture apis are authenticated redacted and durable scenario binds this restarted manager value to LogRing's manager input. + manager=restarted_manager, ring=LogRing(), probe=object(), + # What: arrange footprint fn lambda pid lifecycle pool lifecycle proxy pool proxy for the scenario; why: test router test activity and opt in capture apis are authenticated redacted and durable requires this concrete input or helper state before exercising the behavior under test. + footprint_fn=lambda pid: {}, lifecycle_pool=lifecycle, proxy_pool=proxy, + # What: arrange catalog to str; why: the activity and opt in capture apis are authenticated redacted and durable scenario binds this catalog doc value to str's catalog input. + catalog=catalog_doc, router=restarted_router, activity_path=str(activity_path), + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_activity_and_opt_in_capture_apis_are_authenticated_redacted_and_durable groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture restarted; why: the activity and opt in capture apis are authenticated redacted and durable test asserts the response, state, or failure produced by this call. + restarted = TestClient(restarted_app) + # What: act by evaluating page restarted get router activity headers headers json; why: test router test activity and opt in capture apis are authenticated redacted and durable captures the behavior or response that its following assertions inspect. + page = restarted.get("/router/activity", headers=headers).json() + # What: assert that page count equals 1; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert page["count"] == 1 + # What: assert that page data 0 has capture is false; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert page["data"][0]["hasCapture"] is False + # What: assert that page data 0 session id equals row session id; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert page["data"][0]["sessionId"] == row["sessionId"] + # What: assert that page persistence equals enabled true healthy true error; why: this assertion protects the activity and opt in capture apis are authenticated redacted and durable regression after the test's arranged inputs and exercised call. + assert page["persistence"] == {"enabled": True, "healthy": True, "error": None} + # What: assert the expected restarted get outcome; why: test router test activity and opt in capture apis are authenticated redacted and durable protects its regression by requiring this observable result after the exercised behavior. + assert restarted.get( + # What: arrange f router captures page data 0 id headers headers for the scenario; why: test router test activity and opt in capture apis are authenticated redacted and durable requires this concrete input or helper state before exercising the behavior under test. + f'/router/captures/{page["data"][0]["id"]}', headers=headers + # What: arrange status code == 404 for the scenario; why: test router test activity and opt in capture apis are authenticated redacted and durable requires this concrete input or helper state before exercising the behavior under test. + ).status_code == 404 + + +# What: parameterize test_all_routed_text_endpoints_share_stable_unknown_model_error with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test all routed text endpoints share stable unknown model error. +@pytest.mark.parametrize( + # What: arrange the path portion of the enclosing predicate; why: this clause remains in the all routed text endpoints share stable unknown model error scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "path", + # What: arrange the grouped source fragment for the scenario; why: test all routed text endpoints share stable unknown model error requires this concrete input or helper state before exercising the behavior under test. + ( + # What: arrange the v1 chat completions portion of the enclosing predicate; why: this clause remains in the all routed text endpoints share stable unknown model error scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/chat/completions", + # What: arrange the v1 completions portion of the enclosing predicate; why: this clause remains in the all routed text endpoints share stable unknown model error scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/completions", + # What: arrange the v1 responses portion of the enclosing predicate; why: this clause remains in the all routed text endpoints share stable unknown model error scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/responses", + # What: arrange the v1 messages portion of the enclosing predicate; why: this clause remains in the all routed text endpoints share stable unknown model error scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/messages", + # What: arrange the v1 messages count tokens portion of the enclosing predicate; why: this clause remains in the all routed text endpoints share stable unknown model error scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "/v1/messages/count_tokens", + # What: arrange the grouped source fragment for the scenario; why: test all routed text endpoints share stable unknown model error requires this concrete input or helper state before exercising the behavior under test. + ), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_all_routed_text_endpoints_share_stable_unknown_model_error groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +) +# What: define the test_all_routed_text_endpoints_share_stable_unknown_model_error test around path and monkeypatch; why: this test groups the arrange, act, and assertions that protect the all routed text endpoints share stable unknown model error outcome. +def test_all_routed_text_endpoints_share_stable_unknown_model_error(path, monkeypatch): + # What: act by calling Manager and capture manager; why: the all routed text endpoints share stable unknown model error test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the all routed text endpoints share stable unknown model error test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"known": ModelProfile("known", "known.gguf", ())}) + # What: act by calling RoutingCoordinator and capture router; why: the all routed text endpoints share stable unknown model error test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange monkeypatch setattr for the scenario; why: test all routed text endpoints share stable unknown model error requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the all routed text endpoints share stable unknown model error scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs value for value in fixture fragment; why: the all routed text endpoints share stable unknown model error scenario feeds this byte-preserved fragment through lambda **kwargs: (_ for _ in ()).throw(AssertionError("unknown model rea before asserting its protocol or parser r. + lambda **kwargs: (_ for _ in ()).throw(AssertionError("unknown model reached upstream")), + # What: arrange the monkeypatch.setattr call with throw; why: test_all_routed_text_endpoints_share_stable_unknown_model_error groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_all_routed_text_endpoints_share_stable_unknown_model_error releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the all routed text endpoints share stable unknown model error test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_all_routed_text_endpoints_share_stable_unknown_model_error; why: test_all_routed_text_endpoints_share_stable_unknown_model_error consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the all routed text endpoints share stable unknown model error scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_all_routed_text_endpoints_share_stable_unknown_model_error groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the all routed text endpoints share stable unknown model error test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.post and capture response; why: the all routed text endpoints share stable unknown model error test asserts the response, state, or failure produced by this call. + response = client.post( + # What: arrange the model field as missing; why: test_all_routed_text_endpoints_share_stable_unknown_model_error sends this field through response so the router selects the canonical model or alias for upstream dispatch. + path, json={"model": "missing"}, headers={"X-FT-Request-ID": "reusable-failure"} + # What: arrange the client.post call with json and headers; why: test_all_routed_text_endpoints_share_stable_unknown_model_error groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.post and capture repeated; why: the all routed text endpoints share stable unknown model error test asserts the response, state, or failure produced by this call. + repeated = client.post( + # What: arrange the model field as missing; why: test_all_routed_text_endpoints_share_stable_unknown_model_error sends this field through repeated so the router selects the canonical model or alias for upstream dispatch. + path, json={"model": "missing"}, headers={"X-FT-Request-ID": "reusable-failure"} + # What: arrange the client.post call with json and headers; why: test_all_routed_text_endpoints_share_stable_unknown_model_error groups the supplied clauses as one client.post call before its value is consumed. + ) + + # What: assert that response status code equals 404; why: this assertion protects the all routed text endpoints share stable unknown model error regression after the test's arranged inputs and exercised call. + assert response.status_code == 404 + # What: assert that response json error type equals unknown model; why: this assertion protects the all routed text endpoints share stable unknown model error regression after the test's arranged inputs and exercised call. + assert response.json()["error"]["type"] == "unknown_model" + # What: assert that missing is present in response json error message; why: this assertion protects the all routed text endpoints share stable unknown model error regression after the test's arranged inputs and exercised call. + assert "missing" in response.json()["error"]["message"] + # What: assert that repeated status code equals 404; why: this assertion protects the all routed text endpoints share stable unknown model error regression after the test's arranged inputs and exercised call. + assert repeated.status_code == 404 + # What: assert that repeated json error type equals unknown model; why: this assertion protects the all routed text endpoints share stable unknown model error regression after the test's arranged inputs and exercised call. + assert repeated.json()["error"]["type"] == "unknown_model" + # What: assert that manager calls equals group delimiter; why: this assertion protects the all routed text endpoints share stable unknown model error regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + + +# What: define the test_router_preserves_upstream_error_status_headers_and_body test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the router preserves upstream error status headers and body outcome. +def test_router_preserves_upstream_error_status_headers_and_body(monkeypatch): + # What: act by calling Manager and capture manager; why: the router preserves upstream error status headers and body test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the router preserves upstream error status headers and body test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"low": ModelProfile("low", "low.gguf", ())}) + # What: act by calling RoutingCoordinator and capture router; why: the router preserves upstream error status headers and body test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + + # What: define the upstream test helper around captured fixture state; why: the router preserves upstream error status headers and body scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: assert that kwargs path and query equals v1 responses; why: this assertion protects the router preserves upstream error status headers and body regression after the test's arranged inputs and exercised call. + assert kwargs["path_and_query"] == "/v1/responses" + # What: return upstream response and bytes io and 429 and content type and retry after from the upstream test helper; why: the router preserves upstream error status headers and body scenario uses this helper result in its subsequent act or assertion. + return UpstreamResponse( + # What: arrange status to UpstreamResponse; why: the router preserves upstream error status headers and body scenario binds this 429 value to UpstreamResponse's status input. + status=429, + # What: arrange headers Content Type application json Retry After 2 Content Length 999 for the scenario; why: test router preserves upstream error stat requires this concrete input or helper state before exercising the behavior under test. + headers={"Content-Type": "application/json", "Retry-After": "2", "Content-Length": "999"}, + # What: arrange raw to BytesIO; why: the router preserves upstream error status headers and body scenario binds this bytes io value to BytesIO's raw input. + raw=BytesIO(b'{"error":{"message":"busy"}}'), + # What: arrange the grouped source fragment for the scenario; why: test router test router preserves upstream error status headers and body requires this concrete input or helper state before exercising the behavior under test. + ) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the router preserves upstream error status headers and body scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before asserting its p. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_preserves_upstream_error_status_headers_and_body releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router preserves upstream error status headers and body test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_preserves_upstream_error_status_headers_and_body; why: test_router_preserves_upstream_error_status_headers_and_body consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router preserves upstream error status headers and body scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_preserves_upstream_error_status_headers_and_body groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the router preserves upstream error status headers and body test asserts the response, state, or failure produced by this call. + response = TestClient(app).post("/v1/responses", json={"model": "low", "input": "private"}) + + # What: assert that response status code equals 429; why: this assertion protects the router preserves upstream error status headers and body regression after the test's arranged inputs and exercised call. + assert response.status_code == 429 + # What: assert that response headers retry after equals 2; why: this assertion protects the router preserves upstream error status headers and body regression after the test's arranged inputs and exercised call. + assert response.headers["retry-after"] == "2" + # What: assert that response content equals b error message busy; why: this assertion protects the router preserves upstream error status headers and body regression after the test's arranged inputs and exercised call. + assert response.content == b'{"error":{"message":"busy"}}' + # What: assert that content length is absent from response headers; why: this assertion protects the router preserves upstream error status headers and body regression after the test's arranged inputs and exercised call. + assert "content-length" not in response.headers + # What: assert that router status active requests equals 0; why: this assertion protects the router preserves upstream error status headers and body regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + # What: assert that router status terminal streams equals 1; why: this assertion protects the router preserves upstream error status headers and body regression after the test's arranged inputs and exercised call. + assert router.status()["terminalStreams"] == 1 + + +# What: define the test_failed_upstream_connect_releases_lease_and_request_id_reservation test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the failed upstream connect releases lease and request id reservation outcome. +def test_failed_upstream_connect_releases_lease_and_request_id_reservation(monkeypatch): + # What: act by calling Manager and capture manager; why: the failed upstream connect releases lease and request id reservation test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the failed upstream connect releases lease and request id reservation test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"low": ModelProfile("low", "low.gguf", ())}) + # What: act by calling RoutingCoordinator and capture router; why: the failed upstream connect releases lease and request id reservation test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange monkeypatch setattr for the scenario; why: test failed upstream connect releases lease and request id reservation requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the failed upstream connect releases lease and request id reservation scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs value for value in fixture fragment; why: the failed upstream connect releases lease and request id reservation scenario feeds this byte-preserved fragment through lambda **kwargs: (_ for _ in ()).throw(OSError("fixture unavailable")) before asserting its protocol or par. + lambda **kwargs: (_ for _ in ()).throw(OSError("fixture unavailable")), + # What: arrange the monkeypatch.setattr call with throw; why: test_failed_upstream_connect_releases_lease_and_request_id_reservation groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_failed_upstream_connect_releases_lease_and_request_id_reservation releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the failed upstream connect releases lease and request id reservation test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_failed_upstream_connect_releases_lease_and_request_id_reservation; why: test_failed_upstream_connect_releases_lease_and_request_id_reservation consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the failed upstream connect releases lease and request id reservation scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_failed_upstream_connect_releases_lease_and_request_id_reservation groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the failed upstream connect releases lease and request id reservation test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.post and capture responses; why: the failed upstream connect releases lease and request id reservation test asserts the response, state, or failure produced by this call. + responses = [ + # What: act by calling client.post with v1 and chat and completions; why: the failed upstream connect releases lease and request id reservation scenario observes the client.post return value during v1 chat completions json model low. + client.post( + # What: arrange the model field as low; why: test_failed_upstream_connect_releases_lease_and_request_id_reservation sends this field through responses so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "low"}, + # What: arrange the x ft request id field as retry after connect failure; why: test_failed_upstream_connect_releases_lease_and_request_id_reservation carries x ft request id through responses into assert response status code for response in responses equals. + headers={"X-FT-Request-ID": "retry-after-connect-failure"}, + # What: arrange the client.post call with json and headers; why: test_failed_upstream_connect_releases_lease_and_request_id_reservation groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling range with 2; why: the failed upstream connect releases lease and request id reservation scenario observes the range return value while evaluating for _ in range(2). + for _ in range(2) + # What: arrange the responses expression with responses client post v1 chat completions json model low headers; why: test_failed_upstream_connect_releases_lease_and_request_id_reservation groups the supplied clauses as one responses expression before its value is consumed. + ] + + # What: assert that response status code for response in responses equals 502 502; why: this assertion protects the failed upstream connect releases lease and request id reservation regression after the test's arranged inputs and exercised call. + assert [response.status_code for response in responses] == [502, 502] + # What: assert that all response json error type equals upstream unavailable for response in responses; why: this assertion protects the failed upstream connect releases lease and request id reservation regression after the test's arranged inputs and exercised call. + assert all(response.json()["error"]["type"] == "upstream_unavailable" for response in responses) + # What: assert that router status active requests equals 0; why: this assertion protects the failed upstream connect releases lease and request id reservation regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + # What: assert that router status admissions equals 2; why: this assertion protects the failed upstream connect releases lease and request id reservation regression after the test's arranged inputs and exercised call. + assert router.status()["admissions"] == 2 + + +# What: define the test_alias_routes_to_canonical_residency_and_model_list_respects_visibility test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the alias routes to canonical residency and model list respects visibility outcome. +def test_alias_routes_to_canonical_residency_and_model_list_respects_visibility(monkeypatch): + # What: act by calling Manager and capture manager; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with canonical and hidden; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility groups the supplied clauses as one catalog_doc mapping before its value. + { + # What: arrange the canonical field as model profile and canonical and shared and gguf and compat id; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility carries canonical through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "canonical": ModelProfile( + # What: arrange aliases to ModelProfile; why: the alias routes to canonical residency and model list respects visibility scenario binds this compat id value to ModelProfile's aliases input. + "canonical", "shared.gguf", (), aliases=("compat-id",) + # What: arrange the ModelProfile call with aliases; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility groups the supplied clauses as one ModelProfile call before its value is consumed. + ), + # What: arrange the hidden field as model profile and hidden and hidden and gguf and true; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility carries hidden through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "hidden": ModelProfile( + # What: arrange aliases to ModelProfile; why: the alias routes to canonical residency and model list respects visibility scenario binds this private id value to ModelProfile's aliases input. + "hidden", "hidden.gguf", (), aliases=("private-id",), unlisted=True + # What: arrange the ModelProfile call with aliases and unlisted; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility groups the supplied clauses as one ModelProfile call before its value is consumed. + ), + # What: arrange the catalog_doc mapping with canonical and hidden; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange settings to RouterSettings; why: the alias routes to canonical residency and model list respects visibility scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(include_aliases_in_list=True), + # What: arrange the ModelCatalog call with settings; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange calls as the fixture input; why: the alias routes to canonical residency and model list respects visibility test consumes this named precondition before exercising the behavior. + calls = [] + + # What: define the upstream test helper around captured fixture state; why: the alias routes to canonical residency and model list respects visibility scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling calls.append with kwargs; why: the alias routes to canonical residency and model list respects visibility scenario observes the calls.append return value during return upstream response. + calls.append(kwargs) + # What: return upstream response and bytes io and 200 and content type and application from the upstream test helper; why: the alias routes to canonical residency and model list respects visibility scenario uses this helper result in its subsequent act or assertion. + return UpstreamResponse( + # What: arrange status to UpstreamResponse; why: the alias routes to canonical residency and model list respects visibility scenario binds this 200 value to UpstreamResponse's status input. + status=200, + # What: arrange headers Content Type application json for the scenario; why: test router test alias routes to canonical residency and model list respects visibility requires this concrete input or helper state before exercising the behavior under test. + headers={"Content-Type": "application/json"}, + # What: arrange raw to BytesIO; why: the alias routes to canonical residency and model list respects visibility scenario binds this bytes io value to BytesIO's raw input. + raw=BytesIO(b'{"ok":true}'), + # What: arrange the grouped source fragment for the scenario; why: test router test alias routes to canonical residency and model list respects visibility requires this concrete. + ) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the alias routes to canonical residency and model list respects visibility scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_alias_routes_to_canonical_residency_and_model_list_respects_visibility; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the alias routes to canonical residency and model list respects visibility scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.get and capture listed; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + listed = client.get("/v1/models") + # What: act by calling client.post and capture alias response; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + alias_response = client.post( + # What: arrange content to client.post; why: the alias routes to canonical residency and model list respects visibility scenario binds this the named fixture input value to client.post's content input. + "/v1/chat/completions", content=b'{"model":"compat-id","max_tokens":1}', + # What: arrange the content type field as application and json; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility carries content type through alias response into assert alias response status code equals canonical response status code equals 200. + headers={"Content-Type": "application/json"}, + # What: arrange the client.post call with content and headers; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.get and capture loaded; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + loaded = client.get("/v1/models") + # What: act by calling client.post and capture canonical response; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + canonical_response = client.post( + # What: arrange the model field as canonical; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility sends this field through canonical response so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "canonical", "max_tokens": 1} + # What: arrange the client.post call with json; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.post and capture hidden response; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + hidden_response = client.post( + # What: arrange the model field as private id; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility sends this field through hidden response so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "private-id", "max_tokens": 1} + # What: arrange the client.post call with json; why: test_alias_routes_to_canonical_residency_and_model_list_respects_visibility groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.post and capture unloaded; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + unloaded = client.post("/router/unload", json={"name": "private-id"}) + + # What: act by calling listed.json and capture listed data; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + listed_data = listed.json()["data"] + # What: assert that item id for item in listed data equals canonical compat id; why: this assertion protects the alias routes to canonical residency and model list respects visibility regression after the test's arranged inputs and exercised call. + assert [item["id"] for item in listed_data] == ["canonical", "compat-id"] + # What: assert that item status value for item in equals unloaded; why: this assertion protects the alias routes to canonical residency and model list respects visibility regression after the test's arranged inputs and exercised call. + assert {item["status"]["value"] for item in listed_data} == {"unloaded"} + # What: act by calling loaded.json and capture loaded data; why: the alias routes to canonical residency and model list respects visibility test asserts the response, state, or failure produced by this call. + loaded_data = loaded.json()["data"] + # What: assert the expected item id item status value for item in loaded data == outcome; why: test router test alias routes to canonical residency and model list respects visibility protects its regression by requiring this observable result after the exercised behavior. + assert {item["id"]: item["status"]["value"] for item in loaded_data} == { + # What: arrange canonical loaded compat id loaded for the scenario; why: test router test alias routes to canonical residency and model list respects visibility requires this concrete input or helper state before exercising the behavior under test. + "canonical": "loaded", "compat-id": "loaded", + # What: arrange the grouped source fragment for the scenario; why: test router test alias routes to canonical residency and model list respects visibility requires this concrete input or. + } + # What: assert that alias response status code equals canonical response status code equals 200; why: this assertion protects the alias routes to canonical residency and model list respects visibility regression after the test's arranged inputs and exercised call. + assert alias_response.status_code == canonical_response.status_code == 200 + # What: assert that hidden response status code equals 200; why: this assertion protects the alias routes to canonical residency and model list respects visibility regression after the test's arranged inputs and exercised call. + assert hidden_response.status_code == 200 + # What: assert that unloaded json unloaded is true; why: this assertion protects the alias routes to canonical residency and model list respects visibility regression after the test's arranged inputs and exercised call. + assert unloaded.json()["unloaded"] is True + # What: assert the expected manager calls == outcome; why: test router test alias routes to canonical residency and model list respects visibility protects its regression by requiring this observable result after the exercised behavior. + assert manager.calls == [ + # What: arrange start shared gguf for the scenario; why: test router test alias routes to canonical residency and model list respects visibility requires this concrete input or helper state before exercising the behavior under test. + ("start", "shared.gguf"), + # What: arrange switch hidden gguf for the scenario; why: test router test alias routes to canonical residency and model list respects visibility requires this concrete input or helper state before exercising the behavior under test. + ("switch", "hidden.gguf"), + # What: arrange stop 30.0 for the scenario; why: test router test alias routes to canonical residency and model list respects visibility requires this concrete input or helper state before exercising the behavior under test. + ("stop", 30.0), + # What: arrange the grouped source fragment for the scenario; why: test router test alias routes to canonical residency and model list respects visibility requires this concrete input. + ] + # What: assert that calls 0 body equals b model compat id max tokens 1; why: this assertion protects the alias routes to canonical residency and model list respects visibility regression after the test's arranged inputs and exercised call. + assert calls[0]["body"] == b'{"model":"compat-id","max_tokens":1}' + # What: assert that router status active profile is group delimiter; why: this assertion protects the alias routes to canonical residency and model list respects visibility regression after the test's arranged inputs and exercised call. + assert router.status()["activeProfile"] is None + + +# What: define the test_pin_and_warm_selectors_resolve_against_one_resident_slot test around local fixtures; why: this test groups the arrange, act, and assertions that protect the pin and warm selectors resolve against one resident slot outcome. +def test_pin_and_warm_selectors_resolve_against_one_resident_slot(): + # What: act by calling Manager and capture manager; why: the pin and warm selectors resolve against one resident slot test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the pin and warm selectors resolve against one resident slot test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with a and b; why: test_pin_and_warm_selectors_resolve_against_one_resident_slot groups the supplied clauses as one catalog_doc mapping before its value is consumed. + { + # What: arrange the a field as model profile and a and a and gguf; why: test_pin_and_warm_selectors_resolve_against_one_resident_slot carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "a": ModelProfile("a", "a.gguf", ()), + # What: arrange the b field as model profile and b and b and gguf; why: test_pin_and_warm_selectors_resolve_against_one_resident_slot carries b through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "b": ModelProfile("b", "b.gguf", ()), + # What: arrange the catalog_doc mapping with a and b; why: test_pin_and_warm_selectors_resolve_against_one_resident_slot groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange selectors to ModelCatalog; why: the pin and warm selectors resolve against one resident slot scenario binds this model selector and pinned and warm and pinned and pin value to ModelCatalog's selectors input. + selectors={ + # What: arrange the pinned field as model selector and pinned and pin and a and b; why: test_pin_and_warm_selectors_resolve_against_one_resident_slot carries pinned through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "pinned": ModelSelector("pinned", "pin", ("a", "b")), + # What: arrange the warm field as model selector and warm and warm and a and b; why: test_pin_and_warm_selectors_resolve_against_one_resident_slot carries warm through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "warm": ModelSelector("warm", "warm", ("a", "b")), + # What: arrange the catalog_doc mapping with pinned and warm; why: test_pin_and_warm_selectors_resolve_against_one_resident_slot groups the supplied clauses as one catalog_doc mapping before its value is consumed. + }, + # What: arrange the ModelCatalog call with selectors; why: test_pin_and_warm_selectors_resolve_against_one_resident_slot groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the pin and warm selectors resolve against one resident slot test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange the exact router acquire b release fixture fragment; why: the pin and warm selectors resolve against one resident slot scenario feeds this byte-preserved fragment through router.acquire("b").release() before asserting its protocol or parser result. + router.acquire("b").release() + + # What: act by calling router.acquire and capture warm; why: the pin and warm selectors resolve against one resident slot test asserts the response, state, or failure produced by this call. + warm = router.acquire("warm") + # What: assert that warm profile name warm model id warm selector id equals b b warm; why: this assertion protects the pin and warm selectors resolve against one resident slot regression after the test's arranged inputs and exercised call. + assert (warm.profile.name, warm.model_id, warm.selector_id) == ("b", "b", "warm") + # What: act by calling warm.release with the declared inputs; why: the pin and warm selectors resolve against one resident slot scenario observes the warm.release return value during pinned router acquire pinned. + warm.release() + + # What: act by calling router.acquire and capture pinned; why: the pin and warm selectors resolve against one resident slot test asserts the response, state, or failure produced by this call. + pinned = router.acquire("pinned") + # What: assert the expected pinned profile name pinned model id pinned selector id == outcome; why: test router test pin and warm selectors resolve against one resident slot protects its regression by requiring this observable result after the exercised behavior. + assert (pinned.profile.name, pinned.model_id, pinned.selector_id) == ( + # What: arrange a a pinned for the scenario; why: test router test pin and warm selectors resolve against one resident slot requires this concrete input or helper state before exercising the behavior under test. + "a", "a", "pinned", + # What: arrange the grouped source fragment for the scenario; why: test router test pin and warm selectors resolve against one resident slot requires this concrete input or helper state before exercising the behavior under test. + ) + # What: act by calling pinned.release with the declared inputs; why: the pin and warm selectors resolve against one resident slot scenario observes the pinned.release return value during assert manager calls start b gguf switch a gguf. + pinned.release() + # What: assert that manager calls equals start b gguf switch a gguf; why: this assertion protects the pin and warm selectors resolve against one resident slot regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "b.gguf"), ("switch", "a.gguf")] + + +# What: define the test_warm_selector_cold_fallback_uses_first_target test around local fixtures; why: this test groups the arrange, act, and assertions that protect the warm selector cold fallback uses first target outcome. +def test_warm_selector_cold_fallback_uses_first_target(): + # What: act by calling ModelCatalog and capture catalog doc; why: the warm selector cold fallback uses first target test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with a and b; why: test_warm_selector_cold_fallback_uses_first_target groups the supplied clauses as one catalog_doc mapping before its value is consumed. + { + # What: arrange the a field as model profile and a and a and gguf; why: test_warm_selector_cold_fallback_uses_first_target carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "a": ModelProfile("a", "a.gguf", ()), + # What: arrange the b field as model profile and b and b and gguf; why: test_warm_selector_cold_fallback_uses_first_target carries b through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "b": ModelProfile("b", "b.gguf", ()), + # What: arrange the catalog_doc mapping with a and b; why: test_warm_selector_cold_fallback_uses_first_target groups the supplied clauses as one catalog_doc mapping before its value is consumed. + }, + # What: arrange the warm field as model selector and warm and warm and a and b; why: test_warm_selector_cold_fallback_uses_first_target carries warm through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + selectors={"warm": ModelSelector("warm", "warm", ("a", "b"))}, + # What: arrange the ModelCatalog call with selectors; why: test_warm_selector_cold_fallback_uses_first_target groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the warm selector cold fallback uses first target test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(Manager(), catalog_doc, object(), ready_fn=ready) + # What: act by calling router.acquire and capture lease; why: the warm selector cold fallback uses first target test asserts the response, state, or failure produced by this call. + lease = router.acquire("warm") + # What: assert that lease profile name lease model id equals a a; why: this assertion protects the warm selector cold fallback uses first target regression after the test's arranged inputs and exercised call. + assert (lease.profile.name, lease.model_id) == ("a", "a") + # What: act by calling lease.release with the declared inputs; why: the warm selector cold fallback uses first target scenario observes the lease.release return value during the enclosing return. + lease.release() + + +# What: parameterize test_selector_reservation_uses_atomically_resolved_target_loading_policy with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test selector reservation uses atomically resolved target loading policy. +@pytest.mark.parametrize("target_setting,global_setting,expected", [ + # What: arrange the grouped expression portion of the enclosing predicate; why: this clause remains in the selector reservation uses atomically resolved target loading policy scenario\'s enclosing expression so its grouping and evaluation order stay intact. + (False, True, False), + # What: arrange the grouped expression portion of the enclosing predicate; why: this clause remains in the selector reservation uses atomically resolved target loading policy scenario\'s enclosing expression so its grouping and evaluation order stay intact. + (True, False, True), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_selector_reservation_uses_atomically_resolved_target_loading_policy groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_selector_reservation_uses_atomically_resolved_target_loading_policy test around target setting and global setting and expected; why: this test groups the arrange, act, and assertions that protect the selector reservation uses atomically resolved target loading policy outcome. +def test_selector_reservation_uses_atomically_resolved_target_loading_policy( + # What: arrange target setting global setting expected for the scenario; why: test selector reservation uses atomically resolved target loading policy requires this concrete input or helper state before exercising the behavior under test. + target_setting, global_setting, expected +# What: arrange the grouped source fragment for the scenario; why: test selector reservation uses atomically resolved target loading policy requires this concrete input or helper state before exercising the behavior under test. +): + # What: act by calling ModelCatalog and capture catalog doc; why: the selector reservation uses atomically resolved target loading policy test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the a field as model profile and target setting and a and a and gguf; why: test_selector_reservation_uses_atomically_resolved_target_loading_policy carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"a": ModelProfile("a", "a.gguf", (), send_loading_state=target_setting)}, + # What: arrange settings to RouterSettings; why: the selector reservation uses atomically resolved target loading policy scenario binds this router settings and global setting value to RouterSettings's settings input. + settings=RouterSettings(send_loading_state=global_setting), + # What: arrange the public field as model selector and public and pin and a; why: test_selector_reservation_uses_atomically_resolved_target_loading_policy carries public through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + selectors={"public": ModelSelector("public", "pin", ("a",))}, + # What: arrange the ModelCatalog call with settings and selectors; why: test_selector_reservation_uses_atomically_resolved_target_loading_policy groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the selector reservation uses atomically resolved target loading policy test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(Manager(), catalog_doc, object(), ready_fn=ready) + # What: arrange reserved as the fixture input; why: the selector reservation uses atomically resolved target loading policy test consumes this named precondition before exercising the behavior. + reserved = [] + + # What: act by calling router.acquire and capture lease; why: the selector reservation uses atomically resolved target loading policy test asserts the response, state, or failure produced by this call. + lease = router.acquire( + # What: arrange the public portion of lease; why: the selector reservation uses atomically resolved target loading policy scenario uses this clause to evaluate lease as one grouped value. + "public", + # What: arrange the loading input for test_selector_reservation_uses_atomically_resolved_target_loading_policy; why: test_selector_reservation_uses_atomically_resolved_target_loading_policy consumes loading during signature binding, so callers must bind it with the other signature inputs. + on_reserved=lambda loading, position: reserved.append((loading, position)), + # What: arrange apply loading policy to router.acquire; why: the selector reservation uses atomically resolved target loading policy scenario binds this true value to router.acquire's apply loading policy input. + apply_loading_policy=True, + # What: arrange the router.acquire call with on reserved and apply loading policy; why: test_selector_reservation_uses_atomically_resolved_target_loading_policy groups the supplied clauses as one router.acquire call before its value is consumed. + ) + # What: act by calling lease.release with the declared inputs; why: the selector reservation uses atomically resolved target loading policy scenario observes the lease.release return value during assert reserved expected. + lease.release() + + # What: assert that reserved equals expected 1; why: this assertion protects the selector reservation uses atomically resolved target loading policy regression after the test's arranged inputs and exercised call. + assert reserved == [(expected, 1)] + + +# What: define the test_warm_selector_joins_the_first_starting_target test around local fixtures; why: this test groups the arrange, act, and assertions that protect the warm selector joins the first starting target outcome. +def test_warm_selector_joins_the_first_starting_target(): + # What: act by calling Manager and capture manager; why: the warm selector joins the first starting target test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling threading.Event and capture activation started; why: the warm selector joins the first starting target test asserts the response, state, or failure produced by this call. + activation_started = threading.Event() + # What: act by calling threading.Event and capture finish activation; why: the warm selector joins the first starting target test asserts the response, state, or failure produced by this call. + finish_activation = threading.Event() + # What: act by calling ModelCatalog and capture catalog doc; why: the warm selector joins the first starting target test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with a and b; why: test_warm_selector_joins_the_first_starting_target groups the supplied clauses as one catalog_doc mapping before its value is consumed. + { + # What: arrange the a field as model profile and a and a and gguf; why: test_warm_selector_joins_the_first_starting_target carries a through catalog doc into router routing coordinator manager catalog doc object ready fn blocking ready. + "a": ModelProfile("a", "a.gguf", ()), + # What: arrange the b field as model profile and b and b and gguf; why: test_warm_selector_joins_the_first_starting_target carries b through catalog doc into router routing coordinator manager catalog doc object ready fn blocking ready. + "b": ModelProfile("b", "b.gguf", ()), + # What: arrange the catalog_doc mapping with a and b; why: test_warm_selector_joins_the_first_starting_target groups the supplied clauses as one catalog_doc mapping before its value is consumed. + }, + # What: arrange the warm field as model selector and warm and warm and a and b; why: test_warm_selector_joins_the_first_starting_target carries warm through catalog doc into router routing coordinator manager catalog doc object ready fn blocking ready. + selectors={"warm": ModelSelector("warm", "warm", ("a", "b"))}, + # What: arrange the ModelCatalog call with selectors; why: test_warm_selector_joins_the_first_starting_target groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + + # What: define the blocking_ready test helper around manager and probe and pid and port and timeout s; why: the warm selector joins the first starting target scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def blocking_ready(manager, probe, *, pid, port, timeout_s): + # What: act by calling activation_started.set with the declared inputs; why: the warm selector joins the first starting target scenario observes the activation_started.set return value during assert finish activation wait. + activation_started.set() + # What: assert that finish activation wait 2; why: this assertion protects the warm selector joins the first starting target regression after the test's arranged inputs and exercised call. + assert finish_activation.wait(2) + # What: arrange the ready field as true; why: blocking_ready carries ready into return {"ready": True, "health": {"status": "ok"}}. + return {"ready": True, "health": {"status": "ok"}} + + # What: act by calling RoutingCoordinator and capture router; why: the warm selector joins the first starting target test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=blocking_ready) + # What: arrange leases as the fixture input; why: the warm selector joins the first starting target test consumes this named precondition before exercising the behavior. + leases = [] + # What: act by calling threading.Thread and capture first; why: the warm selector joins the first starting target test asserts the response, state, or failure produced by this call. + first = threading.Thread(target=lambda: leases.append(router.acquire("b"))) + # What: act by calling threading.Thread and capture second; why: the warm selector joins the first starting target test asserts the response, state, or failure produced by this call. + second = threading.Thread(target=lambda: leases.append(router.acquire("warm"))) + # What: act by calling first.start with the declared inputs; why: the warm selector joins the first starting target scenario observes the first.start return value during assert activation started wait. + first.start() + # What: assert that activation started wait 1; why: this assertion protects the warm selector joins the first starting target regression after the test's arranged inputs and exercised call. + assert activation_started.wait(1) + # What: act by calling second.start with the declared inputs; why: the warm selector joins the first starting target scenario observes the second.start return value during for value in range. + second.start() + # What: act across range to perform status and router; why: the warm selector joins the first starting target scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the warm selector joins the first starting target scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the warm selector joins the first starting target scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the warm selector joins the first starting target scenario observes the time.sleep return value during assert router status queued requests. + time.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the warm selector joins the first starting target regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: act by calling finish_activation.set with the declared inputs; why: the warm selector joins the first starting target scenario observes the finish_activation.set return value during first join. + finish_activation.set() + # What: act by calling first.join with 2; why: the warm selector joins the first starting target scenario observes the first.join return value during second join. + first.join(2) + # What: act by calling second.join with 2; why: the warm selector joins the first starting target scenario observes the second.join return value during assert not first is alive and not second is alive. + second.join(2) + + # What: assert that not first is alive and not second is alive; why: this assertion protects the warm selector joins the first starting target regression after the test's arranged inputs and exercised call. + assert not first.is_alive() and not second.is_alive() + # What: act by calling next and capture selector lease; why: the warm selector joins the first starting target test asserts the response, state, or failure produced by this call. + selector_lease = next(lease for lease in leases if lease.selector_id == "warm") + # What: assert that selector lease profile name selector lease model id equals b b; why: this assertion protects the warm selector joins the first starting target regression after the test's arranged inputs and exercised call. + assert (selector_lease.profile.name, selector_lease.model_id) == ("b", "b") + # What: assert that manager calls equals start b gguf; why: this assertion protects the warm selector joins the first starting target regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "b.gguf")] + # What: act across leases to perform release and lease; why: the warm selector joins the first starting target scenario repeats the body only while or for the loop header admits an iteration. + for lease in leases: + # What: act by calling lease.release with the declared inputs; why: the warm selector joins the first starting target scenario observes the lease.release return value during the enclosing return. + lease.release() + + +# What: define the test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the selector rewrites before target alias filters and is not an upstream id outcome. +def test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id( + # What: arrange monkeypatch for the scenario; why: test selector rewrites before target alias filters and is not an upstream id requires this concrete input or helper state before exercising the behavior under test. + monkeypatch +# What: arrange the grouped source fragment for the scenario; why: test selector rewrites before target alias filters and is not an upstream id requires this concrete input or helper state before exercising the behavior under test. +): + # What: act by calling RequestField and capture alias fields; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + alias_fields = (("a:high", ( + # What: act by calling RequestField with temperature and 0 1; why: the selector rewrites before target alias filters and is not an upstream id scenario observes the RequestField return value while evaluating RequestField(("temperature",), "0.1"). + RequestField(("temperature",), "0.1"), + # What: arrange the grouped expression portion of alias fields; why: the selector rewrites before target alias filters and is not an upstream id scenario uses this clause to evaluate alias fields as one grouped value. + )),) + # What: act by calling ModelCatalog and capture catalog doc; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the a field as model profile and alias fields and a and private and gguf; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"a": ModelProfile( + # What: arrange aliases to ModelProfile; why: the selector rewrites before target alias filters and is not an upstream id scenario binds this a and high value to ModelProfile's aliases input. + "a", "private.gguf", (), aliases=("a:high",), + # What: arrange set fields by id to ModelProfile; why: the selector rewrites before target alias filters and is not an upstream id scenario binds this alias fields value to ModelProfile's set fields by id input. + set_fields_by_id=alias_fields, + # What: arrange the catalog_doc mapping with a; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id groups the supplied clauses as one catalog_doc mapping before its value is consumed. + )}, + # What: arrange selectors to ModelCatalog; why: the selector rewrites before target alias filters and is not an upstream id scenario binds this model selector and public and public and pin and public value to ModelCatalog's selectors input. + selectors={ + # What: arrange the public field as model selector and public and pin and public and model; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id carries public through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "public": ModelSelector( + # What: arrange the public pin a high public model portion of catalog doc; why: the selector rewrites before target alias filters and is not an upstream id scenario uses this clause to evaluate catalog doc as one grouped value. + "public", "pin", ("a:high",), "Public Model", "Stable target" + # What: arrange the ModelSelector call with ordered positional inputs; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id groups the supplied clauses as one ModelSelector call before its value is consumed. + ), + # What: arrange the catalog_doc mapping with public; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id groups the supplied clauses as one catalog_doc mapping before its value is consumed. + }, + # What: arrange the ModelCatalog call with selectors; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling Manager and capture manager; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange seen as the fixture input; why: the selector rewrites before target alias filters and is not an upstream id test consumes this named precondition before exercising the behavior. + seen = {} + # What: act by calling LogRing and capture router ring; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + router_ring = LogRing() + + # What: define the upstream test helper around captured fixture state; why: the selector rewrites before target alias filters and is not an upstream id scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling seen.update with kwargs; why: the selector rewrites before target alias filters and is not an upstream id scenario observes the seen.update return value during return upstream response content type application json bytes io. + seen.update(kwargs) + # What: arrange the helper response as UpstreamResponse 200 Content Type application json BytesIO b; why: test router test feeds this result into the. + return UpstreamResponse(200, {"Content-Type": "application/json"}, BytesIO(b'{}')) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the selector rewrites before target alias filters and is not an upstream id scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the selector rewrites before target alias filters and is not an upstream id scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange router ring to build_app; why: the selector rewrites before target alias filters and is not an upstream id scenario binds this router ring value to build_app's router ring input. + router_ring=router_ring, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.post and capture response; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + response = client.post( + # What: arrange the model field as public; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id sends this field through response so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "public", "messages": []} + # What: arrange the client.post call with json; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.post and capture direct; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + direct = client.post( + # What: arrange the upstream public v1 chat completions portion of direct; why: the selector rewrites before target alias filters and is not an upstream id scenario uses this clause to evaluate direct as one grouped value. + "/upstream/public/v1/chat/completions", + # What: arrange the model field as public; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id sends this field through direct so the router selects the canonical model or alias for upstream dispatch. + json={"model": "public", "messages": []}, + # What: arrange the client.post call with json; why: test_selector_rewrites_before_target_alias_filters_and_is_not_an_upstream_id groups the supplied clauses as one client.post call before its value is consumed. + ) + + # What: assert that response status code equals 200; why: this assertion protects the selector rewrites before target alias filters and is not an upstream id regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that json loads seen body model equals a high; why: this assertion protects the selector rewrites before target alias filters and is not an upstream id regression after the test's arranged inputs and exercised call. + assert json.loads(seen["body"])["model"] == "a:high" + # What: assert that json loads seen body temperature equals 0 1; why: this assertion protects the selector rewrites before target alias filters and is not an upstream id regression after the test's arranged inputs and exercised call. + assert json.loads(seen["body"])["temperature"] == 0.1 + # What: assert that direct status code equals 404; why: this assertion protects the selector rewrites before target alias filters and is not an upstream id regression after the test's arranged inputs and exercised call. + assert direct.status_code == 404 + # What: act by calling json.loads and capture events; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + events = [json.loads(item["text"]) for item in router_ring.since(0)[0]] + # What: act by calling next and capture admitted; why: the selector rewrites before target alias filters and is not an upstream id test asserts the response, state, or failure produced by this call. + admitted = next(event for event in events if event["event"] == "admitted") + # What: assert that admitted profile equals a; why: this assertion protects the selector rewrites before target alias filters and is not an upstream id regression after the test's arranged inputs and exercised call. + assert admitted["profile"] == "a" + # What: assert that admitted selector equals public; why: this assertion protects the selector rewrites before target alias filters and is not an upstream id regression after the test's arranged inputs and exercised call. + assert admitted["selector"] == "public" + # What: assert that admitted target equals a high; why: this assertion protects the selector rewrites before target alias filters and is not an upstream id regression after the test's arranged inputs and exercised call. + assert admitted["target"] == "a:high" + + +# What: define the test_selector_model_listing_uses_strategy_specific_loaded_status test around local fixtures; why: this test groups the arrange, act, and assertions that protect the selector model listing uses strategy specific loaded status outcome. +def test_selector_model_listing_uses_strategy_specific_loaded_status(): + # What: act by calling Manager and capture manager; why: the selector model listing uses strategy specific loaded status test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the selector model listing uses strategy specific loaded status test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with a and b; why: test_selector_model_listing_uses_strategy_specific_loaded_status groups the supplied clauses as one catalog_doc mapping before its value. + { + # What: arrange the a field as model profile and a and a and gguf; why: test_selector_model_listing_uses_strategy_specific_loaded_status carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "a": ModelProfile("a", "a.gguf", ()), + # What: arrange the b field as model profile and b and b and gguf; why: test_selector_model_listing_uses_strategy_specific_loaded_status carries b through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "b": ModelProfile("b", "b.gguf", ()), + # What: arrange the catalog_doc mapping with a and b; why: test_selector_model_listing_uses_strategy_specific_loaded_status groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange selectors to ModelCatalog; why: the selector model listing uses strategy specific loaded status scenario binds this model selector and pin and warm and hidden and pin value to ModelCatalog's selectors input. + selectors={ + # What: arrange the pin field as model selector and pin and pin and pinned and first; why: test_selector_model_listing_uses_strategy_specific_loaded_status carries pin through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "pin": ModelSelector( + # What: arrange the pin pin a b pinned first portion of catalog doc; why: the selector model listing uses strategy specific loaded status scenario uses this clause to evaluate catalog doc as one grouped value. + "pin", "pin", ("a", "b"), "Pinned", "First only", + # What: arrange metadata json to ModelSelector; why: the selector model listing uses strategy specific loaded status scenario binds this tier and stable and type and operator value value to ModelSelector's metadata json input. + metadata_json='{"tier":"stable","type":"operator-value"}', + # What: arrange the ModelSelector call with metadata json; why: test_selector_model_listing_uses_strategy_specific_loaded_status groups the supplied clauses as one ModelSelector call before its value is consumed. + ), + # What: arrange the warm field as model selector and warm and warm and a and b; why: test_selector_model_listing_uses_strategy_specific_loaded_status carries warm through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "warm": ModelSelector("warm", "warm", ("a", "b")), + # What: arrange the hidden field as model selector and hidden and pin and b and true; why: test_selector_model_listing_uses_strategy_specific_loaded_status carries hidden through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "hidden": ModelSelector("hidden", "pin", ("b",), unlisted=True), + # What: arrange the catalog_doc mapping with pin and warm and hidden; why: test_selector_model_listing_uses_strategy_specific_loaded_status groups the supplied clauses as one catalog_doc mapping before its value is consumed. + }, + # What: arrange the ModelCatalog call with selectors; why: test_selector_model_listing_uses_strategy_specific_loaded_status groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the selector model listing uses strategy specific loaded status test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange the exact router acquire b release fixture fragment; why: the selector model listing uses strategy specific loaded status scenario feeds this byte-preserved fragment through router.acquire("b").release() before asserting its protocol or parser result. + router.acquire("b").release() + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_selector_model_listing_uses_strategy_specific_loaded_status releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the selector model listing uses strategy specific loaded status test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_selector_model_listing_uses_strategy_specific_loaded_status; why: test_selector_model_listing_uses_strategy_specific_loaded_status consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the selector model listing uses strategy specific loaded status scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_selector_model_listing_uses_strategy_specific_loaded_status groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the selector model listing uses strategy specific loaded status test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling operation.json and capture records; why: the selector model listing uses strategy specific loaded status test asserts the response, state, or failure produced by this call. + records = {item["id"]: item for item in client.get("/v1/models").json()["data"]} + # What: act by calling operation.json and capture management; why: the selector model listing uses strategy specific loaded status test asserts the response, state, or failure produced by this call. + management = client.get("/router/profiles").json() + + # What: assert that hidden is absent from records; why: this assertion protects the selector model listing uses strategy specific loaded status regression after the test's arranged inputs and exercised call. + assert "hidden" not in records + # What: assert that records pin status value equals unloaded; why: this assertion protects the selector model listing uses strategy specific loaded status regression after the test's arranged inputs and exercised call. + assert records["pin"]["status"]["value"] == "unloaded" + # What: assert that records warm status value equals loaded; why: this assertion protects the selector model listing uses strategy specific loaded status regression after the test's arranged inputs and exercised call. + assert records["warm"]["status"]["value"] == "loaded" + # What: assert that records pin name equals pinned; why: this assertion protects the selector model listing uses strategy specific loaded status regression after the test's arranged inputs and exercised call. + assert records["pin"]["name"] == "Pinned" + # What: assert that records pin description equals first only; why: this assertion protects the selector model listing uses strategy specific loaded status regression after the test's arranged inputs and exercised call. + assert records["pin"]["description"] == "First only" + # What: assert the expected records pin meta == freetoken outcome; why: test router test selector model listing uses strategy specific loaded status protects its regression by requiring this observable result after the exercised behavior. + assert records["pin"]["meta"] == {"freetoken": { + # What: arrange tier stable type selector strategy pin for the scenario; why: test router test selector model listing uses strategy specific loaded status requires this concrete input or helper state before exercising the behavior under test. + "tier": "stable", "type": "selector", "strategy": "pin", + # What: arrange targets a b for the scenario; why: test router test selector model listing uses strategy specific loaded status requires this concrete input or helper state before exercising the behavior under test. + "targets": ["a", "b"], + # What: arrange the grouped source fragment for the scenario; why: test router test selector model listing uses strategy specific loaded status requires this concrete input or helper state before exercising. + }} + # What: assert the expected item name for item in management selectors == outcome; why: test router test selector model listing uses strategy specific loaded status protects its regression by requiring this observable result after the exercised behavior. + assert {item["name"] for item in management["selectors"]} == { + # What: arrange pin warm hidden for the scenario; why: test router test selector model listing uses strategy specific loaded status requires this concrete input or helper state before exercising the behavior under test. + "pin", "warm", "hidden", + # What: arrange the grouped source fragment for the scenario; why: test router test selector model listing uses strategy specific loaded status requires this concrete input or helper state before. + } + + +# What: define the test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models test around local fixtures; why: this test groups the arrange, act, and assertions that protect the runtime profile pins compose before warm selectors and can shadow models outcome. +def test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models(): + # What: act by calling ModelCatalog and capture catalog doc; why: the runtime profile pins compose before warm selectors and can shadow models test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with a and b; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models groups the supplied clauses as one catalog_doc mapping before its value. + { + # What: arrange the a field as model profile and a and a and gguf; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "a": ModelProfile("a", "a.gguf", ()), + # What: arrange the b field as model profile and b and b and gguf; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models carries b through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "b": ModelProfile("b", "b.gguf", ()), + # What: arrange the catalog_doc mapping with a and b; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange the warm field as model selector and warm and warm and a and b; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models carries warm through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + selectors={"warm": ModelSelector("warm", "warm", ("a", "b"))}, + # What: arrange the coding field as routing profile and coding and a and b and disabled; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models carries coding through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + routing_profiles={"coding": RoutingProfile( + # What: arrange the coding a b disabled public warm portion of catalog doc; why: the runtime profile pins compose before warm selectors and can shadow models scenario uses this clause to evaluate catalog doc as one grouped value. + "coding", (("a", "b"), ("disabled", None), ("public", "warm")) + # What: arrange the catalog_doc mapping with coding; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models groups the supplied clauses as one catalog_doc mapping before its value is consumed. + )}, + # What: arrange the ModelCatalog call with selectors and routing profiles; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling Manager and capture manager; why: the runtime profile pins compose before warm selectors and can shadow models test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the runtime profile pins compose before warm selectors and can shadow models test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange the exact router acquire b release fixture fragment; why: the runtime profile pins compose before warm selectors and can shadow models scenario feeds this byte-preserved fragment through router.acquire("b").release() before asserting its protocol or parser result. + router.acquire("b").release() + # What: assert that router set active routing profile coding equals coding; why: this assertion protects the runtime profile pins compose before warm selectors and can shadow models regression after the test's arranged inputs and exercised call. + assert router.set_active_routing_profile("coding") == "coding" + + # What: act by calling router.acquire and capture selected; why: the runtime profile pins compose before warm selectors and can shadow models test asserts the response, state, or failure produced by this call. + selected = router.acquire("public") + # What: Assert assert in test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models uses this assert to implement the named assert operation. + assert ( + # What: Assert selected profile name in test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models uses this assert to implement the named selected profile name operation. + selected.profile.name, + # What: Assert selected model id in test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models uses this assert to implement the named selected model id operation. + selected.model_id, + # What: Assert selected selector id in test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models uses this assert to implement the named selected selector id operation. + selected.selector_id, + # What: Assert selected routing profile id in test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models uses this assert to implement the named selected routing profile id operation. + selected.routing_profile_id, + # What: Assert selected pin id in test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models uses this assert to implement the named selected pin id operation. + selected.pin_id, + # What: Assert equals b b warm coding public in test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models; why: test_runtime_profile_pins_compose_before_warm_selectors_and_can_shadow_models uses this assert to implement the named equals b b warm coding public operation. + ) == ("b", "b", "warm", "coding", "public") + # What: act by calling selected.release with the declared inputs; why: the runtime profile pins compose before warm selectors and can shadow models scenario observes the selected.release return value during shadowed router acquire a. + selected.release() + # What: act by calling router.acquire and capture shadowed; why: the runtime profile pins compose before warm selectors and can shadow models test asserts the response, state, or failure produced by this call. + shadowed = router.acquire("a") + # What: assert that shadowed profile name shadowed model id shadowed pin id equals b b a; why: this assertion protects the runtime profile pins compose before warm selectors and can shadow models regression after the test's arranged inputs and exercised call. + assert (shadowed.profile.name, shadowed.model_id, shadowed.pin_id) == ("b", "b", "a") + # What: act by calling shadowed.release with the declared inputs; why: the runtime profile pins compose before warm selectors and can shadow models scenario observes the shadowed.release return value during with pytest raises routing error match disabled by. + shadowed.release() + # What: arrange with pytest raises RoutingError match disabled by routing profile as disabled for the scenario; why: test raises routing error match disabled by in test runtime profile pins compose before warm requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(RoutingError, match="disabled by routing profile") as disabled: + # What: arrange the exact router acquire disabled fixture fragment; why: the runtime profile pins compose before warm selectors and can shadow models scenario feeds this byte-preserved fragment through router.acquire("disabled") before asserting its protocol or parser result. + router.acquire("disabled") + # What: assert that disabled value code equals unknown model; why: this assertion protects the runtime profile pins compose before warm selectors and can shadow models regression after the test's arranged inputs and exercised call. + assert disabled.value.code == "unknown_model" + # What: assert that router has routable id disabled is false; why: this assertion protects the runtime profile pins compose before warm selectors and can shadow models regression after the test's arranged inputs and exercised call. + assert router.has_routable_id("disabled") is False + # What: assert that manager calls equals start b gguf; why: this assertion protects the runtime profile pins compose before warm selectors and can shadow models regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "b.gguf")] + + # What: assert that router set active routing profile is group delimiter; why: this assertion protects the runtime profile pins compose before warm selectors and can shadow models regression after the test's arranged inputs and exercised call. + assert router.set_active_routing_profile(None) is None + # What: arrange with pytest raises RoutingError as missing for the scenario; why: test raises routing error as missing requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(RoutingError) as missing: + # What: arrange the exact router acquire public fixture fragment; why: the runtime profile pins compose before warm selectors and can shadow models scenario feeds this byte-preserved fragment through router.acquire("public") before asserting its protocol or parser result. + router.acquire("public") + # What: assert that missing value code equals unknown model; why: this assertion protects the runtime profile pins compose before warm selectors and can shadow models regression after the test's arranged inputs and exercised call. + assert missing.value.code == "unknown_model" + # What: arrange with pytest raises RoutingError as unknown profile for the scenario; why: test raises routing error as unknown profile requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(RoutingError) as unknown_profile: + # What: arrange the exact router set active routing profile missing fixture fragment; why: the runtime profile pins compose before warm selectors and can shadow models scenario feeds this byte-preserved fragment through router.set_active_routing_profile("missing") before asserting its protocol or parser result. + router.set_active_routing_profile("missing") + # What: assert that unknown profile value code equals unknown profile; why: this assertion protects the runtime profile pins compose before warm selectors and can shadow models regression after the test's arranged inputs and exercised call. + assert unknown_profile.value.code == "unknown_profile" + + +# What: define the test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the runtime profile http rewrites before alias filters and lists virtual pins outcome. +def test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins( + # What: arrange monkeypatch for the scenario; why: test runtime profile http rewrites before alias filters and lists virtual pins requires this concrete input or helper state before exercising the behavior under test. + monkeypatch +# What: arrange the grouped source fragment for the scenario; why: test runtime profile http rewrites before alias filters and lists virtual pins requires this concrete input or helper state before exercising the behavior under test. +): + # What: act by calling ModelCatalog and capture catalog doc; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the a field as model profile and request field and a and private and gguf; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"a": ModelProfile( + # What: arrange aliases to ModelProfile; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario binds this a and high value to ModelProfile's aliases input. + "a", "private.gguf", (), aliases=("a:high",), + # What: arrange set fields by id to RequestField; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario binds this request field and a and high and 0 1 and temperature value to RequestField's set fields by id input. + set_fields_by_id=(("a:high", (RequestField(("temperature",), "0.1"),)),), + # What: arrange the catalog_doc mapping with a; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins groups the supplied clauses as one catalog_doc mapping before its value is consumed. + )}, + # What: arrange the coding field as routing profile and coding and coding and mode and disabled; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins carries coding through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + routing_profiles={"coding": RoutingProfile( + # What: arrange the coding portion of catalog doc; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario uses this clause to evaluate catalog doc as one grouped value. + "coding", + # What: arrange the disabled public a high portion of catalog doc; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario uses this clause to evaluate catalog doc as one grouped value. + (("disabled", None), ("public", "a:high")), + # What: arrange the coding mode portion of catalog doc; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario uses this clause to evaluate catalog doc as one grouped value. + "Coding mode", + # What: arrange the catalog_doc mapping with coding; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins groups the supplied clauses as one catalog_doc mapping before its value is consumed. + )}, + # What: arrange the ModelCatalog call with routing profiles; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling Manager and capture manager; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange seen as the fixture input; why: the runtime profile http rewrites before alias filters and lists virtual pins test consumes this named precondition before exercising the behavior. + seen = [] + # What: act by calling LogRing and capture ring; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + ring = LogRing() + + # What: define the upstream test helper around captured fixture state; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling seen.append with kwargs; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario observes the seen.append return value during return upstream response content type application json bytes io. + seen.append(kwargs) + # What: arrange the helper response as UpstreamResponse 200 Content Type application json BytesIO b; why: test runtime profile http rewrites feeds this result into the behavior whose outcome is asserted. + return UpstreamResponse(200, {"Content-Type": "application/json"}, BytesIO(b'{}')) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) befo. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange router ring to build_app; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario binds this ring value to build_app's router ring input. + router_ring=ring, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling operation.json and capture initial; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + initial = client.get("/router/profiles").json() + # What: act by calling client.put and capture activated; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + activated = client.put("/router/profiles/active", json={"name": "coding"}) + # What: act by calling client.post and capture routed; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + routed = client.post( + # What: arrange the model field as public; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins sends this field through routed so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "public", "messages": []} + # What: arrange the client.post call with json; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.post and capture direct; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + direct = client.post( + # What: arrange the upstream public custom fpart opaque a portion of direct; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario uses this clause to evaluate direct as one grouped value. + "/upstream/public/custom%2Fpart?opaque=a%2Fb", + # What: arrange content to client.post; why: the runtime profile http rewrites before alias filters and lists virtual pins scenario binds this the named fixture input value to client.post's content input. + content=b'{"model":"public"}', + # What: arrange the content type field as application and json; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins carries content type through direct into assert routed status code equals direct status code equals 200. + headers={"Content-Type": "application/json"}, + # What: arrange the client.post call with content and headers; why: test_runtime_profile_http_rewrites_before_alias_filters_and_lists_virtual_pins groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling operation.json and capture listed; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + listed = {item["id"]: item for item in client.get("/v1/models").json()["data"]} + # What: act by calling client.post and capture disabled; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + disabled = client.post("/v1/chat/completions", json={"model": "disabled"}) + # What: act by calling client.put and capture cleared; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + cleared = client.put("/router/profiles/active", json={"name": None}) + # What: act by calling client.put and capture missing; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + missing = client.put("/router/profiles/active", json={"name": "missing"}) + + # What: assert that initial active routing profile is group delimiter; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert initial["activeRoutingProfile"] is None + # What: assert the expected initial routingProfiles == outcome; why: test router test runtime profile http rewrites before alias filters and lists virtual pins protects its regression by requiring this observable result after the exercised behavior. + assert initial["routingProfiles"] == [{ + # What: arrange name coding description Coding mode for the scenario; why: test router test runtime profile http rewrites before alias filters and lists virtual pins requires this concrete input or helper state before exercising the behavior under test. + "name": "coding", "description": "Coding mode", + # What: arrange pins disabled None public a high for the scenario; why: test router test runtime profile http rewrites before alias filters and lists virtual pins requires this concrete input or helper state before exercising the behavior under test. + "pins": {"disabled": None, "public": "a:high"}, + # What: arrange the grouped source fragment for the scenario; why: test router test runtime profile http rewrites before alias filters and lists virtual pins requires this concrete input. + }] + # What: assert that activated json equals active coding; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert activated.json() == {"active": "coding"} + # What: assert that routed status code equals direct status code equals 200; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert routed.status_code == direct.status_code == 200 + # What: assert the expected json loads seen 0 body == outcome; why: test router test runtime profile http rewrites before alias filters and lists virtual pins protects its regression by requiring this observable result after the exercised behavior. + assert json.loads(seen[0]["body"]) == { + # What: arrange model a high messages temperature 0.1 for the scenario; why: test router test runtime profile http rewrites before alias filters and lists virtual pins requires this concrete input or helper state before exercising the behavior under test. + "model": "a:high", "messages": [], "temperature": 0.1, + # What: arrange the grouped source fragment for the scenario; why: test router test runtime profile http rewrites before alias filters and lists virtual pins requires this concrete. + } + # What: assert that seen 1 path and query equals custom 2 fpart opaque a 2; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert seen[1]["path_and_query"] == "/custom%2Fpart?opaque=a%2Fb" + # What: assert that json loads seen 1 body equals model public temperature 0 1; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert json.loads(seen[1]["body"]) == {"model": "public", "temperature": 0.1} + # What: assert that listed public status value equals unloaded; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert listed["public"]["status"]["value"] == "unloaded" + # What: assert that listed public meta equals freetoken type profile; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert listed["public"]["meta"] == {"freetoken": {"type": "profile"}} + # What: assert that disabled is absent from listed; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert "disabled" not in listed + # What: assert that disabled status code equals 404; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert disabled.status_code == 404 + # What: assert that cleared json equals active; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert cleared.json() == {"active": None} + # What: assert that missing status code equals 404; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert missing.status_code == 404 + # What: act by calling json.loads and capture events; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + events = [json.loads(item["text"]) for item in ring.since(0)[0]] + # What: act by calling next and capture admitted; why: the runtime profile http rewrites before alias filters and lists virtual pins test asserts the response, state, or failure produced by this call. + admitted = next(event for event in events if event["event"] == "admitted") + # What: assert that admitted routing profile equals coding; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert admitted["routingProfile"] == "coding" + # What: assert that admitted pin equals public; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert admitted["pin"] == "public" + # What: assert that admitted target equals a high; why: this assertion protects the runtime profile http rewrites before alias filters and lists virtual pins regression after the test's arranged inputs and exercised call. + assert admitted["target"] == "a:high" + + +# What: define the test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target test around local fixtures; why: this test groups the arrange, act, and assertions that protect the runtime profile direct upstream uses longest pin and rejects selector target outcome. +def test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target(): + # What: act by calling ModelCatalog and capture catalog doc; why: the runtime profile direct upstream uses longest pin and rejects selector target test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with a and b; why: test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target groups the supplied clauses as one catalog_doc mapping before its value. + { + # What: arrange the a field as model profile and a and a and gguf; why: test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "a": ModelProfile("a", "a.gguf", ()), + # What: arrange the b field as model profile and b and b and gguf; why: test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target carries b through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "b": ModelProfile("b", "b.gguf", ()), + # What: arrange the catalog_doc mapping with a and b; why: test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange the virtual field as model selector and virtual and pin and a; why: test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target carries virtual through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + selectors={"virtual": ModelSelector("virtual", "pin", ("a",))}, + # What: arrange the coding field as routing profile and coding and author and a and author; why: test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target carries coding through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + routing_profiles={"coding": RoutingProfile( + # What: arrange the coding author a author public b portion of catalog doc; why: the runtime profile direct upstream uses longest pin and rejects selector target scenario uses this clause to evaluate catalog doc as one grouped value. + "coding", (("author", "a"), ("author/public", "b"), ("select", "virtual")) + # What: arrange the catalog_doc mapping with coding; why: test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target groups the supplied clauses as one catalog_doc mapping before its value is consumed. + )}, + # What: arrange the ModelCatalog call with selectors and routing profiles; why: test_runtime_profile_direct_upstream_uses_longest_pin_and_rejects_selector_target groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the runtime profile direct upstream uses longest pin and rejects selector target test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(Manager(), catalog_doc, object(), ready_fn=ready) + # What: arrange the exact router set active routing profile coding fixture fragment; why: the runtime profile direct upstream uses longest pin and rejects selector target scenario feeds this byte-preserved fragment through router.set_active_routing_profile("coding") before asserting its protocol or parser result. + router.set_active_routing_profile("coding") + + # What: assert the expected router resolve upstream path author public v1 stats 2 == outcome; why: test runtime profile direct upstream uses longest protects its regression by requiring this observable result after the exercised behavior. + assert router.resolve_upstream_path("author/public/v1/stats")[:2] == ( + # What: arrange author public b for the scenario; why: test router test runtime profile direct upstream uses longest pin and rejects selector target requires this concrete input or helper state before exercising the behavior under test. + "author/public", "b", + # What: arrange the grouped source fragment for the scenario; why: test router test runtime profile direct upstream uses longest pin and rejects selector target requires this concrete input or helper state before exercising the behavior under test. + ) + # What: assert that router resolve upstream path author public v1 stats 3 equals v1 stats; why: this assertion protects the runtime profile direct upstream uses longest pin and rejects selector target regression after the test's arranged inputs and exercised call. + assert router.resolve_upstream_path("author/public/v1/stats")[3] == "/v1/stats" + # What: assert the pytest.raises failure context; why: the runtime profile direct upstream uses longest pin and rejects selector target scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match="configured model ID"): + # What: arrange the exact router resolve upstream path select v1 stats fixture fragment; why: the runtime profile direct upstream uses longest pin and rejects selector target scenario feeds this byte-preserved fragment through router.resolve_upstream_path("select/v1/stats") before asserting its protocol or par. + router.resolve_upstream_path("select/v1/stats") + + +# What: define the test_catalog_reload_clears_active_runtime_profile test around local fixtures; why: this test groups the arrange, act, and assertions that protect the catalog reload clears active runtime profile outcome. +def test_catalog_reload_clears_active_runtime_profile(): + # What: act by calling RoutingProfile and capture profile; why: the catalog reload clears active runtime profile test asserts the response, state, or failure produced by this call. + profile = RoutingProfile("coding", (("public", "a"),)) + # What: act by calling ModelCatalog and capture current; why: the catalog reload clears active runtime profile test asserts the response, state, or failure produced by this call. + current = ModelCatalog( + # What: arrange the a field as model profile and a and a and gguf; why: test_catalog_reload_clears_active_runtime_profile carries a through current into router routing coordinator manager current object ready fn ready. + {"a": ModelProfile("a", "a.gguf", ())}, routing_profiles={"coding": profile} + # What: arrange the ModelCatalog call with routing profiles; why: test_catalog_reload_clears_active_runtime_profile groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the catalog reload clears active runtime profile test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(Manager(), current, object(), ready_fn=ready) + # What: arrange the exact router set active routing profile coding fixture fragment; why: the catalog reload clears active runtime profile scenario feeds this byte-preserved fragment through router.set_active_routing_profile("coding") before asserting its protocol or parser result. + router.set_active_routing_profile("coding") + + # What: act by calling router.replace_catalog with model catalog and model profile and profile and a and a; why: the catalog reload clears active runtime profile scenario observes the router.replace_catalog return value during a model profile a a gguf routing profiles coding. + router.replace_catalog(ModelCatalog( + # What: arrange the a field as model profile and a and a and gguf; why: test_catalog_reload_clears_active_runtime_profile carries a into {"a": ModelProfile("a", "a.gguf", ())}, routing_profiles={"coding": prof. + {"a": ModelProfile("a", "a.gguf", ())}, routing_profiles={"coding": profile} + # What: arrange the router.replace_catalog call with model catalog; why: test_catalog_reload_clears_active_runtime_profile groups the supplied clauses as one router.replace_catalog call before its value is consumed. + )) + + # What: act by calling router.control_plane_snapshot and capture catalog snapshot and route state; why: the catalog reload clears active runtime profile test asserts the response, state, or failure produced by this call. + catalog_snapshot, route_state = router.control_plane_snapshot() + # What: assert that catalog snapshot routing profile coding equals profile; why: this assertion protects the catalog reload clears active runtime profile regression after the test's arranged inputs and exercised call. + assert catalog_snapshot.routing_profile("coding") == profile + # What: assert that route state active routing profile is group delimiter; why: this assertion protects the catalog reload clears active runtime profile regression after the test's arranged inputs and exercised call. + assert route_state["activeRoutingProfile"] is None + # What: assert that router has routable id public is false; why: this assertion protects the catalog reload clears active runtime profile regression after the test's arranged inputs and exercised call. + assert router.has_routable_id("public") is False + + +# What: define the test_management_load_ignores_active_routing_profile_pin test around local fixtures; why: this test groups the arrange, act, and assertions that protect the management load ignores active routing profile pin outcome. +def test_management_load_ignores_active_routing_profile_pin(): + # What: act by calling ModelCatalog and capture catalog doc; why: the management load ignores active routing profile pin test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with a and b; why: test_management_load_ignores_active_routing_profile_pin groups the supplied clauses as one catalog_doc mapping before its value is consumed. + { + # What: arrange the a field as model profile and a and a and gguf; why: test_management_load_ignores_active_routing_profile_pin carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "a": ModelProfile("a", "a.gguf", ()), + # What: arrange the b field as model profile and b and b and gguf; why: test_management_load_ignores_active_routing_profile_pin carries b through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "b": ModelProfile("b", "b.gguf", ()), + # What: arrange the catalog_doc mapping with a and b; why: test_management_load_ignores_active_routing_profile_pin groups the supplied clauses as one catalog_doc mapping before its value is consumed. + }, + # What: arrange the coding field as routing profile and coding and a and b; why: test_management_load_ignores_active_routing_profile_pin carries coding through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + routing_profiles={"coding": RoutingProfile("coding", (("a", "b"),))}, + # What: arrange the ModelCatalog call with routing profiles; why: test_management_load_ignores_active_routing_profile_pin groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling Manager and capture manager; why: the management load ignores active routing profile pin test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the management load ignores active routing profile pin test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange the exact router set active routing profile coding fixture fragment; why: the management load ignores active routing profile pin scenario feeds this byte-preserved fragment through router.set_active_routing_profile("coding") before asserting its protocol or parser result. + router.set_active_routing_profile("coding") + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_management_load_ignores_active_routing_profile_pin releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the management load ignores active routing profile pin test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_management_load_ignores_active_routing_profile_pin; why: test_management_load_ignores_active_routing_profile_pin consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the management load ignores active routing profile pin scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_management_load_ignores_active_routing_profile_pin groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture loaded; why: the management load ignores active routing profile pin test asserts the response, state, or failure produced by this call. + loaded = TestClient(app).post("/router/load", json={"name": "a"}) + + # What: assert that loaded status code equals 200; why: this assertion protects the management load ignores active routing profile pin regression after the test's arranged inputs and exercised call. + assert loaded.status_code == 200 + # What: assert that loaded json profile equals a; why: this assertion protects the management load ignores active routing profile pin regression after the test's arranged inputs and exercised call. + assert loaded.json()["profile"] == "a" + # What: assert that manager calls equals start a gguf; why: this assertion protects the management load ignores active routing profile pin regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "a.gguf")] + + +# What: define the test_queued_request_keeps_its_atomic_routing_profile_snapshot test around local fixtures; why: this test groups the arrange, act, and assertions that protect the queued request keeps its atomic routing profile snapshot outcome. +def test_queued_request_keeps_its_atomic_routing_profile_snapshot(): + # What: act by calling ModelCatalog and capture catalog doc; why: the queued request keeps its atomic routing profile snapshot test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with a and b; why: test_queued_request_keeps_its_atomic_routing_profile_snapshot groups the supplied clauses as one catalog_doc mapping before its value is consumed. + { + # What: arrange the a field as model profile and a and a and gguf; why: test_queued_request_keeps_its_atomic_routing_profile_snapshot carries a through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "a": ModelProfile("a", "a.gguf", ()), + # What: arrange the b field as model profile and b and b and gguf; why: test_queued_request_keeps_its_atomic_routing_profile_snapshot carries b through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "b": ModelProfile("b", "b.gguf", ()), + # What: arrange the catalog_doc mapping with a and b; why: test_queued_request_keeps_its_atomic_routing_profile_snapshot groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange the coding field as routing profile and coding and public and a; why: test_queued_request_keeps_its_atomic_routing_profile_snapshot carries coding through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + routing_profiles={"coding": RoutingProfile("coding", (("public", "a"),))}, + # What: arrange the ModelCatalog call with routing profiles; why: test_queued_request_keeps_its_atomic_routing_profile_snapshot groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling Manager and capture manager; why: the queued request keeps its atomic routing profile snapshot test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the queued request keeps its atomic routing profile snapshot test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the queued request keeps its atomic routing profile snapshot test asserts the response, state, or failure produced by this call. + active = router.acquire("b") + # What: arrange the exact router set active routing profile coding fixture fragment; why: the queued request keeps its atomic routing profile snapshot scenario feeds this byte-preserved fragment through router.set_active_routing_profile("coding") before asserting its protocol or parser result. + router.set_active_routing_profile("coding") + # What: arrange leases as the fixture input; why: the queued request keeps its atomic routing profile snapshot test consumes this named precondition before exercising the behavior. + leases = [] + # What: act by calling threading.Thread and capture waiting; why: the queued request keeps its atomic routing profile snapshot test asserts the response, state, or failure produced by this call. + waiting = threading.Thread(target=lambda: leases.append(router.acquire("public"))) + # What: act by calling waiting.start with the declared inputs; why: the queued request keeps its atomic routing profile snapshot scenario observes the waiting.start return value during for value in range. + waiting.start() + # What: act across range to perform status and router; why: the queued request keeps its atomic routing profile snapshot scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the queued request keeps its atomic routing profile snapshot scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the queued request keeps its atomic routing profile snapshot scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the queued request keeps its atomic routing profile snapshot scenario observes the time.sleep return value during assert router status queued requests. + time.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the queued request keeps its atomic routing profile snapshot regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + + # What: act by calling router.set_active_routing_profile with the named fixture input; why: the queued request keeps its atomic routing profile snapshot scenario observes the router.set_active_routing_profile return value during active release. + router.set_active_routing_profile(None) + # What: act by calling active.release with the declared inputs; why: the queued request keeps its atomic routing profile snapshot scenario observes the active.release return value during waiting join. + active.release() + # What: act by calling waiting.join with 2; why: the queued request keeps its atomic routing profile snapshot scenario observes the waiting.join return value during assert not waiting is alive. + waiting.join(2) + + # What: assert that waiting is alive is false; why: this assertion protects the queued request keeps its atomic routing profile snapshot regression after the test's arranged inputs and exercised call. + assert not waiting.is_alive() + # What: act by calling leases.pop and capture lease; why: the queued request keeps its atomic routing profile snapshot test asserts the response, state, or failure produced by this call. + lease = leases.pop() + # What: assert the expected lease profile name lease routing profile id lease pin id == outcome; why: test router test queued request keeps its atomic routing profile snapshot protects its regression by requiring this observable result after the exercised behavior. + assert (lease.profile.name, lease.routing_profile_id, lease.pin_id) == ( + # What: arrange a coding public for the scenario; why: test router test queued request keeps its atomic routing profile snapshot requires this concrete input or helper state before exercising the behavior under test. + "a", "coding", "public", + # What: arrange the grouped source fragment for the scenario; why: test router test queued request keeps its atomic routing profile snapshot requires this concrete input or helper state before exercising the behavior under test. + ) + # What: act by calling lease.release with the declared inputs; why: the queued request keeps its atomic routing profile snapshot scenario observes the lease.release return value during assert manager calls start b gguf switch a gguf. + lease.release() + # What: assert that manager calls equals start b gguf switch a gguf; why: this assertion protects the queued request keeps its atomic routing profile snapshot regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "b.gguf"), ("switch", "a.gguf")] + + +# What: define the test_model_list_renders_capability_metadata_for_canonical_and_alias test around local fixtures; why: this test groups the arrange, act, and assertions that protect the model list renders capability metadata for canonical and alias outcome. +def test_model_list_renders_capability_metadata_for_canonical_and_alias(): + # What: act by calling Manager and capture manager; why: the model list renders capability metadata for canonical and alias test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the model list renders capability metadata for canonical and alias test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with canonical; why: test_model_list_renders_capability_metadata_for_canonical_and_alias groups the supplied clauses as one catalog_doc mapping before its value. + { + # What: arrange the canonical field as model profile and model capabilities and canonical and private and gguf; why: test_model_list_renders_capability_metadata_for_canonical_and_alias carries canonical through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "canonical": ModelProfile( + # What: arrange the canonical portion of catalog doc; why: the model list renders capability metadata for canonical and alias scenario uses this clause to evaluate catalog doc as one grouped value. + "canonical", + # What: arrange the private gguf portion of catalog doc; why: the model list renders capability metadata for canonical and alias scenario uses this clause to evaluate catalog doc as one grouped value. + "private.gguf", + # What: arrange the catalog_doc collection with ordered entries; why: test_model_list_renders_capability_metadata_for_canonical_and_alias groups the supplied clauses as one catalog_doc collection before its value is consumed. + (), + # What: arrange aliases to ModelProfile; why: the model list renders capability metadata for canonical and alias scenario binds this compat id value to ModelProfile's aliases input. + aliases=("compat-id",), + # What: arrange capabilities to ModelCapabilities; why: the model list renders capability metadata for canonical and alias scenario binds this model capabilities and true and 32768 and text and text value to ModelCapabilities's capabilities input. + capabilities=ModelCapabilities(("text",), ("text",), True, 32768), + # What: arrange display name to ModelProfile; why: the model list renders capability metadata for canonical and alias scenario binds this canonical and model value to ModelProfile's display name input. + display_name=" Canonical Model ", + # What: arrange description to ModelProfile; why: the model list renders capability metadata for canonical and alias scenario binds this public and description value to ModelProfile's description input. + description=" Public description ", + # What: arrange metadata json to ModelProfile; why: the model list renders capability metadata for canonical and alias scenario binds this architecture and operator and context window and custom value to ModelProfile's metadata json input. + metadata_json=( + # What: arrange the exact architecture operator context window custom remain fixture fragment; why: the model list renders capability metadata for canonical and alias scenario feeds this byte-preserved fragment through catalog doc before asserting its protocol or parser result. + # What: arrange the exact type operator fixture fragment; why: the model list renders capability metadata for canonical and alias scenario feeds this byte-preserved fragment through catalog doc before asserting its protocol or parser result. + '{"architecture":"operator","context_window":1,"custom":"remain",' + '"type":"operator"}' + # What: arrange the ModelProfile call with aliases and capabilities and display name and description and metadata json; why: test_model_list_renders_capability_metadata_for_canonical_and_alias groups the supplied clauses as one ModelProfile call before its value is consumed. + ), + # What: arrange the ModelProfile call with aliases and capabilities and display name and description and metadata json; why: test_model_list_renders_capability_metadata_for_canonical_and_alias groups the supplied clauses as one ModelProfile call before its value is consumed. + ) + # What: arrange the catalog_doc mapping with canonical; why: test_model_list_renders_capability_metadata_for_canonical_and_alias groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange settings to RouterSettings; why: the model list renders capability metadata for canonical and alias scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(include_aliases_in_list=True), + # What: arrange the ModelCatalog call with settings; why: test_model_list_renders_capability_metadata_for_canonical_and_alias groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the model list renders capability metadata for canonical and alias test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_model_list_renders_capability_metadata_for_canonical_and_alias releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the model list renders capability metadata for canonical and alias test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_model_list_renders_capability_metadata_for_canonical_and_alias; why: test_model_list_renders_capability_metadata_for_canonical_and_alias consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the model list renders capability metadata for canonical and alias scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_model_list_renders_capability_metadata_for_canonical_and_alias groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.json and capture data; why: the model list renders capability metadata for canonical and alias test asserts the response, state, or failure produced by this call. + data = TestClient(app).get("/v1/models").json()["data"] + + # What: assert that record id for record in data equals canonical compat id; why: this assertion protects the model list renders capability metadata for canonical and alias regression after the test's arranged inputs and exercised call. + assert [record["id"] for record in data] == ["canonical", "compat-id"] + # What: act across data to perform record; why: the model list renders capability metadata for canonical and alias scenario repeats the body only while or for the loop header admits an iteration. + for record in data: + # What: assert that record name equals canonical model; why: this assertion protects the model list renders capability metadata for canonical and alias regression after the test's arranged inputs and exercised call. + assert record["name"] == "Canonical Model" + # What: assert that record description equals public description; why: this assertion protects the model list renders capability metadata for canonical and alias regression after the test's arranged inputs and exercised call. + assert record["description"] == "Public description" + # What: assert the expected record architecture == outcome; why: test router test model list renders capability metadata for canonical and alias protects its regression by requiring this observable result after the exercised behavior. + assert record["architecture"] == { + # What: arrange input modalities text for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + "input_modalities": ["text"], + # What: arrange output modalities text for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + "output_modalities": ["text"], + # What: arrange modality text text for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + "modality": "text->text", + # What: arrange the grouped source fragment for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that record capabilities equals function calling true; why: this assertion protects the model list renders capability metadata for canonical and alias regression after the test's arranged inputs and exercised call. + assert record["capabilities"] == {"function_calling": True} + # What: assert that record supported parameters equals tools tool choice; why: this assertion protects the model list renders capability metadata for canonical and alias regression after the test's arranged inputs and exercised call. + assert record["supported_parameters"] == ["tools", "tool_choice"] + # What: assert that record context length equals 32768; why: this assertion protects the model list renders capability metadata for canonical and alias regression after the test's arranged inputs and exercised call. + assert record["context_length"] == 32768 + # What: assert that record context window equals 32768; why: this assertion protects the model list renders capability metadata for canonical and alias regression after the test's arranged inputs and exercised call. + assert record["context_window"] == 32768 + # What: assert the expected data 0 meta == outcome; why: test router test model list renders capability metadata for canonical and alias protects its regression by requiring this observable result after the exercised behavior. + assert data[0]["meta"] == { + # What: arrange n ctx 32768 for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + "n_ctx": 32768, + # What: arrange freetoken for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + "freetoken": { + # What: arrange aliases compat id custom remain type model for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + "aliases": ["compat-id"], "custom": "remain", "type": "model", + # What: arrange the grouped source fragment for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + }, + # What: arrange the grouped source fragment for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert the expected data 1 meta == outcome; why: test router test model list renders capability metadata for canonical and alias protects its regression by requiring this observable result after the exercised behavior. + assert data[1]["meta"] == { + # What: arrange n ctx 32768 for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + "n_ctx": 32768, + # What: arrange freetoken for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + "freetoken": { + # What: arrange custom remain modelID canonical type alias for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + "custom": "remain", "modelID": "canonical", "type": "alias", + # What: arrange the grouped source fragment for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + }, + # What: arrange the grouped source fragment for the scenario; why: test router test model list renders capability metadata for canonical and alias requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that private gguf is absent from str data; why: this assertion protects the model list renders capability metadata for canonical and alias regression after the test's arranged inputs and exercised call. + assert "private.gguf" not in str(data) + + +# What: define the test_model_list_omits_empty_capability_metadata test around local fixtures; why: this test groups the arrange, act, and assertions that protect the model list omits empty capability metadata outcome. +def test_model_list_omits_empty_capability_metadata(): + # What: act by calling Manager and capture manager; why: the model list omits empty capability metadata test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the model list omits empty capability metadata test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"plain": ModelProfile("plain", "private.gguf", ())}) + # What: act by calling RoutingCoordinator and capture router; why: the model list omits empty capability metadata test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_model_list_omits_empty_capability_metadata releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the model list omits empty capability metadata test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_model_list_omits_empty_capability_metadata; why: test_model_list_omits_empty_capability_metadata consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the model list omits empty capability metadata scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_model_list_omits_empty_capability_metadata groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.json and capture record; why: the model list omits empty capability metadata test asserts the response, state, or failure produced by this call. + record = TestClient(app).get("/v1/models").json()["data"][0] + + # What: assert the expected not outcome; why: test router test model list omits empty capability metadata protects its regression by requiring this observable result after the exercised behavior. + assert not { + # What: arrange architecture capabilities supported parameters context length for the scenario; why: test router test model list omits empty capability metadata requires this concrete input or helper state before exercising the behavior under test. + "architecture", "capabilities", "supported_parameters", "context_length", + # What: arrange context window for the scenario; why: test router test model list omits empty capability metadata requires this concrete input or helper state before exercising the behavior under test. + "context_window", + # What: arrange intersection record for the scenario; why: test router test model list omits empty capability metadata requires this concrete input or helper state before exercising the behavior under test. + }.intersection(record) + # What: assert that record meta equals freetoken type model; why: this assertion protects the model list omits empty capability metadata regression after the test's arranged inputs and exercised call. + assert record["meta"] == {"freetoken": {"type": "model"}} + + +# What: define the test_openai_model_list_reports_canonical_and_alias_loaded_while_activating test around local fixtures; why: this test groups the arrange, act, and assertions that protect the openai model list reports canonical and alias loaded while activating outcome. +def test_openai_model_list_reports_canonical_and_alias_loaded_while_activating(): + # What: act by calling Manager and capture manager; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling threading.Event and capture activation started; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + activation_started = threading.Event() + # What: act by calling threading.Event and capture finish activation; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + finish_activation = threading.Event() + # What: act by calling ModelCatalog and capture catalog doc; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the canonical field as model profile and canonical and shared and gguf and compat id; why: test_openai_model_list_reports_canonical_and_alias_loaded_while_activating carries canonical through catalog doc into router routing coordinator manager catalog doc object ready fn blocking ready. + {"canonical": ModelProfile( + # What: arrange aliases to ModelProfile; why: the openai model list reports canonical and alias loaded while activating scenario binds this compat id value to ModelProfile's aliases input. + "canonical", "shared.gguf", (), aliases=("compat-id",) + # What: arrange the catalog_doc mapping with canonical; why: test_openai_model_list_reports_canonical_and_alias_loaded_while_activating groups the supplied clauses as one catalog_doc mapping before its value is consumed. + )}, + # What: arrange settings to RouterSettings; why: the openai model list reports canonical and alias loaded while activating scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(include_aliases_in_list=True), + # What: arrange the ModelCatalog call with settings; why: test_openai_model_list_reports_canonical_and_alias_loaded_while_activating groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + + # What: define the blocking_ready test helper around manager and probe and pid and port and timeout s; why: the openai model list reports canonical and alias loaded while activating scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def blocking_ready(manager, probe, *, pid, port, timeout_s): + # What: act by calling activation_started.set with the declared inputs; why: the openai model list reports canonical and alias loaded while activating scenario observes the activation_started.set return value during assert finish activation wait. + activation_started.set() + # What: assert that finish activation wait 2; why: this assertion protects the openai model list reports canonical and alias loaded while activating regression after the test's arranged inputs and exercised call. + assert finish_activation.wait(2) + # What: arrange the ready field as true; why: blocking_ready carries ready into return {"ready": True, "health": {"status": "ok"}}. + return {"ready": True, "health": {"status": "ok"}} + + # What: act by calling RoutingCoordinator and capture router; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=blocking_ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_openai_model_list_reports_canonical_and_alias_loaded_while_activating releases this resource or lock after app build app on both success and failure paths. + with ( + # What: act by calling ThreadPoolExecutor with 1; why: the openai model list reports canonical and alias loaded while activating scenario observes the ThreadPoolExecutor return value during thread pool executor as lifecycle. + ThreadPoolExecutor(1) as activation, + # What: act by calling ThreadPoolExecutor with 1; why: the openai model list reports canonical and alias loaded while activating scenario observes the ThreadPoolExecutor return value during thread pool executor as proxy. + ThreadPoolExecutor(1) as lifecycle, + # What: act by calling ThreadPoolExecutor with 1; why: the openai model list reports canonical and alias loaded while activating scenario observes the ThreadPoolExecutor return value while evaluating ThreadPoolExecutor(1) as proxy. + ThreadPoolExecutor(1) as proxy, + # What: arrange the grouped source fragment for the scenario; why: test openai model list reports canonical and alias loaded while activating requires this concrete input or helper state before exercising the behavior under test. + ): + # What: act by calling build_app and capture app; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_openai_model_list_reports_canonical_and_alias_loaded_while_activating; why: test_openai_model_list_reports_canonical_and_alias_loaded_while_activating consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the openai model list reports canonical and alias loaded while activating scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_openai_model_list_reports_canonical_and_alias_loaded_while_activating groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling activation.submit and capture future; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + future = activation.submit(router.acquire, "compat-id") + # What: assert that activation started wait 2; why: this assertion protects the openai model list reports canonical and alias loaded while activating regression after the test's arranged inputs and exercised call. + assert activation_started.wait(2) + # What: establish the handler boundary for the protected operation; why: test_openai_model_list_reports_canonical_and_alias_loaded_while_activating routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: act by calling operation.json and capture status; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + status = client.get("/router/status").json() + # What: act by calling operation.json and capture listed; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + listed = client.get("/v1/models").json()["data"] + # What: run finish activation set on every exit path; why: test_openai_model_list_reports_canonical_and_alias_loaded_while_activating performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act by calling finish_activation.set with the declared inputs; why: the openai model list reports canonical and alias loaded while activating scenario observes the finish_activation.set return value during lease future result timeout. + finish_activation.set() + # What: act by calling future.result and capture lease; why: the openai model list reports canonical and alias loaded while activating test asserts the response, state, or failure produced by this call. + lease = future.result(timeout=2) + # What: act by calling lease.release with the declared inputs; why: the openai model list reports canonical and alias loaded while activating scenario observes the lease.release return value during assert status active profile is. + lease.release() + + # What: assert that status active profile is group delimiter; why: this assertion protects the openai model list reports canonical and alias loaded while activating regression after the test's arranged inputs and exercised call. + assert status["activeProfile"] is None + # What: assert that status activating profile equals canonical; why: this assertion protects the openai model list reports canonical and alias loaded while activating regression after the test's arranged inputs and exercised call. + assert status["activatingProfile"] == "canonical" + # What: assert the expected item id item status value for item in listed == outcome; why: test router test openai model list reports canonical and alias loaded while activating protects its regression by requiring this observable result after the exercised behavior. + assert {item["id"]: item["status"]["value"] for item in listed} == { + # What: arrange canonical loaded compat id loaded for the scenario; why: test router test openai model list reports canonical and alias loaded while activating requires this concrete input or helper state before exercising the behavior under test. + "canonical": "loaded", "compat-id": "loaded", + # What: arrange the grouped source fragment for the scenario; why: test router test openai model list reports canonical and alias loaded while activating requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that router status activating profile is group delimiter; why: this assertion protects the openai model list reports canonical and alias loaded while activating regression after the test's arranged inputs and exercised call. + assert router.status()["activatingProfile"] is None + + +# What: define the test_model_listing_does_not_claim_loaded_before_manager_owns_starting_child test around local fixtures; why: this test groups the arrange, act, and assertions that protect the model listing does not claim loaded before manager owns starting child outcome. +def test_model_listing_does_not_claim_loaded_before_manager_owns_starting_child(): + # What: act by calling threading.Event and capture start entered; why: the model listing does not claim loaded before manager owns starting child test asserts the response, state, or failure produced by this call. + start_entered = threading.Event() + # What: act by calling threading.Event and capture finish start; why: the model listing does not claim loaded before manager owns starting child test asserts the response, state, or failure produced by this call. + finish_start = threading.Event() + + # What: define BlockingStartManager as the owner of start; why: daemon callers use this class boundary so those methods share one blocking start manager state invariant. + class BlockingStartManager(Manager): + # What: define the start test helper around model and port and args; why: the model listing does not claim loaded before manager owns starting child scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def start(self, model, port, args): + # What: act by calling start_entered.set with the declared inputs; why: the model listing does not claim loaded before manager owns starting child scenario observes the start_entered.set return value during assert finish start wait. + start_entered.set() + # What: assert that finish start wait 2; why: this assertion protects the model listing does not claim loaded before manager owns starting child regression after the test's arranged inputs and exercised call. + assert finish_start.wait(2) + # What: return start and model and port and args from the start test helper; why: the model listing does not claim loaded before manager owns starting child scenario uses this helper result in its subsequent act or assertion. + return super().start(model, port, args) + + # What: act by calling BlockingStartManager and capture manager; why: the model listing does not claim loaded before manager owns starting child test asserts the response, state, or failure produced by this call. + manager = BlockingStartManager() + # What: act by calling RoutingCoordinator and capture router; why: the model listing does not claim loaded before manager owns starting child test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor managed context before future activation submit router acquire low; why: test_model_listing_does_not_claim_loaded_before_manager_owns_starting_child releases this resource or lock after future activation submit router acquire low on both success and failure paths. + with ThreadPoolExecutor(1) as activation: + # What: act by calling activation.submit and capture future; why: the model listing does not claim loaded before manager owns starting child test asserts the response, state, or failure produced by this call. + future = activation.submit(router.acquire, "low") + # What: assert that start entered wait 2; why: this assertion protects the model listing does not claim loaded before manager owns starting child regression after the test's arranged inputs and exercised call. + assert start_entered.wait(2) + # What: establish the handler boundary for the protected operation; why: test_model_listing_does_not_claim_loaded_before_manager_owns_starting_child routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: act by calling router.model_listing_snapshot and capture and loaded profiles; why: the model listing does not claim loaded before manager owns starting child test asserts the response, state, or failure produced by this call. + _, loaded_profiles = router.model_listing_snapshot() + # What: act by calling router.status and capture status; why: the model listing does not claim loaded before manager owns starting child test asserts the response, state, or failure produced by this call. + status = router.status() + # What: run finish start set on every exit path; why: test_model_listing_does_not_claim_loaded_before_manager_owns_starting_child performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act by calling finish_start.set with the declared inputs; why: the model listing does not claim loaded before manager owns starting child scenario observes the finish_start.set return value during lease future result timeout. + finish_start.set() + # What: act by calling future.result and capture lease; why: the model listing does not claim loaded before manager owns starting child test asserts the response, state, or failure produced by this call. + lease = future.result(timeout=2) + # What: act by calling lease.release with the declared inputs; why: the model listing does not claim loaded before manager owns starting child scenario observes the lease.release return value during assert loaded profiles frozenset. + lease.release() + + # What: assert that loaded profiles equals frozenset; why: this assertion protects the model listing does not claim loaded before manager owns starting child regression after the test's arranged inputs and exercised call. + assert loaded_profiles == frozenset() + # What: assert that status activating profile equals low; why: this assertion protects the model listing does not claim loaded before manager owns starting child regression after the test's arranged inputs and exercised call. + assert status["activatingProfile"] == "low" + # What: assert that router model listing snapshot 1 equals frozenset low; why: this assertion protects the model listing does not claim loaded before manager owns starting child regression after the test's arranged inputs and exercised call. + assert router.model_listing_snapshot()[1] == frozenset({"low"}) + + +# What: define the test_browser_cors_preflight_is_side_effect_free_and_sanitizes_headers test around local fixtures; why: this test groups the arrange, act, and assertions that protect the browser cors preflight is side effect free and sanitizes headers outcome. +def test_browser_cors_preflight_is_side_effect_free_and_sanitizes_headers(): + # What: act by calling Manager and capture manager; why: the browser cors preflight is side effect free and sanitizes headers test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_browser_cors_preflight_is_side_effect_free_and_sanitizes_headers releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the browser cors preflight is side effect free and sanitizes headers test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_browser_cors_preflight_is_side_effect_free_and_sanitizes_headers; why: test_browser_cors_preflight_is_side_effect_free_and_sanitizes_headers consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to catalog; why: the browser cors preflight is side effect free and sanitizes headers scenario binds this lifecycle value to catalog's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog(), token="control-secret", + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_browser_cors_preflight_is_side_effect_free_and_sanitizes_headers groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the browser cors preflight is side effect free and sanitizes headers test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.options and capture preflight; why: the browser cors preflight is side effect free and sanitizes headers test asserts the response, state, or failure produced by this call. + preflight = client.options( + # What: arrange the does not exist portion of preflight; why: the browser cors preflight is side effect free and sanitizes headers scenario uses this clause to evaluate preflight as one grouped value. + "/does-not-exist", + # What: arrange the access control request headers field as content type and bad and header and x ft token; why: test_browser_cors_preflight_is_side_effect_free_and_sanitizes_headers carries access control request headers through preflight into assert preflight status code equals 204. + headers={"Access-Control-Request-Headers": "Content-Type, bad header, X-FT-Token"}, + # What: arrange the client.options call with headers; why: test_browser_cors_preflight_is_side_effect_free_and_sanitizes_headers groups the supplied clauses as one client.options call before its value is consumed. + ) + # What: act by calling client.options and capture default preflight; why: the browser cors preflight is side effect free and sanitizes headers test asserts the response, state, or failure produced by this call. + default_preflight = client.options("/v1/chat/completions") + + # What: assert that preflight status code equals 204; why: this assertion protects the browser cors preflight is side effect free and sanitizes headers regression after the test's arranged inputs and exercised call. + assert preflight.status_code == 204 + # What: assert that preflight headers access control allow origin equals group delimiter; why: this assertion protects the browser cors preflight is side effect free and sanitizes headers regression after the test's arranged inputs and exercised call. + assert preflight.headers["access-control-allow-origin"] == "*" + # What: assert the expected preflight headers access control allow methods == outcome; why: test router test browser cors preflight is side effect free and sanitizes headers protects its regression by requiring this observable result after the exercised behavior. + assert preflight.headers["access-control-allow-methods"] == ( + # What: arrange GET POST PUT PATCH DELETE OPTIONS for the scenario; why: test router test browser cors preflight is side effect free and sanitizes headers requires this concrete input or helper state before exercising the behavior under test. + "GET, POST, PUT, PATCH, DELETE, OPTIONS" + # What: arrange the grouped source fragment for the scenario; why: test router test browser cors preflight is side effect free and sanitizes headers requires this concrete input or helper state before exercising the behavior under test. + ) + # What: assert that preflight headers access control allow headers equals content type x ft token; why: this assertion protects the browser cors preflight is side effect free and sanitizes headers regression after the test's arranged inputs and exercised call. + assert preflight.headers["access-control-allow-headers"] == "Content-Type, X-FT-Token" + # What: assert that preflight headers access control max age equals 86400; why: this assertion protects the browser cors preflight is side effect free and sanitizes headers regression after the test's arranged inputs and exercised call. + assert preflight.headers["access-control-max-age"] == "86400" + # What: assert the expected default preflight headers access control allow headers == outcome; why: test router test browser cors preflight is side effect free and sanitizes headers protects its regression by requiring this observable result after the exercised behavior. + assert default_preflight.headers["access-control-allow-headers"] == ( + # What: arrange Content Type Authorization Accept X Requested With for the scenario; why: test router test browser cors preflight is side effect free and sanitizes headers requires this concrete input or helper state before exercising the behavior under test. + "Content-Type, Authorization, Accept, X-Requested-With" + # What: arrange the grouped source fragment for the scenario; why: test router test browser cors preflight is side effect free and sanitizes headers requires this concrete input or helper state before exercising the behavior under test. + ) + # What: assert that manager calls equals group delimiter; why: this assertion protects the browser cors preflight is side effect free and sanitizes headers regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + + +# What: define the test_models_alias_matches_public_listing_and_keeps_control_auth_separate test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the models alias matches public listing and keeps control auth separate outcome. +def test_models_alias_matches_public_listing_and_keeps_control_auth_separate(monkeypatch): + # What: arrange the exact monkeypatch setattr freetoken daemon app time time lambda fixture fragment; why: the models alias matches public listing and keeps control auth separate scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.time.time", lambda: 1234567890 before asse. + monkeypatch.setattr("freetoken.daemon.app.time.time", lambda: 1234567890) + # What: act by calling Manager and capture manager; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the visible field as model profile and visible and private and gguf; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate carries visible through catalog doc into lifecycle pool lifecycle proxy pool proxy catalog catalog doc. + {"visible": ModelProfile("visible", "private.gguf", ())}, + # What: arrange settings to RouterSettings; why: the models alias matches public listing and keeps control auth separate scenario binds this router settings and router key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("router-key",)), + # What: arrange the ModelCatalog call with settings; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_models_alias_matches_public_listing_and_keeps_control_auth_separate; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the models alias matches public listing and keeps control auth separate scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, + # What: arrange token to build_app; why: the models alias matches public listing and keeps control auth separate scenario binds this control secret value to build_app's token input. + token="control-secret", + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.get and capture denied; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + denied = client.get("/v1/models", headers={"Origin": "https://client.example"}) + # What: act by calling client.get and capture listed; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + listed = client.get( + # What: arrange the v1 models portion of listed; why: the models alias matches public listing and keeps control auth separate scenario uses this clause to evaluate listed as one grouped value. + "/v1/models", + # What: arrange the origin field as https and client and example; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate carries origin through listed into assert listed status code equals 200. + headers={"Origin": "https://client.example", "Authorization": "Bearer router-key"}, + # What: arrange the client.get call with headers; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate groups the supplied clauses as one client.get call before its value is consumed. + ) + # What: act by calling client.get and capture alias denied; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + alias_denied = client.get("/models", headers={"X-FT-Token": "control-secret"}) + # What: act by calling client.get and capture alias; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + alias = client.get( + # What: arrange the models portion of alias; why: the models alias matches public listing and keeps control auth separate scenario uses this clause to evaluate alias as one grouped value. + "/models", + # What: arrange the origin field as https and client and example; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate carries origin through alias into assert alias status code equals 200. + headers={"Origin": "https://client.example", "Authorization": "Bearer router-key"}, + # What: arrange the client.get call with headers; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate groups the supplied clauses as one client.get call before its value is consumed. + ) + # What: act by calling client.get and capture profiles; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + profiles = client.get( + # What: arrange the x ft token field as control secret; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate carries x ft token through profiles into router profiles headers authorization bearer router key. + "/router/profiles", headers={"X-FT-Token": "control-secret"} + # What: arrange the client.get call with headers; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate groups the supplied clauses as one client.get call before its value is consumed. + ) + # What: act by calling client.get and capture profiles denied; why: the models alias matches public listing and keeps control auth separate test asserts the response, state, or failure produced by this call. + profiles_denied = client.get( + # What: arrange the authorization field as bearer and router key; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate carries authorization through profiles denied into assert profiles denied status code equals 401. + "/router/profiles", headers={"Authorization": "Bearer router-key"} + # What: arrange the client.get call with headers; why: test_models_alias_matches_public_listing_and_keeps_control_auth_separate groups the supplied clauses as one client.get call before its value is consumed. + ) + + # What: assert that denied status code equals 401; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert denied.status_code == 401 + # What: assert that listed status code equals 200; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert listed.status_code == 200 + # What: assert that listed headers access control allow origin equals https client example; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert listed.headers["access-control-allow-origin"] == "https://client.example" + # What: assert that item id for item in listed json equals visible; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert [item["id"] for item in listed.json()["data"]] == ["visible"] + # What: assert that alias denied status code equals 401; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert alias_denied.status_code == 401 + # What: assert that alias status code equals 200; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert alias.status_code == 200 + # What: assert that alias json equals listed json; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert alias.json() == listed.json() + # What: assert that alias headers access control allow origin equals https client example; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert alias.headers["access-control-allow-origin"] == "https://client.example" + # What: assert that profiles status code equals 200; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert profiles.status_code == 200 + # What: assert that profiles json data 0 model equals private gguf; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert profiles.json()["data"][0]["model"] == "private.gguf" + # What: assert that profiles denied status code equals 401; why: this assertion protects the models alias matches public listing and keeps control auth separate regression after the test's arranged inputs and exercised call. + assert profiles_denied.status_code == 401 + + +# What: define the test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the explicit cancel while upstream connects closes result and releases lease outcome. +def test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease(monkeypatch): + # What: act by calling Manager and capture manager; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"low": ModelProfile("low", "low.gguf", ())}) + # What: act by calling RoutingCoordinator and capture router; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling threading.Event and capture connecting; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + connecting = threading.Event() + # What: act by calling threading.Event and capture finish connect; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + finish_connect = threading.Event() + # What: act by calling BytesIO and capture raw; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + raw = BytesIO(b"must not stream") + + # What: define the upstream test helper around captured fixture state; why: the explicit cancel while upstream connects closes result and releases lease scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling connecting.set with the declared inputs; why: the explicit cancel while upstream connects closes result and releases lease scenario observes the connecting.set return value during assert finish connect wait. + connecting.set() + # What: assert that finish connect wait 2; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert finish_connect.wait(2) + # What: arrange the helper response as UpstreamResponse 200 Content Type text event stream raw; why: test explicit cancel while upstream connects closes feeds this result into the behavior whose outcome is asserted. + return UpstreamResponse(200, {"Content-Type": "text/event-stream"}, raw) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the explicit cancel while upstream connects closes result and releases lease scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) befor. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease; why: test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the explicit cancel while upstream connects closes result and releases lease scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: arrange response as the fixture input; why: the explicit cancel while upstream connects closes result and releases lease test consumes this named precondition before exercising the behavior. + response = [] + # What: act by calling threading.Thread and capture thread; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + thread = threading.Thread(target=lambda: response.append(client.post( + # What: arrange the model field as low; why: test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease sends this field through thread so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "low"}, + # What: arrange the x ft request id field as cancel during connect; why: test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease carries x ft request id through thread into thread start. + headers={"X-FT-Request-ID": "cancel-during-connect"}, + # What: arrange the threading.Thread call with target; why: test_explicit_cancel_while_upstream_connects_closes_result_and_releases_lease groups the supplied clauses as one threading.Thread call before its value is consumed. + ))) + # What: act by calling thread.start with the declared inputs; why: the explicit cancel while upstream connects closes result and releases lease scenario observes the thread.start return value during assert connecting wait. + thread.start() + # What: assert that connecting wait 1; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert connecting.wait(1) + # What: assert the expected client get router requests json data == outcome; why: test router test explicit cancel while upstream connects closes result and releases lease protects its regression by requiring this observable result after the exercised behavior. + assert client.get("/router/requests").json()["data"] == [ + # What: arrange id cancel during connect profile low for the scenario; why: test router test explicit cancel while upstream connects closes result and releases lease requires this concrete input or helper state before exercising the behavior under test. + {"id": "cancel-during-connect", "profile": "low"} + # What: arrange the grouped source fragment for the scenario; why: test router test explicit cancel while upstream connects closes result and releases lease requires this concrete input or helper state before exercising the behavior under test. + ] + # What: act by calling client.post and capture cancelled; why: the explicit cancel while upstream connects closes result and releases lease test asserts the response, state, or failure produced by this call. + cancelled = client.post("/router/requests/cancel-during-connect/cancel") + # What: assert that cancelled json equals cancelled true id cancel during connect; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert cancelled.json() == {"cancelled": True, "id": "cancel-during-connect"} + # What: act by calling finish_connect.set with the declared inputs; why: the explicit cancel while upstream connects closes result and releases lease scenario observes the finish_connect.set return value during thread join. + finish_connect.set() + # What: act by calling thread.join with 2; why: the explicit cancel while upstream connects closes result and releases lease scenario observes the thread.join return value during assert not thread is alive. + thread.join(2) + # What: assert that thread is alive is false; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert not thread.is_alive() + + # What: assert that response 0 status code equals 409; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert response[0].status_code == 409 + # What: assert that response 0 json error type equals request cancelled; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert response[0].json()["error"]["type"] == "request_cancelled" + # What: assert that raw closed; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert raw.closed + # What: assert that router status active requests equals 0; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + # What: assert that router status admissions equals 1; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert router.status()["admissions"] == 1 + # What: assert that router status cancellations equals 1; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert router.status()["cancellations"] == 1 + # What: assert that router status terminal streams equals 0; why: this assertion protects the explicit cancel while upstream connects closes result and releases lease regression after the test's arranged inputs and exercised call. + assert router.status()["terminalStreams"] == 0 + + +# What: define the test_disconnect_while_upstream_connects_closes_orphaned_result test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the disconnect while upstream connects closes orphaned result outcome. +def test_disconnect_while_upstream_connects_closes_orphaned_result(monkeypatch): + # What: act by calling Manager and capture manager; why: the disconnect while upstream connects closes orphaned result test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the disconnect while upstream connects closes orphaned result test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"low": ModelProfile("low", "low.gguf", ())}) + # What: act by calling RoutingCoordinator and capture router; why: the disconnect while upstream connects closes orphaned result test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling threading.Event and capture connecting; why: the disconnect while upstream connects closes orphaned result test asserts the response, state, or failure produced by this call. + connecting = threading.Event() + # What: act by calling threading.Event and capture finish connect; why: the disconnect while upstream connects closes orphaned result test asserts the response, state, or failure produced by this call. + finish_connect = threading.Event() + # What: act by calling BytesIO and capture raw; why: the disconnect while upstream connects closes orphaned result test asserts the response, state, or failure produced by this call. + raw = BytesIO(b"must not stream") + + # What: define the upstream test helper around captured fixture state; why: the disconnect while upstream connects closes orphaned result scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling connecting.set with the declared inputs; why: the disconnect while upstream connects closes orphaned result scenario observes the connecting.set return value during assert finish connect wait. + connecting.set() + # What: assert that finish connect wait 2; why: this assertion protects the disconnect while upstream connects closes orphaned result regression after the test's arranged inputs and exercised call. + assert finish_connect.wait(2) + # What: arrange the helper response as UpstreamResponse 200 Content Type text event stream raw; why: test router test disconnect while upstream connects closes orphaned result feeds this result into the behavior whose outcome is asserted. + return UpstreamResponse(200, {"Content-Type": "text/event-stream"}, raw) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the disconnect while upstream connects closes orphaned result scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before asserting its. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + + # What: define the scenario test helper around app; why: the disconnect while upstream connects closes orphaned result scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def scenario(app): + # What: act by calling httpx.ASGITransport and capture transport; why: the disconnect while upstream connects closes orphaned result test asserts the response, state, or failure produced by this call. + transport = httpx.ASGITransport(app=app) + # What: arrange async with httpx AsyncClient transport transport base url http test as client for the scenario; why: test router test requires this concrete input or helper state before exercising the behavior under test. + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: + # What: act by calling asyncio.create_task and capture request; why: the disconnect while upstream connects closes orphaned result test asserts the response, state, or failure produced by this call. + request = asyncio.create_task(client.post( + # What: arrange the model field as low; why: scenario sends this field through request so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "low"}, + # What: arrange the x ft request id field as disconnect during connect; why: scenario carries x ft request id through request into request cancel. + headers={"X-FT-Request-ID": "disconnect-during-connect"}, + # What: arrange the asyncio.create_task call with post; why: scenario groups the supplied clauses as one asyncio.create_task call before its value is consumed. + )) + # What: act across range to perform is set and connecting; why: the disconnect while upstream connects closes orphaned result scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on is set and connecting before the computed value; why: the disconnect while upstream connects closes orphaned result scenario admits the computed value only for this predicate and excludes the opposite state. + if connecting.is_set(): + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the disconnect while upstream connects closes orphaned result scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the disconnect while upstream connects closes orphaned result scenario observes the asyncio.sleep return value during assert connecting is set. + await asyncio.sleep(0.01) + # What: assert that connecting is set; why: this assertion protects the disconnect while upstream connects closes orphaned result regression after the test's arranged inputs and exercised call. + assert connecting.is_set() + # What: act by calling request.cancel with the declared inputs; why: the disconnect while upstream connects closes orphaned result scenario observes the request.cancel return value during with pytest raises asyncio cancelled error. + request.cancel() + # What: assert the pytest.raises failure context; why: the disconnect while upstream connects closes orphaned result scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(asyncio.CancelledError): + # What: arrange the await request portion of the enclosing predicate; why: this clause remains in the disconnect while upstream connects closes orphaned result scenario\'s enclosing expression so its grouping and evaluation order stay intact. + await request + # What: assert that router status active requests equals 0; why: this assertion protects the disconnect while upstream connects closes orphaned result regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + # What: assert that await client get router requests json data equals group delimiter; why: this assertion protects the disconnect while upstream connects closes orphaned result regression after the test's arranged inputs and exercised call. + assert (await client.get("/router/requests")).json()["data"] == [] + # What: act by calling finish_connect.set with the declared inputs; why: the disconnect while upstream connects closes orphaned result scenario observes the finish_connect.set return value during for value in range. + finish_connect.set() + # What: act across range to perform closed and raw; why: the disconnect while upstream connects closes orphaned result scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on closed and raw before the computed value; why: the disconnect while upstream connects closes orphaned result scenario admits the computed value only for this predicate and excludes the opposite state. + if raw.closed: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the disconnect while upstream connects closes orphaned result scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the disconnect while upstream connects closes orphaned result scenario observes the asyncio.sleep return value during assert raw closed. + await asyncio.sleep(0.01) + # What: assert that raw closed; why: this assertion protects the disconnect while upstream connects closes orphaned result regression after the test's arranged inputs and exercised call. + assert raw.closed + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_disconnect_while_upstream_connects_closes_orphaned_result releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the disconnect while upstream connects closes orphaned result test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_disconnect_while_upstream_connects_closes_orphaned_result; why: test_disconnect_while_upstream_connects_closes_orphaned_result consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the disconnect while upstream connects closes orphaned result scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_disconnect_while_upstream_connects_closes_orphaned_result groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling asyncio.run with scenario and app; why: the disconnect while upstream connects closes orphaned result scenario observes the asyncio.run return value during assert router status admissions. + asyncio.run(scenario(app)) + + # What: assert that router status admissions equals 1; why: this assertion protects the disconnect while upstream connects closes orphaned result regression after the test's arranged inputs and exercised call. + assert router.status()["admissions"] == 1 + # What: assert that router status cancellations equals 1; why: this assertion protects the disconnect while upstream connects closes orphaned result regression after the test's arranged inputs and exercised call. + assert router.status()["cancellations"] == 1 + # What: assert that router status terminal streams equals 0; why: this assertion protects the disconnect while upstream connects closes orphaned result regression after the test's arranged inputs and exercised call. + assert router.status()["terminalStreams"] == 0 + + +# What: parameterize test_router_inference_and_management_accept_pinned_api_key_forms with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test router inference and management accept pinned api key forms. +@pytest.mark.parametrize( + # What: arrange the headers portion of the enclosing predicate; why: this clause remains in the router inference and management accept pinned api key forms scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "headers", + # What: arrange the grouped source fragment for the scenario; why: test router inference and management accept pinned api key forms requires this concrete input or helper state before exercising the behavior under test. + [ + # What: arrange the authorization field as bearer and key; why: test_router_inference_and_management_accept_pinned_api_key_forms carries authorization into {"Authorization": "Bearer key"}. + {"Authorization": "Bearer key"}, + # What: arrange the authorization field as bearer and key; why: test_router_inference_and_management_accept_pinned_api_key_forms carries authorization into {"Authorization": "bearer key"}. + {"Authorization": "bearer key"}, + # What: arrange the authorization field as decode and b64encode and base64 and basic; why: test_router_inference_and_management_accept_pinned_api_key_forms carries authorization into {"Authorization": "Basic " + base64.b64encode(b"operator:key").decode()}. + {"Authorization": "Basic " + base64.b64encode(b"operator:key").decode()}, + # What: arrange the x api key field as key; why: test_router_inference_and_management_accept_pinned_api_key_forms carries x api key into {"X-Api-Key": "key"}. + {"X-Api-Key": "key"}, + # What: arrange the authorization field as basic and not base64; why: test_router_inference_and_management_accept_pinned_api_key_forms carries authorization into {"Authorization": "Basic !!!not-base64", "X-Api-Key": "key"}. + {"Authorization": "Basic !!!not-base64", "X-Api-Key": "key"}, + # What: arrange the grouped source fragment for the scenario; why: test router inference and management accept pinned api key forms requires this concrete input or helper state before exercising the behavior under test. + ], +# What: arrange the pytest.mark.parametrize call with decode; why: test_router_inference_and_management_accept_pinned_api_key_forms groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +) +# What: define the test_router_inference_and_management_accept_pinned_api_key_forms test around headers; why: this test groups the arrange, act, and assertions that protect the router inference and management accept pinned api key forms outcome. +def test_router_inference_and_management_accept_pinned_api_key_forms(headers): + # What: act by calling Manager and capture manager; why: the router inference and management accept pinned api key forms test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the router inference and management accept pinned api key forms test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_router_inference_and_management_accept_pinned_api_key_forms carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the router inference and management accept pinned api key forms scenario binds this router settings and key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("key",)), + # What: arrange the ModelCatalog call with settings; why: test_router_inference_and_management_accept_pinned_api_key_forms groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the router inference and management accept pinned api key forms test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_inference_and_management_accept_pinned_api_key_forms releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router inference and management accept pinned api key forms test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_inference_and_management_accept_pinned_api_key_forms; why: test_router_inference_and_management_accept_pinned_api_key_forms consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router inference and management accept pinned api key forms scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_inference_and_management_accept_pinned_api_key_forms groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the router inference and management accept pinned api key forms test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.post and capture denied; why: the router inference and management accept pinned api key forms test asserts the response, state, or failure produced by this call. + denied = client.post("/v1/chat/completions", json={"model": "low"}) + # What: assert that denied status code equals 401; why: this assertion protects the router inference and management accept pinned api key forms regression after the test's arranged inputs and exercised call. + assert denied.status_code == 401 + # What: assert that denied headers www authenticate equals basic realm freetoken swap; why: this assertion protects the router inference and management accept pinned api key forms regression after the test's arranged inputs and exercised call. + assert denied.headers["www-authenticate"] == 'Basic realm="freetoken-swap"' + # What: assert that client get router status status code equals 401; why: this assertion protects the router inference and management accept pinned api key forms regression after the test's arranged inputs and exercised call. + assert client.get("/router/status").status_code == 401 + # What: act by calling client.get and capture allowed; why: the router inference and management accept pinned api key forms test asserts the response, state, or failure produced by this call. + allowed = client.get("/router/status", headers=headers) + # What: assert that allowed status code equals 200; why: this assertion protects the router inference and management accept pinned api key forms regression after the test's arranged inputs and exercised call. + assert allowed.status_code == 200 + # What: assert that manager calls equals group delimiter; why: this assertion protects the router inference and management accept pinned api key forms regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + + +# What: parameterize test_explicit_authorization_key_takes_precedence_over_x_api_key with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test explicit authorization key takes precedence over x api key. +@pytest.mark.parametrize( + # What: arrange the authorization portion of the enclosing predicate; why: this clause remains in the explicit authorization key takes precedence over x api key scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "authorization", + # What: arrange the grouped source fragment for the scenario; why: test explicit authorization key takes precedence over x api key requires this concrete input or helper state before exercising the behavior under test. + [ + # What: arrange the bearer wrong portion of the enclosing predicate; why: this clause remains in the explicit authorization key takes precedence over x api key scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "Bearer wrong", + # What: act by calling operation.decode with the declared inputs; why: the explicit authorization key takes precedence over x api key scenario observes the operation.decode return value during basic base64 b64encode b operator xff decode. + "Basic " + base64.b64encode(b"operator:wrong").decode(), + # What: act by calling operation.decode with the declared inputs; why: the explicit authorization key takes precedence over x api key scenario observes the operation.decode return value while evaluating "Basic " + base64.b64encode(b"operator:\xff").decode(). + "Basic " + base64.b64encode(b"operator:\xff").decode(), + # What: arrange the grouped source fragment for the scenario; why: test explicit authorization key takes precedence over x api key requires this concrete input or helper state before exercising the behavior under test. + ], +# What: arrange the pytest.mark.parametrize call with decode; why: test_explicit_authorization_key_takes_precedence_over_x_api_key groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +) +# What: define the test_explicit_authorization_key_takes_precedence_over_x_api_key test around authorization; why: this test groups the arrange, act, and assertions that protect the explicit authorization key takes precedence over x api key outcome. +def test_explicit_authorization_key_takes_precedence_over_x_api_key(authorization): + # What: act by calling ModelCatalog and capture catalog doc; why: the explicit authorization key takes precedence over x api key test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_explicit_authorization_key_takes_precedence_over_x_api_key carries low through catalog doc into lifecycle pool lifecycle proxy pool proxy catalog catalog doc. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the explicit authorization key takes precedence over x api key scenario binds this router settings and key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("key",)), + # What: arrange the ModelCatalog call with settings; why: test_explicit_authorization_key_takes_precedence_over_x_api_key groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling Manager and capture manager; why: the explicit authorization key takes precedence over x api key test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_explicit_authorization_key_takes_precedence_over_x_api_key releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the explicit authorization key takes precedence over x api key test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_explicit_authorization_key_takes_precedence_over_x_api_key; why: test_explicit_authorization_key_takes_precedence_over_x_api_key consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the explicit authorization key takes precedence over x api key scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_explicit_authorization_key_takes_precedence_over_x_api_key groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.get and capture response; why: the explicit authorization key takes precedence over x api key test asserts the response, state, or failure produced by this call. + response = TestClient(app).get( + # What: arrange the router status portion of response; why: the explicit authorization key takes precedence over x api key scenario uses this clause to evaluate response as one grouped value. + "/router/status", + # What: arrange the authorization field as authorization; why: test_explicit_authorization_key_takes_precedence_over_x_api_key carries authorization through response into assert response status code equals 401. + headers={"Authorization": authorization, "X-Api-Key": "key"}, + # What: arrange the operation.get call with headers; why: test_explicit_authorization_key_takes_precedence_over_x_api_key groups the supplied clauses as one operation.get call before its value is consumed. + ) + + # What: assert that response status code equals 401; why: this assertion protects the explicit authorization key takes precedence over x api key regression after the test's arranged inputs and exercised call. + assert response.status_code == 401 + # What: assert that manager calls equals group delimiter; why: this assertion protects the explicit authorization key takes precedence over x api key regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + + +# What: define the test_router_terminates_local_authentication_before_proxying test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the router terminates local authentication before proxying outcome. +def test_router_terminates_local_authentication_before_proxying(monkeypatch): + # What: act by calling Manager and capture manager; why: the router terminates local authentication before proxying test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the router terminates local authentication before proxying test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_router_terminates_local_authentication_before_proxying carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the router terminates local authentication before proxying scenario binds this router settings and router test key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("router-test-key",)), + # What: arrange the ModelCatalog call with settings; why: test_router_terminates_local_authentication_before_proxying groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the router terminates local authentication before proxying test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange observed as the fixture input; why: the router terminates local authentication before proxying test consumes this named precondition before exercising the behavior. + observed = {} + + # What: define the upstream test helper around captured fixture state; why: the router terminates local authentication before proxying scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: arrange the exact observed update key lower value for key value fixture fragment; why: the router terminates local authentication before proxying scenario feeds this byte-preserved fragment through observed.update({key.lower(): value for key, value in forward_headers(kw before asserting its protocol or. + observed.update({key.lower(): value for key, value in forward_headers(kwargs["headers"]).items()}) + # What: arrange the helper response as UpstreamResponse 200 Content Type application json BytesIO b ok true; why: test router test feeds this result into the behavior whose outcome is asserted. + return UpstreamResponse(200, {"Content-Type": "application/json"}, BytesIO(b'{"ok":true}')) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the router terminates local authentication before proxying scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before asserting its pr. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_terminates_local_authentication_before_proxying releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router terminates local authentication before proxying test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_terminates_local_authentication_before_proxying; why: test_router_terminates_local_authentication_before_proxying consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router terminates local authentication before proxying scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange token to build_app; why: the router terminates local authentication before proxying scenario binds this daemon control secret value to build_app's token input. + token="daemon-control-secret", + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_terminates_local_authentication_before_proxying groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the router terminates local authentication before proxying test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange the v1 messages portion of response; why: the router terminates local authentication before proxying scenario uses this clause to evaluate response as one grouped value. + "/v1/messages", + # What: arrange content to operation.post; why: the router terminates local authentication before proxying scenario binds this the named fixture input value to operation.post's content input. + content=b'{"model":"low","messages":[]}', + # What: arrange headers to operation.post; why: the router terminates local authentication before proxying scenario binds this content type and authorization and x api key and x ft token and x correlation id value to operation.post's headers input. + headers={ + # What: arrange the content type field as application and json; why: test_router_terminates_local_authentication_before_proxying carries content type through response into assert response status code equals 200. + "Content-Type": "application/json", + # What: arrange the authorization field as basic and not base64; why: test_router_terminates_local_authentication_before_proxying carries authorization through response into assert response status code equals 200. + "Authorization": "Basic !!!not-base64", + # What: arrange the x api key field as router test key; why: test_router_terminates_local_authentication_before_proxying carries x api key through response into assert response status code equals 200. + "X-Api-Key": "router-test-key", + # What: arrange the x ft token field as daemon control secret; why: test_router_terminates_local_authentication_before_proxying carries x ft token through response into assert response status code equals 200. + "X-FT-Token": "daemon-control-secret", + # What: arrange the x correlation id field as client safe id; why: test_router_terminates_local_authentication_before_proxying carries x correlation id through response into assert response status code equals 200. + "X-Correlation-ID": "client-safe-id", + # What: arrange the response mapping with content type and authorization and x api key and x ft token and x correlation id; why: test_router_terminates_local_authentication_before_proxying groups the supplied clauses as one response mapping before its value is consumed. + }, + # What: arrange the operation.post call with content and headers; why: test_router_terminates_local_authentication_before_proxying groups the supplied clauses as one operation.post call before its value is consumed. + ) + # What: assert that response status code equals 200; why: this assertion protects the router terminates local authentication before proxying regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that observed x correlation id equals client safe id; why: this assertion protects the router terminates local authentication before proxying regression after the test's arranged inputs and exercised call. + assert observed["x-correlation-id"] == "client-safe-id" + # What: assert that authorization is absent from observed; why: this assertion protects the router terminates local authentication before proxying regression after the test's arranged inputs and exercised call. + assert "authorization" not in observed + # What: assert that x api key is absent from observed; why: this assertion protects the router terminates local authentication before proxying regression after the test's arranged inputs and exercised call. + assert "x-api-key" not in observed + # What: assert that x ft token is absent from observed; why: this assertion protects the router terminates local authentication before proxying regression after the test's arranged inputs and exercised call. + assert "x-ft-token" not in observed + + +# What: define the test_proxy_response_headers_do_not_apply_inbound_credential_filtering test around local fixtures; why: this test groups the arrange, act, and assertions that protect the proxy response headers do not apply inbound credential filtering outcome. +def test_proxy_response_headers_do_not_apply_inbound_credential_filtering(): + # What: Assert assert response headers in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named assert response headers operation. + assert response_headers( + # What: Assert group delimiter in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named group delimiter operation. + { + # What: Assert authorization engine challenge metadata in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named authorization engine challenge metadata operation. + "Authorization": "Engine challenge metadata", + # What: Assert x ft token engine defined response value in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named x ft token engine defined response value operation. + "X-FT-Token": "engine-defined-response-value", + # What: Assert connection close in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named connection close operation. + "Connection": "close", + # What: Assert group delimiter in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named group delimiter operation. + } + # What: Assert equals in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named equals operation. + ) == { + # What: Assert authorization engine challenge metadata in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named authorization engine challenge metadata operation. + "Authorization": "Engine challenge metadata", + # What: Assert x ft token engine defined response value in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named x ft token engine defined response value operation. + "X-FT-Token": "engine-defined-response-value", + # What: Assert group delimiter in test_proxy_response_headers_do_not_apply_inbound_credential_filtering; why: test_proxy_response_headers_do_not_apply_inbound_credential_filtering uses this assert to implement the named group delimiter operation. + } + + +# What: define the test_router_reload_atomically_replaces_a_valid_catalog test around tmp path; why: this test groups the arrange, act, and assertions that protect the router reload atomically replaces a valid catalog outcome. +def test_router_reload_atomically_replaces_a_valid_catalog(tmp_path): + # What: arrange path as tmp path and models and toml; why: the router reload atomically replaces a valid catalog test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models one nmodel one gguf n encoding fixture fragment; why: the router reload atomically replaces a valid catalog scenario feeds this byte-preserved fragment through path.write_text("[models.one]\nmodel = 'one.gguf'\n", encoding="utf-8") before asserting its protocol or p. + path.write_text("[models.one]\nmodel = 'one.gguf'\n", encoding="utf-8") + # What: act by calling Manager and capture manager; why: the router reload atomically replaces a valid catalog test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog.load and capture catalog doc; why: the router reload atomically replaces a valid catalog test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog.load(str(path)) + # What: act by calling RoutingCoordinator and capture router; why: the router reload atomically replaces a valid catalog test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_reload_atomically_replaces_a_valid_catalog releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router reload atomically replaces a valid catalog test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_reload_atomically_replaces_a_valid_catalog; why: test_router_reload_atomically_replaces_a_valid_catalog consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router reload atomically replaces a valid catalog scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange catalog path to str; why: the router reload atomically replaces a valid catalog scenario binds this str and path value to str's catalog path input. + catalog_path=str(path), + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_reload_atomically_replaces_a_valid_catalog groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the router reload atomically replaces a valid catalog test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: arrange the exact path write text models two nmodel two gguf n encoding fixture fragment; why: the router reload atomically replaces a valid catalog scenario feeds this byte-preserved fragment through path.write_text("[models.two]\nmodel = 'two.gguf'\n", encoding="utf-8") before asserting its protocol. + path.write_text("[models.two]\nmodel = 'two.gguf'\n", encoding="utf-8") + # What: act by calling client.post and capture reloaded; why: the router reload atomically replaces a valid catalog test asserts the response, state, or failure produced by this call. + reloaded = client.post("/router/reload") + # What: assert that reloaded status code equals 200; why: this assertion protects the router reload atomically replaces a valid catalog regression after the test's arranged inputs and exercised call. + assert reloaded.status_code == 200 + # What: assert that item name for item in reloaded json equals two; why: this assertion protects the router reload atomically replaces a valid catalog regression after the test's arranged inputs and exercised call. + assert [item["name"] for item in reloaded.json()["models"]] == ["two"] + # What: arrange the exact path write text models bad nmodel n encoding utf 8 fixture fragment; why: the router reload atomically replaces a valid catalog scenario feeds this byte-preserved fragment through path.write_text("[models.bad]\nmodel = ''\n", encoding="utf-8") before asserting its protocol or parser r. + path.write_text("[models.bad]\nmodel = ''\n", encoding="utf-8") + # What: act by calling client.post and capture rejected; why: the router reload atomically replaces a valid catalog test asserts the response, state, or failure produced by this call. + rejected = client.post("/router/reload") + # What: assert that rejected status code equals 400; why: this assertion protects the router reload atomically replaces a valid catalog regression after the test's arranged inputs and exercised call. + assert rejected.status_code == 400 + # What: assert that item name for item in client get equals two; why: this assertion protects the router reload atomically replaces a valid catalog regression after the test's arranged inputs and exercised call. + assert [item["name"] for item in client.get("/router/profiles").json()["data"]] == ["two"] + + +# What: define the test_router_catalog_reload_rotates_bearer_keys_atomically test around tmp path; why: this test groups the arrange, act, and assertions that protect the router catalog reload rotates bearer keys atomically outcome. +def test_router_catalog_reload_rotates_bearer_keys_atomically(tmp_path): + # What: arrange path as tmp path and models and toml; why: the router catalog reload rotates bearer keys atomically test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with router and api keys and first key and models; why: the router catalog reload rotates bearer keys atomically scenario observes the path.write_text return value during router napi keys first key n models low nmodel. + path.write_text( + # What: arrange the exact router napi keys first key n models low nmodel fixture fragment; why: the router catalog reload rotates bearer keys atomically scenario feeds this byte-preserved fragment through "[router]\napi_keys = ['first-key']\n[models.low]\nmodel = 'low.gguf'\n" before asserting its protocol or. + "[router]\napi_keys = ['first-key']\n[models.low]\nmodel = 'low.gguf'\n", + # What: arrange the exact encoding utf 8 fixture fragment; why: the router catalog reload rotates bearer keys atomically scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_router_catalog_reload_rotates_bearer_keys_atomically groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: act by calling Manager and capture manager; why: the router catalog reload rotates bearer keys atomically test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog.load and capture catalog doc; why: the router catalog reload rotates bearer keys atomically test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog.load(str(path)) + # What: act by calling RoutingCoordinator and capture router; why: the router catalog reload rotates bearer keys atomically test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_catalog_reload_rotates_bearer_keys_atomically releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router catalog reload rotates bearer keys atomically test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_catalog_reload_rotates_bearer_keys_atomically; why: test_router_catalog_reload_rotates_bearer_keys_atomically consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router catalog reload rotates bearer keys atomically scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange catalog path to str; why: the router catalog reload rotates bearer keys atomically scenario binds this str and path value to str's catalog path input. + catalog_path=str(path), + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_catalog_reload_rotates_bearer_keys_atomically groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the router catalog reload rotates bearer keys atomically test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling path.write_text with router and api keys and second key and models; why: the router catalog reload rotates bearer keys atomically scenario observes the path.write_text return value during router napi keys second key n models low nmodel. + path.write_text( + # What: arrange the exact router napi keys second key n models low nmodel fixture fragment; why: the router catalog reload rotates bearer keys atomically scenario feeds this byte-preserved fragment through "[router]\napi_keys = ['second-key']\n[models.low]\nmodel = 'low.gguf'\n before asserting its protoco. + "[router]\napi_keys = ['second-key']\n[models.low]\nmodel = 'low.gguf'\n", + # What: arrange the exact encoding utf 8 fixture fragment; why: the router catalog reload rotates bearer keys atomically scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_router_catalog_reload_rotates_bearer_keys_atomically groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: act by calling client.post and capture reloaded; why: the router catalog reload rotates bearer keys atomically test asserts the response, state, or failure produced by this call. + reloaded = client.post("/router/reload", headers={"Authorization": "Bearer first-key"}) + # What: act by calling client.get and capture old key; why: the router catalog reload rotates bearer keys atomically test asserts the response, state, or failure produced by this call. + old_key = client.get("/router/status", headers={"Authorization": "Bearer first-key"}) + # What: act by calling client.get and capture new key; why: the router catalog reload rotates bearer keys atomically test asserts the response, state, or failure produced by this call. + new_key = client.get("/router/status", headers={"Authorization": "Bearer second-key"}) + # What: assert that reloaded status code equals 200; why: this assertion protects the router catalog reload rotates bearer keys atomically regression after the test's arranged inputs and exercised call. + assert reloaded.status_code == 200 + # What: assert that old key status code equals 401; why: this assertion protects the router catalog reload rotates bearer keys atomically regression after the test's arranged inputs and exercised call. + assert old_key.status_code == 401 + # What: assert that new key status code equals 200; why: this assertion protects the router catalog reload rotates bearer keys atomically regression after the test's arranged inputs and exercised call. + assert new_key.status_code == 200 + + +# What: define the test_router_reload_rejects_redefining_active_profile test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router reload rejects redefining active profile outcome. +def test_router_reload_rejects_redefining_active_profile(): + # What: act by calling Manager and capture manager; why: the router reload rejects redefining active profile test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the router reload rejects redefining active profile test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: act by calling router.acquire and capture lease; why: the router reload rejects redefining active profile test asserts the response, state, or failure produced by this call. + lease = router.acquire("low") + # What: act by calling ModelCatalog and capture replacement; why: the router reload rejects redefining active profile test asserts the response, state, or failure produced by this call. + replacement = ModelCatalog({"low": ModelProfile("low", "changed.gguf", ())}) + # What: assert the pytest.raises failure context; why: the router reload rejects redefining active profile scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RoutingError, match="cannot redefine") as exc: + # What: act by calling router.replace_catalog with replacement; why: the router reload rejects redefining active profile scenario observes the router.replace_catalog return value during assert exc value status code. + router.replace_catalog(replacement) + # What: assert that exc value status code equals 409; why: this assertion protects the router reload rejects redefining active profile regression after the test's arranged inputs and exercised call. + assert exc.value.status_code == 409 + # What: act by calling lease.release with the declared inputs; why: the router reload rejects redefining active profile scenario observes the lease.release return value during the enclosing return. + lease.release() + + +# What: define the test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router reload refuses active scheduling or effective lifecycle changes outcome. +def test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes(): + # What: act by calling Manager and capture manager; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture current; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + current = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf and g; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes carries low through current into router routing coordinator manager current object ready fn ready. + {"low": ModelProfile("low", "low.gguf", (), group="g")}, + # What: arrange settings to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this router settings and routing group and 4 and 12 and g value to RouterSettings's settings input. + settings=RouterSettings( + # What: arrange default ttl s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 4 value to RouterSettings's default ttl s input. + default_ttl_s=4, + # What: arrange unload timeout s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 12 value to RouterSettings's unload timeout s input. + unload_timeout_s=12, + # What: arrange groups to RoutingGroup; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this routing group and g and low and true and false value to RoutingGroup's groups input. + groups=(RoutingGroup("g", ("low",), swap=True, persistent=False),), + # What: arrange the RouterSettings call with default ttl s and unload timeout s and groups; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one RouterSettings call before its value is consumed. + ), + # What: arrange the ModelCatalog call with settings; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, current, object(), ready_fn=ready) + # What: act by calling router.acquire and capture lease; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + lease = router.acquire("low") + + # What: act by calling ModelCatalog and capture changed priority; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + changed_priority = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf and 1; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes carries low through changed priority into changed priority. + {"low": ModelProfile("low", "low.gguf", (), priority=1, group="g")}, + # What: arrange settings to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this router settings and routing group and 4 and 12 and g value to RouterSettings's settings input. + settings=RouterSettings( + # What: arrange default ttl s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 4 value to RouterSettings's default ttl s input. + default_ttl_s=4, + # What: arrange unload timeout s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 12 value to RouterSettings's unload timeout s input. + unload_timeout_s=12, + # What: arrange groups to RoutingGroup; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this routing group and g and low and true and false value to RoutingGroup's groups input. + groups=(RoutingGroup("g", ("low",), swap=True, persistent=False),), + # What: arrange the RouterSettings call with default ttl s and unload timeout s and groups; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one RouterSettings call before its value is consumed. + ), + # What: arrange the ModelCatalog call with settings; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling ModelCatalog and capture changed default ttl; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + changed_default_ttl = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf and g; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes carries low through changed default ttl into changed default ttl. + {"low": ModelProfile("low", "low.gguf", (), group="g")}, + # What: arrange settings to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this router settings and routing group and 5 and 12 and g value to RouterSettings's settings input. + settings=RouterSettings( + # What: arrange default ttl s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 5 value to RouterSettings's default ttl s input. + default_ttl_s=5, + # What: arrange unload timeout s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 12 value to RouterSettings's unload timeout s input. + unload_timeout_s=12, + # What: arrange groups to RoutingGroup; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this routing group and g and low and true and false value to RoutingGroup's groups input. + groups=(RoutingGroup("g", ("low",), swap=True, persistent=False),), + # What: arrange the RouterSettings call with default ttl s and unload timeout s and groups; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one RouterSettings call before its value is consumed. + ), + # What: arrange the ModelCatalog call with settings; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling ModelCatalog and capture changed default unload; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + changed_default_unload = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf and g; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes carries low through changed default unload into changed default unload. + {"low": ModelProfile("low", "low.gguf", (), group="g")}, + # What: arrange settings to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this router settings and routing group and 4 and 13 and g value to RouterSettings's settings input. + settings=RouterSettings( + # What: arrange default ttl s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 4 value to RouterSettings's default ttl s input. + default_ttl_s=4, + # What: arrange unload timeout s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 13 value to RouterSettings's unload timeout s input. + unload_timeout_s=13, + # What: arrange groups to RoutingGroup; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this routing group and g and low and true and false value to RoutingGroup's groups input. + groups=(RoutingGroup("g", ("low",), swap=True, persistent=False),), + # What: arrange the RouterSettings call with default ttl s and unload timeout s and groups; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one RouterSettings call before its value is consumed. + ), + # What: arrange the ModelCatalog call with settings; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling ModelCatalog and capture changed group policy; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + changed_group_policy = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf and g; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes carries low through changed group policy into changed group policy. + {"low": ModelProfile("low", "low.gguf", (), group="g")}, + # What: arrange settings to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this router settings and routing group and 4 and 12 and g value to RouterSettings's settings input. + settings=RouterSettings( + # What: arrange default ttl s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 4 value to RouterSettings's default ttl s input. + default_ttl_s=4, + # What: arrange unload timeout s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 12 value to RouterSettings's unload timeout s input. + unload_timeout_s=12, + # What: arrange groups to RoutingGroup; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this routing group and g and low and false and true value to RoutingGroup's groups input. + groups=(RoutingGroup("g", ("low",), swap=False, persistent=True),), + # What: arrange the RouterSettings call with default ttl s and unload timeout s and groups; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one RouterSettings call before its value is consumed. + ), + # What: arrange the ModelCatalog call with settings; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling ModelCatalog and capture changed request filter; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + changed_request_filter = ModelCatalog( + # What: arrange the low field as model profile and request field and low and low and gguf; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes carries low through changed request filter into changed request filter. + {"low": ModelProfile( + # What: arrange group to ModelProfile; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this g value to ModelProfile's group input. + "low", "low.gguf", (), group="g", + # What: arrange set fields to RequestField; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this request field and 0 2 and temperature value to RequestField's set fields input. + set_fields=(RequestField(("temperature",), "0.2"),), + # What: arrange the changed_request_filter mapping with low; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one changed_request_filter mapping before its value is consumed. + )}, + # What: arrange settings to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this router settings and routing group and 4 and 12 and g value to RouterSettings's settings input. + settings=RouterSettings( + # What: arrange default ttl s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 4 value to RouterSettings's default ttl s input. + default_ttl_s=4, + # What: arrange unload timeout s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 12 value to RouterSettings's unload timeout s input. + unload_timeout_s=12, + # What: arrange groups to RoutingGroup; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this routing group and g and low and true and false value to RoutingGroup's groups input. + groups=(RoutingGroup("g", ("low",), swap=True, persistent=False),), + # What: arrange the RouterSettings call with default ttl s and unload timeout s and groups; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one RouterSettings call before its value is consumed. + ), + # What: arrange the ModelCatalog call with settings; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling ModelCatalog and capture changed transport targets; why: the router reload refuses active scheduling or effective lifecycle changes test asserts the response, state, or failure produced by this call. + changed_transport_targets = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf and g; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes carries low through changed transport targets into changed transport targets. + {"low": ModelProfile( + # What: arrange low low gguf group g check endpoint ready for the scenario; why: test router test router reload refuses active scheduling or effective lifecycle changes requires this concrete input or helper state before exercising the behavior under test. + "low", "low.gguf", (), group="g", check_endpoint="/ready", + # What: arrange proxy to ModelProfile; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this http and port and gateway value to ModelProfile's proxy input. + proxy="http://127.0.0.1:${PORT}/gateway", + # What: arrange use model name to ModelProfile; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this engine low value to ModelProfile's use model name input. + use_model_name="engine-low", + # What: arrange the changed_transport_targets mapping with low; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one changed_transport_targets mapping before its value is consumed. + )}, + # What: arrange settings to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this router settings and routing group and 4 and 12 and g value to RouterSettings's settings input. + settings=RouterSettings( + # What: arrange default ttl s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 4 value to RouterSettings's default ttl s input. + default_ttl_s=4, + # What: arrange unload timeout s to RouterSettings; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this 12 value to RouterSettings's unload timeout s input. + unload_timeout_s=12, + # What: arrange groups to RoutingGroup; why: the router reload refuses active scheduling or effective lifecycle changes scenario binds this routing group and g and low and true and false value to RoutingGroup's groups input. + groups=(RoutingGroup("g", ("low",), swap=True, persistent=False),), + # What: arrange the RouterSettings call with default ttl s and unload timeout s and groups; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one RouterSettings call before its value is consumed. + ), + # What: arrange the ModelCatalog call with settings; why: test_router_reload_refuses_active_scheduling_or_effective_lifecycle_changes groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: arrange for replacement in for the scenario; why: test router reload refuses active scheduling or effective lifecycle changes requires this concrete input or helper state before exercising the behavior under test. + for replacement in ( + # What: arrange the changed priority portion of the enclosing predicate; why: this clause remains in the router reload refuses active scheduling or effective lifecycle changes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + changed_priority, + # What: arrange the changed default ttl portion of the enclosing predicate; why: this clause remains in the router reload refuses active scheduling or effective lifecycle changes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + changed_default_ttl, + # What: arrange the changed default unload portion of the enclosing predicate; why: this clause remains in the router reload refuses active scheduling or effective lifecycle changes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + changed_default_unload, + # What: arrange the changed group policy portion of the enclosing predicate; why: this clause remains in the router reload refuses active scheduling or effective lifecycle changes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + changed_group_policy, + # What: arrange the changed request filter portion of the enclosing predicate; why: this clause remains in the router reload refuses active scheduling or effective lifecycle changes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + changed_request_filter, + # What: arrange the changed transport targets portion of the enclosing predicate; why: this clause remains in the router reload refuses active scheduling or effective lifecycle changes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + changed_transport_targets, + # What: arrange the grouped source fragment for the scenario; why: test router reload refuses active scheduling or effective lifecycle changes requires this concrete input or helper state before exercising the behavior under test. + ): + # What: assert the pytest.raises failure context; why: the router reload refuses active scheduling or effective lifecycle changes scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RoutingError, match="cannot redefine") as exc: + # What: act by calling router.replace_catalog with replacement; why: the router reload refuses active scheduling or effective lifecycle changes scenario observes the router.replace_catalog return value during assert exc value status code. + router.replace_catalog(replacement) + # What: assert that exc value status code equals 409; why: this assertion protects the router reload refuses active scheduling or effective lifecycle changes regression after the test's arranged inputs and exercised call. + assert exc.value.status_code == 409 + # What: assert that router catalog is current; why: this assertion protects the router reload refuses active scheduling or effective lifecycle changes regression after the test's arranged inputs and exercised call. + assert router.catalog is current + # What: act by calling lease.release with the declared inputs; why: the router reload refuses active scheduling or effective lifecycle changes scenario observes the lease.release return value during the enclosing return. + lease.release() + + +# What: define the test_router_reload_cannot_race_atomic_profile_lookup_and_dynamic_port_binding test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router reload cannot race atomic profile lookup and dynamic port binding outcome. +def test_router_reload_cannot_race_atomic_profile_lookup_and_dynamic_port_binding(): + # What: act by calling threading.Event and capture entered; why: the router reload cannot race atomic profile lookup and dynamic port binding test asserts the response, state, or failure produced by this call. + entered = threading.Event() + # What: act by calling threading.Event and capture release status; why: the router reload cannot race atomic profile lookup and dynamic port binding test asserts the response, state, or failure produced by this call. + release_status = threading.Event() + + # What: define BlockingStatusManager as the owner of status; why: daemon callers use this class boundary so those methods share one blocking status manager state invariant. + class BlockingStatusManager(Manager): + # What: arrange block next status False for the scenario; why: test router test router reload cannot race atomic profile lookup and dynamic port binding requires this concrete input or helper state before exercising the behavior under test. + block_next_status = False + + # What: define the status test helper around captured fixture state; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def status(self): + # What: act on block next status before block next status; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario admits block next status only for this predicate and excludes the opposite state. + if self.block_next_status: + # What: arrange self block next status False for the scenario; why: test router test router reload cannot race atomic profile lookup and dynamic port binding requires this concrete input or helper state before exercising the behavior under test. + self.block_next_status = False + # What: act by calling entered.set with the declared inputs; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario observes the entered.set return value during assert release status wait. + entered.set() + # What: assert that release status wait 2; why: this assertion protects the router reload cannot race atomic profile lookup and dynamic port binding regression after the test's arranged inputs and exercised call. + assert release_status.wait(2) + # What: return status and super from the status test helper; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario uses this helper result in its subsequent act or assertion. + return super().status() + + # What: act by calling BlockingStatusManager and capture manager; why: the router reload cannot race atomic profile lookup and dynamic port binding test asserts the response, state, or failure produced by this call. + manager = BlockingStatusManager() + # What: act by calling ModelCatalog and capture current; why: the router reload cannot race atomic profile lookup and dynamic port binding test asserts the response, state, or failure produced by this call. + current = ModelCatalog({"low": ModelProfile("low", "low.gguf", (), port=0)}) + # What: act by calling RoutingCoordinator and capture router; why: the router reload cannot race atomic profile lookup and dynamic port binding test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator( + # What: arrange ready fn to object; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario binds this ready value to object's ready fn input. + manager, current, object(), ready_fn=ready, port_allocator=lambda: 20101 + # What: arrange the RoutingCoordinator call with ready fn and port allocator; why: test_router_reload_cannot_race_atomic_profile_lookup_and_dynamic_port_binding groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + # What: arrange block next status as true; why: the router reload cannot race atomic profile lookup and dynamic port binding test consumes this named precondition before exercising the behavior. + manager.block_next_status = True + # What: arrange acquired as the fixture input; why: the router reload cannot race atomic profile lookup and dynamic port binding test consumes this named precondition before exercising the behavior. + acquired = [] + # What: act by calling threading.Thread and capture acquire thread; why: the router reload cannot race atomic profile lookup and dynamic port binding test asserts the response, state, or failure produced by this call. + acquire_thread = threading.Thread(target=lambda: acquired.append(router.acquire("low"))) + # What: act by calling acquire_thread.start with the declared inputs; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario observes the acquire_thread.start return value during assert entered wait. + acquire_thread.start() + # What: assert that entered wait 1; why: this assertion protects the router reload cannot race atomic profile lookup and dynamic port binding regression after the test's arranged inputs and exercised call. + assert entered.wait(1) + + # What: act by calling ModelCatalog and capture replacement; why: the router reload cannot race atomic profile lookup and dynamic port binding test asserts the response, state, or failure produced by this call. + replacement = ModelCatalog({"low": ModelProfile("low", "changed.gguf", (), port=0)}) + # What: arrange reload result as the fixture input; why: the router reload cannot race atomic profile lookup and dynamic port binding test consumes this named precondition before exercising the behavior. + reload_result = {} + + # What: define the reload_catalog test helper around captured fixture state; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def reload_catalog(): + # What: establish the handler boundary for the protected operation; why: reload_catalog routes failures to routing error while preserving cleanup and success flow. + try: + # What: act by calling router.replace_catalog with replacement; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario observes the router.replace_catalog return value during except routing error as exc. + router.replace_catalog(replacement) + # What: handle routing error by reload result error exc; why: reload_catalog converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: arrange reload result entry as exc; why: the router reload cannot race atomic profile lookup and dynamic port binding test consumes this named precondition before exercising the behavior. + reload_result["error"] = exc + + # What: act by calling threading.Thread and capture reload thread; why: the router reload cannot race atomic profile lookup and dynamic port binding test asserts the response, state, or failure produced by this call. + reload_thread = threading.Thread(target=reload_catalog) + # What: act by calling reload_thread.start with the declared inputs; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario observes the reload_thread.start return value during time sleep. + reload_thread.start() + # What: act by calling time.sleep with 0 05; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario observes the time.sleep return value during assert reload thread is alive. + time.sleep(0.05) + # What: assert that reload thread is alive; why: this assertion protects the router reload cannot race atomic profile lookup and dynamic port binding regression after the test's arranged inputs and exercised call. + assert reload_thread.is_alive() + + # What: act by calling release_status.set with the declared inputs; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario observes the release_status.set return value during acquire thread join. + release_status.set() + # What: act by calling acquire_thread.join with 2; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario observes the acquire_thread.join return value during reload thread join. + acquire_thread.join(2) + # What: act by calling reload_thread.join with 2; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario observes the reload_thread.join return value during assert not acquire thread is alive and not reload thread is alive. + reload_thread.join(2) + # What: assert that not acquire thread is alive and not reload thread is alive; why: this assertion protects the router reload cannot race atomic profile lookup and dynamic port binding regression after the test's arranged inputs and exercised call. + assert not acquire_thread.is_alive() and not reload_thread.is_alive() + # What: assert that reload result error code equals reload conflict; why: this assertion protects the router reload cannot race atomic profile lookup and dynamic port binding regression after the test's arranged inputs and exercised call. + assert reload_result["error"].code == "reload_conflict" + # What: assert that router catalog is current; why: this assertion protects the router reload cannot race atomic profile lookup and dynamic port binding regression after the test's arranged inputs and exercised call. + assert router.catalog is current + # What: assert that manager model equals low gguf; why: this assertion protects the router reload cannot race atomic profile lookup and dynamic port binding regression after the test's arranged inputs and exercised call. + assert manager.model == "low.gguf" + # What: act by calling operation.release with the declared inputs; why: the router reload cannot race atomic profile lookup and dynamic port binding scenario observes the operation.release return value during the enclosing return. + acquired.pop().release() + + +# What: define the test_router_reload_cannot_redefine_a_profile_already_queued_for_admission test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router reload cannot redefine a profile already queued for admission outcome. +def test_router_reload_cannot_redefine_a_profile_already_queued_for_admission(): + # What: act by calling Manager and capture manager; why: the router reload cannot redefine a profile already queued for admission test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling catalog and capture current; why: the router reload cannot redefine a profile already queued for admission test asserts the response, state, or failure produced by this call. + current = catalog() + # What: act by calling RoutingCoordinator and capture router; why: the router reload cannot redefine a profile already queued for admission test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, current, object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the router reload cannot redefine a profile already queued for admission test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: arrange queued lease as the fixture input; why: the router reload cannot redefine a profile already queued for admission test consumes this named precondition before exercising the behavior. + queued_lease = [] + # What: act by calling threading.Thread and capture queued; why: the router reload cannot redefine a profile already queued for admission test asserts the response, state, or failure produced by this call. + queued = threading.Thread(target=lambda: queued_lease.append(router.acquire("high"))) + # What: act by calling queued.start with the declared inputs; why: the router reload cannot redefine a profile already queued for admission scenario observes the queued.start return value during for value in range. + queued.start() + # What: act across range to perform status and router; why: the router reload cannot redefine a profile already queued for admission scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the router reload cannot redefine a profile already queued for admission scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the router reload cannot redefine a profile already queued for admission scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the router reload cannot redefine a profile already queued for admission scenario observes the time.sleep return value during assert router status queued requests. + time.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the router reload cannot redefine a profile already queued for admission regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + + # What: act by calling ModelCatalog and capture replacement; why: the router reload cannot redefine a profile already queued for admission test asserts the response, state, or failure produced by this call. + replacement = ModelCatalog({ + # What: arrange the low field as model profile and low and low and gguf; why: test_router_reload_cannot_redefine_a_profile_already_queued_for_admission carries low through replacement into router replace catalog replacement. + "low": ModelProfile("low", "low.gguf", ()), + # What: arrange the high field as model profile and high and changed and gguf and 10; why: test_router_reload_cannot_redefine_a_profile_already_queued_for_admission carries high through replacement into router replace catalog replacement. + "high": ModelProfile("high", "changed.gguf", (), priority=10), + # What: arrange the ModelCatalog call with model profile; why: test_router_reload_cannot_redefine_a_profile_already_queued_for_admission groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: assert the pytest.raises failure context; why: the router reload cannot redefine a profile already queued for admission scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RoutingError, match="admission or lifecycle") as exc: + # What: act by calling router.replace_catalog with replacement; why: the router reload cannot redefine a profile already queued for admission scenario observes the router.replace_catalog return value during assert exc value code reload conflict. + router.replace_catalog(replacement) + # What: assert that exc value code equals reload conflict; why: this assertion protects the router reload cannot redefine a profile already queued for admission regression after the test's arranged inputs and exercised call. + assert exc.value.code == "reload_conflict" + # What: assert that router catalog is current; why: this assertion protects the router reload cannot redefine a profile already queued for admission regression after the test's arranged inputs and exercised call. + assert router.catalog is current + + # What: act by calling active.release with the declared inputs; why: the router reload cannot redefine a profile already queued for admission scenario observes the active.release return value during queued join. + active.release() + # What: act by calling queued.join with 2; why: the router reload cannot redefine a profile already queued for admission scenario observes the queued.join return value during assert not queued is alive. + queued.join(2) + # What: assert that queued is alive is false; why: this assertion protects the router reload cannot redefine a profile already queued for admission regression after the test's arranged inputs and exercised call. + assert not queued.is_alive() + # What: assert that manager model equals high gguf; why: this assertion protects the router reload cannot redefine a profile already queued for admission regression after the test's arranged inputs and exercised call. + assert manager.model == "high.gguf" + # What: act by calling operation.release with the declared inputs; why: the router reload cannot redefine a profile already queued for admission scenario observes the operation.release return value during the enclosing return. + queued_lease.pop().release() + + +# What: define the test_persistent_group_protects_the_single_resident_slot_until_unloaded test around local fixtures; why: this test groups the arrange, act, and assertions that protect the persistent group protects the single resident slot until unloaded outcome. +def test_persistent_group_protects_the_single_resident_slot_until_unloaded(): + # What: act by calling Manager and capture manager; why: the persistent group protects the single resident slot until unloaded test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the persistent group protects the single resident slot until unloaded test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with keep and other; why: test_persistent_group_protects_the_single_resident_slot_until_unloaded groups the supplied clauses as one catalog_doc mapping before its value. + { + # What: arrange the keep field as model profile and keep and keep and gguf and resident; why: test_persistent_group_protects_the_single_resident_slot_until_unloaded carries keep through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "keep": ModelProfile("keep", "keep.gguf", (), group="resident"), + # What: arrange the other field as model profile and other and other and gguf; why: test_persistent_group_protects_the_single_resident_slot_until_unloaded carries other through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "other": ModelProfile("other", "other.gguf", ()), + # What: arrange the catalog_doc mapping with keep and other; why: test_persistent_group_protects_the_single_resident_slot_until_unloaded groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange settings to RouterSettings; why: the persistent group protects the single resident slot until unloaded scenario binds this router settings and routing group and resident and keep and false value to RouterSettings's settings input. + settings=RouterSettings(groups=( + # What: arrange swap to RoutingGroup; why: the persistent group protects the single resident slot until unloaded scenario binds this false value to RoutingGroup's swap input. + RoutingGroup("resident", ("keep",), swap=False, persistent=True), + # What: arrange the RouterSettings call with groups; why: test_persistent_group_protects_the_single_resident_slot_until_unloaded groups the supplied clauses as one RouterSettings call before its value is consumed. + )), + # What: arrange the ModelCatalog call with settings; why: test_persistent_group_protects_the_single_resident_slot_until_unloaded groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the persistent group protects the single resident slot until unloaded test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange the exact router acquire keep release fixture fragment; why: the persistent group protects the single resident slot until unloaded scenario feeds this byte-preserved fragment through router.acquire("keep").release() before asserting its protocol or parser result. + router.acquire("keep").release() + # What: assert the pytest.raises failure context; why: the persistent group protects the single resident slot until unloaded scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RoutingError, match="single resident-model slot") as exc: + # What: arrange the exact router acquire other fixture fragment; why: the persistent group protects the single resident slot until unloaded scenario feeds this byte-preserved fragment through router.acquire("other") before asserting its protocol or parser result. + router.acquire("other") + # What: assert that exc value code equals capacity unavailable; why: this assertion protects the persistent group protects the single resident slot until unloaded regression after the test's arranged inputs and exercised call. + assert exc.value.code == "capacity_unavailable" + # What: assert that router status resident profiles equals keep; why: this assertion protects the persistent group protects the single resident slot until unloaded regression after the test's arranged inputs and exercised call. + assert router.status()["residentProfiles"] == ["keep"] + # What: assert that router evict idle keep is true; why: this assertion protects the persistent group protects the single resident slot until unloaded regression after the test's arranged inputs and exercised call. + assert router.evict_idle("keep") is True + # What: arrange the exact router acquire other release fixture fragment; why: the persistent group protects the single resident slot until unloaded scenario feeds this byte-preserved fragment through router.acquire("other").release() before asserting its protocol or parser result. + router.acquire("other").release() + # What: assert the expected manager calls == outcome; why: test router test persistent group protects the single resident slot until unloaded protects its regression by requiring this observable result after the exercised behavior. + assert manager.calls == [ + # What: arrange start keep gguf stop 30.0 start other gguf for the scenario; why: test router test persistent group protects the single resident slot until unloaded requires this concrete input or helper state before exercising the behavior under test. + ("start", "keep.gguf"), ("stop", 30.0), ("start", "other.gguf"), + # What: arrange the grouped source fragment for the scenario; why: test router test persistent group protects the single resident slot until unloaded requires this concrete input or helper state before exercising the behavior under test. + ] + + +# What: define the test_explicit_router_cancel_closes_an_inflight_upstream test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the explicit router cancel closes an inflight upstream outcome. +def test_explicit_router_cancel_closes_an_inflight_upstream(monkeypatch): + # What: define BlockingRaw as the owner of __init__ and read and close; why: daemon callers use this class boundary so those methods share one blocking raw state invariant. + class BlockingRaw: + # What: define the __init__ test helper around captured fixture state; why: the explicit router cancel closes an inflight upstream scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def __init__(self): + # What: act by calling threading.Event and capture read started; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + self.read_started = threading.Event() + # What: act by calling threading.Event and capture closed; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + self.closed = threading.Event() + + # What: define the read test helper around size; why: the explicit router cancel closes an inflight upstream scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def read(self, size): + # What: act by calling self.read_started.set with the declared inputs; why: the explicit router cancel closes an inflight upstream scenario observes the self.read_started.set return value during self closed wait. + self.read_started.set() + # What: act by calling self.closed.wait with 2; why: the explicit router cancel closes an inflight upstream scenario observes the self.closed.wait return value during return b. + self.closed.wait(2) + # What: return the named fixture input from the read test helper; why: the explicit router cancel closes an inflight upstream scenario uses this helper result in its subsequent act or assertion. + return b"" + + # What: define the close test helper around captured fixture state; why: the explicit router cancel closes an inflight upstream scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def close(self): + # What: act by calling self.closed.set with the declared inputs; why: the explicit router cancel closes an inflight upstream scenario observes the self.closed.set return value during the enclosing return. + self.closed.set() + + # What: act by calling BlockingRaw and capture raw; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + raw = BlockingRaw() + # What: act by calling Manager and capture manager; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"low": ModelProfile("low", "low.gguf", ())}) + # What: act by calling RoutingCoordinator and capture router; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange upstream calls as the fixture input; why: the explicit router cancel closes an inflight upstream test consumes this named precondition before exercising the behavior. + upstream_calls = [] + + # What: define the upstream test helper around captured fixture state; why: the explicit router cancel closes an inflight upstream scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling upstream_calls.append with kwargs; why: the explicit router cancel closes an inflight upstream scenario observes the upstream_calls.append return value during return upstream response content type text event stream raw. + upstream_calls.append(kwargs) + # What: arrange the helper response as UpstreamResponse 200 Content Type text event stream raw; why: test router test explicit router cancel closes an inflight upstream feeds this result into the behavior whose outcome is asserted. + return UpstreamResponse(200, {"Content-Type": "text/event-stream"}, raw) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the explicit router cancel closes an inflight upstream scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before asserting its protoc. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_explicit_router_cancel_closes_an_inflight_upstream releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_explicit_router_cancel_closes_an_inflight_upstream; why: test_explicit_router_cancel_closes_an_inflight_upstream consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the explicit router cancel closes an inflight upstream scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_explicit_router_cancel_closes_an_inflight_upstream groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: arrange response as the fixture input; why: the explicit router cancel closes an inflight upstream test consumes this named precondition before exercising the behavior. + response = [] + # What: act by calling threading.Thread and capture thread; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + thread = threading.Thread(target=lambda: response.append(client.post( + # What: arrange the model field as low; why: test_explicit_router_cancel_closes_an_inflight_upstream sends this field through thread so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "low"}, headers={"X-FT-Request-ID": "cancel-me"}, + # What: arrange the threading.Thread call with target; why: test_explicit_router_cancel_closes_an_inflight_upstream groups the supplied clauses as one threading.Thread call before its value is consumed. + ))) + # What: act by calling thread.start with the declared inputs; why: the explicit router cancel closes an inflight upstream scenario observes the thread.start return value during assert raw read started wait. + thread.start() + # What: assert that raw read started wait 1; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert raw.read_started.wait(1) + # What: act by calling operation.json and capture active; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + active = client.get("/router/requests").json()["data"] + # What: assert that active equals id cancel me profile low; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert active == [{"id": "cancel-me", "profile": "low"}] + # What: act by calling client.post and capture duplicate; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + duplicate = client.post( + # What: arrange the model field as low; why: test_explicit_router_cancel_closes_an_inflight_upstream sends this field through duplicate so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "low"}, + # What: arrange the x ft request id field as cancel me; why: test_explicit_router_cancel_closes_an_inflight_upstream carries x ft request id through duplicate into assert duplicate status code equals 409. + headers={"X-FT-Request-ID": "cancel-me"}, + # What: arrange the client.post call with json and headers; why: test_explicit_router_cancel_closes_an_inflight_upstream groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: assert that duplicate status code equals 409; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert duplicate.status_code == 409 + # What: assert that duplicate json error type equals request conflict; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert duplicate.json()["error"]["type"] == "request_conflict" + # What: assert that len upstream calls equals 1; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert len(upstream_calls) == 1 + # What: assert that router status admissions equals 1; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert router.status()["admissions"] == 1 + # What: act by calling client.post and capture cancelled; why: the explicit router cancel closes an inflight upstream test asserts the response, state, or failure produced by this call. + cancelled = client.post("/router/requests/cancel-me/cancel") + # What: assert that cancelled json equals cancelled true id cancel me; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert cancelled.json() == {"cancelled": True, "id": "cancel-me"} + # What: act by calling thread.join with 2; why: the explicit router cancel closes an inflight upstream scenario observes the thread.join return value during assert not thread is alive. + thread.join(2) + # What: assert that thread is alive is false; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert not thread.is_alive() + # What: assert that response 0 status code equals 200; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert response[0].status_code == 200 + # What: assert that router status cancellations equals 1; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert router.status()["cancellations"] == 1 + # What: assert that router status terminal streams equals 0; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert router.status()["terminalStreams"] == 0 + # What: assert that freetoken swap terminal streams total 0 is present in router prometheus; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert "freetoken_swap_terminal_streams_total 0" in router.prometheus() + # What: assert that router status active requests equals 0; why: this assertion protects the explicit router cancel closes an inflight upstream regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + + +# What: define the test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes test around local fixtures; why: this test groups the arrange, act, and assertions that protect the native proxy uses a real loopback http upstream and preserves sse bytes outcome. +def test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes(): + # What: arrange seen as the fixture input; why: the native proxy uses a real loopback http upstream and preserves sse bytes test consumes this named precondition before exercising the behavior. + seen = {} + + # What: define Handler as the owner of do_POST and log_message; why: daemon callers use this class boundary so those methods share one handler state invariant. + class Handler(BaseHTTPRequestHandler): + # What: define the do_POST test helper around captured fixture state; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def do_POST(self): + # What: arrange seen entry as path; why: the native proxy uses a real loopback http upstream and preserves sse bytes test consumes this named precondition before exercising the behavior. + seen["path"] = self.path + # What: act by calling self.rfile.read and capture seen entry; why: the native proxy uses a real loopback http upstream and preserves sse bytes test asserts the response, state, or failure produced by this call. + seen["body"] = self.rfile.read(int(self.headers["Content-Length"])) + # What: arrange seen authorization self headers get Authorization for the scenario; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes requires this concrete input or helper state before exercising the behavior under test. + seen["authorization"] = self.headers.get("Authorization") + # What: arrange seen daemon token self headers get X FT Token for the scenario; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes requires this concrete input or helper state before exercising the behavior under test. + seen["daemon_token"] = self.headers.get("X-FT-Token") + # What: arrange seen correlation self headers get X Correlation ID for the scenario; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes requires this concrete input or helper state before exercising the behavior under test. + seen["correlation"] = self.headers.get("X-Correlation-ID") + # What: act by calling self.send_response with 200; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario observes the self.send_response return value during self send header content type text event stream. + self.send_response(200) + # What: arrange the exact self send header content type text event stream fixture fragment; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario feeds this byte-preserved fragment through self.send_header("Content-Type", "text/event-stream") before asserting its protoco. + self.send_header("Content-Type", "text/event-stream") + # What: arrange the exact self send header x engine loopback fixture fragment; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario feeds this byte-preserved fragment through self.send_header("X-Engine", "loopback") before asserting its protocol or parser result. + self.send_header("X-Engine", "loopback") + # What: act by calling self.end_headers with the declared inputs; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario observes the self.end_headers return value during self wfile write b data ok true n. + self.end_headers() + # What: act by calling self.wfile.write with the named fixture input; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario observes the self.wfile.write return value during the enclosing return. + self.wfile.write(b"data: {\"ok\":true}\n\ndata: [DONE]\n\n") + + # What: define the log_message test helper around format; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def log_message(self, format, *args): + # What: ignore the anticipated exception handled by this branch; why: log_message continues its retry or cleanup path instead of re-raising that transient failure. + pass + + # What: act by calling ThreadingHTTPServer and capture server; why: the native proxy uses a real loopback http upstream and preserves sse bytes test asserts the response, state, or failure produced by this call. + server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + # What: act by calling threading.Thread and capture worker; why: the native proxy uses a real loopback http upstream and preserves sse bytes test asserts the response, state, or failure produced by this call. + worker = threading.Thread(target=server.serve_forever, daemon=True) + # What: act by calling worker.start with the declared inputs; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario observes the worker.start return value during try. + worker.start() + # What: establish the handler boundary for the protected operation; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: act by calling Manager and capture manager; why: the native proxy uses a real loopback http upstream and preserves sse bytes test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: arrange port as server address and server and 1; why: the native proxy uses a real loopback http upstream and preserves sse bytes test consumes this named precondition before exercising the behavior. + port = server.server_address[1] + # What: act by calling ModelCatalog and capture catalog doc; why: the native proxy uses a real loopback http upstream and preserves sse bytes test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and port and low and low and gguf; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile( + # What: arrange the low portion of catalog doc; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario uses this clause to evaluate catalog doc as one grouped value. + "low", + # What: arrange the low gguf portion of catalog doc; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario uses this clause to evaluate catalog doc as one grouped value. + "low.gguf", + # What: arrange the catalog_doc collection with ordered entries; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes groups the supplied clauses as one catalog_doc collection before its value is consumed. + (), + # What: arrange port to ModelProfile; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario binds this port value to ModelProfile's port input. + port=port, + # What: arrange proxy to ModelProfile; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario binds this http and port and gateway value to ModelProfile's proxy input. + proxy="http://127.0.0.1:${PORT}/gateway", + # What: arrange the catalog_doc mapping with low; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes groups the supplied clauses as one catalog_doc mapping before its value is consumed. + )}, + # What: arrange settings to RouterSettings; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario binds this router settings and router test key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("router-test-key",)), + # What: arrange the ModelCatalog call with settings; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the native proxy uses a real loopback http upstream and preserves sse bytes test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the native proxy uses a real loopback http upstream and preserves sse bytes test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange token to build_app; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario binds this daemon control secret value to build_app's token input. + token="daemon-control-secret", + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: arrange payload as the fixture input; why: the native proxy uses a real loopback http upstream and preserves sse bytes test consumes this named precondition before exercising the behavior. + payload = b'{"model":"low","stream":true,"messages":[]}' + # What: act by calling operation.post and capture response; why: the native proxy uses a real loopback http upstream and preserves sse bytes test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange content to operation.post; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario binds this payload value to operation.post's content input. + "/v1/chat/completions", content=payload, + # What: arrange headers to operation.post; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario binds this content type and authorization and x ft token and x correlation id and application value to operation.post's headers input. + headers={ + # What: arrange the content type field as application and json; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes carries content type through response into assert response status code equals 200. + "Content-Type": "application/json", + # What: arrange the authorization field as bearer and router test key; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes carries authorization through response into assert response status code equals 200. + "Authorization": "Bearer router-test-key", + # What: arrange the x ft token field as daemon control secret; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes carries x ft token through response into assert response status code equals 200. + "X-FT-Token": "daemon-control-secret", + # What: arrange the x correlation id field as client safe id; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes carries x correlation id through response into assert response status code equals 200. + "X-Correlation-ID": "client-safe-id", + # What: arrange the response mapping with content type and authorization and x ft token and x correlation id; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes groups the supplied clauses as one response mapping before its value is consumed. + }, + # What: arrange the operation.post call with content and headers; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes groups the supplied clauses as one operation.post call before its value is consumed. + ) + # What: assert that response status code equals 200; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that response headers x engine equals loopback; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert response.headers["x-engine"] == "loopback" + # What: assert that response content equals b data ok true n ndata; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert response.content == b"data: {\"ok\":true}\n\ndata: [DONE]\n\n" + # What: assert the expected seen == outcome; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes protects its regression by requiring this observable result after the exercised behavior. + assert seen == { + # What: arrange path gateway v1 chat completions for the scenario; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes requires this concrete input or helper state before exercising the behavior under test. + "path": "/gateway/v1/chat/completions", + # What: arrange body payload for the scenario; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes requires this concrete input or helper state before exercising the behavior under test. + "body": payload, + # What: arrange authorization None for the scenario; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes requires this concrete input or helper state before exercising the behavior under test. + "authorization": None, + # What: arrange daemon token None for the scenario; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes requires this concrete input or helper state before exercising the behavior under test. + "daemon_token": None, + # What: arrange correlation client safe id for the scenario; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes requires this concrete input or helper state before exercising the behavior under test. + "correlation": "client-safe-id", + # What: arrange the grouped source fragment for the scenario; why: test router test native proxy uses a real loopback http upstream and preserves sse bytes requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that router status active requests equals 0; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + # What: assert that router status terminal streams equals 1; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert router.status()["terminalStreams"] == 1 + # What: assert that router status last ttft ms is not group delimiter; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert router.status()["lastTtftMs"] is not None + # What: assert that router status last duration ms is not group delimiter; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert router.status()["lastDurationMs"] is not None + # What: assert that router status last activation ms is not group delimiter; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert router.status()["lastActivationMs"] is not None + # What: assert that router status last queue wait ms is not group delimiter; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert router.status()["lastQueueWaitMs"] is not None + # What: assert that router status last response bytes equals len response content; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert router.status()["lastResponseBytes"] == len(response.content) + # What: assert that router status last proxy bytes per second is not group delimiter; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert router.status()["lastProxyBytesPerSecond"] is not None + # What: act by calling router.prometheus and capture metrics; why: the native proxy uses a real loopback http upstream and preserves sse bytes test asserts the response, state, or failure produced by this call. + metrics = router.prometheus() + # What: assert that freetoken swap last ttft ms is present in metrics; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert "freetoken_swap_last_ttft_ms" in metrics + # What: assert that freetoken swap last activation ms is present in metrics; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert "freetoken_swap_last_activation_ms" in metrics + # What: assert that freetoken swap last queue wait ms is present in metrics; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert "freetoken_swap_last_queue_wait_ms" in metrics + # What: assert that f freetoken swap last response bytes len response content is present in metrics; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert f"freetoken_swap_last_response_bytes {len(response.content)}" in metrics + # What: assert that freetoken swap last proxy bytes per second is present in metrics; why: this assertion protects the native proxy uses a real loopback http upstream and preserves sse bytes regression after the test's arranged inputs and exercised call. + assert "freetoken_swap_last_proxy_bytes_per_second" in metrics + # What: run server shutdown on every exit path; why: test_native_proxy_uses_a_real_loopback_http_upstream_and_preserves_sse_bytes performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act by calling server.shutdown with the declared inputs; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario observes the server.shutdown return value during server server close. + server.shutdown() + # What: act by calling server.server_close with the declared inputs; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario observes the server.server_close return value during worker join. + server.server_close() + # What: act by calling worker.join with 2; why: the native proxy uses a real loopback http upstream and preserves sse bytes scenario observes the worker.join return value during the enclosing return. + worker.join(2) + + +# What: define the test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the streaming chat emits cold queue feedback then preserves upstream sse outcome. +def test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse(monkeypatch): + # What: act by calling Manager and capture manager; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with low and high; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse groups the supplied clauses as one catalog_doc mapping before its value. + { + # What: arrange the low field as model profile and low and low and gguf; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "low": ModelProfile("low", "low.gguf", ()), + # What: arrange the high field as model profile and request field and high and high and gguf; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse carries high through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "high": ModelProfile( + # What: arrange the high high gguf portion of catalog doc; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario uses this clause to evaluate catalog doc as one grouped value. + "high", "high.gguf", (), + # What: arrange set fields to RequestField; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario binds this request field and 0 2 and temperature value to RequestField's set fields input. + set_fields=(RequestField(("temperature",), "0.2"),), + # What: arrange the ModelProfile call with set fields; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse groups the supplied clauses as one ModelProfile call before its value is consumed. + ), + # What: arrange the catalog_doc mapping with low and high; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange settings to RouterSettings; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario binds this router settings and true and 1 value to RouterSettings's settings input. + settings=RouterSettings(send_loading_state=True, capture_buffer_mb=1), + # What: arrange the ModelCatalog call with settings; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: arrange upstream body as the fixture input; why: the streaming chat emits cold queue feedback then preserves upstream sse test consumes this named precondition before exercising the behavior. + upstream_body = b'data: {"token":"real"}\n\ndata: [DONE]\n\n' + # What: arrange seen as the fixture input; why: the streaming chat emits cold queue feedback then preserves upstream sse test consumes this named precondition before exercising the behavior. + seen = {} + + # What: define the upstream test helper around captured fixture state; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: act by calling seen.update with kwargs; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario observes the seen.update return value during return upstream response. + seen.update(kwargs) + # What: return upstream response and bytes io and upstream body and 200 and content type from the upstream test helper; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario uses this helper result in its subsequent act or assertion. + return UpstreamResponse( + # What: arrange 200 Content Type text event stream BytesIO upstream body for the scenario; why: test router test streaming chat emits cold queue feedback then preserves upstream sse requires this concrete input or helper state before exercising the behavior under test. + 200, {"Content-Type": "text/event-stream"}, BytesIO(upstream_body) + # What: arrange the grouped source fragment for the scenario; why: test router test streaming chat emits cold queue feedback then preserves upstream sse requires this concrete input or helper state before exercising the behavior under test. + ) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before as. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: arrange responses as the fixture input; why: the streaming chat emits cold queue feedback then preserves upstream sse test consumes this named precondition before exercising the behavior. + responses = [] + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(2) as lifecycle, ThreadPoolExecutor(2) as proxy: + # What: act by calling build_app and capture app; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling threading.Thread and capture thread; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + thread = threading.Thread(target=lambda: responses.append(client.post( + # What: arrange the v1 chat completions portion of thread; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario uses this clause to evaluate thread as one grouped value. + "/v1/chat/completions", + # What: arrange the model field as high; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse sends this field through thread so the router selects the canonical model or alias for upstream dispatch. + json={"model": "high", "stream": True, "messages": []}, + # What: arrange the x ft request id field as cold feedback; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse carries x ft request id through thread into thread start. + headers={"X-FT-Request-ID": "cold-feedback"}, + # What: arrange the threading.Thread call with target; why: test_streaming_chat_emits_cold_queue_feedback_then_preserves_upstream_sse groups the supplied clauses as one threading.Thread call before its value is consumed. + ))) + # What: act by calling thread.start with the declared inputs; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario observes the thread.start return value during for value in range. + thread.start() + # What: act across range to perform status and router; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the streaming chat emits cold queue feedback then preserves upstream sse scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario observes the time.sleep return value during assert router status queued requests. + time.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: act by calling active.release with the declared inputs; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario observes the active.release return value during thread join. + active.release() + # What: act by calling thread.join with 3; why: the streaming chat emits cold queue feedback then preserves upstream sse scenario observes the thread.join return value during assert not thread is alive. + thread.join(3) + # What: assert that thread is alive is false; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert not thread.is_alive() + # What: act by calling operation.json and capture activity; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + activity = client.get("/router/activity").json()["data"] + # What: assert that len activity equals 1 and activity 0 has capture is true; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert len(activity) == 1 and activity[0]["hasCapture"] is True + # What: act by calling operation.json and capture capture; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + capture = client.get(f'/router/captures/{activity[0]["id"]}').json() + # What: assert that base64 b64decode capture response body base64 equals responses 0 content; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert base64.b64decode(capture["responseBodyBase64"]) == responses[0].content + + # What: arrange response as responses and 0; why: the streaming chat emits cold queue feedback then preserves upstream sse test consumes this named precondition before exercising the behavior. + response = responses[0] + # What: assert that response status code equals 200; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that response headers content type startswith text event stream; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert response.headers["content-type"].startswith("text/event-stream") + # What: assert that response headers x ft request id equals cold feedback; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert response.headers["x-ft-request-id"] == "cold-feedback" + # What: act by calling response.content.decode and capture content; why: the streaming chat emits cold queue feedback then preserves upstream sse test asserts the response, state, or failure produced by this call. + content = response.content.decode("utf-8") + # What: assert that reasoning content freetoken swap loading model high n is present in content; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert '"reasoning_content":"freetoken-swap loading model: high\\n"' in content + # What: assert that reasoning content n queue position 1 is present in content; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert '"reasoning_content":"\\nQueue position: #1 "' in content + # What: assert that response content endswith upstream body; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert response.content.endswith(upstream_body) + # What: assert that json loads seen body temperature equals 0 2; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert json.loads(seen["body"])["temperature"] == 0.2 + # What: assert that router status reserved requests equals 0; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 0 + # What: assert that router status active requests equals 0; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + # What: assert that router status terminal streams equals 1; why: this assertion protects the streaming chat emits cold queue feedback then preserves upstream sse regression after the test's arranged inputs and exercised call. + assert router.status()["terminalStreams"] == 1 + + +# What: define the test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the loading feedback warm path and per model disable preserve exact response outcome. +def test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response(monkeypatch): + # What: arrange upstream body as the fixture input; why: the loading feedback warm path and per model disable preserve exact response test consumes this named precondition before exercising the behavior. + upstream_body = b'data: {"token":"unchanged"}\n\ndata: [DONE]\n\n' + + # What: define the upstream test helper around captured fixture state; why: the loading feedback warm path and per model disable preserve exact response scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: return upstream response and bytes io and upstream body and 201 and content type from the upstream test helper; why: the loading feedback warm path and per model disable preserve exact response scenario uses this helper result in its subsequent act or assertion. + return UpstreamResponse( + # What: arrange the grouped expression portion of the enclosing predicate; why: this clause remains in the loading feedback warm path and per model disable preserve exact response scenario\'s enclosing expression so its grouping and evaluation order stay intact. + 201, + # What: arrange Content Type text event stream X Engine exact for the scenario; why: test router test loading feedback warm path and per model disable preserve exact response requires this concrete input or helper state before exercising the behavior under test. + {"Content-Type": "text/event-stream", "X-Engine": "exact"}, + # What: act by calling BytesIO with upstream body; why: the loading feedback warm path and per model disable preserve exact response scenario observes the BytesIO return value while evaluating BytesIO(upstream_body). + BytesIO(upstream_body), + # What: arrange the grouped source fragment for the scenario; why: test router test loading feedback warm path and per model disable preserve exact response requires this concrete input or helper state before exercising the behavior under test. + ) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the loading feedback warm path and per model disable preserve exact response scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) befor. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: act across the computed value to perform manager and manager; why: the loading feedback warm path and per model disable preserve exact response scenario repeats the body only while or for the loop header admits an iteration. + for warm, override in ((True, None), (False, False)): + # What: act by calling Manager and capture manager; why: the loading feedback warm path and per model disable preserve exact response test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelProfile and capture profile; why: the loading feedback warm path and per model disable preserve exact response test asserts the response, state, or failure produced by this call. + profile = ModelProfile("low", "low.gguf", (), send_loading_state=override) + # What: act by calling ModelCatalog and capture catalog doc; why: the loading feedback warm path and per model disable preserve exact response test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as profile; why: test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": profile}, settings=RouterSettings(send_loading_state=True) + # What: arrange the ModelCatalog call with settings; why: test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the loading feedback warm path and per model disable preserve exact response test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act on warm before release and acquire and router; why: the loading feedback warm path and per model disable preserve exact response scenario admits release and acquire and router only for this predicate and excludes the opposite state. + if warm: + # What: arrange the exact router acquire low release fixture fragment; why: the loading feedback warm path and per model disable preserve exact response scenario feeds this byte-preserved fragment through router.acquire("low").release() before asserting its protocol or parser result. + router.acquire("low").release() + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the loading feedback warm path and per model disable preserve exact response test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response; why: test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the loading feedback warm path and per model disable preserve exact response scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the loading feedback warm path and per model disable preserve exact response test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange the v1 chat completions portion of response; why: the loading feedback warm path and per model disable preserve exact response scenario uses this clause to evaluate response as one grouped value. + "/v1/chat/completions", + # What: arrange the model field as low; why: test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response sends this field through response so the router selects the canonical model or alias for upstream dispatch. + json={"model": "low", "stream": True, "messages": []}, + # What: arrange the operation.post call with json; why: test_loading_feedback_warm_path_and_per_model_disable_preserve_exact_response groups the supplied clauses as one operation.post call before its value is consumed. + ) + # What: assert that response status code equals 201; why: this assertion protects the loading feedback warm path and per model disable preserve exact response regression after the test's arranged inputs and exercised call. + assert response.status_code == 201 + # What: assert that response headers x engine equals exact; why: this assertion protects the loading feedback warm path and per model disable preserve exact response regression after the test's arranged inputs and exercised call. + assert response.headers["x-engine"] == "exact" + # What: assert that response content equals upstream body; why: this assertion protects the loading feedback warm path and per model disable preserve exact response regression after the test's arranged inputs and exercised call. + assert response.content == upstream_body + + +# What: define the test_loading_feedback_never_turns_concurrency_rejection_into_sse test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the loading feedback never turns concurrency rejection into sse outcome. +def test_loading_feedback_never_turns_concurrency_rejection_into_sse(monkeypatch): + # What: act by calling Manager and capture manager; why: the loading feedback never turns concurrency rejection into sse test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelProfile and capture profile; why: the loading feedback never turns concurrency rejection into sse test asserts the response, state, or failure produced by this call. + profile = ModelProfile("low", "low.gguf", (), concurrency_limit=1) + # What: act by calling ModelCatalog and capture catalog doc; why: the loading feedback never turns concurrency rejection into sse test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as profile; why: test_loading_feedback_never_turns_concurrency_rejection_into_sse carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": profile}, settings=RouterSettings(send_loading_state=True) + # What: arrange the ModelCatalog call with settings; why: test_loading_feedback_never_turns_concurrency_rejection_into_sse groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the loading feedback never turns concurrency rejection into sse test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the loading feedback never turns concurrency rejection into sse test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: arrange monkeypatch setattr for the scenario; why: test loading feedback never turns concurrency rejection into sse requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the loading feedback never turns concurrency rejection into sse scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs pytest fail over limit request reached fixture fragment; why: the loading feedback never turns concurrency rejection into sse scenario feeds this byte-preserved fragment through lambda **kwargs: pytest.fail("over-limit request reached upstream") before asserting its prot. + lambda **kwargs: pytest.fail("over-limit request reached upstream"), + # What: arrange the monkeypatch.setattr call with fail; why: test_loading_feedback_never_turns_concurrency_rejection_into_sse groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_loading_feedback_never_turns_concurrency_rejection_into_sse releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the loading feedback never turns concurrency rejection into sse test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_loading_feedback_never_turns_concurrency_rejection_into_sse; why: test_loading_feedback_never_turns_concurrency_rejection_into_sse consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the loading feedback never turns concurrency rejection into sse scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_loading_feedback_never_turns_concurrency_rejection_into_sse groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the loading feedback never turns concurrency rejection into sse test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange the v1 chat completions portion of response; why: the loading feedback never turns concurrency rejection into sse scenario uses this clause to evaluate response as one grouped value. + "/v1/chat/completions", + # What: arrange the model field as low; why: test_loading_feedback_never_turns_concurrency_rejection_into_sse sends this field through response so the router selects the canonical model or alias for upstream dispatch. + json={"model": "low", "stream": True, "messages": []}, + # What: arrange the operation.post call with json; why: test_loading_feedback_never_turns_concurrency_rejection_into_sse groups the supplied clauses as one operation.post call before its value is consumed. + ) + + # What: assert that response status code equals 429; why: this assertion protects the loading feedback never turns concurrency rejection into sse regression after the test's arranged inputs and exercised call. + assert response.status_code == 429 + # What: assert that response headers content type startswith application json; why: this assertion protects the loading feedback never turns concurrency rejection into sse regression after the test's arranged inputs and exercised call. + assert response.headers["content-type"].startswith("application/json") + # What: assert that response headers retry after equals 1; why: this assertion protects the loading feedback never turns concurrency rejection into sse regression after the test's arranged inputs and exercised call. + assert response.headers["retry-after"] == "1" + # What: assert that response json error type equals concurrency limit; why: this assertion protects the loading feedback never turns concurrency rejection into sse regression after the test's arranged inputs and exercised call. + assert response.json()["error"]["type"] == "concurrency_limit" + # What: assert that b loading model is absent from response content; why: this assertion protects the loading feedback never turns concurrency rejection into sse regression after the test's arranged inputs and exercised call. + assert b"loading model" not in response.content + # What: act by calling active.release with the declared inputs; why: the loading feedback never turns concurrency rejection into sse scenario observes the active.release return value during the enclosing return. + active.release() + + +# What: define the test_loading_feedback_frames_activation_failure_and_done test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the loading feedback frames activation failure and done outcome. +def test_loading_feedback_frames_activation_failure_and_done(monkeypatch): + # What: act by calling Manager and capture manager; why: the loading feedback frames activation failure and done test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the loading feedback frames activation failure and done test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_loading_feedback_frames_activation_failure_and_done carries low through catalog doc into router routing coordinator manager catalog doc object ready fn fail ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the loading feedback frames activation failure and done scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(send_loading_state=True), + # What: arrange the ModelCatalog call with settings; why: test_loading_feedback_frames_activation_failure_and_done groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + + # What: define the fail_ready test helper around manager and probe and pid and port and timeout s; why: the loading feedback frames activation failure and done scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def fail_ready(manager, probe, *, pid, port, timeout_s): + # What: act by calling time.sleep with 0 05; why: the loading feedback frames activation failure and done scenario observes the time.sleep return value during return ready reason qualification failed. + time.sleep(0.05) + # What: arrange the ready field as false; why: fail_ready carries ready into return {"ready": False, "reason": "qualification failed"}. + return {"ready": False, "reason": "qualification failed"} + + # What: act by calling RoutingCoordinator and capture router; why: the loading feedback frames activation failure and done test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=fail_ready) + # What: arrange monkeypatch setattr for the scenario; why: test loading feedback frames activation failure and done requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the loading feedback frames activation failure and done scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs pytest fail failed activation reached fixture fragment; why: the loading feedback frames activation failure and done scenario feeds this byte-preserved fragment through lambda **kwargs: pytest.fail("failed activation reached upstream") before asserting its protocol or pa. + lambda **kwargs: pytest.fail("failed activation reached upstream"), + # What: arrange the monkeypatch.setattr call with fail; why: test_loading_feedback_frames_activation_failure_and_done groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_loading_feedback_frames_activation_failure_and_done releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the loading feedback frames activation failure and done test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_loading_feedback_frames_activation_failure_and_done; why: test_loading_feedback_frames_activation_failure_and_done consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the loading feedback frames activation failure and done scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_loading_feedback_frames_activation_failure_and_done groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the loading feedback frames activation failure and done test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange the v1 chat completions portion of response; why: the loading feedback frames activation failure and done scenario uses this clause to evaluate response as one grouped value. + "/v1/chat/completions", + # What: arrange the model field as low; why: test_loading_feedback_frames_activation_failure_and_done sends this field through response so the router selects the canonical model or alias for upstream dispatch. + json={"model": "low", "stream": True, "messages": []}, + # What: arrange the operation.post call with json; why: test_loading_feedback_frames_activation_failure_and_done groups the supplied clauses as one operation.post call before its value is consumed. + ) + + # What: assert that response status code equals 200; why: this assertion protects the loading feedback frames activation failure and done regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that response headers content type startswith text event stream; why: this assertion protects the loading feedback frames activation failure and done regression after the test's arranged inputs and exercised call. + assert response.headers["content-type"].startswith("text/event-stream") + # What: assert that b qualification failed is present in response content; why: this assertion protects the loading feedback frames activation failure and done regression after the test's arranged inputs and exercised call. + assert b"qualification failed" in response.content + # What: assert that b type engine not ready is present in response content; why: this assertion protects the loading feedback frames activation failure and done regression after the test's arranged inputs and exercised call. + assert b'"type":"engine_not_ready"' in response.content + # What: assert that response content endswith b data done n n; why: this assertion protects the loading feedback frames activation failure and done regression after the test's arranged inputs and exercised call. + assert response.content.endswith(b"data: [DONE]\n\n") + # What: assert the expected all outcome; why: test router test loading feedback frames activation failure and done protects its regression by requiring this observable result after the exercised behavior. + assert all( + # What: arrange not line or line startswith b data for the scenario; why: test router test loading feedback frames activation failure and done requires this concrete input or helper state before exercising the behavior under test. + not line or line.startswith(b"data: ") + # What: arrange for line in response content rstrip splitlines for the scenario; why: test router test loading feedback frames activation failure and done requires this concrete input or helper state before exercising the behavior under test. + for line in response.content.rstrip().splitlines() + # What: arrange the grouped source fragment for the scenario; why: test router test loading feedback frames activation failure and done requires this concrete input or helper state before exercising the behavior under test. + ) + # What: assert that router status reserved requests equals 0; why: this assertion protects the loading feedback frames activation failure and done regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 0 + # What: assert that router status active requests equals 0; why: this assertion protects the loading feedback frames activation failure and done regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + + +# What: define the test_loading_feedback_frames_upstream_connect_failure_and_releases_lease test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the loading feedback frames upstream connect failure and releases lease outcome. +def test_loading_feedback_frames_upstream_connect_failure_and_releases_lease(monkeypatch): + # What: act by calling Manager and capture manager; why: the loading feedback frames upstream connect failure and releases lease test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the loading feedback frames upstream connect failure and releases lease test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_loading_feedback_frames_upstream_connect_failure_and_releases_lease carries low through catalog doc into router routing coordinator manager catalog doc object ready fn slow ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the loading feedback frames upstream connect failure and releases lease scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(send_loading_state=True), + # What: arrange the ModelCatalog call with settings; why: test_loading_feedback_frames_upstream_connect_failure_and_releases_lease groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + + # What: define the slow_ready test helper around manager and probe and pid and port and timeout s; why: the loading feedback frames upstream connect failure and releases lease scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def slow_ready(manager, probe, *, pid, port, timeout_s): + # What: act by calling time.sleep with 0 05; why: the loading feedback frames upstream connect failure and releases lease scenario observes the time.sleep return value during return ready health status ok. + time.sleep(0.05) + # What: arrange the ready field as true; why: slow_ready carries ready into return {"ready": True, "health": {"status": "ok"}}. + return {"ready": True, "health": {"status": "ok"}} + + # What: act by calling RoutingCoordinator and capture router; why: the loading feedback frames upstream connect failure and releases lease test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=slow_ready) + # What: arrange monkeypatch setattr for the scenario; why: test loading feedback frames upstream connect failure and releases lease requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the loading feedback frames upstream connect failure and releases lease scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs value for value in fixture fragment; why: the loading feedback frames upstream connect failure and releases lease scenario feeds this byte-preserved fragment through lambda **kwargs: (_ for _ in ()).throw(OSError("connection refused")) before asserting its protocol or pa. + lambda **kwargs: (_ for _ in ()).throw(OSError("connection refused")), + # What: arrange the monkeypatch.setattr call with throw; why: test_loading_feedback_frames_upstream_connect_failure_and_releases_lease groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_loading_feedback_frames_upstream_connect_failure_and_releases_lease releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the loading feedback frames upstream connect failure and releases lease test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_loading_feedback_frames_upstream_connect_failure_and_releases_lease; why: test_loading_feedback_frames_upstream_connect_failure_and_releases_lease consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the loading feedback frames upstream connect failure and releases lease scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_loading_feedback_frames_upstream_connect_failure_and_releases_lease groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the loading feedback frames upstream connect failure and releases lease test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange the v1 chat completions portion of response; why: the loading feedback frames upstream connect failure and releases lease scenario uses this clause to evaluate response as one grouped value. + "/v1/chat/completions", + # What: arrange the model field as low; why: test_loading_feedback_frames_upstream_connect_failure_and_releases_lease sends this field through response so the router selects the canonical model or alias for upstream dispatch. + json={"model": "low", "stream": True, "messages": []}, + # What: arrange the operation.post call with json; why: test_loading_feedback_frames_upstream_connect_failure_and_releases_lease groups the supplied clauses as one operation.post call before its value is consumed. + ) + + # What: assert that response status code equals 200; why: this assertion protects the loading feedback frames upstream connect failure and releases lease regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that b connection refused is present in response content; why: this assertion protects the loading feedback frames upstream connect failure and releases lease regression after the test's arranged inputs and exercised call. + assert b"connection refused" in response.content + # What: assert that b type upstream unavailable is present in response content; why: this assertion protects the loading feedback frames upstream connect failure and releases lease regression after the test's arranged inputs and exercised call. + assert b'"type":"upstream_unavailable"' in response.content + # What: assert that response content endswith b data done n n; why: this assertion protects the loading feedback frames upstream connect failure and releases lease regression after the test's arranged inputs and exercised call. + assert response.content.endswith(b"data: [DONE]\n\n") + # What: assert that router status active requests equals 0; why: this assertion protects the loading feedback frames upstream connect failure and releases lease regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + # What: assert that router status reserved requests equals 0; why: this assertion protects the loading feedback frames upstream connect failure and releases lease regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 0 + + +# What: define the test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the loading feedback explicit queue cancellation is in band and releases outcome. +def test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases(monkeypatch): + # What: act by calling Manager and capture manager; why: the loading feedback explicit queue cancellation is in band and releases test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the loading feedback explicit queue cancellation is in band and releases test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with low and high; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases groups the supplied clauses as one catalog_doc mapping before its value. + { + # What: arrange the low field as model profile and low and low and gguf; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "low": ModelProfile("low", "low.gguf", ()), + # What: arrange the high field as model profile and high and high and gguf; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases carries high through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "high": ModelProfile("high", "high.gguf", ()), + # What: arrange the catalog_doc mapping with low and high; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases groups the supplied clauses as one catalog_doc mapping before its value. + }, + # What: arrange settings to RouterSettings; why: the loading feedback explicit queue cancellation is in band and releases scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(send_loading_state=True), + # What: arrange the ModelCatalog call with settings; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the loading feedback explicit queue cancellation is in band and releases test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the loading feedback explicit queue cancellation is in band and releases test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: arrange monkeypatch setattr for the scenario; why: test loading feedback explicit queue cancellation is in band and releases requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the loading feedback explicit queue cancellation is in band and releases scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs pytest fail cancelled request reached fixture fragment; why: the loading feedback explicit queue cancellation is in band and releases scenario feeds this byte-preserved fragment through lambda **kwargs: pytest.fail("cancelled request reached upstream") before asserting i. + lambda **kwargs: pytest.fail("cancelled request reached upstream"), + # What: arrange the monkeypatch.setattr call with fail; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: arrange responses as the fixture input; why: the loading feedback explicit queue cancellation is in band and releases test consumes this named precondition before exercising the behavior. + responses = [] + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(2) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the loading feedback explicit queue cancellation is in band and releases test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the loading feedback explicit queue cancellation is in band and releases scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the loading feedback explicit queue cancellation is in band and releases test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling threading.Thread and capture thread; why: the loading feedback explicit queue cancellation is in band and releases test asserts the response, state, or failure produced by this call. + thread = threading.Thread(target=lambda: responses.append(client.post( + # What: arrange the v1 chat completions portion of thread; why: the loading feedback explicit queue cancellation is in band and releases scenario uses this clause to evaluate thread as one grouped value. + "/v1/chat/completions", + # What: arrange the model field as high; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases sends this field through thread so the router selects the canonical model or alias for upstream dispatch. + json={"model": "high", "stream": True, "messages": []}, + # What: arrange the x ft request id field as cancel loading; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases carries x ft request id through thread into thread start. + headers={"X-FT-Request-ID": "cancel-loading"}, + # What: arrange the threading.Thread call with target; why: test_loading_feedback_explicit_queue_cancellation_is_in_band_and_releases groups the supplied clauses as one threading.Thread call before its value is consumed. + ))) + # What: act by calling thread.start with the declared inputs; why: the loading feedback explicit queue cancellation is in band and releases scenario observes the thread.start return value during for value in range. + thread.start() + # What: act across range to perform status and router; why: the loading feedback explicit queue cancellation is in band and releases scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the loading feedback explicit queue cancellation is in band and releases scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the loading feedback explicit queue cancellation is in band and releases scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the loading feedback explicit queue cancellation is in band and releases scenario observes the time.sleep return value during assert router status queued requests. + time.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the loading feedback explicit queue cancellation is in band and releases regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: act by calling client.post and capture cancelled; why: the loading feedback explicit queue cancellation is in band and releases test asserts the response, state, or failure produced by this call. + cancelled = client.post("/router/requests/cancel-loading/cancel") + # What: assert that cancelled json equals cancelled true id cancel loading; why: this assertion protects the loading feedback explicit queue cancellation is in band and releases regression after the test's arranged inputs and exercised call. + assert cancelled.json() == {"cancelled": True, "id": "cancel-loading"} + # What: act by calling thread.join with 3; why: the loading feedback explicit queue cancellation is in band and releases scenario observes the thread.join return value during assert not thread is alive. + thread.join(3) + # What: assert that thread is alive is false; why: this assertion protects the loading feedback explicit queue cancellation is in band and releases regression after the test's arranged inputs and exercised call. + assert not thread.is_alive() + + # What: assert that responses 0 status code equals 200; why: this assertion protects the loading feedback explicit queue cancellation is in band and releases regression after the test's arranged inputs and exercised call. + assert responses[0].status_code == 200 + # What: assert that b type request cancelled is present in responses 0 content; why: this assertion protects the loading feedback explicit queue cancellation is in band and releases regression after the test's arranged inputs and exercised call. + assert b'"type":"request_cancelled"' in responses[0].content + # What: assert that responses 0 content endswith b data done n n; why: this assertion protects the loading feedback explicit queue cancellation is in band and releases regression after the test's arranged inputs and exercised call. + assert responses[0].content.endswith(b"data: [DONE]\n\n") + # What: assert that router status queued requests equals 0; why: this assertion protects the loading feedback explicit queue cancellation is in band and releases regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 0 + # What: assert that router status reserved requests equals 1; why: this assertion protects the loading feedback explicit queue cancellation is in band and releases regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 1 + # What: act by calling active.release with the declared inputs; why: the loading feedback explicit queue cancellation is in band and releases scenario observes the active.release return value during the enclosing return. + active.release() + + +# What: define the test_loading_feedback_cancellation_during_activation_is_not_completion_credit test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the loading feedback cancellation during activation is not completion credit outcome. +def test_loading_feedback_cancellation_during_activation_is_not_completion_credit(monkeypatch): + # What: act by calling Manager and capture manager; why: the loading feedback cancellation during activation is not completion credit test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the loading feedback cancellation during activation is not completion credit test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_loading_feedback_cancellation_during_activation_is_not_completion_credit carries low through catalog doc into router routing coordinator manager catalog doc object ready fn blocking ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the loading feedback cancellation during activation is not completion credit scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(send_loading_state=True), + # What: arrange the ModelCatalog call with settings; why: test_loading_feedback_cancellation_during_activation_is_not_completion_credit groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling threading.Event and capture activation started; why: the loading feedback cancellation during activation is not completion credit test asserts the response, state, or failure produced by this call. + activation_started = threading.Event() + # What: act by calling threading.Event and capture finish activation; why: the loading feedback cancellation during activation is not completion credit test asserts the response, state, or failure produced by this call. + finish_activation = threading.Event() + + # What: define the blocking_ready test helper around manager and probe and pid and port and timeout s; why: the loading feedback cancellation during activation is not completion credit scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def blocking_ready(manager, probe, *, pid, port, timeout_s): + # What: act by calling activation_started.set with the declared inputs; why: the loading feedback cancellation during activation is not completion credit scenario observes the activation_started.set return value during assert finish activation wait. + activation_started.set() + # What: assert that finish activation wait 2; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert finish_activation.wait(2) + # What: arrange the ready field as true; why: blocking_ready carries ready into return {"ready": True, "health": {"status": "ok"}}. + return {"ready": True, "health": {"status": "ok"}} + + # What: act by calling RoutingCoordinator and capture router; why: the loading feedback cancellation during activation is not completion credit test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=blocking_ready) + # What: arrange monkeypatch setattr for the scenario; why: test loading feedback cancellation during activation is not completion credit requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the loading feedback cancellation during activation is not completion credit scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs pytest fail cancelled activation reached fixture fragment; why: the loading feedback cancellation during activation is not completion credit scenario feeds this byte-preserved fragment through lambda **kwargs: pytest.fail("cancelled activation reached upstream") before a. + lambda **kwargs: pytest.fail("cancelled activation reached upstream"), + # What: arrange the monkeypatch.setattr call with fail; why: test_loading_feedback_cancellation_during_activation_is_not_completion_credit groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: arrange responses as the fixture input; why: the loading feedback cancellation during activation is not completion credit test consumes this named precondition before exercising the behavior. + responses = [] + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_loading_feedback_cancellation_during_activation_is_not_completion_credit releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(2) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the loading feedback cancellation during activation is not completion credit test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_loading_feedback_cancellation_during_activation_is_not_completion_credit; why: test_loading_feedback_cancellation_during_activation_is_not_completion_credit consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the loading feedback cancellation during activation is not completion credit scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_loading_feedback_cancellation_during_activation_is_not_completion_credit groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the loading feedback cancellation during activation is not completion credit test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling threading.Thread and capture thread; why: the loading feedback cancellation during activation is not completion credit test asserts the response, state, or failure produced by this call. + thread = threading.Thread(target=lambda: responses.append(client.post( + # What: arrange the v1 chat completions portion of thread; why: the loading feedback cancellation during activation is not completion credit scenario uses this clause to evaluate thread as one grouped value. + "/v1/chat/completions", + # What: arrange the model field as low; why: test_loading_feedback_cancellation_during_activation_is_not_completion_credit sends this field through thread so the router selects the canonical model or alias for upstream dispatch. + json={"model": "low", "stream": True, "messages": []}, + # What: arrange the x ft request id field as cancel activation; why: test_loading_feedback_cancellation_during_activation_is_not_completion_credit carries x ft request id through thread into thread start. + headers={"X-FT-Request-ID": "cancel-activation"}, + # What: arrange the threading.Thread call with target; why: test_loading_feedback_cancellation_during_activation_is_not_completion_credit groups the supplied clauses as one threading.Thread call before its value is consumed. + ))) + # What: act by calling thread.start with the declared inputs; why: the loading feedback cancellation during activation is not completion credit scenario observes the thread.start return value during assert activation started wait. + thread.start() + # What: assert that activation started wait 1; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert activation_started.wait(1) + # What: act by calling client.post and capture cancelled; why: the loading feedback cancellation during activation is not completion credit test asserts the response, state, or failure produced by this call. + cancelled = client.post("/router/requests/cancel-activation/cancel") + # What: assert that cancelled json equals cancelled true id cancel activation; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert cancelled.json() == {"cancelled": True, "id": "cancel-activation"} + # What: act by calling finish_activation.set with the declared inputs; why: the loading feedback cancellation during activation is not completion credit scenario observes the finish_activation.set return value during thread join. + finish_activation.set() + # What: act by calling thread.join with 3; why: the loading feedback cancellation during activation is not completion credit scenario observes the thread.join return value during assert not thread is alive. + thread.join(3) + # What: assert that thread is alive is false; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert not thread.is_alive() + + # What: assert that responses 0 status code equals 200; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert responses[0].status_code == 200 + # What: assert that b type request cancelled is present in responses 0 content; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert b'"type":"request_cancelled"' in responses[0].content + # What: assert that responses 0 content endswith b data done n n; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert responses[0].content.endswith(b"data: [DONE]\n\n") + # What: assert that router status active requests equals 0; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + # What: assert that router status reserved requests equals 0; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 0 + # What: assert that router status terminal streams equals 0; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert router.status()["terminalStreams"] == 0 + # What: assert that router status cancellations equals 1; why: this assertion protects the loading feedback cancellation during activation is not completion credit regression after the test's arranged inputs and exercised call. + assert router.status()["cancellations"] == 1 + + +# What: define the test_loading_feedback_disconnect_cancels_queued_ownership test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the loading feedback disconnect cancels queued ownership outcome. +def test_loading_feedback_disconnect_cancels_queued_ownership(monkeypatch): + # What: act by calling Manager and capture manager; why: the loading feedback disconnect cancels queued ownership test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the loading feedback disconnect cancels queued ownership test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the catalog_doc mapping with low and high; why: test_loading_feedback_disconnect_cancels_queued_ownership groups the supplied clauses as one catalog_doc mapping before its value is consumed. + { + # What: arrange the low field as model profile and low and low and gguf; why: test_loading_feedback_disconnect_cancels_queued_ownership carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "low": ModelProfile("low", "low.gguf", ()), + # What: arrange the high field as model profile and high and high and gguf; why: test_loading_feedback_disconnect_cancels_queued_ownership carries high through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "high": ModelProfile("high", "high.gguf", ()), + # What: arrange the catalog_doc mapping with low and high; why: test_loading_feedback_disconnect_cancels_queued_ownership groups the supplied clauses as one catalog_doc mapping before its value is consumed. + }, + # What: arrange settings to RouterSettings; why: the loading feedback disconnect cancels queued ownership scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(send_loading_state=True), + # What: arrange the ModelCatalog call with settings; why: test_loading_feedback_disconnect_cancels_queued_ownership groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the loading feedback disconnect cancels queued ownership test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the loading feedback disconnect cancels queued ownership test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: arrange monkeypatch setattr for the scenario; why: test loading feedback disconnect cancels queued ownership requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the loading feedback disconnect cancels queued ownership scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the exact lambda kwargs pytest fail disconnected request reached fixture fragment; why: the loading feedback disconnect cancels queued ownership scenario feeds this byte-preserved fragment through lambda **kwargs: pytest.fail("disconnected request reached upstream") before asserting its protoco. + lambda **kwargs: pytest.fail("disconnected request reached upstream"), + # What: arrange the monkeypatch.setattr call with fail; why: test_loading_feedback_disconnect_cancels_queued_ownership groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: define the scenario test helper around app; why: the loading feedback disconnect cancels queued ownership scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def scenario(app): + # What: act by calling operation.encode and capture body; why: the loading feedback disconnect cancels queued ownership test asserts the response, state, or failure produced by this call. + body = json.dumps({"model": "high", "stream": True, "messages": []}).encode() + # What: act by calling asyncio.Event and capture disconnect; why: the loading feedback disconnect cancels queued ownership test asserts the response, state, or failure produced by this call. + disconnect = asyncio.Event() + # What: arrange request sent as false; why: the loading feedback disconnect cancels queued ownership test consumes this named precondition before exercising the behavior. + request_sent = False + # What: arrange sent as the fixture input; why: the loading feedback disconnect cancels queued ownership test consumes this named precondition before exercising the behavior. + sent = [] + + # What: define the receive test helper around captured fixture state; why: the loading feedback disconnect cancels queued ownership scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def receive(): + # What: arrange the nonlocal request sent portion of the enclosing predicate; why: this clause remains in the loading feedback disconnect cancels queued ownership scenario\'s enclosing expression so its grouping and evaluation order stay intact. + nonlocal request_sent + # What: act on request sent before request sent; why: the loading feedback disconnect cancels queued ownership scenario admits request sent only for this predicate and excludes the opposite state. + if not request_sent: + # What: arrange request sent as true; why: the loading feedback disconnect cancels queued ownership test consumes this named precondition before exercising the behavior. + request_sent = True + # What: arrange the type field as http and request; why: receive carries type into return {"type": "http.request", "body": body, "more_body": False}. + return {"type": "http.request", "body": body, "more_body": False} + # What: act by calling disconnect.wait with the declared inputs; why: the loading feedback disconnect cancels queued ownership scenario observes the disconnect.wait return value during return type http disconnect. + await disconnect.wait() + # What: arrange the type field as http and disconnect; why: receive carries type into return {"type": "http.disconnect"}. + return {"type": "http.disconnect"} + + # What: define the send test helper around message; why: the loading feedback disconnect cancels queued ownership scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def send(message): + # What: act by calling sent.append with message; why: the loading feedback disconnect cancels queued ownership scenario observes the sent.append return value during the enclosing return. + sent.append(message) + + # What: act by calling operation.encode and capture scope; why: the loading feedback disconnect cancels queued ownership test asserts the response, state, or failure produced by this call. + scope = { + # What: arrange the version field as 3 0; why: scenario carries version through scope into request asyncio create task app scope receive send. + "type": "http", "asgi": {"version": "3.0"}, "http_version": "1.1", + # What: arrange the method field as post; why: scenario carries method through scope into request asyncio create task app scope receive send. + "method": "POST", "scheme": "http", "path": "/v1/chat/completions", + # What: arrange the raw path field as the fixture input; why: scenario carries raw path through scope into request asyncio create task app scope receive send. + "raw_path": b"/v1/chat/completions", "query_string": b"", "root_path": "", + # What: arrange the headers field as encode and str and len and body; why: scenario carries headers through scope into request asyncio create task app scope receive send. + "headers": [ + # What: arrange the b content type b application json portion of scope; why: the loading feedback disconnect cancels queued ownership scenario uses this clause to evaluate scope as one grouped value. + (b"content-type", b"application/json"), + # What: act by calling operation.encode with the declared inputs; why: the loading feedback disconnect cancels queued ownership scenario observes the operation.encode return value during b x ft request id b disconnect loading. + (b"content-length", str(len(body)).encode()), + # What: arrange the b x ft request id b disconnect loading portion of scope; why: the loading feedback disconnect cancels queued ownership scenario uses this clause to evaluate scope as one grouped value. + (b"x-ft-request-id", b"disconnect-loading"), + # What: arrange the scope collection with the named fixture input and encode and str and len and body and the named fixture input; why: scenario groups the supplied clauses as one scope collection before its value is consumed. + ], + # What: arrange the client field as 127 0 0 1 and 1; why: scenario carries client through scope into request asyncio create task app scope receive send. + "client": ("127.0.0.1", 1), "server": ("127.0.0.1", 80), + # What: arrange the scope mapping with type and asgi and http version and method and scheme; why: scenario groups the supplied clauses as one scope mapping before its value is consumed. + } + # What: act by calling asyncio.create_task and capture request; why: the loading feedback disconnect cancels queued ownership test asserts the response, state, or failure produced by this call. + request = asyncio.create_task(app(scope, receive, send)) + # What: act across range to perform status and router; why: the loading feedback disconnect cancels queued ownership scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: arrange if router status queuedRequests == 1 for the scenario; why: test router test loading feedback disconnect cancels queued ownership requires this concrete input or helper state before exercising the behavior under test. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the loading feedback disconnect cancels queued ownership scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the loading feedback disconnect cancels queued ownership scenario observes the asyncio.sleep return value during assert router status queued requests. + await asyncio.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the loading feedback disconnect cancels queued ownership regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: act by calling disconnect.set with the declared inputs; why: the loading feedback disconnect cancels queued ownership scenario observes the disconnect.set return value during await asyncio wait for request. + disconnect.set() + # What: act by calling asyncio.wait_for with request and 2; why: the loading feedback disconnect cancels queued ownership scenario observes the asyncio.wait_for return value during for value in range. + await asyncio.wait_for(request, 2) + # What: act across range to perform status and router; why: the loading feedback disconnect cancels queued ownership scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: arrange if router status queuedRequests == 0 for the scenario; why: test router test loading feedback disconnect cancels queued ownership requires this concrete input or helper state before exercising the behavior under test. + if router.status()["queuedRequests"] == 0: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the loading feedback disconnect cancels queued ownership scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the loading feedback disconnect cancels queued ownership scenario observes the asyncio.sleep return value during assert router status queued requests. + await asyncio.sleep(0.01) + # What: assert that router status queued requests equals 0; why: this assertion protects the loading feedback disconnect cancels queued ownership regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 0 + # What: assert that any message type equals http response start for message in sent; why: this assertion protects the loading feedback disconnect cancels queued ownership regression after the test's arranged inputs and exercised call. + assert any(message["type"] == "http.response.start" for message in sent) + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_loading_feedback_disconnect_cancels_queued_ownership releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(2) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the loading feedback disconnect cancels queued ownership test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_loading_feedback_disconnect_cancels_queued_ownership; why: test_loading_feedback_disconnect_cancels_queued_ownership consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the loading feedback disconnect cancels queued ownership scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_loading_feedback_disconnect_cancels_queued_ownership groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling asyncio.run with scenario and app; why: the loading feedback disconnect cancels queued ownership scenario observes the asyncio.run return value during assert router status reserved requests. + asyncio.run(scenario(app)) + + # What: assert that router status reserved requests equals 1; why: this assertion protects the loading feedback disconnect cancels queued ownership regression after the test's arranged inputs and exercised call. + assert router.status()["reservedRequests"] == 1 + # What: assert that router status cancellations equals 1; why: this assertion protects the loading feedback disconnect cancels queued ownership regression after the test's arranged inputs and exercised call. + assert router.status()["cancellations"] == 1 + # What: act by calling active.release with the declared inputs; why: the loading feedback disconnect cancels queued ownership scenario observes the active.release return value during assert manager calls start low gguf. + active.release() + # What: assert that manager calls equals start low gguf; why: this assertion protects the loading feedback disconnect cancels queued ownership regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + + +# What: parameterize test_loading_feedback_is_only_for_strictly_streaming_chat_requests with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test loading feedback is only for strictly streaming chat requests. +@pytest.mark.parametrize( + # What: arrange the path payload portion of the enclosing predicate; why: this clause remains in the loading feedback is only for strictly streaming chat requests scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "path,payload", + # What: arrange the grouped source fragment for the scenario; why: test loading feedback is only for strictly streaming chat requests requires this concrete input or helper state before exercising the behavior under test. + [ + # What: arrange v1 chat completions model low stream False messages for the scenario; why: test router test loading feedback is only for strictly streaming chat requests requires this concrete input or helper state before exercising the behavior under test. + ("/v1/chat/completions", {"model": "low", "stream": False, "messages": []}), + # What: arrange v1 chat completions model low stream 1 messages for the scenario; why: test router test loading feedback is only for strictly streaming chat requests requires this concrete input or helper state before exercising the behavior under test. + ("/v1/chat/completions", {"model": "low", "stream": 1, "messages": []}), + # What: arrange v1 completions model low stream True prompt for the scenario; why: test router test loading feedback is only for strictly streaming chat requests requires this concrete input or helper state before exercising the behavior under test. + ("/v1/completions", {"model": "low", "stream": True, "prompt": ""}), + # What: arrange v1 messages model low stream True messages for the scenario; why: test router test loading feedback is only for strictly streaming chat requests requires this concrete input or helper state before exercising the behavior under test. + ("/v1/messages", {"model": "low", "stream": True, "messages": []}), + # What: arrange the grouped source fragment for the scenario; why: test loading feedback is only for strictly streaming chat requests requires this concrete input or helper state before exercising the behavior under test. + ], +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +) +# What: define the test_loading_feedback_is_only_for_strictly_streaming_chat_requests test around monkeypatch and path and payload; why: this test groups the arrange, act, and assertions that protect the loading feedback is only for strictly streaming chat requests outcome. +def test_loading_feedback_is_only_for_strictly_streaming_chat_requests( + # What: arrange the monkeypatch input for test_loading_feedback_is_only_for_strictly_streaming_chat_requests; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests consumes monkeypatch during monkeypatch setattr, so callers must bind it with the other signature inputs. + monkeypatch, path, payload +# What: arrange the grouped source fragment for the scenario; why: test loading feedback is only for strictly streaming chat requests requires this concrete input or helper state before exercising the. +): + # What: arrange body as the fixture input; why: the loading feedback is only for strictly streaming chat requests test consumes this named precondition before exercising the behavior. + body = b'{"ordinary":true}' + # What: act by calling ModelCatalog and capture catalog doc; why: the loading feedback is only for strictly streaming chat requests test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the loading feedback is only for strictly streaming chat requests scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(send_loading_state=True), + # What: arrange the ModelCatalog call with settings; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling Manager and capture manager; why: the loading feedback is only for strictly streaming chat requests test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the loading feedback is only for strictly streaming chat requests test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange monkeypatch setattr for the scenario; why: test loading feedback is only for strictly streaming chat requests requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact freetoken daemon app open upstream fixture fragment; why: the loading feedback is only for strictly streaming chat requests scenario feeds this byte-preserved fragment through "freetoken.daemon.app.open_upstream" before asserting its protocol or parser result. + "freetoken.daemon.app.open_upstream", + # What: arrange the kwargs input for test_loading_feedback_is_only_for_strictly_streaming_chat_requests; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests consumes kwargs during signature binding, so callers must bind it with the other signature inputs. + lambda **kwargs: UpstreamResponse( + # What: arrange the content type field as application and json; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests carries content type into 202, {"Content-Type": "application/json", "X-Mode": "ordinary"}, BytesIO. + 202, {"Content-Type": "application/json", "X-Mode": "ordinary"}, BytesIO(body) + # What: arrange the UpstreamResponse call with bytes io; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests groups the supplied clauses as one UpstreamResponse call before its value is consumed. + ), + # What: arrange the monkeypatch.setattr call with upstream response; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the loading feedback is only for strictly streaming chat requests test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_loading_feedback_is_only_for_strictly_streaming_chat_requests; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the loading feedback is only for strictly streaming chat requests scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_loading_feedback_is_only_for_strictly_streaming_chat_requests groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the loading feedback is only for strictly streaming chat requests test asserts the response, state, or failure produced by this call. + response = TestClient(app).post(path, json=payload) + + # What: assert that response status code equals 202; why: this assertion protects the loading feedback is only for strictly streaming chat requests regression after the test's arranged inputs and exercised call. + assert response.status_code == 202 + # What: assert that response headers x mode equals ordinary; why: this assertion protects the loading feedback is only for strictly streaming chat requests regression after the test's arranged inputs and exercised call. + assert response.headers["x-mode"] == "ordinary" + # What: assert that response content equals body; why: this assertion protects the loading feedback is only for strictly streaming chat requests regression after the test's arranged inputs and exercised call. + assert response.content == body + + +# What: define the test_request_filter_is_explicit_top_level_removal_and_default_is_byte_preserving test around local fixtures; why: this test groups the arrange, act, and assertions that protect the request filter is explicit top level removal and default is byte preserving outcome. +def test_request_filter_is_explicit_top_level_removal_and_default_is_byte_preserving(): + # What: arrange raw as the fixture input; why: the request filter is explicit top level removal and default is byte preserving test consumes this named precondition before exercising the behavior. + raw = b'{"model":"low", "metadata":{"private":true}, "user":"operator"}' + # What: assert that filter request body raw equals raw; why: this assertion protects the request filter is explicit top level removal and default is byte preserving regression after the test's arranged inputs and exercised call. + assert filter_request_body(raw, ()) == raw + # What: assert that filter request body raw metadata user equals b model low; why: this assertion protects the request filter is explicit top level removal and default is byte preserving regression after the test's arranged inputs and exercised call. + assert filter_request_body(raw, ("metadata", "user")) == b'{"model":"low"}' + + +# What: define the test_request_filter_applies_nested_drop_global_and_requested_id_fields_in_order test around local fixtures; why: this test groups the arrange, act, and assertions that protect the request filter applies nested drop global and requested id fields in order outcome. +def test_request_filter_applies_nested_drop_global_and_requested_id_fields_in_order(): + # What: arrange raw as the fixture input; why: the request filter applies nested drop global and requested id fields in order test consumes this named precondition before exercising the behavior. + raw = ( + # What: arrange the b model low high metadata private portion of raw; why: the request filter applies nested drop global and requested id fields in order scenario uses this clause to evaluate raw as one grouped value. + b'{"model":"low:high","metadata":{"private":true,"keep":1},' + # What: arrange the b max tokens top p stream false stop portion of raw; why: the request filter applies nested drop global and requested id fields in order scenario uses this clause to evaluate raw as one grouped value. + b'"max_tokens":7,"top_p":0.9,"stream":false,"stop":null,' + # What: arrange the b chat template kwargs enable thinking false portion of raw; why: the request filter applies nested drop global and requested id fields in order scenario uses this clause to evaluate raw as one grouped value. + b'"chat_template_kwargs":{"enable_thinking":false}}' + # What: arrange the raw expression with raw b model low high metadata private true keep; why: test_request_filter_applies_nested_drop_global_and_requested_id_fields_in_order groups the supplied clauses as one raw expression before its value is consumed. + ) + # What: act by calling RequestField and capture global fields; why: the request filter applies nested drop global and requested id fields in order test asserts the response, state, or failure produced by this call. + global_fields = ( + # What: act by calling RequestField with max tokens and 1000; why: the request filter applies nested drop global and requested id fields in order scenario observes the RequestField return value during request field stream true soft. + RequestField(("max_tokens",), "1000"), + # What: arrange RequestField stream true soft True for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + RequestField(("stream",), "true", soft=True), + # What: arrange RequestField stop configured soft True for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + RequestField(("stop",), '"configured"', soft=True), + # What: arrange RequestField top p 0.2 soft True for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + RequestField(("top_p",), "0.2", soft=True), + # What: act by calling RequestField with temperature and 0 5; why: the request filter applies nested drop global and requested id fields in order scenario observes the RequestField return value during request field chat template kwargs reasoning effort medium. + RequestField(("temperature",), "0.5"), + # What: act by calling RequestField with chat template kwargs and reasoning effort and medium; why: the request filter applies nested drop global and requested id fields in order scenario observes the RequestField return value while evaluating RequestField(("chat_template_kwargs", "reasoning_effort"), '"medium. + RequestField(("chat_template_kwargs", "reasoning_effort"), '"medium"'), + # What: arrange the grouped source fragment for the scenario; why: test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + ) + # What: act by calling RequestField and capture by id; why: the request filter applies nested drop global and requested id fields in order test asserts the response, state, or failure produced by this call. + by_id = (("low:high", ( + # What: arrange RequestField max tokens 2000 soft True for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + RequestField(("max_tokens",), "2000", soft=True), + # What: act by calling RequestField with temperature and 0 1; why: the request filter applies nested drop global and requested id fields in order scenario observes the RequestField return value during request field chat template kwargs reasoning effort high. + RequestField(("temperature",), "0.1"), + # What: act by calling RequestField with chat template kwargs and reasoning effort and high; why: the request filter applies nested drop global and requested id fields in order scenario observes the RequestField return value while evaluating RequestField(("chat_template_kwargs", "reasoning_effort"), '"high"'). + RequestField(("chat_template_kwargs", "reasoning_effort"), '"high"'), + # What: arrange the grouped expression portion of by id; why: the request filter applies nested drop global and requested id fields in order scenario uses this clause to evaluate by id as one grouped value. + )),) + + # What: act by calling json.loads and capture filtered; why: the request filter applies nested drop global and requested id fields in order test asserts the response, state, or failure produced by this call. + filtered = json.loads(filter_request_body( + # What: arrange the raw portion of filtered; why: the request filter applies nested drop global and requested id fields in order scenario uses this clause to evaluate filtered as one grouped value. + raw, + # What: arrange the metadata private top p portion of filtered; why: the request filter applies nested drop global and requested id fields in order scenario uses this clause to evaluate filtered as one grouped value. + ("metadata.private", "top_p"), + # What: arrange the global fields portion of filtered; why: the request filter applies nested drop global and requested id fields in order scenario uses this clause to evaluate filtered as one grouped value. + global_fields, + # What: arrange the by id portion of filtered; why: the request filter applies nested drop global and requested id fields in order scenario uses this clause to evaluate filtered as one grouped value. + by_id, + # What: arrange requested model to json.loads; why: the request filter applies nested drop global and requested id fields in order scenario binds this low and high value to json.loads's requested model input. + requested_model="low:high", + # What: arrange rewrite model to json.loads; why: the request filter applies nested drop global and requested id fields in order scenario binds this engine model value to json.loads's rewrite model input. + rewrite_model="engine-model", + # What: arrange the json.loads call with filter request body; why: test_request_filter_applies_nested_drop_global_and_requested_id_fields_in_order groups the supplied clauses as one json.loads call before its value is consumed. + )) + + # What: assert the expected filtered == outcome; why: test router test request filter applies nested drop global and requested id fields in order protects its regression by requiring this observable result after the exercised behavior. + assert filtered == { + # What: arrange model engine model for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "model": "engine-model", + # What: arrange metadata keep 1 for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "metadata": {"keep": 1}, + # What: arrange max tokens 1000 for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "max_tokens": 1000, + # What: arrange stream False for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "stream": False, + # What: arrange stop None for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "stop": None, + # What: arrange top p 0.2 for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "top_p": 0.2, + # What: arrange temperature 0.1 for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "temperature": 0.1, + # What: arrange chat template kwargs for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "chat_template_kwargs": { + # What: arrange enable thinking False for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "enable_thinking": False, + # What: arrange reasoning effort high for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + "reasoning_effort": "high", + # What: arrange the grouped source fragment for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + }, + # What: arrange the grouped source fragment for the scenario; why: test router test request filter applies nested drop global and requested id fields in order requires this concrete input or helper state before exercising the behavior under test. + } + + +# What: define the test_loading_feedback_policy_fails_closed_for_a_removed_model test around local fixtures; why: this test groups the arrange, act, and assertions that protect the loading feedback policy fails closed for a removed model outcome. +def test_loading_feedback_policy_fails_closed_for_a_removed_model(): + # What: act by calling RoutingCoordinator and capture router; why: the loading feedback policy fails closed for a removed model test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(Manager(), catalog(), object(), ready_fn=ready) + # What: assert that router loading feedback enabled missing is false; why: this assertion protects the loading feedback policy fails closed for a removed model regression after the test's arranged inputs and exercised call. + assert router.loading_feedback_enabled("missing") is False + + +# What: define the test_router_applies_variant_filters_to_inference_and_json_upstream_only test around monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the router applies variant filters to inference and json upstream only outcome. +def test_router_applies_variant_filters_to_inference_and_json_upstream_only( + # What: arrange monkeypatch tmp path for the scenario; why: test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange path as tmp path and models and toml; why: the router applies variant filters to inference and json upstream only test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: act by calling path.write_text with models and low and model and private; why: the router applies variant filters to inference and json upstream only scenario observes the path.write_text return value during models low. + path.write_text( + # What: arrange the exact models low fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through """[models.low] before asserting its protocol or parser result. + # What: arrange the exact model private gguf fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through """[models.low] before asserting its protocol or parser result. + # What: arrange the exact use model name engine model fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through """[models.low] before asserting its protocol or parser result. + # What: arrange the exact drop fields user fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through """[models.low] before asserting its protocol or parser result. + # What: arrange the exact models low set fields fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through """[models.low] before asserting its protocol or parser result. + # What: arrange models low for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + # What: arrange the exact max tokens fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through """[models.low] before asserting its protocol or parser result. + # What: arrange the exact models low set fields by id low high fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through """[models.low] before asserting its protocol or parser result. + # What: arrange models low for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + # What: arrange the exact metadata variant high fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through """[models.low] before asserting its protocol or parser result. + # What: arrange the exact the grouped expression fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through """[models.low] before asserting its protocol or parser result. + """[models.low] +model = "private.gguf" +use_model_name = "engine-model" +drop_fields = ["user"] +[models.low.set_fields] +temperature = 0.5 +"max_tokens?" = 100 +[models.low.set_fields_by_id."low:high"] +temperature = 0.1 +"metadata.variant" = "high" +""", + # What: arrange the exact encoding utf 8 fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: act by calling ModelCatalog.load and capture catalog doc; why: the router applies variant filters to inference and json upstream only test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog.load(str(path)) + # What: act by calling Manager and capture manager; why: the router applies variant filters to inference and json upstream only test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling RoutingCoordinator and capture router; why: the router applies variant filters to inference and json upstream only test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange seen as the fixture input; why: the router applies variant filters to inference and json upstream only test consumes this named precondition before exercising the behavior. + seen = [] + + # What: define the upstream test helper around captured fixture state; why: the router applies variant filters to inference and json upstream only scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: arrange the exact seen append kwargs body fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through seen.append(kwargs["body"]) before asserting its protocol or parser result. + seen.append(kwargs["body"]) + # What: arrange the helper response as UpstreamResponse 200 Content Type application json BytesIO b; why: test router applies variant filter feeds this result into the behavior whose outcome is asserted. + return UpstreamResponse(200, {"Content-Type": "application/json"}, BytesIO(b'{}')) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the router applies variant filters to inference and json upstream only scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before asse. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router applies variant filters to inference and json upstream only test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_applies_variant_filters_to_inference_and_json_upstream_only; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router applies variant filters to inference and json upstream only scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the router applies variant filters to inference and json upstream only test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.post and capture routed; why: the router applies variant filters to inference and json upstream only test asserts the response, state, or failure produced by this call. + routed = client.post( + # What: arrange the v1 chat completions portion of routed; why: the router applies variant filters to inference and json upstream only scenario uses this clause to evaluate routed as one grouped value. + "/v1/chat/completions", + # What: arrange the model field as low and high; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only sends this field through routed so the router selects the canonical model or alias for upstream dispatch. + json={"model": "low:high", "max_tokens": 7, "user": "private"}, + # What: arrange the client.post call with json; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.post and capture direct json; why: the router applies variant filters to inference and json upstream only test asserts the response, state, or failure produced by this call. + direct_json = client.post( + # What: arrange the upstream low high custom portion of direct json; why: the router applies variant filters to inference and json upstream only scenario uses this clause to evaluate direct json as one grouped value. + "/upstream/low:high/custom", + # What: arrange content b model low high user private for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + content=b'{"model":"low:high","user":"private"}', + # What: arrange the content type field as application and json; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only carries content type through direct json into assert routed status code equals direct json status code equals direct raw status code equals. + headers={"Content-Type": "application/json"}, + # What: arrange the client.post call with content and headers; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.post and capture direct raw; why: the router applies variant filters to inference and json upstream only test asserts the response, state, or failure produced by this call. + direct_raw = client.post( + # What: arrange the upstream low high custom portion of direct raw; why: the router applies variant filters to inference and json upstream only scenario uses this clause to evaluate direct raw as one grouped value. + "/upstream/low:high/custom", + # What: arrange content b not json private body for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + content=b"not-json-private-body", + # What: arrange the content type field as application and octet stream; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only carries content type through direct raw into assert routed status code equals direct json status code equals direct raw status code equals. + headers={"Content-Type": "application/octet-stream"}, + # What: arrange the client.post call with content and headers; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.post and capture malformed json; why: the router applies variant filters to inference and json upstream only test asserts the response, state, or failure produced by this call. + malformed_json = client.post( + # What: arrange the upstream low high custom portion of malformed json; why: the router applies variant filters to inference and json upstream only scenario uses this clause to evaluate malformed json as one grouped value. + "/upstream/low:high/custom", + # What: arrange content to client.post; why: the router applies variant filters to inference and json upstream only scenario binds this the named fixture input value to client.post's content input. + content=b"{", + # What: arrange the content type field as application and json; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only carries content type through malformed json into assert malformed json status code equals 400. + headers={"Content-Type": "application/json"}, + # What: arrange the client.post call with content and headers; why: test_router_applies_variant_filters_to_inference_and_json_upstream_only groups the supplied clauses as one client.post call before its value is consumed. + ) + + # What: assert that routed status code equals direct json status code equals direct raw status code equals 200; why: this assertion protects the router applies variant filters to inference and json upstream only regression after the test's arranged inputs and exercised call. + assert routed.status_code == direct_json.status_code == direct_raw.status_code == 200 + # What: assert that malformed json status code equals 400; why: this assertion protects the router applies variant filters to inference and json upstream only regression after the test's arranged inputs and exercised call. + assert malformed_json.status_code == 400 + # What: assert that malformed json json error type equals invalid request; why: this assertion protects the router applies variant filters to inference and json upstream only regression after the test's arranged inputs and exercised call. + assert malformed_json.json()["error"]["type"] == "invalid_request" + # What: assert the expected json loads seen 0 == outcome; why: test router test router applies variant filters to inference and json upstream only protects its regression by requiring this observable result after the exercised behavior. + assert json.loads(seen[0]) == { + # What: arrange model engine model max tokens 7 temperature 0.1 for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + "model": "engine-model", "max_tokens": 7, "temperature": 0.1, + # What: arrange metadata variant high for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + "metadata": {"variant": "high"}, + # What: arrange the grouped source fragment for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert the expected json loads seen 1 == outcome; why: test router test router applies variant filters to inference and json upstream only protects its regression by requiring this observable result after the exercised behavior. + assert json.loads(seen[1]) == { + # What: arrange model engine model temperature 0.1 max tokens 100 for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + "model": "engine-model", "temperature": 0.1, "max_tokens": 100, + # What: arrange metadata variant high for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + "metadata": {"variant": "high"}, + # What: arrange the grouped source fragment for the scenario; why: test router test router applies variant filters to inference and json upstream only requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that seen 2 equals b not json private body; why: this assertion protects the router applies variant filters to inference and json upstream only regression after the test's arranged inputs and exercised call. + assert seen[2] == b"not-json-private-body" + # What: assert that len seen equals 3; why: this assertion protects the router applies variant filters to inference and json upstream only regression after the test's arranged inputs and exercised call. + assert len(seen) == 3 + # What: assert that router status active requests equals 0; why: this assertion protects the router applies variant filters to inference and json upstream only regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + + +# What: define the test_router_event_log_is_bounded_private_and_protected test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the router event log is bounded private and protected outcome. +def test_router_event_log_is_bounded_private_and_protected(monkeypatch): + """Router events are useful operational evidence without retaining prompts or secrets.""" + # What: document router events are useful operational evidence in the test_router_event_log_is_bounded_private_and_protected docstring; why: introspection and maintainers read this exact docstring fragment to understand test router event log is bounded private and protected behavior without executing it. + # What: act by calling Manager and capture manager; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_router_event_log_is_bounded_private_and_protected carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the router event log is bounded private and protected scenario binds this router settings and router test key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("router-test-key",)), + # What: arrange the ModelCatalog call with settings; why: test_router_event_log_is_bounded_private_and_protected groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: act by calling LogRing and capture router ring; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + router_ring = LogRing(capacity=1) + + # What: define the upstream test helper around captured fixture state; why: the router event log is bounded private and protected scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def upstream(**kwargs): + # What: arrange the helper response as UpstreamResponse 200 Content Type application json BytesIO b ok true; why: test router event log is bounded private feeds this result into the behavior whose outcome is asserted. + return UpstreamResponse(200, {"Content-Type": "application/json"}, BytesIO(b'{"ok":true}')) + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream upstream fixture fragment; why: the router event log is bounded private and protected scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) before asserting its protoco. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_event_log_is_bounded_private_and_protected releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_event_log_is_bounded_private_and_protected; why: test_router_event_log_is_bounded_private_and_protected consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router event log is bounded private and protected scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange router ring to build_app; why: the router event log is bounded private and protected scenario binds this router ring value to build_app's router ring input. + router_ring=router_ring, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_event_log_is_bounded_private_and_protected groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: assert that client get router logs status code equals 401; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert client.get("/router/logs").status_code == 401 + # What: act by calling client.post and capture response; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + response = client.post( + # What: arrange the upstream low private token in path access token do not log portion of response; why: the router event log is bounded private and protected scenario uses this clause to evaluate response as one grouped value. + "/upstream/low/private-token-in-path?access_token=do-not-log", + # What: arrange content to client.post; why: the router event log is bounded private and protected scenario binds this the named fixture input value to client.post's content input. + content=b'{"model":"low","messages":["private prompt"]}', + # What: arrange the content type field as application and json; why: test_router_event_log_is_bounded_private_and_protected carries content type through response into assert response status code equals 200. + headers={"Content-Type": "application/json", "Authorization": "Bearer router-test-key"}, + # What: arrange the client.post call with content and headers; why: test_router_event_log_is_bounded_private_and_protected groups the supplied clauses as one client.post call before its value is consumed. + ) + # A legacy direct lifecycle request must not replace a resident routed + # child behind the coordinator's lease/residency bookkeeping. + # What: act by calling client.post and capture blocked; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + blocked = client.post("/engine/stop", headers={"Authorization": "Bearer router-test-key"}) + # What: assert that response status code equals 200; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that blocked status code equals 409; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert blocked.status_code == 409 + # What: assert that manager model equals low gguf; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert manager.model == "low.gguf" + # What: act by calling router_ring.since and capture records and cursor; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + records, cursor = router_ring.since(0) + # What: assert that cursor equals 2; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert cursor == 2 + # What: assert that len records equals 1; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert len(records) == 1 # the configured bounded ring evicted admission + # What: act by calling json.loads and capture events; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + events = [json.loads(record["text"]) for record in records] + # What: assert that event event for event in events equals request finished; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert [event["event"] for event in events] == ["request_finished"] + # What: assert that events 1 response bytes equals len b ok true; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert events[-1]["responseBytes"] == len(b'{"ok":true}') + # What: act by calling json.dumps and capture serialized; why: the router event log is bounded private and protected test asserts the response, state, or failure produced by this call. + serialized = json.dumps(events) + # What: assert that private prompt is absent from serialized; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert "private prompt" not in serialized + # What: assert that do not log is absent from serialized; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert "do-not-log" not in serialized + # What: assert that private token in path is absent from serialized; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert "private-token-in-path" not in serialized + # What: assert that router test key is absent from serialized; why: this assertion protects the router event log is bounded private and protected regression after the test's arranged inputs and exercised call. + assert "router-test-key" not in serialized + + +# What: define the test_invalid_router_request_id_cannot_activate_an_engine test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the invalid router request id cannot activate an engine outcome. +def test_invalid_router_request_id_cannot_activate_an_engine(monkeypatch): + # What: act by calling Manager and capture manager; why: the invalid router request id cannot activate an engine test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the invalid router request id cannot activate an engine test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({"low": ModelProfile("low", "low.gguf", ())}) + # What: act by calling RoutingCoordinator and capture router; why: the invalid router request id cannot activate an engine test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + + # What: define the unexpected_upstream test helper around captured fixture state; why: the invalid router request id cannot activate an engine scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def unexpected_upstream(**kwargs): # pragma: no cover - establishes the no-activation contract + # What: raise AssertionError for the caller; why: unexpected_upstream stops this rejected path before it can mutate state, dispatch work, or report success. + raise AssertionError("invalid request ids must be rejected before proxy connection") + + # What: arrange the exact monkeypatch setattr freetoken daemon app open upstream unexpected upstream fixture frag; why: the invalid router request id cannot activate an engine scenario feeds this byte-preserved fragment through monkeypatch.setattr("freetoken.daemon.app.open_upstream", unexpected_ups before asserti. + monkeypatch.setattr("freetoken.daemon.app.open_upstream", unexpected_upstream) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_invalid_router_request_id_cannot_activate_an_engine releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the invalid router request id cannot activate an engine test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_invalid_router_request_id_cannot_activate_an_engine; why: test_invalid_router_request_id_cannot_activate_an_engine consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the invalid router request id cannot activate an engine scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_invalid_router_request_id_cannot_activate_an_engine groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the invalid router request id cannot activate an engine test asserts the response, state, or failure produced by this call. + response = TestClient(app).post( + # What: arrange the model field as low; why: test_invalid_router_request_id_cannot_activate_an_engine sends this field through response so the router selects the canonical model or alias for upstream dispatch. + "/v1/chat/completions", json={"model": "low"}, headers={"X-FT-Request-ID": "x" * 129}, + # What: arrange the operation.post call with json and headers; why: test_invalid_router_request_id_cannot_activate_an_engine groups the supplied clauses as one operation.post call before its value is consumed. + ) + # What: assert that response status code equals 400; why: this assertion protects the invalid router request id cannot activate an engine regression after the test's arranged inputs and exercised call. + assert response.status_code == 400 + # What: assert that manager calls equals group delimiter; why: this assertion protects the invalid router request id cannot activate an engine regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + + +# What: define the test_router_management_load_uses_native_admission_and_authentication test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router management load uses native admission and authentication outcome. +def test_router_management_load_uses_native_admission_and_authentication(): + # What: act by calling Manager and capture manager; why: the router management load uses native admission and authentication test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the router management load uses native admission and authentication test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf and 0; why: test_router_management_load_uses_native_admission_and_authentication carries low through catalog doc into manager catalog doc object ready fn ready port allocator lambda. + {"low": ModelProfile("low", "low.gguf", (), port=0)}, + # What: arrange settings to RouterSettings; why: the router management load uses native admission and authentication scenario binds this router settings and router test key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("router-test-key",)), + # What: arrange the ModelCatalog call with settings; why: test_router_management_load_uses_native_admission_and_authentication groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the router management load uses native admission and authentication test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator( + # What: arrange ready fn to object; why: the router management load uses native admission and authentication scenario binds this ready value to object's ready fn input. + manager, catalog_doc, object(), ready_fn=ready, port_allocator=lambda: 20777, + # What: arrange the RoutingCoordinator call with ready fn and port allocator; why: test_router_management_load_uses_native_admission_and_authentication groups the supplied clauses as one RoutingCoordinator call before its value is consumed. + ) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_management_load_uses_native_admission_and_authentication releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router management load uses native admission and authentication test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_management_load_uses_native_admission_and_authentication; why: test_router_management_load_uses_native_admission_and_authentication consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router management load uses native admission and authentication scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_management_load_uses_native_admission_and_authentication groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the router management load uses native admission and authentication test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: assert that client post router load json name low equals 401; why: this assertion protects the router management load uses native admission and authentication regression after the test's arranged inputs and exercised call. + assert client.post("/router/load", json={"name": "low"}).status_code == 401 + # What: act by calling client.post and capture loaded; why: the router management load uses native admission and authentication test asserts the response, state, or failure produced by this call. + loaded = client.post( + # What: arrange the name field as low; why: test_router_management_load_uses_native_admission_and_authentication carries name through loaded into assert loaded status code equals 200. + "/router/load", json={"name": "low"}, headers={"Authorization": "Bearer router-test-key"}, + # What: arrange the client.post call with json and headers; why: test_router_management_load_uses_native_admission_and_authentication groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: act by calling client.post and capture missing; why: the router management load uses native admission and authentication test asserts the response, state, or failure produced by this call. + missing = client.post( + # What: arrange the name field as missing; why: test_router_management_load_uses_native_admission_and_authentication carries name through missing into assert missing status code equals 404. + "/router/load", json={"name": "missing"}, headers={"Authorization": "Bearer router-test-key"}, + # What: arrange the client.post call with json and headers; why: test_router_management_load_uses_native_admission_and_authentication groups the supplied clauses as one client.post call before its value is consumed. + ) + # What: assert that loaded status code equals 200; why: this assertion protects the router management load uses native admission and authentication regression after the test's arranged inputs and exercised call. + assert loaded.status_code == 200 + # What: assert that loaded json profile equals low; why: this assertion protects the router management load uses native admission and authentication regression after the test's arranged inputs and exercised call. + assert loaded.json()["profile"] == "low" + # What: assert that loaded json port equals 20777; why: this assertion protects the router management load uses native admission and authentication regression after the test's arranged inputs and exercised call. + assert loaded.json()["port"] == 20777 + # What: assert that loaded json router active profile equals low; why: this assertion protects the router management load uses native admission and authentication regression after the test's arranged inputs and exercised call. + assert loaded.json()["router"]["activeProfile"] == "low" + # What: assert that loaded json router active requests equals 0; why: this assertion protects the router management load uses native admission and authentication regression after the test's arranged inputs and exercised call. + assert loaded.json()["router"]["activeRequests"] == 0 + # What: assert that missing status code equals 404; why: this assertion protects the router management load uses native admission and authentication regression after the test's arranged inputs and exercised call. + assert missing.status_code == 404 + # What: assert that missing json error type equals unknown model; why: this assertion protects the router management load uses native admission and authentication regression after the test's arranged inputs and exercised call. + assert missing.json()["error"]["type"] == "unknown_model" + # What: assert that manager calls equals start low gguf; why: this assertion protects the router management load uses native admission and authentication regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + + +# What: define the test_startup_profile_and_preload_use_native_routing_lifespan test around local fixtures; why: this test groups the arrange, act, and assertions that protect the startup profile and preload use native routing lifespan outcome. +def test_startup_profile_and_preload_use_native_routing_lifespan(): + # What: act by calling Manager and capture manager; why: the startup profile and preload use native routing lifespan test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the startup profile and preload use native routing lifespan test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf and compat low; why: test_startup_profile_and_preload_use_native_routing_lifespan carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "low.gguf", (), aliases=("compat-low",))}, + # What: arrange settings to RouterSettings; why: the startup profile and preload use native routing lifespan scenario binds this router settings and compat low and coding value to RouterSettings's settings input. + settings=RouterSettings( + # What: arrange preload model to RouterSettings; why: the startup profile and preload use native routing lifespan scenario binds this compat low value to RouterSettings's preload model input. + preload_model="compat-low", startup_routing_profile="coding", + # What: arrange the RouterSettings call with preload model and startup routing profile; why: test_startup_profile_and_preload_use_native_routing_lifespan groups the supplied clauses as one RouterSettings call before its value is consumed. + ), + # What: arrange routing profiles to ModelCatalog; why: the startup profile and preload use native routing lifespan scenario binds this routing profile and coding and coding and public and low value to ModelCatalog's routing profiles input. + routing_profiles={ + # What: arrange the coding field as routing profile and coding and public and low; why: test_startup_profile_and_preload_use_native_routing_lifespan carries coding through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + "coding": RoutingProfile("coding", (("public", "low"),)), + # What: arrange the catalog_doc mapping with coding; why: test_startup_profile_and_preload_use_native_routing_lifespan groups the supplied clauses as one catalog_doc mapping before its value is consumed. + }, + # What: arrange the ModelCatalog call with settings and routing profiles; why: test_startup_profile_and_preload_use_native_routing_lifespan groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the startup profile and preload use native routing lifespan test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_startup_profile_and_preload_use_native_routing_lifespan releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the startup profile and preload use native routing lifespan test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_startup_profile_and_preload_use_native_routing_lifespan; why: test_startup_profile_and_preload_use_native_routing_lifespan consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the startup profile and preload use native routing lifespan scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_startup_profile_and_preload_use_native_routing_lifespan groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: enter the TestClient managed context before status client get router status json; why: test_startup_profile_and_preload_use_native_routing_lifespan releases this resource or lock after status client get router status json on both success and failure paths. + with TestClient(app) as client: + # What: act by calling operation.json and capture status; why: the startup profile and preload use native routing lifespan test asserts the response, state, or failure produced by this call. + status = client.get("/router/status").json() + + # What: assert that manager calls equals start low gguf; why: this assertion protects the startup profile and preload use native routing lifespan regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + # What: assert that status active profile equals low; why: this assertion protects the startup profile and preload use native routing lifespan regression after the test's arranged inputs and exercised call. + assert status["activeProfile"] == "low" + # What: assert that status active routing profile equals coding; why: this assertion protects the startup profile and preload use native routing lifespan regression after the test's arranged inputs and exercised call. + assert status["activeRoutingProfile"] == "coding" + # What: assert that status active requests equals 0; why: this assertion protects the startup profile and preload use native routing lifespan regression after the test's arranged inputs and exercised call. + assert status["activeRequests"] == 0 + + +# What: define the test_router_management_load_preserves_failed_switch_recovery_evidence test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router management load preserves failed switch recovery evidence outcome. +def test_router_management_load_preserves_failed_switch_recovery_evidence(): + # What: act by calling Manager and capture manager; why: the router management load preserves failed switch recovery evidence test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling catalog and capture catalog doc; why: the router management load preserves failed switch recovery evidence test asserts the response, state, or failure produced by this call. + catalog_doc = catalog() + + # What: define the selective_ready test helper around manager and probe and pid and port and timeout s; why: the router management load preserves failed switch recovery evidence scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def selective_ready(manager, probe, *, pid, port, timeout_s): + # What: arrange the ready field as model and manager and low and gguf; why: selective_ready carries ready into return {"ready": manager.model == "low.gguf", "reason": "fixture-not-rea. + return {"ready": manager.model == "low.gguf", "reason": "fixture-not-ready"} + + # What: act by calling RoutingCoordinator and capture router; why: the router management load preserves failed switch recovery evidence test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=selective_ready) + # What: arrange the exact router acquire low release fixture fragment; why: the router management load preserves failed switch recovery evidence scenario feeds this byte-preserved fragment through router.acquire("low").release() before asserting its protocol or parser result. + router.acquire("low").release() + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_management_load_preserves_failed_switch_recovery_evidence releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router management load preserves failed switch recovery evidence test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_management_load_preserves_failed_switch_recovery_evidence; why: test_router_management_load_preserves_failed_switch_recovery_evidence consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router management load preserves failed switch recovery evidence scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_management_load_preserves_failed_switch_recovery_evidence groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.post and capture response; why: the router management load preserves failed switch recovery evidence test asserts the response, state, or failure produced by this call. + response = TestClient(app).post("/router/load", json={"name": "high"}) + + # What: assert that response status code equals 503; why: this assertion protects the router management load preserves failed switch recovery evidence regression after the test's arranged inputs and exercised call. + assert response.status_code == 503 + # What: assert that response json error type equals engine not ready; why: this assertion protects the router management load preserves failed switch recovery evidence regression after the test's arranged inputs and exercised call. + assert response.json()["error"]["type"] == "engine_not_ready" + # What: assert that response json recovery launched is true; why: this assertion protects the router management load preserves failed switch recovery evidence regression after the test's arranged inputs and exercised call. + assert response.json()["recovery"]["launched"] is True + # What: assert that manager model equals low gguf; why: this assertion protects the router management load preserves failed switch recovery evidence regression after the test's arranged inputs and exercised call. + assert manager.model == "low.gguf" + # What: assert that router status active profile equals low; why: this assertion protects the router management load preserves failed switch recovery evidence regression after the test's arranged inputs and exercised call. + assert router.status()["activeProfile"] == "low" + # What: assert that router status active identity matches engine is true; why: this assertion protects the router management load preserves failed switch recovery evidence regression after the test's arranged inputs and exercised call. + assert router.status()["activeIdentityMatchesEngine"] is True + + +# What: define the test_router_management_unloads_one_or_all_under_single_resident_policy test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router management unloads one or all under single resident policy outcome. +def test_router_management_unloads_one_or_all_under_single_resident_policy(): + # What: act by calling Manager and capture manager; why: the router management unloads one or all under single resident policy test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling catalog and capture catalog doc; why: the router management unloads one or all under single resident policy test asserts the response, state, or failure produced by this call. + catalog_doc = catalog() + # What: act by calling RoutingCoordinator and capture router; why: the router management unloads one or all under single resident policy test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_management_unloads_one_or_all_under_single_resident_policy releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router management unloads one or all under single resident policy test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_management_unloads_one_or_all_under_single_resident_policy; why: test_router_management_unloads_one_or_all_under_single_resident_policy consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router management unloads one or all under single resident policy scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_management_unloads_one_or_all_under_single_resident_policy groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the router management unloads one or all under single resident policy test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: assert that client post router load json name low equals 200; why: this assertion protects the router management unloads one or all under single resident policy regression after the test's arranged inputs and exercised call. + assert client.post("/router/load", json={"name": "low"}).status_code == 200 + # What: act by calling client.post and capture wrong; why: the router management unloads one or all under single resident policy test asserts the response, state, or failure produced by this call. + wrong = client.post("/router/unload", json={"name": "high"}) + # What: assert that wrong json unloaded is false; why: this assertion protects the router management unloads one or all under single resident policy regression after the test's arranged inputs and exercised call. + assert wrong.json()["unloaded"] is False + # What: assert that wrong json router active profile equals low; why: this assertion protects the router management unloads one or all under single resident policy regression after the test's arranged inputs and exercised call. + assert wrong.json()["router"]["activeProfile"] == "low" + # What: act by calling client.post and capture one; why: the router management unloads one or all under single resident policy test asserts the response, state, or failure produced by this call. + one = client.post("/router/unload", json={"name": "low"}) + # What: assert that one json unloaded is true; why: this assertion protects the router management unloads one or all under single resident policy regression after the test's arranged inputs and exercised call. + assert one.json()["unloaded"] is True + # What: assert that one json router active profile is group delimiter; why: this assertion protects the router management unloads one or all under single resident policy regression after the test's arranged inputs and exercised call. + assert one.json()["router"]["activeProfile"] is None + + # What: assert that client post router load json name high equals 200; why: this assertion protects the router management unloads one or all under single resident policy regression after the test's arranged inputs and exercised call. + assert client.post("/router/load", json={"name": "high"}).status_code == 200 + # What: act by calling client.post and capture all residents; why: the router management unloads one or all under single resident policy test asserts the response, state, or failure produced by this call. + all_residents = client.post("/router/unload") + # What: assert that all residents json unloaded is true; why: this assertion protects the router management unloads one or all under single resident policy regression after the test's arranged inputs and exercised call. + assert all_residents.json()["unloaded"] is True + # What: assert that all residents json router resident profiles equals group delimiter; why: this assertion protects the router management unloads one or all under single resident policy regression after the test's arranged inputs and exercised call. + assert all_residents.json()["router"]["residentProfiles"] == [] + + # What: assert the expected manager calls == outcome; why: test router test router management unloads one or all under single resident policy protects its regression by requiring this observable result after the exercised behavior. + assert manager.calls == [ + # What: arrange start low gguf stop 30.0 for the scenario; why: test router test router management unloads one or all under single resident policy requires this concrete input or helper state before exercising the behavior under test. + ("start", "low.gguf"), ("stop", 30.0), + # What: arrange start high gguf stop 30.0 for the scenario; why: test router test router management unloads one or all under single resident policy requires this concrete input or helper state before exercising the behavior under test. + ("start", "high.gguf"), ("stop", 30.0), + # What: arrange the grouped source fragment for the scenario; why: test router test router management unloads one or all under single resident policy requires this concrete input or helper state before exercising the behavior under test. + ] + + +# What: define the test_router_model_list_hides_model_paths_and_ready_never_cold_loads test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router model list hides model paths and ready never cold loads outcome. +def test_router_model_list_hides_model_paths_and_ready_never_cold_loads(): + # What: act by calling Manager and capture manager; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and private and models and low; why: test_router_model_list_hides_model_paths_and_ready_never_cold_loads carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile( + # What: arrange description to ModelProfile; why: the router model list hides model paths and ready never cold loads scenario binds this public and description value to ModelProfile's description input. + "low", "/private/models/low.gguf", (), description="Public description" + # What: arrange the catalog_doc mapping with low; why: test_router_model_list_hides_model_paths_and_ready_never_cold_loads groups the supplied clauses as one catalog_doc mapping before its value is consumed. + )}, + # What: arrange settings to RouterSettings; why: the router model list hides model paths and ready never cold loads scenario binds this router settings and router test key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("router-test-key",)), + # What: arrange the ModelCatalog call with settings; why: test_router_model_list_hides_model_paths_and_ready_never_cold_loads groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + + # What: define Probe as the owner of fresh_health; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: arrange the def fresh health self port test helper boundary; why: test router test router model list hides model paths and ready never cold loads uses this local double to isolate the behavior checked by its assertions. + def fresh_health(self, port): + # What: arrange the helper response as reachable True status ok maintenance serving; why: test router test router model list hides model paths and ready never cold loads feeds this result into the behavior whose outcome is asserted. + return {"reachable": True, "status": "ok", "maintenance": "serving"} + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_model_list_hides_model_paths_and_ready_never_cold_loads releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_router_model_list_hides_model_paths_and_ready_never_cold_loads; why: test_router_model_list_hides_model_paths_and_ready_never_cold_loads consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=Probe(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the router model list hides model paths and ready never cold loads scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_model_list_hides_model_paths_and_ready_never_cold_loads groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: assert that client get ready status code equals 503; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert client.get("/ready").status_code == 503 + # What: assert that manager calls equals group delimiter; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + # What: assert that client get v1 models status code equals 401; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert client.get("/v1/models").status_code == 401 + # What: act by calling client.get and capture listed; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + listed = client.get("/v1/models", headers={"Authorization": "Bearer router-test-key"}) + # What: arrange the exact router acquire low release fixture fragment; why: the router model list hides model paths and ready never cold loads scenario feeds this byte-preserved fragment through router.acquire("low").release() before asserting its protocol or parser result. + router.acquire("low").release() + # What: assert that client get ready status code equals 200; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert client.get("/ready").status_code == 200 + # What: arrange model as unexpected and gguf; why: the router model list hides model paths and ready never cold loads test consumes this named precondition before exercising the behavior. + manager.model = "unexpected.gguf" + # What: assert that client get ready status code equals 503; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert client.get("/ready").status_code == 503 + # What: act by calling client.get and capture stale listing; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + stale_listing = client.get( + # What: arrange the authorization field as bearer and router test key; why: test_router_model_list_hides_model_paths_and_ready_never_cold_loads carries authorization through stale listing into assert stale listing json data 0 status equals value. + "/v1/models", headers={"Authorization": "Bearer router-test-key"} + # What: arrange the client.get call with headers; why: test_router_model_list_hides_model_paths_and_ready_never_cold_loads groups the supplied clauses as one client.get call before its value is consumed. + ) + # What: assert that stale listing json data 0 status equals value unloaded; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert stale_listing.json()["data"][0]["status"] == {"value": "unloaded"} + # What: act by calling client.get and capture stale status; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + stale_status = client.get("/router/status", headers={"Authorization": "Bearer router-test-key"}) + # What: assert that stale status json resident profiles equals group delimiter; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert stale_status.json()["residentProfiles"] == [] + # What: assert that stale status json active identity matches engine is false; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert stale_status.json()["activeIdentityMatchesEngine"] is False + # What: act by calling client.get and capture stale metrics; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + stale_metrics = client.get("/metrics", headers={"Authorization": "Bearer router-test-key"}) + # What: assert that freetoken swap active identity matches engine 0 is present in stale metrics text; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert "freetoken_swap_active_identity_matches_engine 0" in stale_metrics.text + # What: act by calling client.get and capture stale models; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + stale_models = client.get("/router/models", headers={"Authorization": "Bearer router-test-key"}) + # What: assert that stale models json data 0 resident is false; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert stale_models.json()["data"][0]["resident"] is False + # What: assert that stale models json capacity equals max resident models 1 available resident slots 0; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert stale_models.json()["capacity"] == {"maxResidentModels": 1, "availableResidentSlots": 0} + # What: arrange model as private and models and low and gguf; why: the router model list hides model paths and ready never cold loads test consumes this named precondition before exercising the behavior. + manager.model = "/private/models/low.gguf" + # What: arrange args as unexpected; why: the router model list hides model paths and ready never cold loads test consumes this named precondition before exercising the behavior. + manager.args = ["--unexpected"] + # What: assert that client get ready status code equals 503; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert client.get("/ready").status_code == 503 + # What: arrange args as the fixture input; why: the router model list hides model paths and ready never cold loads test consumes this named precondition before exercising the behavior. + manager.args = [] + # What: arrange port as 1999; why: the router model list hides model paths and ready never cold loads test consumes this named precondition before exercising the behavior. + manager.port = 1999 + # What: assert that client get ready status code equals 503; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert client.get("/ready").status_code == 503 + # What: assert that listed status code equals 200; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert listed.status_code == 200 + # What: act by calling listed.json and capture listed doc; why: the router model list hides model paths and ready never cold loads test asserts the response, state, or failure produced by this call. + listed_doc = listed.json() + # What: assert that listed doc object equals list; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert listed_doc["object"] == "list" + # What: assert that len listed doc data equals 1; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert len(listed_doc["data"]) == 1 + # What: arrange public model as listed doc and 0 and data; why: the router model list hides model paths and ready never cold loads test consumes this named precondition before exercising the behavior. + public_model = listed_doc["data"][0] + # What: assert that public model id equals low; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert public_model["id"] == "low" + # What: assert that public model object equals model; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert public_model["object"] == "model" + # What: assert that public model owned by equals freetoken; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert public_model["owned_by"] == "freetoken" + # What: assert that public model description equals public description; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert public_model["description"] == "Public description" + # What: assert that public model status equals value unloaded; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert public_model["status"] == {"value": "unloaded"} + # What: assert that isinstance public model created int and public model created 0; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert isinstance(public_model["created"], int) and public_model["created"] > 0 + # What: assert that private models is absent from json dumps listed doc; why: this assertion protects the router model list hides model paths and ready never cold loads regression after the test's arranged inputs and exercised call. + assert "/private/models" not in json.dumps(listed_doc) + + +# What: define the test_ready_probe_linearizes_before_a_conflicting_swap test around local fixtures; why: this test groups the arrange, act, and assertions that protect the ready probe linearizes before a conflicting swap outcome. +def test_ready_probe_linearizes_before_a_conflicting_swap(): + # What: act by calling Manager and capture manager; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling threading.Event and capture probe started; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + probe_started = threading.Event() + # What: act by calling threading.Event and capture finish probe; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + finish_probe = threading.Event() + + # What: define Probe as the owner of fresh_health; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: define an uncached health probe for the active engine port; why: readiness checks must bypass a replaced generation's cached response before accepting the new process. + def fresh_health(self, port): + # What: act by calling probe_started.set with the declared inputs; why: the ready probe linearizes before a conflicting swap scenario observes the probe_started.set return value during assert finish probe wait. + probe_started.set() + # What: assert that finish probe wait 2; why: this assertion protects the ready probe linearizes before a conflicting swap regression after the test's arranged inputs and exercised call. + assert finish_probe.wait(2) + # What: arrange the helper response as reachable True status ok maintenance serving; why: test router test ready probe linearizes before a conflicting swap feeds this result into the behavior whose outcome is asserted. + return {"reachable": True, "status": "ok", "maintenance": "serving"} + + # What: act by calling Probe and capture probe; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + probe = Probe() + # What: act by calling RoutingCoordinator and capture router; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), probe, ready_fn=ready) + # What: arrange the exact router acquire low release fixture fragment; why: the ready probe linearizes before a conflicting swap scenario feeds this byte-preserved fragment through router.acquire("low").release() before asserting its protocol or parser result. + router.acquire("low").release() + # What: arrange ready response as the fixture input; why: the ready probe linearizes before a conflicting swap test consumes this named precondition before exercising the behavior. + ready_response = [] + # What: arrange switched as the fixture input; why: the ready probe linearizes before a conflicting swap test consumes this named precondition before exercising the behavior. + switched = [] + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_ready_probe_linearizes_before_a_conflicting_swap releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_ready_probe_linearizes_before_a_conflicting_swap; why: test_ready_probe_linearizes_before_a_conflicting_swap consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=probe, footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to catalog; why: the ready probe linearizes before a conflicting swap scenario binds this lifecycle value to catalog's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog(), router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_ready_probe_linearizes_before_a_conflicting_swap groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling threading.Thread and capture ready thread; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + ready_thread = threading.Thread( + # What: arrange target to ready_response.append; why: the ready probe linearizes before a conflicting swap scenario binds this append and ready response and get and client and ready value to ready_response.append's target input. + target=lambda: ready_response.append(client.get("/ready")) + # What: arrange the threading.Thread call with target; why: test_ready_probe_linearizes_before_a_conflicting_swap groups the supplied clauses as one threading.Thread call before its value is consumed. + ) + # What: act by calling ready_thread.start with the declared inputs; why: the ready probe linearizes before a conflicting swap scenario observes the ready_thread.start return value during assert probe started wait. + ready_thread.start() + # What: assert that probe started wait 1; why: this assertion protects the ready probe linearizes before a conflicting swap regression after the test's arranged inputs and exercised call. + assert probe_started.wait(1) + + # What: define the switch test helper around captured fixture state; why: the ready probe linearizes before a conflicting swap scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def switch(): + # What: act by calling router.acquire and capture lease; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + lease = router.acquire("high") + # What: act by calling switched.append with name and profile and lease; why: the ready probe linearizes before a conflicting swap scenario observes the switched.append return value during lease release. + switched.append(lease.profile.name) + # What: act by calling lease.release with the declared inputs; why: the ready probe linearizes before a conflicting swap scenario observes the lease.release return value during the enclosing return. + lease.release() + + # What: act by calling threading.Thread and capture switch thread; why: the ready probe linearizes before a conflicting swap test asserts the response, state, or failure produced by this call. + switch_thread = threading.Thread(target=switch) + # What: act by calling switch_thread.start with the declared inputs; why: the ready probe linearizes before a conflicting swap scenario observes the switch_thread.start return value during switch thread join. + switch_thread.start() + # What: act by calling switch_thread.join with 0 05; why: the ready probe linearizes before a conflicting swap scenario observes the switch_thread.join return value during assert switch thread is alive. + switch_thread.join(0.05) + # What: assert that switch thread is alive; why: this assertion protects the ready probe linearizes before a conflicting swap regression after the test's arranged inputs and exercised call. + assert switch_thread.is_alive() + # What: assert that manager calls equals start low gguf; why: this assertion protects the ready probe linearizes before a conflicting swap regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + # What: act by calling finish_probe.set with the declared inputs; why: the ready probe linearizes before a conflicting swap scenario observes the finish_probe.set return value during ready thread join. + finish_probe.set() + # What: act by calling ready_thread.join with 2; why: the ready probe linearizes before a conflicting swap scenario observes the ready_thread.join return value during switch thread join. + ready_thread.join(2) + # What: act by calling switch_thread.join with 2; why: the ready probe linearizes before a conflicting swap scenario observes the switch_thread.join return value during assert not ready thread is alive and not switch thread is alive. + switch_thread.join(2) + # What: assert that not ready thread is alive and not switch thread is alive; why: this assertion protects the ready probe linearizes before a conflicting swap regression after the test's arranged inputs and exercised call. + assert not ready_thread.is_alive() and not switch_thread.is_alive() + + # What: assert that ready response 0 status code equals 200; why: this assertion protects the ready probe linearizes before a conflicting swap regression after the test's arranged inputs and exercised call. + assert ready_response[0].status_code == 200 + # What: assert that switched equals high; why: this assertion protects the ready probe linearizes before a conflicting swap regression after the test's arranged inputs and exercised call. + assert switched == ["high"] + # What: assert that manager calls equals start low gguf switch high gguf; why: this assertion protects the ready probe linearizes before a conflicting swap regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf"), ("switch", "high.gguf")] + + +# What: define the test_manual_engine_start_holds_router_lifecycle_barrier test around local fixtures; why: this test groups the arrange, act, and assertions that protect the manual engine start holds router lifecycle barrier outcome. +def test_manual_engine_start_holds_router_lifecycle_barrier(): + # What: act by calling threading.Event and capture entered; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + entered = threading.Event() + # What: act by calling threading.Event and capture finish manual; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + finish_manual = threading.Event() + + # What: define BlockingManager as the owner of start; why: daemon callers use this class boundary so those methods share one blocking manager state invariant. + class BlockingManager(Manager): + # What: define the start test helper around model and port and args; why: the manual engine start holds router lifecycle barrier scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def start(self, model, port, args): + # What: arrange the exact self calls append manual start model fixture fragment; why: the manual engine start holds router lifecycle barrier scenario feeds this byte-preserved fragment through self.calls.append(("manual-start", model)) before asserting its protocol or parser result. + self.calls.append(("manual-start", model)) + # What: act by calling entered.set with the declared inputs; why: the manual engine start holds router lifecycle barrier scenario observes the entered.set return value during assert finish manual wait. + entered.set() + # What: assert that finish manual wait 2; why: this assertion protects the manual engine start holds router lifecycle barrier regression after the test's arranged inputs and exercised call. + assert finish_manual.wait(2) + # What: act by calling list and capture model and port and args; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + self.model, self.port, self.args = model, port, list(args) + # What: arrange pid from 1; why: the manual engine start holds router lifecycle barrier scenario uses pid during return pid self pid before checking the protected result. + self.pid += 1 + # What: arrange the pid field as pid; why: BlockingManager.start carries pid into return {"pid": self.pid}. + return {"pid": self.pid} + + # What: act by calling BlockingManager and capture manager; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + manager = BlockingManager() + # What: act by calling catalog and capture catalog doc; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + catalog_doc = catalog() + # What: act by calling RoutingCoordinator and capture router; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange manual response as the fixture input; why: the manual engine start holds router lifecycle barrier test consumes this named precondition before exercising the behavior. + manual_response = [] + # What: arrange routed lease as the fixture input; why: the manual engine start holds router lifecycle barrier test consumes this named precondition before exercising the behavior. + routed_lease = [] + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_manual_engine_start_holds_router_lifecycle_barrier releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_manual_engine_start_holds_router_lifecycle_barrier; why: test_manual_engine_start_holds_router_lifecycle_barrier consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the manual engine start holds router lifecycle barrier scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_manual_engine_start_holds_router_lifecycle_barrier groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling threading.Thread and capture manual thread; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + manual_thread = threading.Thread(target=lambda: manual_response.append(client.post( + # What: arrange the model field as manual and gguf; why: test_manual_engine_start_holds_router_lifecycle_barrier sends this field through manual thread so the router selects the canonical model or alias for upstream dispatch. + "/engine/start", json={"model": "manual.gguf", "port": 1930} + # What: arrange the threading.Thread call with target; why: test_manual_engine_start_holds_router_lifecycle_barrier groups the supplied clauses as one threading.Thread call before its value is consumed. + ))) + # What: act by calling manual_thread.start with the declared inputs; why: the manual engine start holds router lifecycle barrier scenario observes the manual_thread.start return value during assert entered wait. + manual_thread.start() + # What: assert that entered wait 1; why: this assertion protects the manual engine start holds router lifecycle barrier regression after the test's arranged inputs and exercised call. + assert entered.wait(1) + + # What: define the acquire_routed test helper around captured fixture state; why: the manual engine start holds router lifecycle barrier scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def acquire_routed(): + # What: act by calling router.acquire and capture lease; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + lease = router.acquire("low") + # What: act by calling routed_lease.append with lease; why: the manual engine start holds router lifecycle barrier scenario observes the routed_lease.append return value during the enclosing return. + routed_lease.append(lease) + + # What: act by calling threading.Thread and capture routed thread; why: the manual engine start holds router lifecycle barrier test asserts the response, state, or failure produced by this call. + routed_thread = threading.Thread(target=acquire_routed) + # What: act by calling routed_thread.start with the declared inputs; why: the manual engine start holds router lifecycle barrier scenario observes the routed_thread.start return value during for value in range. + routed_thread.start() + # What: act across range to perform status and router; why: the manual engine start holds router lifecycle barrier scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the manual engine start holds router lifecycle barrier scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the manual engine start holds router lifecycle barrier scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling operation.wait with 0 01; why: the manual engine start holds router lifecycle barrier scenario observes the operation.wait return value during assert router status queued requests. + threading.Event().wait(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the manual engine start holds router lifecycle barrier regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: assert that manager calls equals manual start manual gguf; why: this assertion protects the manual engine start holds router lifecycle barrier regression after the test's arranged inputs and exercised call. + assert manager.calls == [("manual-start", "manual.gguf")] + # What: act by calling finish_manual.set with the declared inputs; why: the manual engine start holds router lifecycle barrier scenario observes the finish_manual.set return value during manual thread join. + finish_manual.set() + # What: act by calling manual_thread.join with 2; why: the manual engine start holds router lifecycle barrier scenario observes the manual_thread.join return value during routed thread join. + manual_thread.join(2) + # What: act by calling routed_thread.join with 2; why: the manual engine start holds router lifecycle barrier scenario observes the routed_thread.join return value during assert not manual thread is alive and not routed thread is alive. + routed_thread.join(2) + # What: assert that not manual thread is alive and not routed thread is alive; why: this assertion protects the manual engine start holds router lifecycle barrier regression after the test's arranged inputs and exercised call. + assert not manual_thread.is_alive() and not routed_thread.is_alive() + + # What: assert that manual response 0 status code equals 200; why: this assertion protects the manual engine start holds router lifecycle barrier regression after the test's arranged inputs and exercised call. + assert manual_response[0].status_code == 200 + # What: assert that manager calls equals manual start manual gguf switch low gguf; why: this assertion protects the manual engine start holds router lifecycle barrier regression after the test's arranged inputs and exercised call. + assert manager.calls == [("manual-start", "manual.gguf"), ("switch", "low.gguf")] + # What: act by calling operation.release with the declared inputs; why: the manual engine start holds router lifecycle barrier scenario observes the operation.release return value during assert router status active requests. + routed_lease.pop().release() + # What: assert that router status active requests equals 0; why: this assertion protects the manual engine start holds router lifecycle barrier regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + + +# What: define the test_manual_lifecycle_claim_rejects_router_ownership_and_requires_matching_token test around local fixtures; why: this test groups the arrange, act, and assertions that protect the manual lifecycle claim rejects router ownership and requires matching token outcome. +def test_manual_lifecycle_claim_rejects_router_ownership_and_requires_matching_token(): + # What: act by calling RoutingCoordinator and capture router; why: the manual lifecycle claim rejects router ownership and requires matching token test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(Manager(), catalog(), object(), ready_fn=ready) + # What: act by calling router.acquire and capture lease; why: the manual lifecycle claim rejects router ownership and requires matching token test asserts the response, state, or failure produced by this call. + lease = router.acquire("low") + # What: arrange with pytest raises RoutingError as conflict for the scenario; why: test raises routing error as conflict requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(RoutingError) as conflict: + # What: act by calling router.begin_manual_lifecycle with the declared inputs; why: the manual lifecycle claim rejects router ownership and requires matching token scenario observes the router.begin_manual_lifecycle return value during assert conflict value code router owned. + router.begin_manual_lifecycle() + # What: assert that conflict value code equals router owned; why: this assertion protects the manual lifecycle claim rejects router ownership and requires matching token regression after the test's arranged inputs and exercised call. + assert conflict.value.code == "router_owned" + # What: assert that conflict value status code equals 409; why: this assertion protects the manual lifecycle claim rejects router ownership and requires matching token regression after the test's arranged inputs and exercised call. + assert conflict.value.status_code == 409 + # What: act by calling lease.release with the declared inputs; why: the manual lifecycle claim rejects router ownership and requires matching token scenario observes the lease.release return value during with pytest raises routing error match router owns. + lease.release() + # What: arrange with pytest raises RoutingError match router owns for the scenario; why: test raises routing error match router owns requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(RoutingError, match="router owns"): + # What: act by calling router.begin_manual_lifecycle with the declared inputs; why: the manual lifecycle claim rejects router ownership and requires matching token scenario observes the router.begin_manual_lifecycle return value during router routing coordinator manager catalog object ready fn. + router.begin_manual_lifecycle() + + # What: act by calling RoutingCoordinator and capture router; why: the manual lifecycle claim rejects router ownership and requires matching token test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(Manager(), catalog(), object(), ready_fn=ready) + # What: act by calling router.begin_manual_lifecycle and capture owner; why: the manual lifecycle claim rejects router ownership and requires matching token test asserts the response, state, or failure produced by this call. + owner = router.begin_manual_lifecycle() + # What: arrange with pytest raises ValueError match not owned for the scenario; why: test raises value error match not owned requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(ValueError, match="not owned"): + # What: act by calling router.end_manual_lifecycle with object; why: the manual lifecycle claim rejects router ownership and requires matching token scenario observes the router.end_manual_lifecycle return value during assert router status switching is. + router.end_manual_lifecycle(object()) + # What: assert that router status switching is true; why: this assertion protects the manual lifecycle claim rejects router ownership and requires matching token regression after the test's arranged inputs and exercised call. + assert router.status()["switching"] is True + # What: act by calling router.begin_manual_lifecycle and capture newer owner; why: the manual lifecycle claim rejects router ownership and requires matching token test asserts the response, state, or failure produced by this call. + newer_owner = router.begin_manual_lifecycle(preempt_manual=True) + # What: act by calling router.end_manual_lifecycle with owner; why: the manual lifecycle claim rejects router ownership and requires matching token scenario observes the router.end_manual_lifecycle return value during assert router status switching is. + router.end_manual_lifecycle(owner) + # What: assert that router status switching is true; why: this assertion protects the manual lifecycle claim rejects router ownership and requires matching token regression after the test's arranged inputs and exercised call. + assert router.status()["switching"] is True + # What: act by calling router.end_manual_lifecycle with newer owner; why: the manual lifecycle claim rejects router ownership and requires matching token scenario observes the router.end_manual_lifecycle return value during assert router status switching is. + router.end_manual_lifecycle(newer_owner) + # What: assert that router status switching is false; why: this assertion protects the manual lifecycle claim rejects router ownership and requires matching token regression after the test's arranged inputs and exercised call. + assert router.status()["switching"] is False + + +# What: define the test_cancelled_manual_start_keeps_barrier_until_executor_finishes test around local fixtures; why: this test groups the arrange, act, and assertions that protect the cancelled manual start keeps barrier until executor finishes outcome. +def test_cancelled_manual_start_keeps_barrier_until_executor_finishes(): + # What: act by calling threading.Event and capture entered; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + entered = threading.Event() + # What: act by calling threading.Event and capture finish manual; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + finish_manual = threading.Event() + + # What: define BlockingManager as the owner of start; why: daemon callers use this class boundary so those methods share one blocking manager state invariant. + class BlockingManager(Manager): + # What: define the start test helper around model and port and args; why: the cancelled manual start keeps barrier until executor finishes scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def start(self, model, port, args): + # What: arrange the exact self calls append manual start model fixture fragment; why: the cancelled manual start keeps barrier until executor finishes scenario feeds this byte-preserved fragment through self.calls.append(("manual-start", model)) before asserting its protocol or parser result. + self.calls.append(("manual-start", model)) + # What: act by calling entered.set with the declared inputs; why: the cancelled manual start keeps barrier until executor finishes scenario observes the entered.set return value during assert finish manual wait. + entered.set() + # What: assert that finish manual wait 2; why: this assertion protects the cancelled manual start keeps barrier until executor finishes regression after the test's arranged inputs and exercised call. + assert finish_manual.wait(2) + # What: act by calling list and capture model and port and args; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + self.model, self.port, self.args = model, port, list(args) + # What: arrange pid from 1; why: the cancelled manual start keeps barrier until executor finishes scenario uses pid during return pid self pid before checking the protected result. + self.pid += 1 + # What: arrange the pid field as pid; why: BlockingManager.start carries pid into return {"pid": self.pid}. + return {"pid": self.pid} + + # What: act by calling BlockingManager and capture manager; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + manager = BlockingManager() + # What: act by calling catalog and capture catalog doc; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + catalog_doc = catalog() + # What: act by calling RoutingCoordinator and capture router; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange routed lease as the fixture input; why: the cancelled manual start keeps barrier until executor finishes test consumes this named precondition before exercising the behavior. + routed_lease = [] + + # What: define the scenario test helper around app; why: the cancelled manual start keeps barrier until executor finishes scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def scenario(app): + # What: act by calling httpx.ASGITransport and capture transport; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + transport = httpx.ASGITransport(app=app) + # What: arrange async with httpx AsyncClient transport transport base url http test as client for the scenario; why: test cancelled manual start keeps barrier until requires this concrete input or helper state before exercising the behavior under test. + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: + # What: act by calling asyncio.create_task and capture manual; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + manual = asyncio.create_task(client.post( + # What: arrange the model field as manual and gguf; why: scenario sends this field through manual so the router selects the canonical model or alias for upstream dispatch. + "/engine/start", json={"model": "manual.gguf", "port": 1930} + # What: arrange the grouped source fragment for the scenario; why: test router test cancelled manual start keeps barrier until executor finishes requires this concrete input or helper state before exercising the behavior under test. + )) + # What: act across range to perform is set and entered; why: the cancelled manual start keeps barrier until executor finishes scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on is set and entered before the computed value; why: the cancelled manual start keeps barrier until executor finishes scenario admits the computed value only for this predicate and excludes the opposite state. + if entered.is_set(): + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the cancelled manual start keeps barrier until executor finishes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the cancelled manual start keeps barrier until executor finishes scenario observes the asyncio.sleep return value during assert entered is set. + await asyncio.sleep(0.01) + # What: assert that entered is set; why: this assertion protects the cancelled manual start keeps barrier until executor finishes regression after the test's arranged inputs and exercised call. + assert entered.is_set() + # What: act by calling manual.cancel with the declared inputs; why: the cancelled manual start keeps barrier until executor finishes scenario observes the manual.cancel return value during def acquire routed. + manual.cancel() + + # What: define the acquire_routed test helper around captured fixture state; why: the cancelled manual start keeps barrier until executor finishes scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def acquire_routed(): + # What: arrange the exact routed lease append router acquire low fixture fragment; why: the cancelled manual start keeps barrier until executor finishes scenario feeds this byte-preserved fragment through routed_lease.append(router.acquire("low")) before asserting its protocol or parser result. + routed_lease.append(router.acquire("low")) + + # What: act by calling threading.Thread and capture routed thread; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + routed_thread = threading.Thread(target=acquire_routed) + # What: act by calling routed_thread.start with the declared inputs; why: the cancelled manual start keeps barrier until executor finishes scenario observes the routed_thread.start return value during for value in range. + routed_thread.start() + # What: act across range to perform status and router; why: the cancelled manual start keeps barrier until executor finishes scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the cancelled manual start keeps barrier until executor finishes scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the cancelled manual start keeps barrier until executor finishes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the cancelled manual start keeps barrier until executor finishes scenario observes the asyncio.sleep return value during assert router status queued requests. + await asyncio.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the cancelled manual start keeps barrier until executor finishes regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + # What: assert that manual done is false; why: this assertion protects the cancelled manual start keeps barrier until executor finishes regression after the test's arranged inputs and exercised call. + assert not manual.done() + # What: assert that manager calls equals manual start manual gguf; why: this assertion protects the cancelled manual start keeps barrier until executor finishes regression after the test's arranged inputs and exercised call. + assert manager.calls == [("manual-start", "manual.gguf")] + # What: act by calling manual.cancel with the declared inputs; why: the cancelled manual start keeps barrier until executor finishes scenario observes the manual.cancel return value during await asyncio sleep. + manual.cancel() + # What: act by calling asyncio.sleep with 0 05; why: the cancelled manual start keeps barrier until executor finishes scenario observes the asyncio.sleep return value during assert not manual done. + await asyncio.sleep(0.05) + # What: assert that manual done is false; why: this assertion protects the cancelled manual start keeps barrier until executor finishes regression after the test's arranged inputs and exercised call. + assert not manual.done() + # What: act by calling finish_manual.set with the declared inputs; why: the cancelled manual start keeps barrier until executor finishes scenario observes the finish_manual.set return value during with pytest raises asyncio cancelled error. + finish_manual.set() + # What: assert the pytest.raises failure context; why: the cancelled manual start keeps barrier until executor finishes scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(asyncio.CancelledError): + # What: arrange the await manual portion of the enclosing predicate; why: this clause remains in the cancelled manual start keeps barrier until executor finishes scenario\'s enclosing expression so its grouping and evaluation order stay intact. + await manual + # What: act by calling routed_thread.join with 2; why: the cancelled manual start keeps barrier until executor finishes scenario observes the routed_thread.join return value during assert not routed thread is alive. + routed_thread.join(2) + # What: assert that routed thread is alive is false; why: this assertion protects the cancelled manual start keeps barrier until executor finishes regression after the test's arranged inputs and exercised call. + assert not routed_thread.is_alive() + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_cancelled_manual_start_keeps_barrier_until_executor_finishes releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the cancelled manual start keeps barrier until executor finishes test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_cancelled_manual_start_keeps_barrier_until_executor_finishes; why: test_cancelled_manual_start_keeps_barrier_until_executor_finishes consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the cancelled manual start keeps barrier until executor finishes scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_cancelled_manual_start_keeps_barrier_until_executor_finishes groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling asyncio.run with scenario and app; why: the cancelled manual start keeps barrier until executor finishes scenario observes the asyncio.run return value during assert manager calls manual start manual gguf switch low gguf. + asyncio.run(scenario(app)) + + # What: assert that manager calls equals manual start manual gguf switch low gguf; why: this assertion protects the cancelled manual start keeps barrier until executor finishes regression after the test's arranged inputs and exercised call. + assert manager.calls == [("manual-start", "manual.gguf"), ("switch", "low.gguf")] + # What: act by calling operation.release with the declared inputs; why: the cancelled manual start keeps barrier until executor finishes scenario observes the operation.release return value during assert router status active requests. + routed_lease.pop().release() + # What: assert that router status active requests equals 0; why: this assertion protects the cancelled manual start keeps barrier until executor finishes regression after the test's arranged inputs and exercised call. + assert router.status()["activeRequests"] == 0 + + +# What: define the test_cancelled_manual_profile_switch_completes_failed_readiness_rollback test around local fixtures; why: this test groups the arrange, act, and assertions that protect the cancelled manual profile switch completes failed readiness rollback outcome. +def test_cancelled_manual_profile_switch_completes_failed_readiness_rollback(): + # What: act by calling threading.Event and capture readiness entered; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + readiness_entered = threading.Event() + # What: act by calling threading.Event and capture finish readiness; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + finish_readiness = threading.Event() + + # What: define RecoveringManager as the owner of switch_for_readiness and recover_switch; why: daemon callers use this class boundary so those methods share one recovering manager state invariant. + class RecoveringManager(Manager): + # What: define the switch_for_readiness test helper around model and port and args and force; why: the cancelled manual profile switch completes failed readiness rollback scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def switch_for_readiness(self, model, port, args, force=False): + # What: arrange the exact self calls append switch model fixture fragment; why: the cancelled manual profile switch completes failed readiness rollback scenario feeds this byte-preserved fragment through self.calls.append(("switch", model)) before asserting its protocol or parser result. + self.calls.append(("switch", model)) + # What: act by calling list and capture previous; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + previous = self.model, self.port, list(self.args) + # What: act by calling list and capture model and port and args; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + self.model, self.port, self.args = model, port, list(args) + # What: arrange pid from 1; why: the cancelled manual profile switch completes failed readiness rollback scenario uses pid during return pid self pid previous before checking the protected result. + self.pid += 1 + # What: arrange the pid field as pid; why: RecoveringManager.switch_for_readiness carries pid into return {"pid": self.pid}, previous. + return {"pid": self.pid}, previous + + # What: define the recover_switch test helper around ticket and force; why: the cancelled manual profile switch completes failed readiness rollback scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def recover_switch(self, ticket, force=False): + # What: arrange the exact self calls append recover ticket fixture fragment; why: the cancelled manual profile switch completes failed readiness rollback scenario feeds this byte-preserved fragment through self.calls.append(("recover", ticket[0])) before asserting its protocol or parser result. + self.calls.append(("recover", ticket[0])) + # What: arrange model and port and args as ticket; why: the cancelled manual profile switch completes failed readiness rollback test consumes this named precondition before exercising the behavior. + self.model, self.port, self.args = ticket + # What: arrange pid from 1; why: the cancelled manual profile switch completes failed readiness rollback scenario uses pid during return launched pid self pid port self port before checking the protected result. + self.pid += 1 + # What: arrange the launched field as true; why: RecoveringManager.recover_switch carries launched into return {"launched": True, "pid": self.pid, "port": self.port}. + return {"launched": True, "pid": self.pid, "port": self.port} + + # What: define Probe as the owner of fresh_health; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: arrange the def fresh health self port test helper boundary; why: test router test cancelled manual profile switch completes failed readiness rollback uses this local double to isolate the behavior checked by its assertions. + def fresh_health(self, port): + # What: act on port before set and readiness entered; why: the cancelled manual profile switch completes failed readiness rollback scenario admits set and readiness entered only for this predicate and excludes the opposite state. + if port == 1923: + # What: act by calling readiness_entered.set with the declared inputs; why: the cancelled manual profile switch completes failed readiness rollback scenario observes the readiness_entered.set return value during assert finish readiness wait. + readiness_entered.set() + # What: assert that finish readiness wait 2; why: this assertion protects the cancelled manual profile switch completes failed readiness rollback regression after the test's arranged inputs and exercised call. + assert finish_readiness.wait(2) + # What: arrange the helper response as reachable True status error maintenance serving; why: test router test cancelled manual profile switch completes failed readiness rollback feeds this result into the behavior whose outcome is asserted. + return {"reachable": True, "status": "error", "maintenance": "serving"} + # What: arrange the helper response as reachable True status ok maintenance serving; why: test router test cancelled manual profile switch completes failed readiness rollback feeds this result into the behavior whose outcome is asserted. + return {"reachable": True, "status": "ok", "maintenance": "serving"} + + # What: act by calling RecoveringManager and capture manager; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + manager = RecoveringManager() + # What: arrange model and port and args as legacy and gguf and 1922; why: the cancelled manual profile switch completes failed readiness rollback test consumes this named precondition before exercising the behavior. + manager.model, manager.port, manager.args = "legacy.gguf", 1922, [] + # What: act by calling ModelCatalog and capture catalog doc; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog({ + # What: arrange the high field as model profile and high and high and gguf and 1923; why: test_cancelled_manual_profile_switch_completes_failed_readiness_rollback carries high through catalog doc into router routing coordinator manager catalog doc probe ready fn ready. + "high": ModelProfile("high", "high.gguf", (), port=1923, ready_timeout_s=1), + # What: arrange the ModelCatalog call with model profile; why: test_cancelled_manual_profile_switch_completes_failed_readiness_rollback groups the supplied clauses as one ModelCatalog call before its value is consumed. + }) + # What: act by calling Probe and capture probe; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + probe = Probe() + # What: act by calling RoutingCoordinator and capture router; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, probe, ready_fn=ready) + + # What: define the scenario test helper around app; why: the cancelled manual profile switch completes failed readiness rollback scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def scenario(app): + # What: act by calling httpx.ASGITransport and capture transport; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + transport = httpx.ASGITransport(app=app) + # What: arrange async with httpx AsyncClient transport transport base url http test as client for the scenario; why: test cancelled manual profile switch c requires this concrete input or helper state before exercising the behavior under test. + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: + # What: act by calling asyncio.create_task and capture request; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + request = asyncio.create_task(client.post( + # What: arrange the name field as high; why: scenario carries name through request into if request done. + "/engine/switch-profile", json={"name": "high"} + # What: arrange the grouped source fragment for the scenario; why: test router test cancelled manual profile switch completes failed readiness rollback requires this concrete input or helper state before exercising the behavior under test. + )) + # What: act across range to perform is set and readiness entered; why: the cancelled manual profile switch completes failed readiness rollback scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on is set and readiness entered before the computed value; why: the cancelled manual profile switch completes failed readiness rollback scenario admits the computed value only for this predicate and excludes the opposite state. + if readiness_entered.is_set(): + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the cancelled manual profile switch completes failed readiness rollback scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the cancelled manual profile switch completes failed readiness rollback scenario observes the asyncio.sleep return value during if request done. + await asyncio.sleep(0.01) + # What: act on done and request before response and result and request; why: the cancelled manual profile switch completes failed readiness rollback scenario admits response and result and request only for this predicate and excludes the opposite state. + if request.done(): + # What: act by calling request.result and capture response; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + response = request.result() + # What: arrange the exact pytest fail f switch profile exited early response status code fixture fragment; why: the cancelled manual profile switch completes failed readiness rollback scenario feeds this byte-preserved fragment through pytest.fail(f"switch-profile exited early: {response.status_code} {. + pytest.fail(f"switch-profile exited early: {response.status_code} {response.text}") + # What: assert that readiness entered is set; why: this assertion protects the cancelled manual profile switch completes failed readiness rollback regression after the test's arranged inputs and exercised call. + assert readiness_entered.is_set() + # What: act by calling request.cancel with the declared inputs; why: the cancelled manual profile switch completes failed readiness rollback scenario observes the request.cancel return value during assert router status switching is. + request.cancel() + # What: assert that router status switching is true; why: this assertion protects the cancelled manual profile switch completes failed readiness rollback regression after the test's arranged inputs and exercised call. + assert router.status()["switching"] is True + # What: act by calling finish_readiness.set with the declared inputs; why: the cancelled manual profile switch completes failed readiness rollback scenario observes the finish_readiness.set return value during with pytest raises asyncio cancelled error. + finish_readiness.set() + # What: assert the pytest.raises failure context; why: the cancelled manual profile switch completes failed readiness rollback scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(asyncio.CancelledError): + # What: arrange the await request portion of the enclosing predicate; why: this clause remains in the cancelled manual profile switch completes failed readiness rollback scenario\'s enclosing expression so its grouping and evaluation order stay intact. + await request + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_cancelled_manual_profile_switch_completes_failed_readiness_rollback releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the cancelled manual profile switch completes failed readiness rollback test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_cancelled_manual_profile_switch_completes_failed_readiness_rollback; why: test_cancelled_manual_profile_switch_completes_failed_readiness_rollback consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=probe, footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the cancelled manual profile switch completes failed readiness rollback scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_cancelled_manual_profile_switch_completes_failed_readiness_rollback groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling asyncio.run with scenario and app; why: the cancelled manual profile switch completes failed readiness rollback scenario observes the asyncio.run return value during assert manager calls switch high gguf recover legacy gguf. + asyncio.run(scenario(app)) + + # What: assert that manager calls equals switch high gguf recover legacy gguf; why: this assertion protects the cancelled manual profile switch completes failed readiness rollback regression after the test's arranged inputs and exercised call. + assert manager.calls == [("switch", "high.gguf"), ("recover", "legacy.gguf")] + # What: assert that manager model equals legacy gguf; why: this assertion protects the cancelled manual profile switch completes failed readiness rollback regression after the test's arranged inputs and exercised call. + assert manager.model == "legacy.gguf" + # What: assert that router status switching is false; why: this assertion protects the cancelled manual profile switch completes failed readiness rollback regression after the test's arranged inputs and exercised call. + assert router.status()["switching"] is False + + +# What: define the test_router_shutdown_drains_active_lease_and_rejects_queued_and_new_admission test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router shutdown drains active lease and rejects queued and new admission outcome. +def test_router_shutdown_drains_active_lease_and_rejects_queued_and_new_admission(): + # What: define ShutdownManager as the owner of shutdown; why: daemon callers use this class boundary so those methods share one shutdown manager state invariant. + class ShutdownManager(Manager): + # What: define the shutdown test helper around timeout and force; why: the router shutdown drains active lease and rejects queued and new admission scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def shutdown(self, timeout=None, force=False): + # What: arrange the exact self calls append shutdown force fixture fragment; why: the router shutdown drains active lease and rejects queued and new admission scenario feeds this byte-preserved fragment through self.calls.append(("shutdown", force)) before asserting its protocol or parser result. + self.calls.append(("shutdown", force)) + # What: arrange model as the fixture input; why: the router shutdown drains active lease and rejects queued and new admission test consumes this named precondition before exercising the behavior. + self.model = None + # What: arrange the stopped field as true; why: ShutdownManager.shutdown carries stopped into return {"stopped": True, "already": False, "accounting": None}. + return {"stopped": True, "already": False, "accounting": None} + + # What: act by calling ShutdownManager and capture manager; why: the router shutdown drains active lease and rejects queued and new admission test asserts the response, state, or failure produced by this call. + manager = ShutdownManager() + # What: act by calling RoutingCoordinator and capture router; why: the router shutdown drains active lease and rejects queued and new admission test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the router shutdown drains active lease and rejects queued and new admission test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: arrange queued result as the fixture input; why: the router shutdown drains active lease and rejects queued and new admission test consumes this named precondition before exercising the behavior. + queued_result = {} + + # What: define the acquire_queued test helper around captured fixture state; why: the router shutdown drains active lease and rejects queued and new admission scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def acquire_queued(): + # What: establish the handler boundary for the protected operation; why: acquire_queued routes failures to routing error while preserving cleanup and success flow. + try: + # What: arrange the exact router acquire high fixture fragment; why: the router shutdown drains active lease and rejects queued and new admission scenario feeds this byte-preserved fragment through router.acquire("high") before asserting its protocol or parser result. + router.acquire("high") + # What: handle routing error by queued result error exc; why: acquire_queued converts that failure into this concrete recovery, response, or cleanup behavior. + except RoutingError as exc: + # What: arrange queued result entry as exc; why: the router shutdown drains active lease and rejects queued and new admission test consumes this named precondition before exercising the behavior. + queued_result["error"] = exc + + # What: act by calling threading.Thread and capture queued; why: the router shutdown drains active lease and rejects queued and new admission test asserts the response, state, or failure produced by this call. + queued = threading.Thread(target=acquire_queued) + # What: act by calling queued.start with the declared inputs; why: the router shutdown drains active lease and rejects queued and new admission scenario observes the queued.start return value during for value in range. + queued.start() + # What: act across range to perform status and router; why: the router shutdown drains active lease and rejects queued and new admission scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: arrange if router status queuedRequests == 1 for the scenario; why: test router test router shutdown drains active lease and rejects queued and new admission requires this concrete input or helper state before exercising the behavior under test. + if router.status()["queuedRequests"] == 1: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the router shutdown drains active lease and rejects queued and new admission scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the router shutdown drains active lease and rejects queued and new admission scenario observes the time.sleep return value during assert router status queued requests. + time.sleep(0.01) + # What: assert that router status queued requests equals 1; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert router.status()["queuedRequests"] == 1 + + # What: arrange shutdown result as the fixture input; why: the router shutdown drains active lease and rejects queued and new admission test consumes this named precondition before exercising the behavior. + shutdown_result = {} + # What: act by calling threading.Thread and capture shutdown; why: the router shutdown drains active lease and rejects queued and new admission test asserts the response, state, or failure produced by this call. + shutdown = threading.Thread( + # What: arrange target to shutdown_result.setdefault; why: the router shutdown drains active lease and rejects queued and new admission scenario binds this setdefault and shutdown result and shutdown and router and result value to shutdown_result.setdefault's target input. + target=lambda: shutdown_result.setdefault("result", router.shutdown(force=True)) + # What: arrange the threading.Thread call with target; why: test_router_shutdown_drains_active_lease_and_rejects_queued_and_new_admission groups the supplied clauses as one threading.Thread call before its value is consumed. + ) + # What: act by calling shutdown.start with the declared inputs; why: the router shutdown drains active lease and rejects queued and new admission scenario observes the shutdown.start return value during for value in range. + shutdown.start() + # What: act across range to perform status and router; why: the router shutdown drains active lease and rejects queued and new admission scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: arrange if router status shuttingDown for the scenario; why: test router test router shutdown drains active lease and rejects queued and new admission requires this concrete input or helper state before exercising the behavior under test. + if router.status()["shuttingDown"]: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the router shutdown drains active lease and rejects queued and new admission scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the router shutdown drains active lease and rejects queued and new admission scenario observes the time.sleep return value during queued join. + time.sleep(0.01) + # What: act by calling queued.join with 2; why: the router shutdown drains active lease and rejects queued and new admission scenario observes the queued.join return value during assert not queued is alive. + queued.join(2) + # What: assert that queued is alive is false; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert not queued.is_alive() + # What: assert that queued result error code equals router shutting down; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert queued_result["error"].code == "router_shutting_down" + # What: assert that shutdown is alive; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert shutdown.is_alive() + # What: assert that router is ready is false; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert router.is_ready() is False + # What: assert that freetoken swap shutting down 1 is present in router prometheus; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert "freetoken_swap_shutting_down 1" in router.prometheus() + # What: assert that manager calls equals start low gguf; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + # What: assert the pytest.raises failure context; why: the router shutdown drains active lease and rejects queued and new admission scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RoutingError) as exc: + # What: arrange the exact router acquire low fixture fragment; why: the router shutdown drains active lease and rejects queued and new admission scenario feeds this byte-preserved fragment through router.acquire("low") before asserting its protocol or parser result. + router.acquire("low") + # What: assert that exc value code equals router shutting down; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert exc.value.code == "router_shutting_down" + + # What: act by calling active.release with the declared inputs; why: the router shutdown drains active lease and rejects queued and new admission scenario observes the active.release return value during shutdown join. + active.release() + # What: act by calling shutdown.join with 2; why: the router shutdown drains active lease and rejects queued and new admission scenario observes the shutdown.join return value during assert not shutdown is alive. + shutdown.join(2) + # What: assert that shutdown is alive is false; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert not shutdown.is_alive() + # What: assert that shutdown result result stopped is true; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert shutdown_result["result"]["stopped"] is True + # What: assert that manager calls equals start low gguf shutdown true; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf"), ("shutdown", True)] + # What: assert that router status active profile is group delimiter; why: this assertion protects the router shutdown drains active lease and rejects queued and new admission regression after the test's arranged inputs and exercised call. + assert router.status()["activeProfile"] is None + + +# What: define the test_failed_router_shutdown_reopens_admission_and_preserves_resident test around local fixtures; why: this test groups the arrange, act, and assertions that protect the failed router shutdown reopens admission and preserves resident outcome. +def test_failed_router_shutdown_reopens_admission_and_preserves_resident(): + # What: define FailingShutdownManager as the owner of shutdown; why: daemon callers use this class boundary so those methods share one failing shutdown manager state invariant. + class FailingShutdownManager(Manager): + # What: define the shutdown test helper around timeout and force; why: the failed router shutdown reopens admission and preserves resident scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def shutdown(self, timeout=None, force=False): + # What: raise RuntimeError for the caller; why: FailingShutdownManager.shutdown stops this rejected path before it can mutate state, dispatch work, or report success. + raise RuntimeError("stop failed") + + # What: act by calling FailingShutdownManager and capture manager; why: the failed router shutdown reopens admission and preserves resident test asserts the response, state, or failure produced by this call. + manager = FailingShutdownManager() + # What: act by calling RoutingCoordinator and capture router; why: the failed router shutdown reopens admission and preserves resident test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: arrange the exact router acquire low release fixture fragment; why: the failed router shutdown reopens admission and preserves resident scenario feeds this byte-preserved fragment through router.acquire("low").release() before asserting its protocol or parser result. + router.acquire("low").release() + + # What: assert the pytest.raises failure context; why: the failed router shutdown reopens admission and preserves resident scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError, match="stop failed"): + # What: act by calling router.shutdown with the declared inputs; why: the failed router shutdown reopens admission and preserves resident scenario observes the router.shutdown return value during assert router status shutting down is. + router.shutdown() + + # What: assert that router status shutting down is false; why: this assertion protects the failed router shutdown reopens admission and preserves resident regression after the test's arranged inputs and exercised call. + assert router.status()["shuttingDown"] is False + # What: assert that router status active profile equals low; why: this assertion protects the failed router shutdown reopens admission and preserves resident regression after the test's arranged inputs and exercised call. + assert router.status()["activeProfile"] == "low" + # What: act by calling router.acquire and capture lease; why: the failed router shutdown reopens admission and preserves resident test asserts the response, state, or failure produced by this call. + lease = router.acquire("low") + # What: act by calling lease.release with the declared inputs; why: the failed router shutdown reopens admission and preserves resident scenario observes the lease.release return value during the enclosing return. + lease.release() + + +# What: define the test_cancelled_daemon_shutdown_finishes_stop_and_requests_process_exit test around local fixtures; why: this test groups the arrange, act, and assertions that protect the cancelled daemon shutdown finishes stop and requests process exit outcome. +def test_cancelled_daemon_shutdown_finishes_stop_and_requests_process_exit(): + # What: act by calling threading.Event and capture entered; why: the cancelled daemon shutdown finishes stop and requests process exit test asserts the response, state, or failure produced by this call. + entered = threading.Event() + # What: act by calling threading.Event and capture finish; why: the cancelled daemon shutdown finishes stop and requests process exit test asserts the response, state, or failure produced by this call. + finish = threading.Event() + + # What: define BlockingShutdownManager as the owner of shutdown; why: daemon callers use this class boundary so those methods share one blocking shutdown manager state invariant. + class BlockingShutdownManager(Manager): + # What: define the shutdown test helper around timeout and force; why: the cancelled daemon shutdown finishes stop and requests process exit scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def shutdown(self, timeout=None, force=False): + # What: act by calling entered.set with the declared inputs; why: the cancelled daemon shutdown finishes stop and requests process exit scenario observes the entered.set return value during assert finish wait. + entered.set() + # What: assert that finish wait 2; why: this assertion protects the cancelled daemon shutdown finishes stop and requests process exit regression after the test's arranged inputs and exercised call. + assert finish.wait(2) + # What: arrange model as the fixture input; why: the cancelled daemon shutdown finishes stop and requests process exit test consumes this named precondition before exercising the behavior. + self.model = None + # What: arrange the stopped field as true; why: BlockingShutdownManager.shutdown carries stopped into return {"stopped": True, "already": False, "accounting": None}. + return {"stopped": True, "already": False, "accounting": None} + + # What: act by calling BlockingShutdownManager and capture manager; why: the cancelled daemon shutdown finishes stop and requests process exit test asserts the response, state, or failure produced by this call. + manager = BlockingShutdownManager() + # What: act by calling RoutingCoordinator and capture router; why: the cancelled daemon shutdown finishes stop and requests process exit test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: arrange exits as the fixture input; why: the cancelled daemon shutdown finishes stop and requests process exit test consumes this named precondition before exercising the behavior. + exits = [] + + # What: define the scenario test helper around app; why: the cancelled daemon shutdown finishes stop and requests process exit scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def scenario(app): + # What: act by calling exits.append and capture request shutdown; why: the cancelled daemon shutdown finishes stop and requests process exit test asserts the response, state, or failure produced by this call. + app.state.request_shutdown = lambda: exits.append("requested") + # What: act by calling httpx.ASGITransport and capture transport; why: the cancelled daemon shutdown finishes stop and requests process exit test asserts the response, state, or failure produced by this call. + transport = httpx.ASGITransport(app=app) + # What: enter the httpx.AsyncClient managed context before request asyncio create task client post shutdown json force; why: scenario releases this resource or lock after request asyncio create task client post shutdown json force on both success and failure paths. + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: + # What: act by calling asyncio.create_task and capture request; why: the cancelled daemon shutdown finishes stop and requests process exit test asserts the response, state, or failure produced by this call. + request = asyncio.create_task(client.post("/shutdown", json={"force": True})) + # What: act across range to perform is set and entered; why: the cancelled daemon shutdown finishes stop and requests process exit scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on is set and entered before the computed value; why: the cancelled daemon shutdown finishes stop and requests process exit scenario admits the computed value only for this predicate and excludes the opposite state. + if entered.is_set(): + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the cancelled daemon shutdown finishes stop and requests process exit scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the cancelled daemon shutdown finishes stop and requests process exit scenario observes the asyncio.sleep return value during assert entered is set. + await asyncio.sleep(0.01) + # What: assert that entered is set; why: this assertion protects the cancelled daemon shutdown finishes stop and requests process exit regression after the test's arranged inputs and exercised call. + assert entered.is_set() + # What: act by calling request.cancel with the declared inputs; why: the cancelled daemon shutdown finishes stop and requests process exit scenario observes the request.cancel return value during request cancel. + request.cancel() + # What: act by calling request.cancel with the declared inputs; why: the cancelled daemon shutdown finishes stop and requests process exit scenario observes the request.cancel return value during await asyncio sleep. + request.cancel() + # What: act by calling asyncio.sleep with 0 05; why: the cancelled daemon shutdown finishes stop and requests process exit scenario observes the asyncio.sleep return value during assert not request done. + await asyncio.sleep(0.05) + # What: assert that request done is false; why: this assertion protects the cancelled daemon shutdown finishes stop and requests process exit regression after the test's arranged inputs and exercised call. + assert not request.done() + # What: act by calling finish.set with the declared inputs; why: the cancelled daemon shutdown finishes stop and requests process exit scenario observes the finish.set return value during with pytest raises asyncio cancelled error. + finish.set() + # What: assert the pytest.raises failure context; why: the cancelled daemon shutdown finishes stop and requests process exit scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(asyncio.CancelledError): + # What: arrange the await request portion of the enclosing predicate; why: this clause remains in the cancelled daemon shutdown finishes stop and requests process exit scenario\'s enclosing expression so its grouping and evaluation order stay intact. + await request + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_cancelled_daemon_shutdown_finishes_stop_and_requests_process_exit releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the cancelled daemon shutdown finishes stop and requests process exit test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_cancelled_daemon_shutdown_finishes_stop_and_requests_process_exit; why: test_cancelled_daemon_shutdown_finishes_stop_and_requests_process_exit consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to catalog; why: the cancelled daemon shutdown finishes stop and requests process exit scenario binds this lifecycle value to catalog's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog(), router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_cancelled_daemon_shutdown_finishes_stop_and_requests_process_exit groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling asyncio.run with scenario and app; why: the cancelled daemon shutdown finishes stop and requests process exit scenario observes the asyncio.run return value during assert exits requested. + asyncio.run(scenario(app)) + + # What: assert that exits equals requested; why: this assertion protects the cancelled daemon shutdown finishes stop and requests process exit regression after the test's arranged inputs and exercised call. + assert exits == ["requested"] + # What: assert that router status shutting down is true; why: this assertion protects the cancelled daemon shutdown finishes stop and requests process exit regression after the test's arranged inputs and exercised call. + assert router.status()["shuttingDown"] is True + + +# What: define the test_daemon_shutdown_latches_before_single_lifecycle_worker_is_available test around local fixtures; why: this test groups the arrange, act, and assertions that protect the daemon shutdown latches before single lifecycle worker is available outcome. +def test_daemon_shutdown_latches_before_single_lifecycle_worker_is_available(): + # What: act by calling threading.Event and capture start entered; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + start_entered = threading.Event() + # What: act by calling threading.Event and capture finish start; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + finish_start = threading.Event() + + # What: define BlockingLifecycleManager as the owner of start and shutdown; why: daemon callers use this class boundary so those methods share one blocking lifecycle manager state invariant. + class BlockingLifecycleManager(Manager): + # What: define the start test helper around model and port and args; why: the daemon shutdown latches before single lifecycle worker is available scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def start(self, model, port, args): + # What: arrange the exact self calls append start model fixture fragment; why: the daemon shutdown latches before single lifecycle worker is available scenario feeds this byte-preserved fragment through self.calls.append(("start", model)) before asserting its protocol or parser result. + self.calls.append(("start", model)) + # What: act by calling start_entered.set with the declared inputs; why: the daemon shutdown latches before single lifecycle worker is available scenario observes the start_entered.set return value during assert finish start wait. + start_entered.set() + # What: assert that finish start wait 2; why: this assertion protects the daemon shutdown latches before single lifecycle worker is available regression after the test's arranged inputs and exercised call. + assert finish_start.wait(2) + # What: act by calling list and capture model and port and args; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + self.model, self.port, self.args = model, port, list(args) + # What: arrange pid from 1; why: the daemon shutdown latches before single lifecycle worker is available scenario uses pid during return pid self pid before checking the protected result. + self.pid += 1 + # What: arrange the pid field as pid; why: BlockingLifecycleManager.start carries pid into return {"pid": self.pid}. + return {"pid": self.pid} + + # What: define the shutdown test helper around timeout and force; why: the daemon shutdown latches before single lifecycle worker is available scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def shutdown(self, timeout=None, force=False): + # What: arrange the exact self calls append shutdown force fixture fragment; why: the daemon shutdown latches before single lifecycle worker is available scenario feeds this byte-preserved fragment through self.calls.append(("shutdown", force)) before asserting its protocol or parser result. + self.calls.append(("shutdown", force)) + # What: arrange model as the fixture input; why: the daemon shutdown latches before single lifecycle worker is available test consumes this named precondition before exercising the behavior. + self.model = None + # What: arrange the stopped field as true; why: BlockingLifecycleManager.shutdown carries stopped into return {"stopped": True, "already": False, "accounting": None}. + return {"stopped": True, "already": False, "accounting": None} + + # What: act by calling BlockingLifecycleManager and capture manager; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + manager = BlockingLifecycleManager() + # What: act by calling RoutingCoordinator and capture router; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: arrange exits as the fixture input; why: the daemon shutdown latches before single lifecycle worker is available test consumes this named precondition before exercising the behavior. + exits = [] + + # What: define the scenario test helper around app; why: the daemon shutdown latches before single lifecycle worker is available scenario calls this helper to produce or observe the exact behavior checked by its assertions. + async def scenario(app): + # What: act by calling exits.append and capture request shutdown; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + app.state.request_shutdown = lambda: exits.append("requested") + # What: act by calling httpx.ASGITransport and capture transport; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + transport = httpx.ASGITransport(app=app) + # What: arrange async with httpx AsyncClient transport transport base url http test as client for the scenario; why: test daemon shutdown latches before si requires this concrete input or helper state before exercising the behavior under test. + async with httpx.AsyncClient(transport=transport, base_url="http://test") as client: + # What: act by calling asyncio.create_task and capture manual; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + manual = asyncio.create_task(client.post( + # What: arrange the model field as legacy and gguf; why: scenario sends this field through manual so the router selects the canonical model or alias for upstream dispatch. + "/engine/start", json={"model": "legacy.gguf", "port": 1930} + # What: arrange the grouped source fragment for the scenario; why: test router test daemon shutdown latches before single lifecycle worker is available requires this concrete input or helper state before exercising the behavior under test. + )) + # What: act across range to perform is set and start entered; why: the daemon shutdown latches before single lifecycle worker is available scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on is set and start entered before the computed value; why: the daemon shutdown latches before single lifecycle worker is available scenario admits the computed value only for this predicate and excludes the opposite state. + if start_entered.is_set(): + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the daemon shutdown latches before single lifecycle worker is available scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the daemon shutdown latches before single lifecycle worker is available scenario observes the asyncio.sleep return value during assert start entered is set. + await asyncio.sleep(0.01) + # What: assert that start entered is set; why: this assertion protects the daemon shutdown latches before single lifecycle worker is available regression after the test's arranged inputs and exercised call. + assert start_entered.is_set() + + # What: act by calling asyncio.create_task and capture shutdown; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + shutdown = asyncio.create_task(client.post("/shutdown", json={})) + # What: act across range to perform status and router; why: the daemon shutdown latches before single lifecycle worker is available scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the daemon shutdown latches before single lifecycle worker is available scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["shuttingDown"]: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the daemon shutdown latches before single lifecycle worker is available scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling asyncio.sleep with 0 01; why: the daemon shutdown latches before single lifecycle worker is available scenario observes the asyncio.sleep return value during assert router status shutting down is. + await asyncio.sleep(0.01) + # What: assert that router status shutting down is true; why: this assertion protects the daemon shutdown latches before single lifecycle worker is available regression after the test's arranged inputs and exercised call. + assert router.status()["shuttingDown"] is True + # What: assert that shutdown done is false; why: this assertion protects the daemon shutdown latches before single lifecycle worker is available regression after the test's arranged inputs and exercised call. + assert not shutdown.done() + # What: assert the pytest.raises failure context; why: the daemon shutdown latches before single lifecycle worker is available scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RoutingError) as exc: + # What: arrange the exact router acquire low fixture fragment; why: the daemon shutdown latches before single lifecycle worker is available scenario feeds this byte-preserved fragment through router.acquire("low") before asserting its protocol or parser result. + router.acquire("low") + # What: assert that exc value code equals router shutting down; why: this assertion protects the daemon shutdown latches before single lifecycle worker is available regression after the test's arranged inputs and exercised call. + assert exc.value.code == "router_shutting_down" + + # What: act by calling finish_start.set with the declared inputs; why: the daemon shutdown latches before single lifecycle worker is available scenario observes the finish_start.set return value during assert await manual status code. + finish_start.set() + # What: assert that await manual status code equals 200; why: this assertion protects the daemon shutdown latches before single lifecycle worker is available regression after the test's arranged inputs and exercised call. + assert (await manual).status_code == 200 + # What: arrange response as shutdown; why: the daemon shutdown latches before single lifecycle worker is available test consumes this named precondition before exercising the behavior. + response = await shutdown + # What: assert that response status code equals 200; why: this assertion protects the daemon shutdown latches before single lifecycle worker is available regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_daemon_shutdown_latches_before_single_lifecycle_worker_is_available releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the daemon shutdown latches before single lifecycle worker is available test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_daemon_shutdown_latches_before_single_lifecycle_worker_is_available; why: test_daemon_shutdown_latches_before_single_lifecycle_worker_is_available consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to catalog; why: the daemon shutdown latches before single lifecycle worker is available scenario binds this lifecycle value to catalog's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog(), router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_daemon_shutdown_latches_before_single_lifecycle_worker_is_available groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling asyncio.run with scenario and app; why: the daemon shutdown latches before single lifecycle worker is available scenario observes the asyncio.run return value during assert manager calls start legacy gguf shutdown. + asyncio.run(scenario(app)) + + # What: assert that manager calls equals start legacy gguf shutdown false; why: this assertion protects the daemon shutdown latches before single lifecycle worker is available regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "legacy.gguf"), ("shutdown", False)] + # What: assert that exits equals requested; why: this assertion protects the daemon shutdown latches before single lifecycle worker is available regression after the test's arranged inputs and exercised call. + assert exits == ["requested"] + + +# What: define the test_coordinated_daemon_exit_drains_then_detaches_once_for_readoption test around local fixtures; why: this test groups the arrange, act, and assertions that protect the coordinated daemon exit drains then detaches once for readoption outcome. +def test_coordinated_daemon_exit_drains_then_detaches_once_for_readoption(): + # What: arrange the class DetachingManager Manager test helper boundary; why: test router test coordinated daemon exit drains then detaches once for readoption uses this local double to isolate the behavior checked by its assertions. + class DetachingManager(Manager): + # What: define the detach test helper around captured fixture state; why: the coordinated daemon exit drains then detaches once for readoption scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def detach(self): + # What: arrange the exact self calls append detach self model fixture fragment; why: the coordinated daemon exit drains then detaches once for readoption scenario feeds this byte-preserved fragment through self.calls.append(("detach", self.model)) before asserting its protocol or parser result. + self.calls.append(("detach", self.model)) + + # What: act by calling DetachingManager and capture manager; why: the coordinated daemon exit drains then detaches once for readoption test asserts the response, state, or failure produced by this call. + manager = DetachingManager() + # What: act by calling RoutingCoordinator and capture router; why: the coordinated daemon exit drains then detaches once for readoption test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: act by calling router.acquire and capture active; why: the coordinated daemon exit drains then detaches once for readoption test asserts the response, state, or failure produced by this call. + active = router.acquire("low") + # What: arrange result as the fixture input; why: the coordinated daemon exit drains then detaches once for readoption test consumes this named precondition before exercising the behavior. + result = {} + # What: act by calling threading.Thread and capture exiting; why: the coordinated daemon exit drains then detaches once for readoption test asserts the response, state, or failure produced by this call. + exiting = threading.Thread( + # What: arrange target to result.setdefault; why: the coordinated daemon exit drains then detaches once for readoption scenario binds this setdefault and result and coordinated exit and router and value value to result.setdefault's target input. + target=lambda: result.setdefault( + # What: arrange stop child to router.coordinated_exit; why: the coordinated daemon exit drains then detaches once for readoption scenario binds this false value to router.coordinated_exit's stop child input. + "value", router.coordinated_exit(stop_child=False) + # What: arrange the result.setdefault call with coordinated exit; why: test_coordinated_daemon_exit_drains_then_detaches_once_for_readoption groups the supplied clauses as one result.setdefault call before its value is consumed. + ) + # What: arrange the threading.Thread call with target; why: test_coordinated_daemon_exit_drains_then_detaches_once_for_readoption groups the supplied clauses as one threading.Thread call before its value is consumed. + ) + # What: act by calling exiting.start with the declared inputs; why: the coordinated daemon exit drains then detaches once for readoption scenario observes the exiting.start return value during for value in range. + exiting.start() + # What: act across range to perform status and router; why: the coordinated daemon exit drains then detaches once for readoption scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the coordinated daemon exit drains then detaches once for readoption scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["shuttingDown"]: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the coordinated daemon exit drains then detaches once for readoption scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the coordinated daemon exit drains then detaches once for readoption scenario observes the time.sleep return value during assert router status shutting down is. + time.sleep(0.01) + # What: assert that router status shutting down is true; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert router.status()["shuttingDown"] is True + # What: assert that exiting is alive; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert exiting.is_alive() + # What: assert that manager calls equals start low gguf; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf")] + + # What: act by calling active.release with the declared inputs; why: the coordinated daemon exit drains then detaches once for readoption scenario observes the active.release return value during exiting join. + active.release() + # What: act by calling exiting.join with 2; why: the coordinated daemon exit drains then detaches once for readoption scenario observes the exiting.join return value during assert not exiting is alive. + exiting.join(2) + # What: assert that exiting is alive is false; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert not exiting.is_alive() + # What: assert that result value is group delimiter; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert result["value"] is None + # What: assert that manager calls equals start low gguf detach low gguf; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf"), ("detach", "low.gguf")] + # What: assert that manager model equals low gguf; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert manager.model == "low.gguf" + # What: assert that router status active profile is group delimiter; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert router.status()["activeProfile"] is None + + # Uvicorn lifespan can run after POST /shutdown already completed. The + # repeated exit hook must not detach or stop the child a second time. + # What: assert that router coordinated exit stop child false is group delimiter; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert router.coordinated_exit(stop_child=False) is None + # What: assert that manager calls equals start low gguf detach low gguf; why: this assertion protects the coordinated daemon exit drains then detaches once for readoption regression after the test's arranged inputs and exercised call. + assert manager.calls == [("start", "low.gguf"), ("detach", "low.gguf")] + + +# What: define the test_coordinated_exit_waits_for_preempted_manual_transaction_token test around local fixtures; why: this test groups the arrange, act, and assertions that protect the coordinated exit waits for preempted manual transaction token outcome. +def test_coordinated_exit_waits_for_preempted_manual_transaction_token(): + # What: define DetachingManager as the owner of detach; why: daemon callers use this class boundary so those methods share one detaching manager state invariant. + class DetachingManager(Manager): + # What: define the detach test helper around captured fixture state; why: the coordinated exit waits for preempted manual transaction token scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def detach(self): + # What: arrange the exact self calls append detach self model fixture fragment; why: the coordinated exit waits for preempted manual transaction token scenario feeds this byte-preserved fragment through self.calls.append(("detach", self.model)) before asserting its protocol or parser result. + self.calls.append(("detach", self.model)) + + # What: act by calling DetachingManager and capture manager; why: the coordinated exit waits for preempted manual transaction token test asserts the response, state, or failure produced by this call. + manager = DetachingManager() + # What: act by calling RoutingCoordinator and capture router; why: the coordinated exit waits for preempted manual transaction token test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog(), object(), ready_fn=ready) + # What: act by calling router.begin_manual_lifecycle and capture older; why: the coordinated exit waits for preempted manual transaction token test asserts the response, state, or failure produced by this call. + older = router.begin_manual_lifecycle() + # What: act by calling router.begin_manual_lifecycle and capture newer; why: the coordinated exit waits for preempted manual transaction token test asserts the response, state, or failure produced by this call. + newer = router.begin_manual_lifecycle(preempt_manual=True) + # What: act by calling router.end_manual_lifecycle with newer; why: the coordinated exit waits for preempted manual transaction token scenario observes the router.end_manual_lifecycle return value during assert router status switching is. + router.end_manual_lifecycle(newer) + # What: assert that router status switching is false; why: this assertion protects the coordinated exit waits for preempted manual transaction token regression after the test's arranged inputs and exercised call. + assert router.status()["switching"] is False + + # What: act by calling threading.Thread and capture exiting; why: the coordinated exit waits for preempted manual transaction token test asserts the response, state, or failure produced by this call. + exiting = threading.Thread( + # What: arrange target to router.coordinated_exit; why: the coordinated exit waits for preempted manual transaction token scenario binds this coordinated exit and router and false value to router.coordinated_exit's target input. + target=lambda: router.coordinated_exit(stop_child=False) + # What: arrange the threading.Thread call with target; why: test_coordinated_exit_waits_for_preempted_manual_transaction_token groups the supplied clauses as one threading.Thread call before its value is consumed. + ) + # What: act by calling exiting.start with the declared inputs; why: the coordinated exit waits for preempted manual transaction token scenario observes the exiting.start return value during for value in range. + exiting.start() + # What: act across range to perform status and router; why: the coordinated exit waits for preempted manual transaction token scenario repeats the body only while or for the loop header admits an iteration. + for _ in range(100): + # What: act on status and router before the computed value; why: the coordinated exit waits for preempted manual transaction token scenario admits the computed value only for this predicate and excludes the opposite state. + if router.status()["shuttingDown"]: + # What: arrange the break portion of the enclosing predicate; why: this clause remains in the coordinated exit waits for preempted manual transaction token scenario\'s enclosing expression so its grouping and evaluation order stay intact. + break + # What: act by calling time.sleep with 0 01; why: the coordinated exit waits for preempted manual transaction token scenario observes the time.sleep return value during assert router status shutting down is. + time.sleep(0.01) + # What: assert that router status shutting down is true; why: this assertion protects the coordinated exit waits for preempted manual transaction token regression after the test's arranged inputs and exercised call. + assert router.status()["shuttingDown"] is True + # What: assert that exiting is alive; why: this assertion protects the coordinated exit waits for preempted manual transaction token regression after the test's arranged inputs and exercised call. + assert exiting.is_alive() + # What: assert that manager calls equals group delimiter; why: this assertion protects the coordinated exit waits for preempted manual transaction token regression after the test's arranged inputs and exercised call. + assert manager.calls == [] + + # What: act by calling router.end_manual_lifecycle with older; why: the coordinated exit waits for preempted manual transaction token scenario observes the router.end_manual_lifecycle return value during exiting join. + router.end_manual_lifecycle(older) + # What: act by calling exiting.join with 2; why: the coordinated exit waits for preempted manual transaction token scenario observes the exiting.join return value during assert not exiting is alive. + exiting.join(2) + # What: assert that exiting is alive is false; why: this assertion protects the coordinated exit waits for preempted manual transaction token regression after the test's arranged inputs and exercised call. + assert not exiting.is_alive() + # What: assert that manager calls equals detach; why: this assertion protects the coordinated exit waits for preempted manual transaction token regression after the test's arranged inputs and exercised call. + assert manager.calls == [("detach", None)] + + +# What: define the test_router_management_ui_has_no_embedded_operational_data_and_hardware_is_gated test around local fixtures; why: this test groups the arrange, act, and assertions that protect the router management ui has no embedded operational data and hardware is gated outcome. +def test_router_management_ui_has_no_embedded_operational_data_and_hardware_is_gated(): + # What: act by calling Manager and capture manager; why: the router management ui has no embedded operational data and hardware is gated test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the router management ui has no embedded operational data and hardware is gated test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and private and models and low; why: test_router_management_ui_has_no_embedded_operational_data_and_hardware_is_gated carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "/private/models/low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the router management ui has no embedded operational data and hardware is gated scenario binds this router settings and router test key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("router-test-key",)), + # What: arrange the ModelCatalog call with settings; why: test_router_management_ui_has_no_embedded_operational_data_and_hardware_is_gated groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the router management ui has no embedded operational data and hardware is gated test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_router_management_ui_has_no_embedded_operational_data_and_hardware_is_gated releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the router management ui has no embedded operational data and hardware is gated test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange manager to LogRing; why: the router management ui has no embedded operational data and hardware is gated scenario binds this manager value to LogRing's manager input. + manager=manager, ring=LogRing(), probe=object(), + # What: arrange the ram bytes field as 123; why: test_router_management_ui_has_no_embedded_operational_data_and_hardware_is_gated carries ram bytes through app into client test client app. + footprint_fn=lambda pid: {"ramBytes": 123, "vramBytes": 456}, + # What: arrange lifecycle pool to build_app; why: the router management ui has no embedded operational data and hardware is gated scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_router_management_ui_has_no_embedded_operational_data_and_hardware_is_gated groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling TestClient and capture client; why: the router management ui has no embedded operational data and hardware is gated test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: act by calling client.get and capture page; why: the router management ui has no embedded operational data and hardware is gated test asserts the response, state, or failure produced by this call. + page = client.get("/ui/") + # What: assert that client get router hardware status code equals 401; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert client.get("/router/hardware").status_code == 401 + # What: act by calling client.get and capture hardware; why: the router management ui has no embedded operational data and hardware is gated test asserts the response, state, or failure produced by this call. + hardware = client.get("/router/hardware", headers={"Authorization": "Bearer router-test-key"}) + # What: assert that page status code equals 200; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert page.status_code == 200 + # What: assert that router load is present in page text; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert "/router/load" in page.text + # What: assert that router hardware is present in page text; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert "/router/hardware" in page.text + # What: assert that router activity limit 25 is present in page text; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert "/router/activity?limit=25" in page.text + # What: assert that router performance is present in page text; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert "/router/performance" in page.text + # What: assert that router captures is present in page text; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert "/router/captures/" in page.text + # What: assert that inner html is absent from page text; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert "innerHTML" not in page.text + # What: assert that captures may contain prompts and are is present in page text; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert "Captures may contain prompts and are fetched only when selected" in page.text + # What: assert that private models low gguf is absent from page text; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert "/private/models/low.gguf" not in page.text + # What: assert that router test key is absent from page text; why: this assertion protects the router management ui has no embedded operational data and hardware is gated regression after the test's arranged inputs and exercised call. + assert "router-test-key" not in page.text + # What: assert the expected hardware json == outcome; why: test router test router management ui has no embedded operational data and hardware is gated protects its regression by requiring this observable result after the exercised behavior. + assert hardware.json() == { + # What: arrange engine running False pid 100 port None for the scenario; why: test router test router management ui has no embedded operational data and hardware is gated requires this concrete input or helper state before exercising the behavior under test. + "engine": {"running": False, "pid": 100, "port": None}, + # What: arrange memory ramBytes 123 vramBytes 456 for the scenario; why: test router test router management ui has no embedded operational data and hardware is gated requires this concrete input or helper state before exercising the behavior under test. + "memory": {"ramBytes": 123, "vramBytes": 456}, + # What: arrange the grouped source fragment for the scenario; why: test router test router management ui has no embedded operational data and hardware is gated requires this concrete input or helper state before exercising the behavior under test. + } + + +# What: define the test_periodic_performance_api_is_authenticated_filterable_and_private test around local fixtures; why: this test groups the arrange, act, and assertions that protect the periodic performance api is authenticated filterable and private outcome. +def test_periodic_performance_api_is_authenticated_filterable_and_private(): + # What: act by calling Manager and capture manager; why: the periodic performance api is authenticated filterable and private test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the periodic performance api is authenticated filterable and private test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and private and models and low; why: test_periodic_performance_api_is_authenticated_filterable_and_private carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "/private/models/low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the periodic performance api is authenticated filterable and private scenario binds this router settings and router test key value to RouterSettings's settings input. + settings=RouterSettings(api_keys=("router-test-key",)), + # What: arrange the ModelCatalog call with settings; why: test_periodic_performance_api_is_authenticated_filterable_and_private groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the periodic performance api is authenticated filterable and private test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: arrange footprint as ram bytes and vram bytes and pids and ram available and vram available; why: the periodic performance api is authenticated filterable and private test consumes this named precondition before exercising the behavior. + footprint = { + # What: arrange the ram bytes field as 123; why: test_periodic_performance_api_is_authenticated_filterable_and_private carries ram bytes through footprint into footprint fn lambda pid footprint. + "ramBytes": 123, "vramBytes": 456, "pids": [100], + # What: arrange the ram available field as true; why: test_periodic_performance_api_is_authenticated_filterable_and_private carries ram available through footprint into footprint fn lambda pid footprint. + "ramAvailable": True, "vramAvailable": True, + # What: arrange the ram source field as test pss; why: test_periodic_performance_api_is_authenticated_filterable_and_private carries ram source through footprint into footprint fn lambda pid footprint. + "ramSource": "test-pss", "vramSource": "test-gpu", + # What: arrange the footprint mapping with ram bytes and vram bytes and pids and ram available and vram available; why: test_periodic_performance_api_is_authenticated_filterable_and_private groups the supplied clauses as one footprint mapping before its value is consumed. + } + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_periodic_performance_api_is_authenticated_filterable_and_private releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the periodic performance api is authenticated filterable and private test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange manager to LogRing; why: the periodic performance api is authenticated filterable and private scenario binds this manager value to LogRing's manager input. + manager=manager, ring=LogRing(), probe=object(), + # What: arrange the pid input for test_periodic_performance_api_is_authenticated_filterable_and_private; why: test_periodic_performance_api_is_authenticated_filterable_and_private consumes pid during signature binding, so callers must bind it with the other signature inputs. + footprint_fn=lambda pid: footprint, + # What: arrange lifecycle pool to build_app; why: the periodic performance api is authenticated filterable and private scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_periodic_performance_api_is_authenticated_filterable_and_private groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling app.state.performance_monitor.sample_once with the declared inputs; why: the periodic performance api is authenticated filterable and private scenario observes the app.state.performance_monitor.sample_once return value during client test client app. + app.state.performance_monitor.sample_once() + # What: act by calling TestClient and capture client; why: the periodic performance api is authenticated filterable and private test asserts the response, state, or failure produced by this call. + client = TestClient(app) + # What: arrange headers as authorization and bearer and router test key; why: the periodic performance api is authenticated filterable and private test consumes this named precondition before exercising the behavior. + headers = {"Authorization": "Bearer router-test-key"} + # What: assert that client get api performance status code equals 401; why: this assertion protects the periodic performance api is authenticated filterable and private regression after the test's arranged inputs and exercised call. + assert client.get("/api/performance").status_code == 401 + # What: act by calling client.get and capture response; why: the periodic performance api is authenticated filterable and private test asserts the response, state, or failure produced by this call. + response = client.get("/api/performance", headers=headers) + # What: act by calling response.json and capture timestamp; why: the periodic performance api is authenticated filterable and private test asserts the response, state, or failure produced by this call. + timestamp = response.json()["sys_stats"][0]["timestamp"] + # What: act by calling client.get and capture filtered; why: the periodic performance api is authenticated filterable and private test asserts the response, state, or failure produced by this call. + filtered = client.get( + # What: arrange the after field as timestamp; why: test_periodic_performance_api_is_authenticated_filterable_and_private carries after through filtered into assert filtered json sys stats equals. + "/router/performance", params={"after": timestamp}, headers=headers + # What: arrange the client.get call with params and headers; why: test_periodic_performance_api_is_authenticated_filterable_and_private groups the supplied clauses as one client.get call before its value is consumed. + ) + # What: act by calling client.get and capture invalid; why: the periodic performance api is authenticated filterable and private test asserts the response, state, or failure produced by this call. + invalid = client.get( + # What: arrange the after field as not a time; why: test_periodic_performance_api_is_authenticated_filterable_and_private carries after through invalid into assert invalid status code equals 400. + "/api/performance", params={"after": "not-a-time"}, headers=headers + # What: arrange the client.get call with params and headers; why: test_periodic_performance_api_is_authenticated_filterable_and_private groups the supplied clauses as one client.get call before its value is consumed. + ) + # What: assert that response status code equals 200; why: this assertion protects the periodic performance api is authenticated filterable and private regression after the test's arranged inputs and exercised call. + assert response.status_code == 200 + # What: assert that response json gpu stats equals group delimiter; why: this assertion protects the periodic performance api is authenticated filterable and private regression after the test's arranged inputs and exercised call. + assert response.json()["gpu_stats"] == [] + # What: assert the expected response json sys stats 0 == outcome; why: test router test periodic performance api is authenticated filterable and private protects its regression by requiring this observable result after the exercised behavior. + assert response.json()["sys_stats"][0] == { + # What: arrange timestamp timestamp scope engine process tree for the scenario; why: test router test periodic performance api is authenticated filterable and private requires this concrete input or helper state before exercising the behavior under test. + "timestamp": timestamp, "scope": "engine-process-tree", + # What: arrange ram bytes 123 vram bytes 456 for the scenario; why: test router test periodic performance api is authenticated filterable and private requires this concrete input or helper state before exercising the behavior under test. + "ram_bytes": 123, "vram_bytes": 456, + # What: arrange ram available True vram available True for the scenario; why: test router test periodic performance api is authenticated filterable and private requires this concrete input or helper state before exercising the behavior under test. + "ram_available": True, "vram_available": True, + # What: arrange ram source test pss vram source test gpu for the scenario; why: test pss vram source test gpu requires this concrete input or helper state before exercising the behavior under test. + "ram_source": "test-pss", "vram_source": "test-gpu", + # What: arrange the grouped source fragment for the scenario; why: test router test periodic performance api is authenticated filterable and private requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that private not in response text and pids not in response text; why: this assertion protects the periodic performance api is authenticated filterable and private regression after the test's arranged inputs and exercised call. + assert "/private/" not in response.text and "pids" not in response.text + # What: assert that filtered json sys stats equals group delimiter; why: this assertion protects the periodic performance api is authenticated filterable and private regression after the test's arranged inputs and exercised call. + assert filtered.json()["sys_stats"] == [] + # What: assert that invalid status code equals 400; why: this assertion protects the periodic performance api is authenticated filterable and private regression after the test's arranged inputs and exercised call. + assert invalid.status_code == 400 + + +# What: define the test_disabled_performance_api_matches_pinned_unavailable_contract test around local fixtures; why: this test groups the arrange, act, and assertions that protect the disabled performance api matches pinned unavailable contract outcome. +def test_disabled_performance_api_matches_pinned_unavailable_contract(): + # What: act by calling Manager and capture manager; why: the disabled performance api matches pinned unavailable contract test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog and capture catalog doc; why: the disabled performance api matches pinned unavailable contract test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog( + # What: arrange the low field as model profile and low and low and gguf; why: test_disabled_performance_api_matches_pinned_unavailable_contract carries low through catalog doc into router routing coordinator manager catalog doc object ready fn ready. + {"low": ModelProfile("low", "low.gguf", ())}, + # What: arrange settings to RouterSettings; why: the disabled performance api matches pinned unavailable contract scenario binds this router settings and true value to RouterSettings's settings input. + settings=RouterSettings(performance_disabled=True), + # What: arrange the ModelCatalog call with settings; why: test_disabled_performance_api_matches_pinned_unavailable_contract groups the supplied clauses as one ModelCatalog call before its value is consumed. + ) + # What: act by calling RoutingCoordinator and capture router; why: the disabled performance api matches pinned unavailable contract test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_disabled_performance_api_matches_pinned_unavailable_contract releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the disabled performance api matches pinned unavailable contract test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_disabled_performance_api_matches_pinned_unavailable_contract; why: test_disabled_performance_api_matches_pinned_unavailable_contract consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the disabled performance api matches pinned unavailable contract scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_disabled_performance_api_matches_pinned_unavailable_contract groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: act by calling operation.get and capture response; why: the disabled performance api matches pinned unavailable contract test asserts the response, state, or failure produced by this call. + response = TestClient(app).get("/api/performance") + # What: assert that response status code equals 503; why: this assertion protects the disabled performance api matches pinned unavailable contract regression after the test's arranged inputs and exercised call. + assert response.status_code == 503 + # What: assert that response json equals enabled false; why: this assertion protects the disabled performance api matches pinned unavailable contract regression after the test's arranged inputs and exercised call. + assert response.json() == {"enabled": False} + + +# What: define the test_catalog_watcher_applies_only_valid_idle_replacements test around tmp path; why: this test groups the arrange, act, and assertions that protect the catalog watcher applies only valid idle replacements outcome. +def test_catalog_watcher_applies_only_valid_idle_replacements(tmp_path): + # What: arrange path as tmp path and models and toml; why: the catalog watcher applies only valid idle replacements test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models a nmodel a gguf n encoding fixture fragment; why: the catalog watcher applies only valid idle replacements scenario feeds this byte-preserved fragment through path.write_text("[models.a]\nmodel = 'a.gguf'\n", encoding="utf-8") before asserting its protocol or parser. + path.write_text("[models.a]\nmodel = 'a.gguf'\n", encoding="utf-8") + # What: act by calling Manager and capture manager; why: the catalog watcher applies only valid idle replacements test asserts the response, state, or failure produced by this call. + manager = Manager() + # What: act by calling ModelCatalog.load and capture catalog doc; why: the catalog watcher applies only valid idle replacements test asserts the response, state, or failure produced by this call. + catalog_doc = ModelCatalog.load(str(path)) + # What: act by calling RoutingCoordinator and capture router; why: the catalog watcher applies only valid idle replacements test asserts the response, state, or failure produced by this call. + router = RoutingCoordinator(manager, catalog_doc, object(), ready_fn=ready) + + # What: define the wait_for test helper around client and result; why: the catalog watcher applies only valid idle replacements scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def wait_for(client, result): + # What: act by calling time.monotonic and capture deadline; why: the catalog watcher applies only valid idle replacements test asserts the response, state, or failure produced by this call. + deadline = time.monotonic() + 2 + # What: act across deadline and monotonic and time to perform result and get and json and client; why: the catalog watcher applies only valid idle replacements scenario repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: act on result and get and json and client before the computed value; why: the catalog watcher applies only valid idle replacements scenario admits the computed value only for this predicate and excludes the opposite state. + if client.get("/router/status").json()["catalogWatch"].get("lastResult") == result: + # What: return the named fixture input from the wait_for test helper; why: the catalog watcher applies only valid idle replacements scenario uses this helper result in its subsequent act or assertion. + return + # What: act by calling time.sleep with 0 02; why: the catalog watcher applies only valid idle replacements scenario observes the time.sleep return value during raise assertion error f catalog watcher did. + time.sleep(0.02) + # What: raise AssertionError for the caller; why: wait_for stops this rejected path before it can mutate state, dispatch work, or report success. + raise AssertionError(f"catalog watcher did not report {result}") + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app; why: test_catalog_watcher_applies_only_valid_idle_replacements releases this resource or lock after app build app on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the catalog watcher applies only valid idle replacements test asserts the response, state, or failure produced by this call. + app = build_app( + # What: arrange the pid input for test_catalog_watcher_applies_only_valid_idle_replacements; why: test_catalog_watcher_applies_only_valid_idle_replacements consumes pid during signature binding, so callers must bind it with the other signature inputs. + manager=manager, ring=LogRing(), probe=object(), footprint_fn=lambda pid: {}, + # What: arrange lifecycle pool to build_app; why: the catalog watcher applies only valid idle replacements scenario binds this lifecycle value to build_app's lifecycle pool input. + lifecycle_pool=lifecycle, proxy_pool=proxy, catalog=catalog_doc, router=router, + # What: arrange catalog path to str; why: the catalog watcher applies only valid idle replacements scenario binds this str and path value to str's catalog path input. + catalog_path=str(path), catalog_watch_interval_s=0.01, + # What: arrange the build_app call with manager and ring and probe and footprint fn and lifecycle pool; why: test_catalog_watcher_applies_only_valid_idle_replacements groups the supplied clauses as one build_app call before its value is consumed. + ) + # What: enter the TestClient managed context before path write text; why: test_catalog_watcher_applies_only_valid_idle_replacements releases this resource or lock after path write text on both success and failure paths. + with TestClient(app) as client: + # What: act by calling path.write_text with router and performance disabled and true and models; why: the catalog watcher applies only valid idle replacements scenario observes the path.write_text return value during router nperformance disabled true n n models b. + path.write_text( + # What: arrange the exact router nperformance disabled true n n models b fixture fragment; why: the catalog watcher applies only valid idle replacements scenario feeds this byte-preserved fragment through "[router]\nperformance_disabled = true\n\n[models.b]\nmodel = 'b.gguf'\n before asserting its prot. + "[router]\nperformance_disabled = true\n\n[models.b]\nmodel = 'b.gguf'\n", + # What: arrange the exact encoding utf 8 fixture fragment; why: the catalog watcher applies only valid idle replacements scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the path.write_text call with encoding; why: test_catalog_watcher_applies_only_valid_idle_replacements groups the supplied clauses as one path.write_text call before its value is consumed. + ) + # What: arrange the exact wait for client reloaded fixture fragment; why: the catalog watcher applies only valid idle replacements scenario feeds this byte-preserved fragment through wait_for(client, "reloaded") before asserting its protocol or parser result. + wait_for(client, "reloaded") + # What: assert that model name for model in client get equals b; why: this assertion protects the catalog watcher applies only valid idle replacements regression after the test's arranged inputs and exercised call. + assert [model["name"] for model in client.get("/router/models").json()["data"]] == ["b"] + # What: assert that app state performance monitor current enabled is false; why: this assertion protects the catalog watcher applies only valid idle replacements regression after the test's arranged inputs and exercised call. + assert app.state.performance_monitor.current()["enabled"] is False + # What: arrange the exact path write text models b nmodel n encoding utf 8 fixture fragment; why: the catalog watcher applies only valid idle replacements scenario feeds this byte-preserved fragment through path.write_text("[models.b]\nmodel = [\n", encoding="utf-8") before asserting its protocol or parser. + path.write_text("[models.b]\nmodel = [\n", encoding="utf-8") + # What: arrange the exact wait for client invalid catalog fixture fragment; why: the catalog watcher applies only valid idle replacements scenario feeds this byte-preserved fragment through wait_for(client, "invalid_catalog") before asserting its protocol or parser result. + wait_for(client, "invalid_catalog") + # What: assert that model name for model in client get equals b; why: this assertion protects the catalog watcher applies only valid idle replacements regression after the test's arranged inputs and exercised call. + assert [model["name"] for model in client.get("/router/models").json()["data"]] == ["b"] + # What: assert that app state catalog watch stop is set; why: this assertion protects the catalog watcher applies only valid idle replacements regression after the test's arranged inputs and exercised call. + assert app.state.catalog_watch_stop.is_set() diff --git a/tests/daemon/test_swap_qualification.py b/tests/daemon/test_swap_qualification.py new file mode 100644 index 000000000..3c4aa1a98 --- /dev/null +++ b/tests/daemon/test_swap_qualification.py @@ -0,0 +1,1950 @@ +"""CPU tests of cancellation evidence gates, not real-model qualification.""" +# What: document cpu tests of cancellation evidence gates in the test_swap_qualification docstring; why: introspection and maintainers read this exact docstring fragment to understand test swap qualification behavior without executing it. + +# What: import importlib util for qualifier using importlib and util; why: qualifier uses importlib util spec from file location, making that imported dependency available to its named operation. +import importlib.util +# What: import base64 for do get using base64; why: do_GET uses base64 b64encode, making that imported dependency available to its named operation. +import base64 +# What: import io for test cancellation requires terminal abort without restart using io; why: test_cancellation_requires_terminal_abort_without_restart uses io bytes io, making that imported dependency available to its named operation. +import io +# What: import json for test native periodic performance gate rejects unavailable or identifying rows using json; why: test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows uses json loads, making that imported dependency available to its named operation. +import json +# What: import socket for test native router benchmark requires final engine listener to close using socket; why: test_native_router_benchmark_requires_final_engine_listener_to_close uses socket socket, making that imported dependency available to its named operation. +import socket +# What: import threading for test cancellation closes real local http stream using threading; why: test_cancellation_closes_real_local_http_stream uses threading event, making that imported dependency available to its named operation. +import threading +# What: import time for fake canary using time; why: fake_canary uses time sleep, making that imported dependency available to its named operation. +import time +# What: arrange from http server import BaseHTTPRequestHandler ThreadingHTTPServer for the scenario; why: test swap qualification requires this concrete input or helper state before exercising the behavior under test. +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +# What: import path for qualifier using pathlib and path; why: qualifier uses path, making that imported dependency available to its named operation. +from pathlib import Path + +# What: import pytest for module initialization using pytest; why: module initialization uses pytest fixture, making that imported dependency available to its named operation. +import pytest + +# What: arrange from freetoken daemon catalog import ModelCatalog for the scenario; why: test swap qualification requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.catalog import ModelCatalog + + +# What: apply pytest.fixture behavior to qualifier; why: Python attaches this named decorator's registration or descriptor semantics to qualifier. +@pytest.fixture +# What: define the qualifier test helper around captured fixture state; why: the qualifier scenario calls this helper to produce or observe the exact behavior checked by its assertions. +def qualifier(): + # What: act by calling Path and capture path; why: the swap qualification test asserts the response, state, or failure produced by this call. + path = Path(__file__).parents[2] / "benchmarks/swap/qualify.py" + # What: act by calling importlib.util.spec_from_file_location and capture spec; why: the swap qualification test asserts the response, state, or failure produced by this call. + spec = importlib.util.spec_from_file_location("swap_qualifier", path) + # What: act by calling importlib.util.module_from_spec and capture module; why: the swap qualification test asserts the response, state, or failure produced by this call. + module = importlib.util.module_from_spec(spec) + # What: act by calling spec.loader.exec_module with module; why: the qualifier scenario observes the spec.loader.exec_module return value during return module. + spec.loader.exec_module(module) + # What: return module from the qualifier test helper; why: the qualifier scenario uses this helper result in its subsequent act or assertion. + return module + + +# What: apply pytest.fixture behavior to native_router_qualifier; why: Python attaches this named decorator's registration or descriptor semantics to native_router_qualifier. +@pytest.fixture +# What: define the native_router_qualifier test helper around captured fixture state; why: the native router qualifier scenario calls this helper to produce or observe the exact behavior checked by its assertions. +def native_router_qualifier(): + # What: act by calling Path and capture path; why: the swap qualification test asserts the response, state, or failure produced by this call. + path = Path(__file__).parents[2] / "benchmarks/swap/qualify_native_router.py" + # What: act by calling importlib.util.spec_from_file_location and capture spec; why: the swap qualification test asserts the response, state, or failure produced by this call. + spec = importlib.util.spec_from_file_location("native_router_qualifier", path) + # What: act by calling importlib.util.module_from_spec and capture module; why: the swap qualification test asserts the response, state, or failure produced by this call. + module = importlib.util.module_from_spec(spec) + # What: act by calling spec.loader.exec_module with module; why: the native router qualifier scenario observes the spec.loader.exec_module return value during return module. + spec.loader.exec_module(module) + # What: return module from the native_router_qualifier test helper; why: the native router qualifier scenario uses this helper result in its subsequent act or assertion. + return module + + +# What: define the stats test helper around active and instance and completed; why: the stats scenario calls this helper to produce or observe the exact behavior checked by its assertions. +def stats(active, *, instance="same", completed=3): + # What: arrange the instance id field as instance; why: stats carries instance id into return {"instance_id": instance, "requests": {"active": active, "complet. + return {"instance_id": instance, "requests": {"active": active, "completed": completed}} + + +# What: parameterize test_maintenance_qualifiers_require_exact_hostname_without_disclosure with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test maintenance qualifiers require exact hostname without disclosure. +@pytest.mark.parametrize("expected", ["", "other-host", "approved-host\x00suffix"]) +# What: define the test_maintenance_qualifiers_require_exact_hostname_without_disclosure test around qualifier and native router qualifier and expected; why: this test groups the arrange, act, and assertions that protect the maintenance qualifiers require exact hostname without disclosure outcome. +def test_maintenance_qualifiers_require_exact_hostname_without_disclosure( + # What: arrange the qualifier input for test_maintenance_qualifiers_require_exact_hostname_without_disclosure; why: test_maintenance_qualifiers_require_exact_hostname_without_disclosure consumes qualifier during for module in qualifier native router qualifier, so callers must bind it with the other signature input. + qualifier, native_router_qualifier, expected +# What: arrange the grouped source fragment for the scenario; why: test maintenance qualifiers require exact hostname without disclosure requires this concrete input or helper state before exercising the behavior under test. +): + # What: act across qualifier and native router qualifier to perform exc and raises and runtime error and require expected hostname and expected; why: the maintenance qualifiers require exact hostname without disclosure scenario repeats the body only while or for the loop header admits an iteration. + for module in (qualifier, native_router_qualifier): + # What: assert the pytest.raises failure context; why: the maintenance qualifiers require exact hostname without disclosure scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError, match="operator-supplied expected hostname") as exc: + # What: arrange the exact module require expected hostname expected actual approved host fixture fragment; why: the maintenance qualifiers require exact hostname without disclosure scenario feeds this byte-preserved fragment through module.require_expected_hostname(expected, actual="approved-host") before. + module.require_expected_hostname(expected, actual="approved-host") + # What: assert that approved host is absent from str exc value; why: this assertion protects the maintenance qualifiers require exact hostname without disclosure regression after the test's arranged inputs and exercised call. + assert "approved-host" not in str(exc.value) + # What: assert that expected not in str exc value or expected equals; why: this assertion protects the maintenance qualifiers require exact hostname without disclosure regression after the test's arranged inputs and exercised call. + assert expected not in str(exc.value) or expected == "" + + # What: assert the expected qualifier require expected hostname outcome; why: test swap qualification test maintenance qualifiers require exact hostname without disclosure protects its regression by requiring this observable result after the exercised behavior. + assert qualifier.require_expected_hostname( + # What: arrange approved host actual approved host for the scenario; why: test swap qualification test maintenance qualifiers require exact hostname without disclosure requires this concrete input or helper state before exercising the behavior under test. + "approved-host", actual="approved-host" + # What: arrange == approved host for the scenario; why: test swap qualification test maintenance qualifiers require exact hostname without disclosure requires this concrete input or helper state before exercising the behavior under test. + ) == "approved-host" + # What: assert the expected native router qualifier require expected hostname outcome; why: test swap qualification test maintenance qualifiers require exact hostname without disclosure protects its regression by requiring this observable result after the exercised behavior. + assert native_router_qualifier.require_expected_hostname( + # What: arrange approved host actual approved host for the scenario; why: test swap qualification test maintenance qualifiers require exact hostname without disclosure requires this concrete input or helper state before exercising the behavior under test. + "approved-host", actual="approved-host" + # What: arrange == approved host for the scenario; why: test swap qualification test maintenance qualifiers require exact hostname without disclosure requires this concrete input or helper state before exercising the behavior under test. + ) == "approved-host" + + +# What: define the test_maintenance_entrypoints_check_hostname_before_side_effects test around qualifier and native router qualifier and monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the maintenance entrypoints check hostname before side effects outcome. +def test_maintenance_entrypoints_check_hostname_before_side_effects( + # What: arrange the qualifier input for test_maintenance_entrypoints_check_hostname_before_side_effects; why: test_maintenance_entrypoints_check_hostname_before_side_effects consumes qualifier during qualifier, so callers must bind it with the other signature inputs. + qualifier, native_router_qualifier, monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test maintenance entrypoints check hostname before side effects requires this concrete input. +): + # What: arrange cases as qualifier and native router qualifier and source and source and python; why: the maintenance entrypoints check hostname before side effects test consumes this named precondition before exercising the behavior. + cases = ( + # What: arrange the cases collection with qualifier and source and source and python and python and; why: test_maintenance_entrypoints_check_hostname_before_side_effects groups the supplied clauses as one cases collection before its value. + ( + # What: arrange the qualifier portion of cases; why: the maintenance entrypoints check hostname before side effects scenario uses this clause to evaluate cases as one grouped value. + qualifier, + # What: arrange the cases collection with source and source and python and python; why: test_maintenance_entrypoints_check_hostname_before_side_effects groups the supplied clauses as one cases collection before its value. + [ + # What: arrange the source source python python llama swap llama swap portion of cases; why: the maintenance entrypoints check hostname before side effects scenario uses this clause to evaluate cases as one grouped value. + "--source", "source", "--python", "python", "--llama-swap", "llama-swap", + # What: arrange the model a a model b b portion of cases; why: the maintenance entrypoints check hostname before side effects scenario uses this clause to evaluate cases as one grouped value. + "--model-a", "a", "--model-b", "b", + # What: arrange the cases collection with source and source and python and python; why: test_maintenance_entrypoints_check_hostname_before_side_effects groups the supplied clauses as one cases collection before its value. + ], + # What: arrange the cases collection with qualifier and source and source and python and python and; why: test_maintenance_entrypoints_check_hostname_before_side_effects groups the supplied clauses as one cases collection before its value. + ), + # What: arrange the cases collection with native router qualifier and source and source and python and; why: test_maintenance_entrypoints_check_hostname_before_side_effects groups the supplied clauses as one cases collection before its value. + ( + # What: arrange the native router qualifier portion of cases; why: the maintenance entrypoints check hostname before side effects scenario uses this clause to evaluate cases as one grouped value. + native_router_qualifier, + # What: arrange the cases collection with source and source and python and python; why: test_maintenance_entrypoints_check_hostname_before_side_effects groups the supplied clauses as one cases collection before its value. + [ + # What: arrange the source source python python portion of cases; why: the maintenance entrypoints check hostname before side effects scenario uses this clause to evaluate cases as one grouped value. + "--source", "source", "--python", "python", + # What: arrange the model a a model b b portion of cases; why: the maintenance entrypoints check hostname before side effects scenario uses this clause to evaluate cases as one grouped value. + "--model-a", "a", "--model-b", "b", + # What: arrange the cases collection with source and source and python and python; why: test_maintenance_entrypoints_check_hostname_before_side_effects groups the supplied clauses as one cases collection before its value. + ], + # What: arrange the cases collection with native router qualifier and source and source and python and; why: test_maintenance_entrypoints_check_hostname_before_side_effects groups the supplied clauses as one cases collection before its value. + ), + # What: arrange the grouped source fragment for the scenario; why: test maintenance entrypoints check hostname before side effects requires this concrete input. + ) + # What: arrange the exact monkeypatch setattr native router qualifier sys platform linux fixture fragment; why: the maintenance entrypoints check hostname before side effects scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.sys, "platform", "linux") before asserting i. + monkeypatch.setattr(native_router_qualifier.sys, "platform", "linux") + # What: act across enumerate and cases to perform artifacts and tmp path and index; why: the maintenance entrypoints check hostname before side effects scenario repeats the body only while or for the loop header admits an iteration. + for index, (module, specific) in enumerate(cases): + # What: arrange artifacts as tmp path and index and must not exist; why: the maintenance entrypoints check hostname before side effects test consumes this named precondition before exercising the behavior. + artifacts = tmp_path / f"must-not-exist-{index}" + # What: act by calling str and capture argv; why: the maintenance entrypoints check hostname before side effects test asserts the response, state, or failure produced by this call. + argv = [ + # What: act by calling str with artifacts; why: the maintenance entrypoints check hostname before side effects scenario observes the str return value during protected service protected protected url http protected. + "qualifier", *specific, "--artifacts", str(artifacts), + # What: arrange the protected service protected protected url http protected portion of argv; why: the maintenance entrypoints check hostname before side effects scenario uses this clause to evaluate argv as one grouped value. + "--protected-service", "protected", "--protected-url", "http://protected", + # What: arrange the expected hostname expected host allow maintenance portion of argv; why: the maintenance entrypoints check hostname before side effects scenario uses this clause to evaluate argv as one grouped value. + "--expected-hostname", "expected-host", "--allow-maintenance", + # What: arrange the argv collection with qualifier and specific and artifacts and str and artifacts; why: test_maintenance_entrypoints_check_hostname_before_side_effects groups the supplied clauses as one argv collection before its value is consumed. + ] + # What: arrange the exact monkeypatch setattr module sys argv argv fixture fragment; why: the maintenance entrypoints check hostname before side effects scenario feeds this byte-preserved fragment through monkeypatch.setattr(module.sys, "argv", argv) before asserting its protocol or parser result. + monkeypatch.setattr(module.sys, "argv", argv) + # What: arrange the exact monkeypatch setattr module socket gethostname lambda different host fixture fragment; why: the maintenance entrypoints check hostname before side effects scenario feeds this byte-preserved fragment through monkeypatch.setattr(module.socket, "gethostname", lambda: "different-hos before. + monkeypatch.setattr(module.socket, "gethostname", lambda: "different-host") + # What: assert the pytest.raises failure context; why: the maintenance entrypoints check hostname before side effects scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError, match="operator-supplied expected hostname"): + # What: act by calling module.main with the declared inputs; why: the maintenance entrypoints check hostname before side effects scenario observes the module.main return value during assert not artifacts exists. + module.main() + # What: assert that artifacts exists is false; why: this assertion protects the maintenance entrypoints check hostname before side effects regression after the test's arranged inputs and exercised call. + assert not artifacts.exists() + + +# What: parameterize test_cancellation_requires_terminal_abort_without_restart with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test cancellation requires terminal abort without restart. +@pytest.mark.parametrize("outcome", ["abort", "restart", "completion", "already-done", "timeout"]) +# What: define the test_cancellation_requires_terminal_abort_without_restart test around qualifier and monkeypatch and outcome; why: this test groups the arrange, act, and assertions that protect the cancellation requires terminal abort without restart outcome. +def test_cancellation_requires_terminal_abort_without_restart(qualifier, monkeypatch, outcome): + # What: act by calling io.BytesIO and capture stream; why: the cancellation requires terminal abort without restart test asserts the response, state, or failure produced by this call. + stream = io.BytesIO(b'data: {"choices":[{"delta":{"content":"1"}}]}\n\n') + # What: arrange the exact monkeypatch setattr qualifier urllib request urlopen lambda a k fixture fragment; why: the cancellation requires terminal abort without restart scenario feeds this byte-preserved fragment through monkeypatch.setattr(qualifier.urllib.request, "urlopen", lambda *a, **k: before asserting its. + monkeypatch.setattr(qualifier.urllib.request, "urlopen", lambda *a, **k: stream) + # What: act by calling stats and capture during; why: the cancellation requires terminal abort without restart test asserts the response, state, or failure produced by this call. + during = stats(0 if outcome == "already-done" else 1) + # What: act by calling stats and capture after; why: the cancellation requires terminal abort without restart test asserts the response, state, or failure produced by this call. + after = stats(1 if outcome == "timeout" else 0, + # What: arrange instance to stats; why: the cancellation requires terminal abort without restart scenario binds this outcome and new and same and restart value to stats's instance input. + instance="new" if outcome == "restart" else "same", + # What: arrange completed to stats; why: the cancellation requires terminal abort without restart scenario binds this outcome and 4 and 3 and completion value to stats's completed input. + completed=4 if outcome == "completion" else 3) + # What: act by calling iter and capture snapshots; why: the cancellation requires terminal abort without restart test asserts the response, state, or failure produced by this call. + snapshots = iter([stats(0), during, after]) + # What: arrange the exact monkeypatch setattr qualifier http lambda a k fixture fragment; why: the cancellation requires terminal abort without restart scenario feeds this byte-preserved fragment through monkeypatch.setattr(qualifier, "http", lambda *a, **k: json.dumps(next(s before asserting its protocol or parse. + monkeypatch.setattr(qualifier, "http", lambda *a, **k: json.dumps(next(snapshots)).encode()) + # What: act on outcome before prefix and evidence and cancellation canary and qualifier; why: the cancellation requires terminal abort without restart scenario admits prefix and evidence and cancellation canary and qualifier only for this predicate and excludes the opposite state. + if outcome == "abort": + # What: act by calling qualifier.cancellation_canary and capture prefix and evidence; why: the cancellation requires terminal abort without restart test asserts the response, state, or failure produced by this call. + prefix, evidence = qualifier.cancellation_canary("http://test", "model-a", seconds=0) + # What: assert that evidence passed; why: this assertion protects the cancellation requires terminal abort without restart regression after the test's arranged inputs and exercised call. + assert evidence["passed"] + # What: assert that b content 1 is present in prefix; why: this assertion protects the cancellation requires terminal abort without restart regression after the test's arranged inputs and exercised call. + assert b'"content":"1"' in prefix + # What: act on outcome before raises and timeout error and cancellation canary and pytest and qualifier; why: the cancellation requires terminal abort without restart scenario admits raises and timeout error and cancellation canary and pytest and qualifier only for this predicate and excludes the opposite state. + elif outcome == "timeout": + # What: arrange with pytest raises TimeoutError match terminal abort for the scenario; why: test raises timeout error match terminal abort requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(TimeoutError, match="terminal abort"): + # What: arrange the exact qualifier cancellation canary http test model a seconds fixture fragment; why: the cancellation requires terminal abort without restart scenario feeds this byte-preserved fragment through qualifier.cancellation_canary("http://test", "model-a", seconds=0) before asserting its proto. + qualifier.cancellation_canary("http://test", "model-a", seconds=0) + # What: select the remaining branch that performs with pytest raises assertion error; why: test_cancellation_requires_terminal_abort_without_restart covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: arrange with pytest raises AssertionError for the scenario; why: test raises assertion error requires this concrete input or helper state before exercising the behavior under test. + with pytest.raises(AssertionError): + # What: arrange the exact qualifier cancellation canary http test model a seconds fixture fragment; why: the cancellation requires terminal abort without restart scenario feeds this byte-preserved fragment through qualifier.cancellation_canary("http://test", "model-a", seconds=0) before asserting its proto. + qualifier.cancellation_canary("http://test", "model-a", seconds=0) + # What: assert that stream closed; why: this assertion protects the cancellation requires terminal abort without restart regression after the test's arranged inputs and exercised call. + assert stream.closed + + +# What: parameterize test_completed_or_empty_stream_is_not_cancellation with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test completed or empty stream is not cancellation. +@pytest.mark.parametrize("body", [b"data: [DONE]\n\n", b"", b": heartbeat\n\n"]) +# What: define the test_completed_or_empty_stream_is_not_cancellation test around qualifier and monkeypatch and body; why: this test groups the arrange, act, and assertions that protect the completed or empty stream is not cancellation outcome. +def test_completed_or_empty_stream_is_not_cancellation(qualifier, monkeypatch, body): + # What: act by calling io.BytesIO and capture stream; why: the completed or empty stream is not cancellation test asserts the response, state, or failure produced by this call. + stream = io.BytesIO(body) + # What: arrange the exact monkeypatch setattr qualifier urllib request urlopen lambda a k fixture fragment; why: the completed or empty stream is not cancellation scenario feeds this byte-preserved fragment through monkeypatch.setattr(qualifier.urllib.request, "urlopen", lambda *a, **k: before asserting its protoc. + monkeypatch.setattr(qualifier.urllib.request, "urlopen", lambda *a, **k: stream) + # What: arrange the exact monkeypatch setattr qualifier http lambda a k fixture fragment; why: the completed or empty stream is not cancellation scenario feeds this byte-preserved fragment through monkeypatch.setattr(qualifier, "http", lambda *a, **k: json.dumps(stats( before asserting its protocol or parser resul. + monkeypatch.setattr(qualifier, "http", lambda *a, **k: json.dumps(stats(0)).encode()) + # What: assert the pytest.raises failure context; why: the completed or empty stream is not cancellation scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError): + # What: arrange the exact qualifier cancellation canary http test model a fixture fragment; why: the completed or empty stream is not cancellation scenario feeds this byte-preserved fragment through qualifier.cancellation_canary("http://test", "model-a") before asserting its protocol or parser result. + qualifier.cancellation_canary("http://test", "model-a") + # What: assert that stream closed; why: this assertion protects the completed or empty stream is not cancellation regression after the test's arranged inputs and exercised call. + assert stream.closed + + +# What: define the test_cancellation_closes_real_local_http_stream test around qualifier; why: this test groups the arrange, act, and assertions that protect the cancellation closes real local http stream outcome. +def test_cancellation_closes_real_local_http_stream(qualifier): + """Exercise the HTTP transport too, using a CPU-only streaming backend.""" + # What: document exercise the http transport too using in the test_cancellation_closes_real_local_http_stream docstring; why: introspection and maintainers read this exact docstring fragment to understand test cancellation closes real local http stream behavior without executing it. + # What: arrange state as active and 0; why: the cancellation closes real local http stream test consumes this named precondition before exercising the behavior. + state = {"active": 0} + # What: act by calling threading.Event and capture disconnected; why: the cancellation closes real local http stream test asserts the response, state, or failure produced by this call. + disconnected = threading.Event() + + # What: define Handler as the owner of log_message and do_GET and do_POST; why: daemon callers use this class boundary so those methods share one handler state invariant. + class Handler(BaseHTTPRequestHandler): + # What: define the log_message test helper around captured fixture state; why: the cancellation closes real local http stream scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def log_message(self, *args): + # What: ignore the anticipated exception handled by this branch; why: log_message continues its retry or cleanup path instead of re-raising that transient failure. + pass + + # What: define the do_GET test helper around captured fixture state; why: the cancellation closes real local http stream scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def do_GET(self): + # What: act by calling operation.encode and capture body; why: the cancellation closes real local http stream test asserts the response, state, or failure produced by this call. + body = json.dumps(stats(state["active"])).encode() + # What: act by calling self.send_response with 200; why: the cancellation closes real local http stream scenario observes the self.send_response return value during self send header content length str len body. + self.send_response(200) + # What: arrange the exact self send header content length str len body fixture fragment; why: the cancellation closes real local http stream scenario feeds this byte-preserved fragment through self.send_header("Content-Length", str(len(body))) before asserting its protocol or parser result. + self.send_header("Content-Length", str(len(body))) + # What: act by calling self.end_headers with the declared inputs; why: the cancellation closes real local http stream scenario observes the self.end_headers return value during self wfile write body. + self.end_headers() + # What: act by calling self.wfile.write with body; why: the cancellation closes real local http stream scenario observes the self.wfile.write return value during the enclosing return. + self.wfile.write(body) + + # What: define the do_POST test helper around captured fixture state; why: the cancellation closes real local http stream scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def do_POST(self): + # What: arrange the exact self rfile read int self headers content length fixture fragment; why: the cancellation closes real local http stream scenario feeds this byte-preserved fragment through self.rfile.read(int(self.headers["Content-Length"])) before asserting its protocol or parser result. + self.rfile.read(int(self.headers["Content-Length"])) + # What: arrange state entry as 1; why: the cancellation closes real local http stream test consumes this named precondition before exercising the behavior. + state["active"] = 1 + # What: act by calling self.send_response with 200; why: the cancellation closes real local http stream scenario observes the self.send_response return value during self send header content type text event stream. + self.send_response(200) + # What: arrange the exact self send header content type text event stream fixture fragment; why: the cancellation closes real local http stream scenario feeds this byte-preserved fragment through self.send_header("Content-Type", "text/event-stream") before asserting its protocol or parser result. + self.send_header("Content-Type", "text/event-stream") + # What: act by calling self.end_headers with the declared inputs; why: the cancellation closes real local http stream scenario observes the self.end_headers return value during try. + self.end_headers() + # What: establish the handler boundary for the protected operation; why: Handler.do_POST routes failures to broken pipe error and connection reset error and connection aborted error while preserving cleanup and success flow. + try: + # What: act by calling time.monotonic and capture deadline; why: the cancellation closes real local http stream test asserts the response, state, or failure produced by this call. + deadline = time.monotonic() + 5 + # What: act across deadline and monotonic and time to perform write and wfile; why: the cancellation closes real local http stream scenario repeats the body only while or for the loop header admits an iteration. + while time.monotonic() < deadline: + # What: act by calling self.wfile.write with the named fixture input; why: the cancellation closes real local http stream scenario observes the self.wfile.write return value during self wfile flush. + self.wfile.write(b'data: {"choices":[{"delta":{"content":"1"}}]}\n\n') + # What: act by calling self.wfile.flush with the declared inputs; why: the cancellation closes real local http stream scenario observes the self.wfile.flush return value during time sleep. + self.wfile.flush() + # What: act by calling time.sleep with 0 01; why: the cancellation closes real local http stream scenario observes the time.sleep return value during except broken pipe error connection reset error connection aborted error. + time.sleep(0.01) + # What: handle broken pipe error and connection reset error and connection aborted error by disconnected set; why: Handler.do_POST converts that failure into this concrete recovery, response, or cleanup behavior. + except (BrokenPipeError, ConnectionResetError, ConnectionAbortedError): + # What: act by calling disconnected.set with the declared inputs; why: the cancellation closes real local http stream scenario observes the disconnected.set return value during state active. + disconnected.set() + # What: arrange state entry as 0; why: the cancellation closes real local http stream test consumes this named precondition before exercising the behavior. + state["active"] = 0 + + # What: act by calling ThreadingHTTPServer and capture server; why: the cancellation closes real local http stream test asserts the response, state, or failure produced by this call. + server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + # What: act by calling threading.Thread and capture worker; why: the cancellation closes real local http stream test asserts the response, state, or failure produced by this call. + worker = threading.Thread(target=server.serve_forever, daemon=True) + # What: act by calling worker.start with the declared inputs; why: the cancellation closes real local http stream scenario observes the worker.start return value during try. + worker.start() + # What: establish the handler boundary for the protected operation; why: test_cancellation_closes_real_local_http_stream routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: act by calling qualifier.cancellation_canary and capture and evidence; why: the cancellation closes real local http stream test asserts the response, state, or failure produced by this call. + _, evidence = qualifier.cancellation_canary( + # What: arrange seconds to qualifier.cancellation_canary; why: the cancellation closes real local http stream scenario binds this 3 value to qualifier.cancellation_canary's seconds input. + f"http://127.0.0.1:{server.server_port}", "model-a", seconds=3 + # What: arrange the qualifier.cancellation_canary call with seconds; why: test_cancellation_closes_real_local_http_stream groups the supplied clauses as one qualifier.cancellation_canary call before its value is consumed. + ) + # What: assert that evidence passed and disconnected is set; why: this assertion protects the cancellation closes real local http stream regression after the test's arranged inputs and exercised call. + assert evidence["passed"] and disconnected.is_set() + # What: run server shutdown on every exit path; why: test_cancellation_closes_real_local_http_stream performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act by calling server.shutdown with the declared inputs; why: the cancellation closes real local http stream scenario observes the server.shutdown return value during server server close. + server.shutdown() + # What: act by calling server.server_close with the declared inputs; why: the cancellation closes real local http stream scenario observes the server.server_close return value during worker join. + server.server_close() + # What: act by calling worker.join with 3; why: the cancellation closes real local http stream scenario observes the worker.join return value during the enclosing return. + worker.join(3) + + +# What: define the test_native_router_benchmark_canary_records_first_byte_and_preserves_sse test around native router qualifier and monkeypatch; why: this test groups the arrange, act, and assertions that protect the native router benchmark canary records first byte and preserves sse outcome. +def test_native_router_benchmark_canary_records_first_byte_and_preserves_sse(native_router_qualifier, monkeypatch): + # What: act by calling io.BytesIO and capture stream; why: the native router benchmark canary records first byte and preserves sse test asserts the response, state, or failure produced by this call. + stream = io.BytesIO( + # What: arrange the b data choices delta content n portion of stream; why: the native router benchmark canary records first byte and preserves sse scenario uses this clause to evaluate stream as one grouped value. + b'data: {"choices":[{"delta":{"content":"4"}}]}\n\n' + # What: arrange the b data choices usage completion tokens n portion of stream; why: the native router benchmark canary records first byte and preserves sse scenario uses this clause to evaluate stream as one grouped value. + b'data: {"choices":[],"usage":{"completion_tokens":1}}\n\n' + # What: arrange the b data done n n portion of stream; why: the native router benchmark canary records first byte and preserves sse scenario uses this clause to evaluate stream as one grouped value. + b"data: [DONE]\n\n" + # What: arrange the io.BytesIO call with ordered positional inputs; why: test_native_router_benchmark_canary_records_first_byte_and_preserves_sse groups the supplied clauses as one io.BytesIO call before its value is consumed. + ) + # What: arrange the exact monkeypatch setattr native router qualifier urllib request urlopen lambda a k fixture f; why: the native router benchmark canary records first byte and preserves sse scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen". + monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", lambda *a, **k: stream) + # What: act by calling iter and capture clock; why: the native router benchmark canary records first byte and preserves sse test asserts the response, state, or failure produced by this call. + clock = iter([10.0, 10.25, 11.25]) + # What: arrange the exact monkeypatch setattr native router qualifier time monotonic lambda next clock fixture fr; why: the native router benchmark canary records first byte and preserves sse scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.time, "monotonic", lambda. + monkeypatch.setattr(native_router_qualifier.time, "monotonic", lambda: next(clock)) + + # What: act by calling native_router_qualifier.canary and capture raw and observation; why: the native router benchmark canary records first byte and preserves sse test asserts the response, state, or failure produced by this call. + raw, observation = native_router_qualifier.canary("http://test", "model-a", direct=False) + + # What: assert that raw endswith b data done n n; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert raw.endswith(b"data: [DONE]\n\n") + # What: assert that observation route equals native router; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["route"] == "native_router" + # What: assert that observation model equals model a; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["model"] == "model-a" + # What: assert that observation response model is group delimiter; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["responseModel"] is None + # What: assert that observation passed is true; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["passed"] is True + # What: assert that observation first byte seconds is not group delimiter; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["firstByteSeconds"] is not None + # What: assert that observation first token seconds equals observation first byte seconds; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["firstTokenSeconds"] == observation["firstByteSeconds"] + # What: assert that observation duration seconds is at least observation first byte seconds; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["durationSeconds"] >= observation["firstByteSeconds"] + # What: assert that observation decode seconds equals 1 0; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["decodeSeconds"] == 1.0 + # What: assert that observation completion tokens equals 1; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["completionTokens"] == 1 + # What: assert that observation completion tokens per second equals 1 0; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["completionTokensPerSecond"] == 1.0 + # What: assert that observation response bytes equals len raw; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert observation["responseBytes"] == len(raw) + # What: assert that stream closed; why: this assertion protects the native router benchmark canary records first byte and preserves sse regression after the test's arranged inputs and exercised call. + assert stream.closed + + +# What: parameterize test_native_router_loading_feedback_gate with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test native router loading feedback gate. +@pytest.mark.parametrize("expected", [True, False]) +# What: define the test_native_router_loading_feedback_gate test around native router qualifier and expected; why: this test groups the arrange, act, and assertions that protect the native router loading feedback gate outcome. +def test_native_router_loading_feedback_gate(native_router_qualifier, expected): + # What: arrange frames as the fixture input; why: the native router loading feedback gate test consumes this named precondition before exercising the behavior. + frames = [ + # What: arrange the b data choices delta reasoning content freetoken swap portion of frames; why: the native router loading feedback gate scenario uses this clause to evaluate frames as one grouped value. + b'data: {"choices":[{"delta":{"reasoning_content":"freetoken-swap "}}]}', + # What: arrange the b data choices delta reasoning content loading portion of frames; why: the native router loading feedback gate scenario uses this clause to evaluate frames as one grouped value. + b'data: {"choices":[{"delta":{"reasoning_content":"loading model: model-b"}}]}', + # What: arrange the b data choices delta content portion of frames; why: the native router loading feedback gate scenario uses this clause to evaluate frames as one grouped value. + b'data: {"choices":[{"delta":{"content":"4"}}]}', + # What: arrange the b data done portion of frames; why: the native router loading feedback gate scenario uses this clause to evaluate frames as one grouped value. + b"data: [DONE]", + # What: arrange the frames collection with the named fixture input and the named fixture input and the named fixture input and the named fixture input; why: test_native_router_loading_feedback_gate groups the supplied clauses as one frames collection before its value is consumed. + ] + # What: act by calling operation.join and capture raw; why: the native router loading feedback gate test asserts the response, state, or failure produced by this call. + raw = b"\n\n".join(frames[2:] if not expected else frames) + b"\n\n" + # What: assert the expected native router qualifier validate loading feedback raw expected expected == outcome; why: test native router loading feedb protects its regression by requiring this observable result after the exercised behavior. + assert native_router_qualifier.validate_loading_feedback(raw, expected=expected) == { + # What: arrange expected expected observed expected passed True for the scenario; why: test swap qualification test native router loading feedback gate requires this concrete input or helper state before exercising the behavior under test. + "expected": expected, "observed": expected, "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router loading feedback gate requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert the pytest.raises failure context; why: the native router loading feedback gate scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError, match="loading feedback"): + # What: arrange expected to native_router_qualifier.validate_loading_feedback; why: the native router loading feedback gate scenario binds this expected value to native_router_qualifier.validate_loading_feedback's expected input. + native_router_qualifier.validate_loading_feedback(raw, expected=not expected) + + +# What: define the test_native_router_canary_separates_loading_first_byte_from_first_token test around native router qualifier and monkeypatch; why: this test groups the arrange, act, and assertions that protect the native router canary separates loading first byte from first token outcome. +def test_native_router_canary_separates_loading_first_byte_from_first_token( + # What: arrange native router qualifier monkeypatch for the scenario; why: test native router canary separates loading first byte from first token requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch +# What: arrange the grouped source fragment for the scenario; why: test native router canary separates loading first byte from first token requires this concrete input or helper state before exercising the behavior under test. +): + # What: act by calling io.BytesIO and capture stream; why: the native router canary separates loading first byte from first token test asserts the response, state, or failure produced by this call. + stream = io.BytesIO( + # What: arrange the b data choices delta reasoning content freetoken swap portion of stream; why: the native router canary separates loading first byte from first token scenario uses this clause to evaluate stream as one grouped value. + b'data: {"choices":[{"delta":{"reasoning_content":"freetoken-swap loading model: a"}}]}\n\n' + # What: arrange the b data choices delta content n portion of stream; why: the native router canary separates loading first byte from first token scenario uses this clause to evaluate stream as one grouped value. + b'data: {"choices":[{"delta":{"content":"4"}}]}\n\n' + # What: arrange the b data choices usage completion tokens n portion of stream; why: the native router canary separates loading first byte from first token scenario uses this clause to evaluate stream as one grouped value. + b'data: {"choices":[],"usage":{"completion_tokens":1}}\n\n' + # What: arrange the b data done n n portion of stream; why: the native router canary separates loading first byte from first token scenario uses this clause to evaluate stream as one grouped value. + b"data: [DONE]\n\n" + # What: arrange the io.BytesIO call with ordered positional inputs; why: test_native_router_canary_separates_loading_first_byte_from_first_token groups the supplied clauses as one io.BytesIO call before its value is consumed. + ) + # What: arrange the exact monkeypatch setattr native router qualifier urllib request urlopen lambda a k fixture f; why: the native router canary separates loading first byte from first token scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", l. + monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", lambda *a, **k: stream) + # What: act by calling iter and capture clock; why: the native router canary separates loading first byte from first token test asserts the response, state, or failure produced by this call. + clock = iter([10.0, 10.1, 15.0, 16.0]) + # What: arrange the exact monkeypatch setattr native router qualifier time monotonic lambda next clock fixture fr; why: the native router canary separates loading first byte from first token scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.time, "monotonic", lambda: n. + monkeypatch.setattr(native_router_qualifier.time, "monotonic", lambda: next(clock)) + + # What: act by calling native_router_qualifier.canary and capture and observation; why: the native router canary separates loading first byte from first token test asserts the response, state, or failure produced by this call. + _, observation = native_router_qualifier.canary("http://test", "model-a", direct=False) + + # What: assert that observation first byte seconds equals pytest approx 0 1; why: this assertion protects the native router canary separates loading first byte from first token regression after the test's arranged inputs and exercised call. + assert observation["firstByteSeconds"] == pytest.approx(0.1) + # What: assert that observation first token seconds equals 5 0; why: this assertion protects the native router canary separates loading first byte from first token regression after the test's arranged inputs and exercised call. + assert observation["firstTokenSeconds"] == 5.0 + # What: assert that observation decode seconds equals 1 0; why: this assertion protects the native router canary separates loading first byte from first token regression after the test's arranged inputs and exercised call. + assert observation["decodeSeconds"] == 1.0 + # What: assert that observation completion tokens per second equals 1 0; why: this assertion protects the native router canary separates loading first byte from first token regression after the test's arranged inputs and exercised call. + assert observation["completionTokensPerSecond"] == 1.0 + + +# What: define the test_native_router_benchmark_rejects_nonterminal_or_wrong_answer_streams test around native router qualifier and monkeypatch; why: this test groups the arrange, act, and assertions that protect the native router benchmark rejects nonterminal or wrong answer streams outcome. +def test_native_router_benchmark_rejects_nonterminal_or_wrong_answer_streams(native_router_qualifier, monkeypatch): + # What: act by calling io.BytesIO and capture stream; why: the native router benchmark rejects nonterminal or wrong answer streams test asserts the response, state, or failure produced by this call. + stream = io.BytesIO(b'data: {"choices":[{"delta":{"content":"5"}}]}\n\n') + # What: arrange the exact monkeypatch setattr native router qualifier urllib request urlopen lambda a k fixture f; why: the native router benchmark rejects nonterminal or wrong answer streams scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen". + monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", lambda *a, **k: stream) + + # What: assert the pytest.raises failure context; why: the native router benchmark rejects nonterminal or wrong answer streams scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError): + # What: arrange the exact native router qualifier canary http test model a direct fixture fragment; why: the native router benchmark rejects nonterminal or wrong answer streams scenario feeds this byte-preserved fragment through native_router_qualifier.canary("http://test", "model-a", direct=True) before asser. + native_router_qualifier.canary("http://test", "model-a", direct=True) + # What: assert that stream closed; why: this assertion protects the native router benchmark rejects nonterminal or wrong answer streams regression after the test's arranged inputs and exercised call. + assert stream.closed + + +# What: define the test_native_router_benchmark_rejects_completed_stream_without_usage test around native router qualifier and monkeypatch; why: this test groups the arrange, act, and assertions that protect the native router benchmark rejects completed stream without usage outcome. +def test_native_router_benchmark_rejects_completed_stream_without_usage(native_router_qualifier, monkeypatch): + # What: act by calling io.BytesIO and capture stream; why: the native router benchmark rejects completed stream without usage test asserts the response, state, or failure produced by this call. + stream = io.BytesIO( + # What: arrange the b data choices delta content n portion of stream; why: the native router benchmark rejects completed stream without usage scenario uses this clause to evaluate stream as one grouped value. + b'data: {"choices":[{"delta":{"content":"4"}}]}\n\n' + # What: arrange the b data done n n portion of stream; why: the native router benchmark rejects completed stream without usage scenario uses this clause to evaluate stream as one grouped value. + b"data: [DONE]\n\n" + # What: arrange the io.BytesIO call with ordered positional inputs; why: test_native_router_benchmark_rejects_completed_stream_without_usage groups the supplied clauses as one io.BytesIO call before its value is consumed. + ) + # What: arrange the exact monkeypatch setattr native router qualifier urllib request urlopen lambda a k fixture f; why: the native router benchmark rejects completed stream without usage scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", l bef. + monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", lambda *a, **k: stream) + + # What: assert the pytest.raises failure context; why: the native router benchmark rejects completed stream without usage scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError, match="usage missing"): + # What: arrange the exact native router qualifier canary http test model a direct fixture fragment; why: the native router benchmark rejects completed stream without usage scenario feeds this byte-preserved fragment through native_router_qualifier.canary("http://test", "model-a", direct=True) before asserting. + native_router_qualifier.canary("http://test", "model-a", direct=True) + # What: assert that stream closed; why: this assertion protects the native router benchmark rejects completed stream without usage regression after the test's arranged inputs and exercised call. + assert stream.closed + + +# What: define the test_native_router_reload_conflict_canary_preserves_active_identity test around native router qualifier and monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the native router reload conflict canary preserves active identity outcome. +def test_native_router_reload_conflict_canary_preserves_active_identity( + # What: arrange native router qualifier monkeypatch tmp path for the scenario; why: test native router reload conflict canary preserves active identity requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test native router reload conflict canary preserves active identity requires this concrete input or helper state before exercising the behavior under test. +): + # What: define the request_json test helper around url and body; why: the native router reload conflict canary preserves active identity scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request_json(url, body=None, **kwargs): + # What: act on endswith and url before httperror and url and error and bytes io and urllib; why: the native router reload conflict canary preserves active identity scenario admits httperror and url and error and bytes io and urllib only for this predicate and excludes the opposite state. + if url.endswith("/router/reload"): + # What: arrange the helper to raise raise native router qualifier urllib error HTTPError url 409 conflict io BytesIO; why: test swap qualification test exercises the concrete failure path rather than a successful substitute. + raise native_router_qualifier.urllib.error.HTTPError(url, 409, "conflict", {}, io.BytesIO()) + # What: assert that url endswith router status; why: this assertion protects the native router reload conflict canary preserves active identity regression after the test's arranged inputs and exercised call. + assert url.endswith("/router/status") + # What: arrange the helper response as b activeProfile model a activeIdentityMatchesEngine True; why: test native router reload conflict canary pre feeds this result into the behavior whose outcome is asserted. + return b"{}", {"activeProfile": "model-a", "activeIdentityMatchesEngine": True} + + # What: arrange the exact monkeypatch setattr native router qualifier request json request json fixture fragment; why: the native router reload conflict canary preserves active identity scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "request_json", request_jso befo. + monkeypatch.setattr(native_router_qualifier, "request_json", request_json) + # What: arrange catalog as tmp path and models and toml; why: the native router reload conflict canary preserves active identity test consumes this named precondition before exercising the behavior. + catalog = tmp_path / "models.toml" + # What: act by calling native_router_qualifier.reload_conflict_canary and capture observation; why: the native router reload conflict canary preserves active identity test asserts the response, state, or failure produced by this call. + observation = native_router_qualifier.reload_conflict_canary( + # What: arrange the http test catalog private a gguf private portion of observation; why: the native router reload conflict canary preserves active identity scenario uses this clause to evaluate observation as one grouped value. + "http://test", catalog, "/private/a.gguf", "/private/b.gguf" + # What: arrange the native_router_qualifier.reload_conflict_canary call with catalog; why: test_native_router_reload_conflict_canary_preserves_active_identity groups the supplied clauses as one native_router_qualifier.reload_conflict_canary call before its value is consumed. + ) + + # What: assert the expected observation == outcome; why: test swap qualification test native router reload conflict canary preserves active identity protects its regression by requiring this observable result after the exercised behavior. + assert observation == { + # What: arrange activeProfile model a rejectedStatus 409 for the scenario; why: test swap qualification test native router reload conflict canary preserves active identity requires this concrete input or helper state before exercising the behavior under test. + "activeProfile": "model-a", "rejectedStatus": 409, + # What: arrange activeIdentityPreserved True passed True for the scenario; why: test swap qualification test native router reload conflict canary preserves active identity requires this concrete input or helper state before exercising the behavior under test. + "activeIdentityPreserved": True, "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router reload conflict canary preserves active identity requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that priority 1 is present in catalog read text encoding utf 8; why: this assertion protects the native router reload conflict canary preserves active identity regression after the test's arranged inputs and exercised call. + assert "priority = 1" in catalog.read_text(encoding="utf-8") + + +# What: define the test_native_router_failed_switch_canary_requires_rollback_and_restored_completion test around native router qualifier and monkeypatch; why: this test groups the arrange, act, and assertions that protect the native router failed switch canary requires rollback and restored completion outcome. +def test_native_router_failed_switch_canary_requires_rollback_and_restored_completion( + # What: arrange native router qualifier monkeypatch for the scenario; why: test native router failed switch canary requires rollback and restored completion requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch +# What: arrange the grouped source fragment for the scenario; why: test native router failed switch canary requires rollback and restored completion requires this concrete input or helper state. +): + # What: act by calling iter and capture statuses; why: the native router failed switch canary requires rollback and restored completion test asserts the response, state, or failure produced by this call. + statuses = iter(( + # What: arrange the grouped source fragment for the scenario; why: test native router failed switch canary requires rollback and restored completion requires this concrete input or helper. + { + # What: arrange the active profile field as model a; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion carries active profile through statuses into return b next statuses. + "activeProfile": "model-a", "activeIdentityMatchesEngine": True, + # What: arrange activeRequests 0 activationFailures 3 for the scenario; why: test swap qualification test native router failed switch canary requires rollback and restored completion requires this concrete input or helper state before exercising the behavior under test. + "activeRequests": 0, "activationFailures": 3, + # What: arrange the grouped source fragment for the scenario; why: test native router failed switch canary requires rollback and restored completion requires this concrete input or. + }, + # What: arrange the grouped source fragment for the scenario; why: test native router failed switch canary requires rollback and restored completion requires this concrete input or helper. + { + # What: arrange the active profile field as model a; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion carries active profile through statuses into return b next statuses. + "activeProfile": "model-a", "activeIdentityMatchesEngine": True, + # What: arrange activeRequests 0 activationFailures 4 for the scenario; why: test swap qualification test native router failed switch canary requires rollback and restored completion requires this concrete input or helper state before exercising the behavior under test. + "activeRequests": 0, "activationFailures": 4, + # What: arrange the grouped source fragment for the scenario; why: test native router failed switch canary requires rollback and restored completion requires this concrete input or. + }, + # What: arrange the iter call with ordered positional inputs; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion groups the supplied clauses as one iter call before its value is consumed. + )) + # What: act by calling operation.encode and capture failure; why: the native router failed switch canary requires rollback and restored completion test asserts the response, state, or failure produced by this call. + failure = json.dumps({ + # What: arrange the type field as engine not ready; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion carries type through failure into url 503 unavailable io bytes io failure. + "error": {"type": "engine_not_ready", "message": "private failure"}, + # What: arrange the launched field as true; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion carries launched through failure into url 503 unavailable io bytes io failure. + "recovery": {"launched": True}, + # What: arrange the encode portion of failure; why: the native router failed switch canary requires rollback and restored completion scenario uses this clause to evaluate failure as one grouped value. + }).encode() + # What: act by calling iter and capture pending; why: the native router failed switch canary requires rollback and restored completion test asserts the response, state, or failure produced by this call. + pending = iter(( + # What: arrange the receipts field as receipt id and existing receipt; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion carries receipts through pending into if url endswith accounting pending. + {"receipts": [{"receiptId": "existing-receipt"}]}, + # What: arrange the receipts field as receipt id and existing receipt and receipt id and failed switch receipt; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion carries receipts through pending into if url endswith accounting pending. + {"receipts": [ + # What: arrange the receipt id field as existing receipt; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion carries receipt id through pending into if url endswith accounting pending. + {"receiptId": "existing-receipt"}, + # What: arrange the receipt id field as failed switch receipt; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion carries receipt id through pending into if url endswith accounting pending. + {"receiptId": "failed-switch-receipt"}, + # What: arrange the pending mapping with receipts; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion groups the supplied clauses as one pending mapping before its value is consumed. + ]}, + # What: arrange the iter call with ordered positional inputs; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion groups the supplied clauses as one iter call before its value is consumed. + )) + + # What: define the request_json test helper around url and body; why: the native router failed switch canary requires rollback and restored completion scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request_json(url, body=None, **kwargs): + # What: act on endswith and url before next and statuses; why: the native router failed switch canary requires rollback and restored completion scenario admits next and statuses only for this predicate and excludes the opposite state. + if url.endswith("/router/status"): + # What: return next and statuses from the request_json test helper; why: the native router failed switch canary requires rollback and restored completion scenario uses this helper result in its subsequent act or assertion. + return b"{}", next(statuses) + # What: act on endswith and url before next and pending; why: the native router failed switch canary requires rollback and restored completion scenario admits next and pending only for this predicate and excludes the opposite state. + if url.endswith("/accounting/pending"): + # What: return next and pending from the request_json test helper; why: the native router failed switch canary requires rollback and restored completion scenario uses this helper result in its subsequent act or assertion. + return b"{}", next(pending) + # What: assert that url endswith router load and body equals name model invalid; why: this assertion protects the native router failed switch canary requires rollback and restored completion regression after the test's arranged inputs and exercised call. + assert url.endswith("/router/load") and body == {"name": "model-invalid"} + # What: arrange the helper to raise raise native router qualifier urllib error HTTPError; why: test native router failed switch canary exercises the concrete failure path rather than a successful substitute. + raise native_router_qualifier.urllib.error.HTTPError( + # What: act by calling io.BytesIO with failure; why: the native router failed switch canary requires rollback and restored completion scenario observes the io.BytesIO return value while evaluating url, 503, "unavailable", {}, io.BytesIO(failure). + url, 503, "unavailable", {}, io.BytesIO(failure) + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router failed switch canary requires rollback and. + ) + + # What: arrange the exact monkeypatch setattr native router qualifier request json request json fixture fragment; why: the native router failed switch canary requires rollback and restored completion scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "request_json", re. + monkeypatch.setattr(native_router_qualifier, "request_json", request_json) + # What: act by calling monkeypatch.setattr with native router qualifier and canary and passed and true; why: the native router failed switch canary requires rollback and restored completion scenario observes the monkeypatch.setattr return value during native router qualifier canary. + monkeypatch.setattr( + # What: arrange the exact native router qualifier canary fixture fragment; why: the native router failed switch canary requires rollback and restored completion scenario feeds this byte-preserved fragment through native_router_qualifier, "canary" before asserting its protocol or parser result. + native_router_qualifier, "canary", + # What: arrange the passed field as true; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion carries passed into lambda base, model, *, direct: (b"data: [DONE]\n\n", {"passed": True}). + lambda base, model, *, direct: (b"data: [DONE]\n\n", {"passed": True}), + # What: arrange the monkeypatch.setattr call with native router qualifier; why: test_native_router_failed_switch_canary_requires_rollback_and_restored_completion groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: act by calling native_router_qualifier.failed_switch_canary and capture failure raw and restored raw and observation; why: the native router failed switch canary requires rollback and restored completion test asserts the response, state, or failure produced by this call. + failure_raw, restored_raw, observation = native_router_qualifier.failed_switch_canary( + # What: arrange http test model invalid model a for the scenario; why: test model invalid model a in test requires this concrete input or helper state before exercising the behavior under test. + "http://test", "model-invalid", "model-a" + # What: arrange the grouped source fragment for the scenario; why: test native router failed switch canary requires rollback and restored completion requires this concrete input or helper. + ) + + # What: assert that failure raw equals failure; why: this assertion protects the native router failed switch canary requires rollback and restored completion regression after the test's arranged inputs and exercised call. + assert failure_raw == failure + # What: assert that restored raw equals b data done n n; why: this assertion protects the native router failed switch canary requires rollback and restored completion regression after the test's arranged inputs and exercised call. + assert restored_raw == b"data: [DONE]\n\n" + # What: assert the expected observation == outcome; why: test swap qualification test native router failed switch canary requires rollback and restored completion protects its regression by requiring this observable result after the exercised behavior. + assert observation == { + # What: arrange failedProfile model invalid restoredProfile model a for the scenario; why: test swap qualification test native router failed switch canary requires rollback and restored completion requires this concrete input or helper state before exercising the behavior under test. + "failedProfile": "model-invalid", "restoredProfile": "model-a", + # What: arrange failureType engine not ready rollbackLaunched True for the scenario; why: test swap qualification test native router failed switch canary requires rollback and restored completion requires this concrete input or helper state before exercising the behavior under test. + "failureType": "engine_not_ready", "rollbackLaunched": True, + # What: arrange activationFailureIncremented True newAccountingReceiptCount 1 for the scenario; why: test swap qualification test requires this concrete input or helper state before exercising the behavior under test. + "activationFailureIncremented": True, "newAccountingReceiptCount": 1, + # What: arrange restoredCompletionPassed True for the scenario; why: test swap qualification test native router failed switch canary requires rollback and restored completion requires this concrete input or helper state before exercising the behavior under test. + "restoredCompletionPassed": True, + # What: arrange passed True for the scenario; why: test swap qualification test native router failed switch canary requires rollback and restored completion requires this concrete input or helper state before exercising the behavior under test. + "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router failed switch canary requires rollback and. + } + + +# What: define the test_native_router_ttl_canary_reloads_temporary_catalog_and_closes_listener test around native router qualifier and monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the native router ttl canary reloads temporary catalog and closes listener outcome. +def test_native_router_ttl_canary_reloads_temporary_catalog_and_closes_listener( + # What: arrange native router qualifier monkeypatch tmp path for the scenario; why: test native router ttl canary reloads temporary requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test native router ttl canary reloads temporary catalog and closes listener requires this concrete input or helper state before exercising the behavior under test. +): + # What: act by calling iter and capture statuses; why: the native router ttl canary reloads temporary catalog and closes listener test asserts the response, state, or failure produced by this call. + statuses = iter(( + # What: arrange the evictions field as 2; why: test_native_router_ttl_canary_reloads_temporary_catalog_and_closes_listener carries evictions through statuses into return b next statuses. + {"evictions": 2, "activeProfile": "model-a"}, + # What: arrange the evictions field as 3; why: test_native_router_ttl_canary_reloads_temporary_catalog_and_closes_listener carries evictions through statuses into return b next statuses. + {"evictions": 3, "activeProfile": None}, + # What: arrange the iter call with ordered positional inputs; why: test_native_router_ttl_canary_reloads_temporary_catalog_and_closes_listener groups the supplied clauses as one iter call before its value is consumed. + )) + + # What: define the request_json test helper around url and body; why: the native router ttl canary reloads temporary catalog and closes listener scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request_json(url, body=None, **kwargs): + # What: act on endswith and url before next and statuses; why: the native router ttl canary reloads temporary catalog and closes listener scenario admits next and statuses only for this predicate and excludes the opposite state. + if url.endswith("/router/status"): + # What: return next and statuses from the request_json test helper; why: the native router ttl canary reloads temporary catalog and closes listener scenario uses this helper result in its subsequent act or assertion. + return b"{}", next(statuses) + # What: arrange if url endswith router unload for the scenario; why: test swap qualification test native router ttl canary reloads temporary catalog and closes listener requires this concrete input or helper state before exercising the behavior under test. + if url.endswith("/router/unload"): + # What: arrange the helper response as b unloaded true unloaded True; why: test swap qualification test native router ttl canary reloads temporary catalog and closes listener feeds this result into the behavior whose outcome is asserted. + return b'{"unloaded":true}', {"unloaded": True} + # What: arrange if url endswith router reload for the scenario; why: test swap qualification test native router ttl canary reloads temporary catalog and closes listener requires this concrete input or helper state before exercising the behavior under test. + if url.endswith("/router/reload"): + # What: arrange the helper response as b reloaded true reloaded True; why: test swap qualification test native router ttl canary reloads temporary catalog and closes listener feeds this result into the behavior whose outcome is asserted. + return b'{"reloaded":true}', {"reloaded": True} + # What: assert that url endswith router load and body equals name model a; why: this assertion protects the native router ttl canary reloads temporary catalog and closes listener regression after the test's arranged inputs and exercised call. + assert url.endswith("/router/load") and body == {"name": "model-a"} + # What: arrange the helper response as b profile model a port 24567 profile model a port 24567; why: test native router ttl canary reloads temporary feeds this result into the behavior whose outcome is asserted. + return b'{"profile":"model-a","port":24567}', {"profile": "model-a", "port": 24567} + + # What: arrange closed as the fixture input; why: the native router ttl canary reloads temporary catalog and closes listener test consumes this named precondition before exercising the behavior. + closed = [] + # What: arrange the exact monkeypatch setattr native router qualifier request json request json fixture fragment; why: the native router ttl canary reloads temporary catalog and closes listener scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "request_json", request_. + monkeypatch.setattr(native_router_qualifier, "request_json", request_json) + # What: arrange monkeypatch setattr native router qualifier require listener closed closed append for the scenario; why: test native router ttl canary reloads temporary catalog and closes listener requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr(native_router_qualifier, "require_listener_closed", closed.append) + # What: arrange catalog as tmp path and models and toml; why: the native router ttl canary reloads temporary catalog and closes listener test consumes this named precondition before exercising the behavior. + catalog = tmp_path / "models.toml" + # What: act by calling native_router_qualifier.ttl_eviction_canary and capture observation; why: the native router ttl canary reloads temporary catalog and closes listener test asserts the response, state, or failure produced by this call. + observation = native_router_qualifier.ttl_eviction_canary( + # What: arrange seconds to native_router_qualifier.ttl_eviction_canary; why: the native router ttl canary reloads temporary catalog and closes listener scenario binds this 1 value to native_router_qualifier.ttl_eviction_canary's seconds input. + "http://test", catalog, "/private/a.gguf", "/private/b.gguf", seconds=1 + # What: arrange the native_router_qualifier.ttl_eviction_canary call with seconds; why: test_native_router_ttl_canary_reloads_temporary_catalog_and_closes_listener groups the supplied clauses as one native_router_qualifier.ttl_eviction_canary call before its value is consumed. + ) + + # What: assert that closed equals 24567; why: this assertion protects the native router ttl canary reloads temporary catalog and closes listener regression after the test's arranged inputs and exercised call. + assert closed == [24567] + # What: assert the expected observation == outcome; why: test swap qualification test native router ttl canary reloads temporary catalog and closes listener protects its regression by requiring this observable result after the exercised behavior. + assert observation == { + # What: arrange profile model a ttlSeconds 2 port 24567 for the scenario; why: test swap qualification test native router ttl canary reloads temporary catalog and closes listener requires this concrete input or helper state before exercising the behavior under test. + "profile": "model-a", "ttlSeconds": 2, "port": 24567, + # What: arrange evictionIncremented True listenerClosed True passed True for the scenario; why: test swap qualification test native router ttl canary reloads temporary catalog and closes listener requires this concrete input or helper state before exercising the behavior under test. + "evictionIncremented": True, "listenerClosed": True, "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router ttl canary reloads temporary catalog and closes listener requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that ttl s 2 is present in catalog read text encoding utf 8; why: this assertion protects the native router ttl canary reloads temporary catalog and closes listener regression after the test's arranged inputs and exercised call. + assert "ttl_s = 2" in catalog.read_text(encoding="utf-8") + + +# What: define the test_native_router_persistent_capacity_canary_requires_release_before_switch test around native router qualifier and monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the native router persistent capacity canary requires release before switch outcome. +def test_native_router_persistent_capacity_canary_requires_release_before_switch( + # What: arrange native router qualifier monkeypatch tmp path for the scenario; why: test native router persistent capacity canary requires release before switch requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test native router persistent capacity canary requires release before switch requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange calls as model b and 0; why: the native router persistent capacity canary requires release before switch test consumes this named precondition before exercising the behavior. + calls = {"model-b": 0} + # What: arrange rejection as the fixture input; why: the native router persistent capacity canary requires release before switch test consumes this named precondition before exercising the behavior. + rejection = b'{"error":{"type":"capacity_unavailable"}}' + + # What: define the request_json test helper around url and body; why: the native router persistent capacity canary requires release before switch scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request_json(url, body=None, **kwargs): + # What: arrange if url endswith router unload for the scenario; why: test swap qualification test native router persistent capacity canary requires release before switch requires this concrete input or helper state before exercising the behavior under test. + if url.endswith("/router/unload"): + # What: arrange the unloaded field as true; why: request_json carries unloaded into return b"{}", {"unloaded": True}. + return b"{}", {"unloaded": True} + # What: arrange if url endswith router reload for the scenario; why: test swap qualification test native router persistent capacity canary requires release before switch requires this concrete input or helper state before exercising the behavior under test. + if url.endswith("/router/reload"): + # What: arrange the reloaded field as true; why: request_json carries reloaded into return b"{}", {"reloaded": True}. + return b"{}", {"reloaded": True} + # What: arrange if url endswith router status for the scenario; why: test swap qualification test native router persistent capacity canary requires release before switch requires this concrete input or helper state before exercising the behavior under test. + if url.endswith("/router/status"): + # What: return active profile and active identity matches engine and persistent and model a and true from the request_json test helper; why: the native router persistent capacity canary requires release before switch scenario uses this helper result in its subsequent act or assertion. + return b"{}", { + # What: arrange activeProfile model a activeIdentityMatchesEngine True for the scenario; why: test swap qualification test requires this concrete input or helper state before exercising the behavior under test. + "activeProfile": "model-a", "activeIdentityMatchesEngine": True, + # What: arrange the persistent field as true; why: request_json carries persistent into "persistent": True. + "persistent": True, + # What: arrange the enclosing predicate collection with the named fixture input and active profile and active identity matches engine and persistent and model a and true; why: request_json groups the supplied clauses as one request_json expression collection before its value is consumed. + } + # What: arrange if url endswith engine status for the scenario; why: test swap qualification test native router persistent capacity canary requires release before switch requires this concrete input or helper state before exercising the behavior under test. + if url.endswith("/engine/status"): + # What: arrange the pid field as 71; why: request_json carries pid into return b"{}", {"pid": 71}. + return b"{}", {"pid": 71} + # What: assert that url endswith router load; why: this assertion protects the native router persistent capacity canary requires release before switch regression after the test's arranged inputs and exercised call. + assert url.endswith("/router/load") + # What: arrange the name field as model a; why: request_json carries name into if body == {"name": "model-a"}. + if body == {"name": "model-a"}: + # What: return profile and pid and router and model a and 71 from the request_json test helper; why: the native router persistent capacity canary requires release before switch scenario uses this helper result in its subsequent act or assertion. + return b"{}", { + # What: arrange the profile field as model a; why: request_json carries profile into "profile": "model-a", "pid": 71. + "profile": "model-a", "pid": 71, + # What: arrange router persistent True activeIdentityMatchesEngine True for the scenario; why: test swap qualification test native router persistent capacity canary requires release before switch requires this concrete input or helper state before exercising the behavior under test. + "router": {"persistent": True, "activeIdentityMatchesEngine": True}, + # What: arrange the enclosing predicate collection with the named fixture input and profile and pid and router and model a and 71; why: request_json groups the supplied clauses as one request_json expression collection before its value is consumed. + } + # What: arrange calls entry from 1; why: the native router persistent capacity canary requires release before switch scenario uses calls entry during if calls model b before checking the protected result. + calls["model-b"] += 1 + # What: act on calls before httperror and url and error and bytes io and rejection; why: the native router persistent capacity canary requires release before switch scenario admits httperror and url and error and bytes io and rejection only for this predicate and excludes the opposite state. + if calls["model-b"] == 1: + # What: arrange the helper to raise raise native router qualifier urllib error HTTPError; why: test swap qualification test exercises the concrete failure path rather than a successful substitute. + raise native_router_qualifier.urllib.error.HTTPError( + # What: act by calling io.BytesIO with rejection; why: the native router persistent capacity canary requires release before switch scenario observes the io.BytesIO return value while evaluating url, 409, "capacity", {}, io.BytesIO(rejection). + url, 409, "capacity", {}, io.BytesIO(rejection) + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router persistent capacity canary requires release before switch requires this concrete input or helper state before exercising the behavior under test. + ) + # What: return profile and pid and router and model b and 72 from the request_json test helper; why: the native router persistent capacity canary requires release before switch scenario uses this helper result in its subsequent act or assertion. + return b"{}", { + # What: arrange the profile field as model b; why: request_json carries profile into "profile": "model-b", "pid": 72. + "profile": "model-b", "pid": 72, + # What: arrange the active identity matches engine field as true; why: request_json carries active identity matches engine into "router": {"activeIdentityMatchesEngine": True}. + "router": {"activeIdentityMatchesEngine": True}, + # What: arrange the enclosing predicate collection with the named fixture input and profile and pid and router and model b and 72; why: request_json groups the supplied clauses as one request_json expression collection before its value is consumed. + } + + # What: arrange the exact monkeypatch setattr native router qualifier request json request json fixture fragment; why: the native router persistent capacity canary requires release before switch scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "request_json", request. + monkeypatch.setattr(native_router_qualifier, "request_json", request_json) + # What: arrange catalog as tmp path and models and toml; why: the native router persistent capacity canary requires release before switch test consumes this named precondition before exercising the behavior. + catalog = tmp_path / "models.toml" + # What: act by calling native_router_qualifier.persistent_capacity_canary and capture raw and observation; why: the native router persistent capacity canary requires release before switch test asserts the response, state, or failure produced by this call. + raw, observation = native_router_qualifier.persistent_capacity_canary( + # What: arrange the http test catalog a gguf b gguf portion of raw and observation; why: the native router persistent capacity canary requires release before switch scenario uses this clause to evaluate raw and observation as one grouped value. + "http://test", catalog, "a.gguf", "b.gguf" + # What: arrange the native_router_qualifier.persistent_capacity_canary call with catalog; why: test_native_router_persistent_capacity_canary_requires_release_before_switch groups the supplied clauses as one native_router_qualifier.persistent_capacity_canary call before its value is consumed. + ) + + # What: assert that raw equals rejection; why: this assertion protects the native router persistent capacity canary requires release before switch regression after the test's arranged inputs and exercised call. + assert raw == rejection + # What: assert that observation passed is true; why: this assertion protects the native router persistent capacity canary requires release before switch regression after the test's arranged inputs and exercised call. + assert observation["passed"] is True + # What: assert that observation resident pid preserved is true; why: this assertion protects the native router persistent capacity canary requires release before switch regression after the test's arranged inputs and exercised call. + assert observation["residentPidPreserved"] is True + # What: act by calling ModelCatalog.load and capture parsed; why: the native router persistent capacity canary requires release before switch test asserts the response, state, or failure produced by this call. + parsed = ModelCatalog.load(str(catalog)) + # What: assert that parsed group for model a persistent is true; why: this assertion protects the native router persistent capacity canary requires release before switch regression after the test's arranged inputs and exercised call. + assert parsed.group_for("model-a").persistent is True + + +# What: define the test_native_router_concurrent_canaries_require_same_residency test around native router qualifier and monkeypatch; why: this test groups the arrange, act, and assertions that protect the native router concurrent canaries require same residency outcome. +def test_native_router_concurrent_canaries_require_same_residency(native_router_qualifier, monkeypatch): + # What: act by calling iter and capture snapshots; why: the native router concurrent canaries require same residency test asserts the response, state, or failure produced by this call. + snapshots = iter(( + # What: arrange the active profile field as model a; why: test_native_router_concurrent_canaries_require_same_residency carries active profile through snapshots into monkeypatch setattr native router qualifier request json lambda a k b. + {"activeProfile": "model-a", "activations": 4, "activeRequests": 0}, + # What: arrange the active profile field as model a; why: test_native_router_concurrent_canaries_require_same_residency carries active profile through snapshots into monkeypatch setattr native router qualifier request json lambda a k b. + {"activeProfile": "model-a", "activations": 4, "activeRequests": 0}, + # What: arrange the iter call with ordered positional inputs; why: test_native_router_concurrent_canaries_require_same_residency groups the supplied clauses as one iter call before its value is consumed. + )) + # What: arrange the exact monkeypatch setattr native router qualifier request json lambda a k fixture fragment; why: the native router concurrent canaries require same residency scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "request_json", lambda *a, before assert. + monkeypatch.setattr(native_router_qualifier, "request_json", lambda *a, **k: (b"{}", next(snapshots))) + + # What: define the fake_canary test helper around base and model and direct; why: the native router concurrent canaries require same residency scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def fake_canary(base, model, *, direct): + # What: assert that base equals http test and model equals model a and direct; why: this assertion protects the native router concurrent canaries require same residency regression after the test's arranged inputs and exercised call. + assert base == "http://test" and model == "model-a" and direct is False + # What: act by calling time.sleep with 0 01; why: the native router concurrent canaries require same residency scenario observes the time.sleep return value during return b data done n n. + time.sleep(0.01) + # What: arrange the passed field as true; why: fake_canary carries passed into return b"data: [DONE]\n\n", {"passed": True}. + return b"data: [DONE]\n\n", {"passed": True} + + # What: arrange the exact monkeypatch setattr native router qualifier canary fake canary fixture fragment; why: the native router concurrent canaries require same residency scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "canary", fake_canary) before asserting its p. + monkeypatch.setattr(native_router_qualifier, "canary", fake_canary) + # What: act by calling native_router_qualifier.concurrent_canaries and capture rows and observation; why: the native router concurrent canaries require same residency test asserts the response, state, or failure produced by this call. + rows, observation = native_router_qualifier.concurrent_canaries("http://test", "model-a", seconds=2) + + # What: assert that len rows equals 2; why: this assertion protects the native router concurrent canaries require same residency regression after the test's arranged inputs and exercised call. + assert len(rows) == 2 + # What: assert the expected observation == outcome; why: test swap qualification test native router concurrent canaries require same residency protects its regression by requiring this observable result after the exercised behavior. + assert observation == { + # What: arrange route native router for the scenario; why: test swap qualification test native router concurrent canaries require same residency requires this concrete input or helper state before exercising the behavior under test. + "route": "native_router", + # What: arrange model model a for the scenario; why: test swap qualification test native router concurrent canaries require same residency requires this concrete input or helper state before exercising the behavior under test. + "model": "model-a", + # What: arrange requests 2 for the scenario; why: test swap qualification test native router concurrent canaries require same residency requires this concrete input or helper state before exercising the behavior under test. + "requests": 2, + # What: arrange activationDelta 0 for the scenario; why: test swap qualification test native router concurrent canaries require same residency requires this concrete input or helper state before exercising the behavior under test. + "activationDelta": 0, + # What: arrange activeRequestsAfter 0 for the scenario; why: test swap qualification test native router concurrent canaries require same residency requires this concrete input or helper state before exercising the behavior under test. + "activeRequestsAfter": 0, + # What: arrange passed True for the scenario; why: test swap qualification test native router concurrent canaries require same residency requires this concrete input or helper state before exercising the behavior under test. + "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router concurrent canaries require same residency requires this concrete input or helper state before exercising the behavior under test. + } + + +# What: define the test_native_router_conflicting_request_canary_queues_then_switches test around native router qualifier and monkeypatch; why: this test groups the arrange, act, and assertions that protect the native router conflicting request canary queues then switches outcome. +def test_native_router_conflicting_request_canary_queues_then_switches( + # What: arrange native router qualifier monkeypatch for the scenario; why: test native router conflicting request canary queues then switches requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch +# What: arrange the grouped source fragment for the scenario; why: test native router conflicting request canary queues then switches requires this concrete input or helper state before exercising the behavior. +): + # What: act by calling threading.Event and capture cancelled; why: the native router conflicting request canary queues then switches test asserts the response, state, or failure produced by this call. + cancelled = threading.Event() + # What: act by calling iter and capture statuses; why: the native router conflicting request canary queues then switches test asserts the response, state, or failure produced by this call. + statuses = iter(( + # What: arrange activeProfile model a activations 10 for the scenario; why: test swap qualification test native router conflicting request canary queues then switches requires this concrete input or helper state before exercising the behavior under test. + {"activeProfile": "model-a", "activations": 10}, + # What: arrange the grouped source fragment for the scenario; why: test native router conflicting request canary queues then switches requires this concrete input or helper state before exercising the. + { + # What: arrange activeProfile model a activations 10 for the scenario; why: test swap qualification test native router conflicting request canary queues then switches requires this concrete input or helper state before exercising the behavior under test. + "activeProfile": "model-a", "activations": 10, + # What: arrange the queued requests field as 1; why: test_native_router_conflicting_request_canary_queues_then_switches carries queued requests through statuses into return b next statuses. + "queuedRequests": 1, "activeRequests": 1, + # What: arrange the active identity matches engine field as true; why: test_native_router_conflicting_request_canary_queues_then_switches carries active identity matches engine through statuses into return b next statuses. + "activeIdentityMatchesEngine": True, + # What: arrange the grouped source fragment for the scenario; why: test native router conflicting request canary queues then switches requires this concrete input or helper state before exercising. + }, + # What: arrange the active profile field as model b; why: test_native_router_conflicting_request_canary_queues_then_switches carries active profile through statuses into return b next statuses. + {"activeProfile": "model-b", "activations": 11, "activeRequests": 0}, + # What: arrange activeProfile model a activations 12 activeRequests 0 for the scenario; why: test swap qualification test native router conflicting request canary queues then switches requires this concrete input or helper state before exercising the behavior under test. + {"activeProfile": "model-a", "activations": 12, "activeRequests": 0}, + # What: arrange the iter call with ordered positional inputs; why: test_native_router_conflicting_request_canary_queues_then_switches groups the supplied clauses as one iter call before its value is consumed. + )) + + # What: define ActiveResponse as the owner of __enter__ and __exit__ and __iter__; why: daemon callers use this class boundary so those methods share one active response state invariant. + class ActiveResponse: + # What: define the __enter__ test helper around captured fixture state; why: the native router conflicting request canary queues then switches scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def __enter__(self): + # What: return no value from the __enter__ test helper; why: the native router conflicting request canary queues then switches scenario uses this helper result in its subsequent act or assertion. + return self + + # What: define the __exit__ test helper around captured fixture state; why: the native router conflicting request canary queues then switches scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def __exit__(self, *args): + # What: ignore the anticipated exception handled by this branch; why: __exit__ continues its retry or cleanup path instead of re-raising that transient failure. + pass + + # What: define the __iter__ test helper around captured fixture state; why: the native router conflicting request canary queues then switches scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def __iter__(self): + # What: arrange the yield b data choices delta content portion of the enclosing predicate; why: this clause remains in the native router conflicting request canary queues then switches scenario\'s enclosing expression so its grouping and evaluation order stay intact. + yield b'data: {"choices":[{"delta":{"content":"1"}}]}\n\n' + # What: act by calling cancelled.wait with 2; why: the native router conflicting request canary queues then switches scenario observes the cancelled.wait return value during the enclosing return. + cancelled.wait(2) + + # What: define the request_json test helper around url and body; why: the native router conflicting request canary queues then switches scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request_json(url, body=None, **kwargs): + # What: act on endswith and url before next and statuses; why: the native router conflicting request canary queues then switches scenario admits next and statuses only for this predicate and excludes the opposite state. + if url.endswith("/router/status"): + # What: return next and statuses from the request_json test helper; why: the native router conflicting request canary queues then switches scenario uses this helper result in its subsequent act or assertion. + return b"{}", next(statuses) + # What: assert that url endswith router requests native qualification conflict cancel; why: this assertion protects the native router conflicting request canary queues then switches regression after the test's arranged inputs and exercised call. + assert url.endswith("/router/requests/native-qualification-conflict/cancel") + # What: act by calling cancelled.set with the declared inputs; why: the native router conflicting request canary queues then switches scenario observes the cancelled.set return value during return b cancelled id native qualification conflict. + cancelled.set() + # What: arrange the cancelled field as true; why: request_json carries cancelled into return b"{}", {"cancelled": True, "id": "native-qualification-conflict"}. + return b"{}", {"cancelled": True, "id": "native-qualification-conflict"} + + # What: arrange canary calls as the fixture input; why: the native router conflicting request canary queues then switches test consumes this named precondition before exercising the behavior. + canary_calls = [] + + # What: define the fake_canary test helper around base and model and direct; why: the native router conflicting request canary queues then switches scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def fake_canary(base, model, *, direct): + # What: act by calling canary_calls.append with model; why: the native router conflicting request canary queues then switches scenario observes the canary_calls.append return value during if model model b. + canary_calls.append(model) + # What: act on model before wait and cancelled; why: the native router conflicting request canary queues then switches scenario admits wait and cancelled only for this predicate and excludes the opposite state. + if model == "model-b": + # What: act by calling cancelled.wait with 2; why: the native router conflicting request canary queues then switches scenario observes the cancelled.wait return value during return f data model n ndata. + cancelled.wait(2) + # What: arrange the helper response as f data model n ndata DONE n n encode passed True; why: test swap qualification test native router conflicting request canary queues then switches feeds this result into the behavior whose outcome is asserted. + return f"data: {model}\n\ndata: [DONE]\n\n".encode(), {"passed": True} + + # What: arrange the exact monkeypatch setattr native router qualifier request json request json fixture fragment; why: the native router conflicting request canary queues then switches scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "request_json", request_jso befor. + monkeypatch.setattr(native_router_qualifier, "request_json", request_json) + # What: arrange the exact monkeypatch setattr native router qualifier urllib request urlopen lambda a k fixture f; why: the native router conflicting request canary queues then switches scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", l befo. + monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", lambda *a, **k: ActiveResponse()) + # What: arrange the exact monkeypatch setattr native router qualifier canary fake canary fixture fragment; why: the native router conflicting request canary queues then switches scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "canary", fake_canary) before asserting. + monkeypatch.setattr(native_router_qualifier, "canary", fake_canary) + + # What: act by calling native_router_qualifier.conflicting_request_canary and capture active and waiting and restored and observation; why: the native router conflicting request canary queues then switches test asserts the response, state, or failure produced by this call. + active, waiting, restored, observation = native_router_qualifier.conflicting_request_canary( + # What: arrange seconds to native_router_qualifier.conflicting_request_canary; why: the native router conflicting request canary queues then switches scenario binds this 2 value to native_router_qualifier.conflicting_request_canary's seconds input. + "http://test", "model-a", "model-b", seconds=2 + # What: arrange the native_router_qualifier.conflicting_request_canary call with seconds; why: test_native_router_conflicting_request_canary_queues_then_switches groups the supplied clauses as one native_router_qualifier.conflicting_request_canary call before its value is consumed. + ) + + # What: assert that b data done is absent from active; why: this assertion protects the native router conflicting request canary queues then switches regression after the test's arranged inputs and exercised call. + assert b"data: [DONE]" not in active + # What: assert that b model b in waiting and b model a in restored; why: this assertion protects the native router conflicting request canary queues then switches regression after the test's arranged inputs and exercised call. + assert b"model-b" in waiting and b"model-a" in restored + # What: assert that canary calls equals model b model a; why: this assertion protects the native router conflicting request canary queues then switches regression after the test's arranged inputs and exercised call. + assert canary_calls == ["model-b", "model-a"] + # What: assert that observation queued behind active is true; why: this assertion protects the native router conflicting request canary queues then switches regression after the test's arranged inputs and exercised call. + assert observation["queuedBehindActive"] is True + # What: assert that observation activation delta equals 2; why: this assertion protects the native router conflicting request canary queues then switches regression after the test's arranged inputs and exercised call. + assert observation["activationDelta"] == 2 + # What: assert that observation passed is true; why: this assertion protects the native router conflicting request canary queues then switches regression after the test's arranged inputs and exercised call. + assert observation["passed"] is True + + +# What: define the test_native_router_cancellation_canary_requires_idle_without_completion_credit test around native router qualifier; why: this test groups the arrange, act, and assertions that protect the native router cancellation canary requires idle without completion credit outcome. +def test_native_router_cancellation_canary_requires_idle_without_completion_credit(native_router_qualifier): + # What: arrange state as active and cancellations and terminal and 0 and 0; why: the native router cancellation canary requires idle without completion credit test consumes this named precondition before exercising the behavior. + state = {"active": 0, "cancellations": 0, "terminal": 0} + # What: act by calling threading.Event and capture cancelled; why: the native router cancellation canary requires idle without completion credit test asserts the response, state, or failure produced by this call. + cancelled = threading.Event() + + # What: define Handler as the owner of log_message and _json and do_GET and do_POST; why: daemon callers use this class boundary so those methods share one handler state invariant. + class Handler(BaseHTTPRequestHandler): + # What: define the log_message test helper around captured fixture state; why: the native router cancellation canary requires idle without completion credit scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def log_message(self, *args): + # What: ignore the anticipated exception handled by this branch; why: log_message continues its retry or cleanup path instead of re-raising that transient failure. + pass + + # What: define the _json test helper around body; why: the native router cancellation canary requires idle without completion credit scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def _json(self, body): + # What: act by calling operation.encode and capture raw; why: the native router cancellation canary requires idle without completion credit test asserts the response, state, or failure produced by this call. + raw = json.dumps(body).encode() + # What: act by calling self.send_response with 200; why: the native router cancellation canary requires idle without completion credit scenario observes the self.send_response return value during self send header content type application json. + self.send_response(200) + # What: arrange the exact self send header content type application json fixture fragment; why: the native router cancellation canary requires idle without completion credit scenario feeds this byte-preserved fragment through self.send_header("Content-Type", "application/json") before asserting its protoco. + self.send_header("Content-Type", "application/json") + # What: arrange the exact self send header content length str len raw fixture fragment; why: the native router cancellation canary requires idle without completion credit scenario feeds this byte-preserved fragment through self.send_header("Content-Length", str(len(raw))) before asserting its protocol or p. + self.send_header("Content-Length", str(len(raw))) + # What: act by calling self.end_headers with the declared inputs; why: the native router cancellation canary requires idle without completion credit scenario observes the self.end_headers return value during self wfile write raw. + self.end_headers() + # What: act by calling self.wfile.write with raw; why: the native router cancellation canary requires idle without completion credit scenario observes the self.wfile.write return value during the enclosing return. + self.wfile.write(raw) + + # What: define the do_GET test helper around captured fixture state; why: the native router cancellation canary requires idle without completion credit scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def do_GET(self): + # What: assert that self path equals router status; why: this assertion protects the native router cancellation canary requires idle without completion credit regression after the test's arranged inputs and exercised call. + assert self.path == "/router/status" + # What: act by calling self._json with state and active requests and cancellations and terminal streams and active; why: the native router cancellation canary requires idle without completion credit scenario observes the self._json return value during active requests state active. + self._json({ + # What: arrange the active requests field as state and active; why: Handler.do_GET carries active requests into "activeRequests": state["active"]. + "activeRequests": state["active"], + # What: arrange the cancellations field as state and cancellations; why: Handler.do_GET carries cancellations into "cancellations": state["cancellations"]. + "cancellations": state["cancellations"], + # What: arrange the terminal streams field as state and terminal; why: Handler.do_GET carries terminal streams into "terminalStreams": state["terminal"]. + "terminalStreams": state["terminal"], + # What: arrange the self._json call with state; why: Handler.do_GET groups the supplied clauses as one self._json call before its value is consumed. + }) + + # What: define the do_POST test helper around captured fixture state; why: the native router cancellation canary requires idle without completion credit scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def do_POST(self): + # What: arrange the exact self rfile read int self headers get content length fixture fragment; why: the native router cancellation canary requires idle without completion credit scenario feeds this byte-preserved fragment through self.rfile.read(int(self.headers.get("Content-Length", "0"))) before asserti. + self.rfile.read(int(self.headers.get("Content-Length", "0"))) + # What: act on path before state; why: the native router cancellation canary requires idle without completion credit scenario admits state only for this predicate and excludes the opposite state. + if self.path == "/v1/chat/completions": + # What: arrange state entry as 1; why: the native router cancellation canary requires idle without completion credit test consumes this named precondition before exercising the behavior. + state["active"] = 1 + # What: act by calling self.send_response with 200; why: the native router cancellation canary requires idle without completion credit scenario observes the self.send_response return value during self send header content type text event stream. + self.send_response(200) + # What: arrange the exact self send header content type text event stream fixture fragment; why: the native router cancellation canary requires idle without completion credit scenario feeds this byte-preserved fragment through self.send_header("Content-Type", "text/event-stream") before asserting its p. + self.send_header("Content-Type", "text/event-stream") + # What: act by calling self.end_headers with the declared inputs; why: the native router cancellation canary requires idle without completion credit scenario observes the self.end_headers return value during self wfile write b data choices delta content. + self.end_headers() + # What: act by calling self.wfile.write with the named fixture input; why: the native router cancellation canary requires idle without completion credit scenario observes the self.wfile.write return value during self wfile flush. + self.wfile.write(b'data: {"choices":[{"delta":{"content":"1"}}]}\n\n') + # What: act by calling self.wfile.flush with the declared inputs; why: the native router cancellation canary requires idle without completion credit scenario observes the self.wfile.flush return value during cancelled wait. + self.wfile.flush() + # What: act by calling cancelled.wait with 3; why: the native router cancellation canary requires idle without completion credit scenario observes the cancelled.wait return value during state active. + cancelled.wait(3) + # What: arrange state entry as 0; why: the native router cancellation canary requires idle without completion credit test consumes this named precondition before exercising the behavior. + state["active"] = 0 + # What: return no value from the do_POST test helper; why: the native router cancellation canary requires idle without completion credit scenario uses this helper result in its subsequent act or assertion. + return + # What: assert that self path equals router requests native qualification cancel cancel; why: this assertion protects the native router cancellation canary requires idle without completion credit regression after the test's arranged inputs and exercised call. + assert self.path == "/router/requests/native-qualification-cancel/cancel" + # What: arrange state entry from 1; why: the native router cancellation canary requires idle without completion credit scenario uses state entry during the enclosing return or state update before checking the protected result. + state["cancellations"] += 1 + # What: act by calling cancelled.set with the declared inputs; why: the native router cancellation canary requires idle without completion credit scenario observes the cancelled.set return value during self json cancelled id native qualification cancel. + cancelled.set() + # What: arrange the cancelled field as true; why: Handler.do_POST carries cancelled into self._json({"cancelled": True, "id": "native-qualification-cancel"}). + self._json({"cancelled": True, "id": "native-qualification-cancel"}) + + # What: act by calling ThreadingHTTPServer and capture server; why: the native router cancellation canary requires idle without completion credit test asserts the response, state, or failure produced by this call. + server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + # What: act by calling threading.Thread and capture worker; why: the native router cancellation canary requires idle without completion credit test asserts the response, state, or failure produced by this call. + worker = threading.Thread(target=server.serve_forever, daemon=True) + # What: act by calling worker.start with the declared inputs; why: the native router cancellation canary requires idle without completion credit scenario observes the worker.start return value during try. + worker.start() + # What: establish the handler boundary for the protected operation; why: test_native_router_cancellation_canary_requires_idle_without_completion_credit routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: act by calling native_router_qualifier.cancellation_canary and capture raw and observation; why: the native router cancellation canary requires idle without completion credit test asserts the response, state, or failure produced by this call. + raw, observation = native_router_qualifier.cancellation_canary( + # What: arrange seconds to native_router_qualifier.cancellation_canary; why: the native router cancellation canary requires idle without completion credit scenario binds this 3 value to native_router_qualifier.cancellation_canary's seconds input. + f"http://127.0.0.1:{server.server_port}", "model-a", seconds=3 + # What: arrange the native_router_qualifier.cancellation_canary call with seconds; why: test_native_router_cancellation_canary_requires_idle_without_completion_credit groups the supplied clauses as one native_router_qualifier.cancellation_canary call before its value is consumed. + ) + # What: assert that b content 1 is present in raw; why: this assertion protects the native router cancellation canary requires idle without completion credit regression after the test's arranged inputs and exercised call. + assert b'"content":"1"' in raw + # What: assert that b data done is absent from raw; why: this assertion protects the native router cancellation canary requires idle without completion credit regression after the test's arranged inputs and exercised call. + assert b"data: [DONE]" not in raw + # What: assert that observation passed is true; why: this assertion protects the native router cancellation canary requires idle without completion credit regression after the test's arranged inputs and exercised call. + assert observation["passed"] is True + # What: assert that observation cancellation incremented is true; why: this assertion protects the native router cancellation canary requires idle without completion credit regression after the test's arranged inputs and exercised call. + assert observation["cancellationIncremented"] is True + # What: assert that observation normal completion credited is false; why: this assertion protects the native router cancellation canary requires idle without completion credit regression after the test's arranged inputs and exercised call. + assert observation["normalCompletionCredited"] is False + # What: assert that state equals active 0 cancellations 1 terminal 0; why: this assertion protects the native router cancellation canary requires idle without completion credit regression after the test's arranged inputs and exercised call. + assert state == {"active": 0, "cancellations": 1, "terminal": 0} + # What: run server shutdown on every exit path; why: test_native_router_cancellation_canary_requires_idle_without_completion_credit performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act by calling server.shutdown with the declared inputs; why: the native router cancellation canary requires idle without completion credit scenario observes the server.shutdown return value during server server close. + server.shutdown() + # What: act by calling server.server_close with the declared inputs; why: the native router cancellation canary requires idle without completion credit scenario observes the server.server_close return value during worker join. + server.server_close() + # What: act by calling worker.join with 3; why: the native router cancellation canary requires idle without completion credit scenario observes the worker.join return value during the enclosing return. + worker.join(3) + + +# What: define the test_native_router_benchmark_keeps_prometheus_capture_private_bytes test around native router qualifier and monkeypatch; why: this test groups the arrange, act, and assertions that protect the native router benchmark keeps prometheus capture private bytes outcome. +def test_native_router_benchmark_keeps_prometheus_capture_private_bytes(native_router_qualifier, monkeypatch): + # What: act by calling io.BytesIO and capture stream; why: the native router benchmark keeps prometheus capture private bytes test asserts the response, state, or failure produced by this call. + stream = io.BytesIO(b"freetoken_swap_admissions_total 3\n") + # What: arrange the exact monkeypatch setattr native router qualifier urllib request urlopen lambda a k fixture f; why: the native router benchmark keeps prometheus capture private bytes scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", l bef. + monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", lambda *a, **k: stream) + + # What: assert that native router qualifier request bytes http test metrics equals b freetoken swap admissions total 3 n; why: this assertion protects the native router benchmark keeps prometheus capture private bytes regression after the test's arranged inputs and exercised call. + assert native_router_qualifier.request_bytes("http://test/metrics") == b"freetoken_swap_admissions_total 3\n" + # What: assert that stream closed; why: this assertion protects the native router benchmark keeps prometheus capture private bytes regression after the test's arranged inputs and exercised call. + assert stream.closed + + +# What: define the test_native_router_credentials_are_scoped_to_the_temporary_origin test around native router qualifier and monkeypatch; why: this test groups the arrange, act, and assertions that protect the native router credentials are scoped to the temporary origin outcome. +def test_native_router_credentials_are_scoped_to_the_temporary_origin( + # What: arrange native router qualifier monkeypatch for the scenario; why: test native router credentials are scoped to the temporary origin requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch +# What: arrange the grouped source fragment for the scenario; why: test native router credentials are scoped to the temporary origin requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange requests as the fixture input; why: the native router credentials are scoped to the temporary origin test consumes this named precondition before exercising the behavior. + requests = [] + + # What: define the urlopen test helper around request; why: the native router credentials are scoped to the temporary origin scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def urlopen(request, **kwargs): + # What: act by calling requests.append with request; why: the native router credentials are scoped to the temporary origin scenario observes the requests.append return value during return io bytes io b. + requests.append(request) + # What: return bytes io and io from the urlopen test helper; why: the native router credentials are scoped to the temporary origin scenario uses this helper result in its subsequent act or assertion. + return io.BytesIO(b'{}') + + # What: arrange the exact monkeypatch setattr native router qualifier urllib request urlopen urlopen fixture frag; why: the native router credentials are scoped to the temporary origin scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", u befor. + monkeypatch.setattr(native_router_qualifier.urllib.request, "urlopen", urlopen) + # What: arrange the exact native router qualifier configure native auth http native test private key fixture frag; why: the native router credentials are scoped to the temporary origin scenario feeds this byte-preserved fragment through native_router_qualifier.configure_native_auth("http://native.test:1964", befor. + native_router_qualifier.configure_native_auth("http://native.test:1964", "private-key") + + # What: arrange the exact native router qualifier request json http native test router status fixture fragment; why: the native router credentials are scoped to the temporary origin scenario feeds this byte-preserved fragment through native_router_qualifier.request_json("http://native.test:1964/router/sta before a. + native_router_qualifier.request_json("http://native.test:1964/router/status") + # What: arrange the exact native router qualifier request json http protected test health fixture fragment; why: the native router credentials are scoped to the temporary origin scenario feeds this byte-preserved fragment through native_router_qualifier.request_json("http://protected.test:8000/health" before asser. + native_router_qualifier.request_json("http://protected.test:8000/health") + # What: arrange the exact native router qualifier request json http native test v1 models fixture fragment; why: the native router credentials are scoped to the temporary origin scenario feeds this byte-preserved fragment through native_router_qualifier.request_json("http://native.test:24567/v1/models before asser. + native_router_qualifier.request_json("http://native.test:24567/v1/models") + + # What: assert that requests 0 get header authorization equals bearer private key; why: this assertion protects the native router credentials are scoped to the temporary origin regression after the test's arranged inputs and exercised call. + assert requests[0].get_header("Authorization") == "Bearer private-key" + # What: assert that requests 1 get header authorization is group delimiter; why: this assertion protects the native router credentials are scoped to the temporary origin regression after the test's arranged inputs and exercised call. + assert requests[1].get_header("Authorization") is None + # What: assert that requests 2 get header authorization is group delimiter; why: this assertion protects the native router credentials are scoped to the temporary origin regression after the test's arranged inputs and exercised call. + assert requests[2].get_header("Authorization") is None + + +# What: define the test_native_router_control_plane_canary_requires_auth_and_captures_evidence test around native router qualifier and tmp path; why: this test groups the arrange, act, and assertions that protect the native router control plane canary requires auth and captures evidence outcome. +def test_native_router_control_plane_canary_requires_auth_and_captures_evidence( + # What: arrange native router qualifier tmp path for the scenario; why: test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange authorized paths as the fixture input; why: the native router control plane canary requires auth and captures evidence test consumes this named precondition before exercising the behavior. + authorized_paths = [] + + # What: define Handler as the owner of log_message and _send and do_GET; why: daemon callers use this class boundary so those methods share one handler state invariant. + class Handler(BaseHTTPRequestHandler): + # What: define the log_message test helper around captured fixture state; why: the native router control plane canary requires auth and captures evidence scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def log_message(self, *args): + # What: ignore the anticipated exception handled by this branch; why: log_message continues its retry or cleanup path instead of re-raising that transient failure. + pass + + # What: define the _send test helper around body and content type; why: the native router control plane canary requires auth and captures evidence scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def _send(self, body, *, content_type="application/json"): + # What: act by calling self.send_response with 200; why: the native router control plane canary requires auth and captures evidence scenario observes the self.send_response return value during self send header content type content type. + self.send_response(200) + # What: arrange the exact self send header content type content type fixture fragment; why: the native router control plane canary requires auth and captures evidence scenario feeds this byte-preserved fragment through self.send_header("Content-Type", content_type) before asserting its protocol or parser r. + self.send_header("Content-Type", content_type) + # What: arrange the exact self send header content length str len body fixture fragment; why: the native router control plane canary requires auth and captures evidence scenario feeds this byte-preserved fragment through self.send_header("Content-Length", str(len(body))) before asserting its protocol or pa. + self.send_header("Content-Length", str(len(body))) + # What: act by calling self.end_headers with the declared inputs; why: the native router control plane canary requires auth and captures evidence scenario observes the self.end_headers return value during self wfile write body. + self.end_headers() + # What: act by calling self.wfile.write with body; why: the native router control plane canary requires auth and captures evidence scenario observes the self.wfile.write return value during the enclosing return. + self.wfile.write(body) + + # What: define the do_GET test helper around captured fixture state; why: the native router control plane canary requires auth and captures evidence scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def do_GET(self): + # What: act by calling operation.decode and capture accepted; why: the native router control plane canary requires auth and captures evidence test asserts the response, state, or failure produced by this call. + accepted = { + # What: arrange the bearer private key portion of accepted; why: the native router control plane canary requires auth and captures evidence scenario uses this clause to evaluate accepted as one grouped value. + "Bearer private-key", + # What: act by calling operation.decode with the declared inputs; why: the native router control plane canary requires auth and captures evidence scenario observes the operation.decode return value while evaluating "Basic " + base64.b64encode(b"operator:private-key").decode(). + "Basic " + base64.b64encode(b"operator:private-key").decode(), + # What: arrange the accepted collection with bearer and private key and decode and b64encode and base64 and basic; why: Handler.do_GET groups the supplied clauses as one accepted collection before its value is consumed. + } + # What: act on accepted and get and headers before send response; why: the native router control plane canary requires auth and captures evidence scenario admits send response only for this predicate and excludes the opposite state. + if ( + # What: act by calling self.headers.get with authorization; why: the native router control plane canary requires auth and captures evidence scenario observes the self.headers.get return value during and self headers get x api key private key. + self.headers.get("Authorization") not in accepted + # What: act by calling self.headers.get with x api key; why: the native router control plane canary requires auth and captures evidence scenario observes the self.headers.get return value while evaluating and self.headers.get("X-Api-Key") != "private-key". + and self.headers.get("X-Api-Key") != "private-key" + # What: arrange the enclosing predicate with if self headers get authorization not in accepted and self headers get x api key; why: Handler.do_GET groups the supplied clauses as one Handler.do_GET expression before its value is consumed. + ): + # What: act by calling self.send_response with 401; why: the native router control plane canary requires auth and captures evidence scenario observes the self.send_response return value during self send header content length. + self.send_response(401) + # What: arrange the exact self send header content length fixture fragment; why: the native router control plane canary requires auth and captures evidence scenario feeds this byte-preserved fragment through self.send_header("Content-Length", "0") before asserting its protocol or parser result. + self.send_header("Content-Length", "0") + # What: act by calling self.end_headers with the declared inputs; why: the native router control plane canary requires auth and captures evidence scenario observes the self.end_headers return value during return. + self.end_headers() + # What: return no value from the do_GET test helper; why: the native router control plane canary requires auth and captures evidence scenario uses this helper result in its subsequent act or assertion. + return + # What: act by calling authorized_paths.append with path; why: the native router control plane canary requires auth and captures evidence scenario observes the authorized_paths.append return value during if self path router status. + authorized_paths.append(self.path) + # What: arrange if self path == router status for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + if self.path == "/router/status": + # What: arrange body as active profile and model a; why: the native router control plane canary requires auth and captures evidence test consumes this named precondition before exercising the behavior. + body = {"activeProfile": "model-a"} + # What: act on path before body and path; why: the native router control plane canary requires auth and captures evidence scenario admits body and path only for this predicate and excludes the opposite state. + elif self.path in ("/v1/models", "/models"): + # What: arrange body as path and object and data and list and id; why: the native router control plane canary requires auth and captures evidence test consumes this named precondition before exercising the behavior. + body = { + # What: arrange the object field as list; why: Handler.do_GET carries object through body into body running requests 0. + "object": "list", + # What: arrange the data field as path and id and created and name and meta; why: Handler.do_GET carries data through body into body running requests 0. + "data": [ + # What: arrange the body mapping with id and created and name and meta; why: Handler.do_GET groups the supplied clauses as one body mapping before its value. + { + # What: arrange the id field as model a; why: Handler.do_GET carries id through body into body running requests 0. + "id": "model-a", + # What: arrange the created field as path and 10 and 11 and v1 and models; why: Handler.do_GET carries created through body into body running requests 0. + "created": 10 if self.path == "/v1/models" else 11, + # What: arrange the name field as qualification and model and a; why: Handler.do_GET carries name through body into body running requests 0. + "name": "Qualification model A", + # What: arrange the freetoken field as aliases and tier and type and qualification and model; why: Handler.do_GET carries freetoken through body into body running requests 0. + "meta": {"freetoken": { + # What: arrange the aliases compat model a portion of body; why: the native router control plane canary requires auth and captures evidence scenario uses this clause to evaluate body as one grouped value. + "aliases": ["compat/model-a"], + # What: arrange the tier qualification type model portion of body; why: the native router control plane canary requires auth and captures evidence scenario uses this clause to evaluate body as one grouped value. + "tier": "qualification", "type": "model", + # What: arrange the body mapping with freetoken; why: Handler.do_GET groups the supplied clauses as one body mapping before its value is consumed. + }}, + # What: arrange the body mapping with id and created and name and meta; why: Handler.do_GET groups the supplied clauses as one body mapping before its. + }, + # What: arrange the id field as model b; why: Handler.do_GET carries id through body into body running requests 0. + {"id": "model-b", "created": 10 if self.path == "/v1/models" else 11}, + # What: arrange the body mapping with id and created and name and meta; why: Handler.do_GET groups the supplied clauses as one body mapping before its value. + { + # What: arrange the id field as compat and model a; why: Handler.do_GET carries id through body into body running requests 0. + "id": "compat/model-a", + # What: arrange the created field as path and 10 and 11 and v1 and models; why: Handler.do_GET carries created through body into body running requests 0. + "created": 10 if self.path == "/v1/models" else 11, + # What: arrange the name field as qualification and model and a; why: Handler.do_GET carries name through body into body running requests 0. + "name": "Qualification model A", + # What: arrange the freetoken field as model id and tier and type and model a and qualification; why: Handler.do_GET carries freetoken through body into body running requests 0. + "meta": {"freetoken": { + # What: arrange the model id model a tier qualification portion of body; why: the native router control plane canary requires auth and captures evidence scenario uses this clause to evaluate body as one grouped value. + "modelID": "model-a", "tier": "qualification", + # What: arrange the type alias portion of body; why: the native router control plane canary requires auth and captures evidence scenario uses this clause to evaluate body as one grouped value. + "type": "alias", + # What: arrange the body mapping with freetoken; why: Handler.do_GET groups the supplied clauses as one body mapping before its value is consumed. + }}, + # What: arrange the body mapping with id and created and name and meta; why: Handler.do_GET groups the supplied clauses as one body mapping before its. + }, + # What: arrange the id field as preferred model; why: Handler.do_GET carries id through body into body running requests 0. + {"id": "preferred-model", "created": 10 if self.path == "/v1/models" else 11}, + # What: arrange the grouped source fragment for the scenario; why: this test requires this concrete input or helper state before exercising the behavior under test. + ], + # What: arrange the body mapping with object and data; why: Handler.do_GET groups the supplied clauses as one body mapping before its value is consumed. + } + # What: arrange elif self path == upstream compat model a v1 stats for the scenario; why: test native router control plane canary requires requires this concrete input or helper state before exercising the behavior under test. + elif self.path == "/upstream/compat/model-a/v1/stats": + # What: arrange body as running requests and 0; why: the native router control plane canary requires auth and captures evidence test consumes this named precondition before exercising the behavior. + body = {"running_requests": 0} + # What: arrange elif self path == router models for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + elif self.path == "/router/models": + # What: arrange body as data and name and resident and check endpoint and use model name; why: the native router control plane canary requires auth and captures evidence test consumes this named precondition before exercising the behavior. + body = {"data": [ + # What: arrange the body mapping with name and resident and check endpoint and use model name; why: Handler.do_GET groups the supplied clauses as one body mapping. + { + # What: arrange the name field as model a; why: Handler.do_GET carries name through body into body. + "name": "model-a", "resident": True, + # What: arrange the check endpoint field as ready; why: Handler.do_GET carries check endpoint through body into body. + "checkEndpoint": "/ready", + # What: arrange the use model name field as model a; why: Handler.do_GET carries use model name through body into body. + "useModelName": "model-a", + # What: arrange the upstream timeout s field as 659; why: Handler.do_GET carries upstream timeout s through body into body. + "upstreamTimeoutS": 659, + # What: arrange the display name field as qualification and model and a; why: Handler.do_GET carries display name through body into body. + "displayName": "Qualification model A", + # What: arrange the tier field as qualification; why: Handler.do_GET carries tier through body into body. + "metadata": {"tier": "qualification", "type": "operator"}, + # What: arrange the body mapping with name and resident and check endpoint and use model name; why: Handler.do_GET groups the supplied clauses as one body. + }, + # What: arrange the body mapping with name and resident and check endpoint; why: Handler.do_GET groups the supplied clauses as one body mapping before its value. + { + # What: arrange the name field as model b; why: Handler.do_GET carries name through body into body. + "name": "model-b", "resident": False, + # What: arrange the check endpoint field as ready; why: Handler.do_GET carries check endpoint through body into body. + "checkEndpoint": "/ready", + # What: arrange the body mapping with name and resident and check endpoint; why: Handler.do_GET groups the supplied clauses as one body mapping before its. + }, + # What: arrange the body mapping with data; why: Handler.do_GET groups the supplied clauses as one body mapping before its value is consumed. + ]} + # What: arrange elif self path == router profiles for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + elif self.path == "/router/profiles": + # What: arrange body as active profile and active routing profile and routing profiles and data and model a; why: the native router control plane canary requires auth and captures evidence test consumes this named precondition before exercising the behavior. + body = { + # What: arrange the active profile field as model a; why: Handler.do_GET carries active profile through body into body. + "activeProfile": "model-a", + # What: arrange the active routing profile field as the fixture input; why: Handler.do_GET carries active routing profile through body into body. + "activeRoutingProfile": None, + # What: arrange the routing profiles portion of body; why: the native router control plane canary requires auth and captures evidence scenario uses this clause to evaluate body as one grouped value. + "routingProfiles": [{ + # What: arrange the name coding pins portion of body; why: the native router control plane canary requires auth and captures evidence scenario uses this clause to evaluate body as one grouped value. + "name": "coding", "pins": { + # What: arrange the disabled model field as the fixture input; why: Handler.do_GET carries disabled model through body into body. + "disabled-model": None, + # What: arrange the profile model field as preferred model; why: Handler.do_GET carries profile model through body into body. + "profile-model": "preferred-model", + # What: arrange the body mapping with disabled model and profile model; why: Handler.do_GET groups the supplied clauses as one body mapping before its value is consumed. + }, + # What: arrange the body collection with name and pins and coding and disabled model and profile model; why: Handler.do_GET groups the supplied clauses as one body collection before its value is consumed. + }], + # What: arrange the name field as model a; why: Handler.do_GET carries name through body into body. + "data": [{"name": "model-a"}, {"name": "model-b"}], + # What: arrange the body mapping with active profile and active routing profile and routing profiles and data; why: Handler.do_GET groups the supplied clauses as one body mapping before its value is consumed. + } + # What: arrange elif self path == api performance for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + elif self.path == "/api/performance": + # What: arrange body as enabled and sys stats and gpu stats and true and timestamp; why: the native router control plane canary requires auth and captures evidence test consumes this named precondition before exercising the behavior. + body = { + # What: arrange the enabled field as true; why: Handler.do_GET carries enabled through body into self send json dumps body encode. + "enabled": True, + # What: arrange the sys stats portion of body; why: the native router control plane canary requires auth and captures evidence scenario uses this clause to evaluate body as one grouped value. + "sys_stats": [{ + # What: arrange the timestamp field as t00 and z; why: Handler.do_GET carries timestamp through body into self send json dumps body encode. + "timestamp": "2026-09-15T00:00:00Z", + # What: arrange the scope field as engine process tree; why: Handler.do_GET carries scope through body into self send json dumps body encode. + "scope": "engine-process-tree", + # What: arrange the ram bytes field as 1024; why: Handler.do_GET carries ram bytes through body into self send json dumps body encode. + "ram_bytes": 1024, + # What: arrange the vram bytes field as 2048; why: Handler.do_GET carries vram bytes through body into self send json dumps body encode. + "vram_bytes": 2048, + # What: arrange the ram available field as true; why: Handler.do_GET carries ram available through body into self send json dumps body encode. + "ram_available": True, + # What: arrange the vram available field as true; why: Handler.do_GET carries vram available through body into self send json dumps body encode. + "vram_available": True, + # What: arrange the ram source field as proc smaps rollup pss; why: Handler.do_GET carries ram source through body into self send json dumps body encode. + "ram_source": "proc-smaps-rollup-pss", + # What: arrange the vram source field as amd smi; why: Handler.do_GET carries vram source through body into self send json dumps body encode. + "vram_source": "amd-smi", + # What: arrange the body collection with timestamp and scope and ram bytes and vram bytes and ram available; why: Handler.do_GET groups the supplied clauses as one body collection before its value is consumed. + }], + # What: arrange the gpu stats field as the fixture input; why: Handler.do_GET carries gpu stats through body into self send json dumps body encode. + "gpu_stats": [], + # What: arrange the body mapping with enabled and sys stats and gpu stats; why: Handler.do_GET groups the supplied clauses as one body mapping before its value is consumed. + } + # What: arrange elif self path == metrics for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + elif self.path == "/metrics": + # What: act by calling self._send with the named fixture input; why: the native router control plane canary requires auth and captures evidence scenario observes the self._send return value during b freetoken swap admissions total n. + self._send( + # What: arrange the b freetoken swap admissions total n portion of the enclosing predicate; why: this clause remains in the native router control plane canary requires auth and captures evidence scenario\'s enclosing expression so its grouping and evaluation order stay intact. + b"freetoken_swap_admissions_total 1\n", + # What: arrange the exact content type text plain version fixture fragment; why: the native router control plane canary requires auth and captures evidence scenario feeds this byte-preserved fragment through content_type="text/plain; version=0.0.4" before asserting its protocol or parser result. + content_type="text/plain; version=0.0.4", + # What: arrange the self._send call with content type; why: Handler.do_GET groups the supplied clauses as one self._send call before its value is consumed. + ) + # What: return no value from the do_GET test helper; why: the native router control plane canary requires auth and captures evidence scenario uses this helper result in its subsequent act or assertion. + return + # What: arrange elif self path == router logs since 0 for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + elif self.path == "/router/logs?since=0": + # What: act by calling self._send with the named fixture input; why: the native router control plane canary requires auth and captures evidence scenario observes the self._send return value during b event router ndata event startup. + self._send( + # What: arrange the b event router ndata event startup portion of the enclosing predicate; why: this clause remains in the native router control plane canary requires auth and captures evidence scenario\'s enclosing expression so its grouping and evaluation order stay intact. + b'event: router\ndata: {"event":"startup"}\n\n' + # What: arrange the b event router ndata event management loaded portion of the enclosing predicate; why: this clause remains in the native router control plane canary requires auth and captures evidence scenario\'s enclosing expression so its grouping and evaluation order stay intact. + b'event: router\ndata: {"event":"management_loaded"}\n\n', + # What: arrange the exact content type text event stream fixture fragment; why: the native router control plane canary requires auth and captures evidence scenario feeds this byte-preserved fragment through content_type="text/event-stream" before asserting its protocol or parser result. + content_type="text/event-stream", + # What: arrange the self._send call with content type; why: Handler.do_GET groups the supplied clauses as one self._send call before its value is consumed. + ) + # What: return no value from the do_GET test helper; why: the native router control plane canary requires auth and captures evidence scenario uses this helper result in its subsequent act or assertion. + return + # What: select the remaining branch that performs self send error; why: do_GET covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: act by calling self.send_error with 404; why: the native router control plane canary requires auth and captures evidence scenario observes the self.send_error return value during return. + self.send_error(404) + # What: return no value from the do_GET test helper; why: the native router control plane canary requires auth and captures evidence scenario uses this helper result in its subsequent act or assertion. + return + # What: act by calling self._send with encode and dumps and body and json; why: the native router control plane canary requires auth and captures evidence scenario observes the self._send return value during the enclosing return. + self._send(json.dumps(body).encode()) + + # What: act by calling ThreadingHTTPServer and capture server; why: the native router control plane canary requires auth and captures evidence test asserts the response, state, or failure produced by this call. + server = ThreadingHTTPServer(("127.0.0.1", 0), Handler) + # What: act by calling threading.Thread and capture worker; why: the native router control plane canary requires auth and captures evidence test asserts the response, state, or failure produced by this call. + worker = threading.Thread(target=server.serve_forever, daemon=True) + # What: act by calling worker.start with the declared inputs; why: the native router control plane canary requires auth and captures evidence scenario observes the worker.start return value during base f http server server port. + worker.start() + # What: arrange base as server port and server and http; why: the native router control plane canary requires auth and captures evidence test consumes this named precondition before exercising the behavior. + base = f"http://127.0.0.1:{server.server_port}" + # What: arrange the exact native router qualifier configure native auth base private key fixture fragment; why: the native router control plane canary requires auth and captures evidence scenario feeds this byte-preserved fragment through native_router_qualifier.configure_native_auth(base, "private-key") before as. + native_router_qualifier.configure_native_auth(base, "private-key") + # What: establish the handler boundary for the protected operation; why: test_native_router_control_plane_canary_requires_auth_and_captures_evidence routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: act by calling native_router_qualifier.control_plane_canary and capture observation; why: the native router control plane canary requires auth and captures evidence test asserts the response, state, or failure produced by this call. + observation = native_router_qualifier.control_plane_canary(base, tmp_path) + # What: run server shutdown on every exit path; why: test_native_router_control_plane_canary_requires_auth_and_captures_evidence performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act by calling server.shutdown with the declared inputs; why: the native router control plane canary requires auth and captures evidence scenario observes the server.shutdown return value during server server close. + server.shutdown() + # What: act by calling server.server_close with the declared inputs; why: the native router control plane canary requires auth and captures evidence scenario observes the server.server_close return value during worker join. + server.server_close() + # What: act by calling worker.join with 3; why: the native router control plane canary requires auth and captures evidence scenario observes the worker.join return value during assert observation passed is. + worker.join(3) + + # What: assert that observation passed is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["passed"] is True + # What: assert that observation unauthenticated control rejected is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["unauthenticatedControlRejected"] is True + # What: assert that observation unauthenticated inference rejected is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["unauthenticatedInferenceRejected"] is True + # What: assert that observation resident profile equals model a; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["residentProfile"] == "model-a" + # What: assert that observation model list alias verified is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["modelListAliasVerified"] is True + # What: assert that observation selector listed is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["selectorListed"] is True + # What: assert that observation routing profile listed is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["routingProfileListed"] is True + # What: assert that observation configured readiness target verified is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["configuredReadinessTargetVerified"] is True + # What: assert that observation configured upstream model name verified is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["configuredUpstreamModelNameVerified"] is True + # What: assert that observation configured upstream timeout verified is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["configuredUpstreamTimeoutVerified"] is True + # What: assert that observation configured model metadata verified is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["configuredModelMetadataVerified"] is True + # What: assert that observation namespaced upstream verified is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["namespacedUpstreamVerified"] is True + # What: assert that observation api key forms verified equals bearer basic x api key; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["apiKeyFormsVerified"] == ["bearer", "basic", "x-api-key"] + # What: assert that observation periodic performance available is true; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert observation["periodicPerformanceAvailable"] is True + # What: assert the expected authorized paths == outcome; why: test swap qualification test native router control plane canary requires auth and captures evidence protects its regression by requiring this observable result after the exercised behavior. + assert authorized_paths == [ + # What: arrange router status router status for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + "/router/status", "/router/status", + # What: arrange v1 models models upstream compat model a v1 stats for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + "/v1/models", "/models", "/upstream/compat/model-a/v1/stats", + # What: arrange router models router profiles api performance metrics for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + "/router/models", "/router/profiles", "/api/performance", "/metrics", + # What: arrange router logs since 0 for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + "/router/logs?since=0", + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete. + ] + # What: assert that b management loaded is present in tmp path control router log sse read bytes; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert b"management_loaded" in (tmp_path / "control-router-log.sse").read_bytes() + # What: assert the expected tmp path control metrics prom read bytes startswith outcome; why: test swap qualification test native router control plane canary requires auth and captures evidence protects its regression by requiring this observable result after the exercised behavior. + assert (tmp_path / "control-metrics.prom").read_bytes().startswith( + # What: arrange b freetoken swap admissions total for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input or helper state before exercising the behavior under test. + b"freetoken_swap_admissions_total" + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router control plane canary requires auth and captures evidence requires this concrete input. + ) + # What: assert that tmp path control auth basic json is file; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert (tmp_path / "control-auth-basic.json").is_file() + # What: assert that tmp path control auth x api key json is file; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert (tmp_path / "control-auth-x-api-key.json").is_file() + # What: assert that tmp path control performance json is file; why: this assertion protects the native router control plane canary requires auth and captures evidence regression after the test's arranged inputs and exercised call. + assert (tmp_path / "control-performance.json").is_file() + + +# What: define the test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows test around native router qualifier; why: this test groups the arrange, act, and assertions that protect the native periodic performance gate rejects unavailable or identifying rows outcome. +def test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows( + # What: arrange native router qualifier for the scenario; why: test native periodic performance gate rejects unavailable or identifying rows requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, +# What: arrange the grouped source fragment for the scenario; why: test native periodic performance gate rejects unavailable or identifying rows requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange valid as enabled and sys stats and gpu stats and true and timestamp; why: the native periodic performance gate rejects unavailable or identifying rows test consumes this named precondition before exercising the behavior. + valid = { + # What: arrange the enabled field as true; why: test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows carries enabled through valid into assert native router qualifier valid periodic performance valid. + "enabled": True, + # What: arrange the sys stats portion of valid; why: the native periodic performance gate rejects unavailable or identifying rows scenario uses this clause to evaluate valid as one grouped value. + "sys_stats": [{ + # What: arrange the timestamp field as t00 and z; why: test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows carries timestamp through valid into assert native router qualifier valid periodic performance valid. + "timestamp": "2026-09-15T00:00:00Z", "scope": "engine-process-tree", + # What: arrange the ram bytes field as 1; why: test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows carries ram bytes through valid into assert native router qualifier valid periodic performance valid. + "ram_bytes": 1, "vram_bytes": 2, + # What: arrange the ram available field as true; why: test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows carries ram available through valid into assert native router qualifier valid periodic performance valid. + "ram_available": True, "vram_available": True, + # What: arrange the ram source field as pss; why: test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows carries ram source through valid into assert native router qualifier valid periodic performance valid. + "ram_source": "pss", "vram_source": "amd-smi", + # What: arrange the valid collection with timestamp and scope and ram bytes and vram bytes and ram available; why: test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows groups the supplied clauses as one valid collection before its value is consumed. + }], + # What: arrange the gpu stats field as the fixture input; why: test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows carries gpu stats through valid into assert native router qualifier valid periodic performance valid. + "gpu_stats": [], + # What: arrange the valid mapping with enabled and sys stats and gpu stats; why: test_native_periodic_performance_gate_rejects_unavailable_or_identifying_rows groups the supplied clauses as one valid mapping before its value is consumed. + } + # What: assert that native router qualifier valid periodic performance valid; why: this assertion protects the native periodic performance gate rejects unavailable or identifying rows regression after the test's arranged inputs and exercised call. + assert native_router_qualifier.valid_periodic_performance(valid) + # What: act across the computed value to perform candidate and loads and json and dumps and valid; why: the native periodic performance gate rejects unavailable or identifying rows scenario repeats the body only while or for the loop header admits an iteration. + for key, value in ( + # What: arrange the ram available vram bytes pids portion of the enclosing predicate; why: this clause remains in the native periodic performance gate rejects unavailable or identifying rows scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("ram_available", False), ("vram_bytes", 0), ("pids", [123]), + # What: arrange the model private path private portion of the enclosing predicate; why: this clause remains in the native periodic performance gate rejects unavailable or identifying rows scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("model", "private"), ("path", "/private"), + # What: arrange the grouped source fragment for the scenario; why: test native periodic performance gate rejects unavailable or identifying rows requires this concrete input or helper state before exercising the behavior under test. + ): + # What: act by calling json.loads and capture candidate; why: the native periodic performance gate rejects unavailable or identifying rows test asserts the response, state, or failure produced by this call. + candidate = json.loads(json.dumps(valid)) + # What: arrange candidate entry entry entry as value; why: the native periodic performance gate rejects unavailable or identifying rows test consumes this named precondition before exercising the behavior. + candidate["sys_stats"][-1][key] = value + # What: assert that native router qualifier valid periodic performance candidate is false; why: this assertion protects the native periodic performance gate rejects unavailable or identifying rows regression after the test's arranged inputs and exercised call. + assert not native_router_qualifier.valid_periodic_performance(candidate) + + +# What: define the test_native_router_benchmark_validates_warm_and_swap_activation_labels test around native router qualifier; why: this test groups the arrange, act, and assertions that protect the native router benchmark validates warm and swap activation labels outcome. +def test_native_router_benchmark_validates_warm_and_swap_activation_labels(native_router_qualifier): + # What: arrange status a as active profile and active requests and activations and model a and 0; why: the native router benchmark validates warm and swap activation labels test consumes this named precondition before exercising the behavior. + status_a = {"activeProfile": "model-a", "activeRequests": 0, "activations": 1} + # What: arrange status b as active profile and active requests and activations and model b and 0; why: the native router benchmark validates warm and swap activation labels test consumes this named precondition before exercising the behavior. + status_b = {"activeProfile": "model-b", "activeRequests": 0, "activations": 2} + # What: assert the expected native router qualifier validate routed trial outcome; why: test swap qualification test native router benchmark validates warm and swap activation labels protects its regression by requiring this observable result after the exercised behavior. + assert native_router_qualifier.validate_routed_trial( + # What: arrange status a alias model a prior activations 1 expected delta 0 for the scenario; why: test swap qualification test native router benchmark validates warm and swap activation labels requires this concrete input or helper state before exercising the behavior under test. + status_a, alias="model-a", prior_activations=1, expected_delta=0 + # What: arrange == 1 for the scenario; why: test swap qualification test native router benchmark validates warm and swap activation labels requires this concrete input or helper state before exercising the behavior under test. + ) == 1 + # What: assert the expected native router qualifier validate routed trial outcome; why: test swap qualification test native router benchmark validates warm and swap activation labels protects its regression by requiring this observable result after the exercised behavior. + assert native_router_qualifier.validate_routed_trial( + # What: arrange status b alias model b prior activations 1 expected delta 1 for the scenario; why: test swap qualification test native router benchmark validates warm and swap activation labels requires this concrete input or helper state before exercising the behavior under test. + status_b, alias="model-b", prior_activations=1, expected_delta=1 + # What: arrange == 2 for the scenario; why: test swap qualification test native router benchmark validates warm and swap activation labels requires this concrete input or helper state before exercising the behavior under test. + ) == 2 + + +# What: define the test_native_router_benchmark_proves_warm_selector_reuses_resident_target test around native router qualifier and monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the native router benchmark proves warm selector reuses resident target outcome. +def test_native_router_benchmark_proves_warm_selector_reuses_resident_target( + # What: arrange native router qualifier monkeypatch tmp path for the scenario; why: test native router benchmark proves warm selector reuses resident target requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test native router benchmark proves warm selector reuses resident target requires this concrete input or helper state before exercising the behavior under test. +): + # What: act by calling iter and capture statuses; why: the native router benchmark proves warm selector reuses resident target test asserts the response, state, or failure produced by this call. + statuses = iter(( + # What: arrange the active profile field as model a; why: test_native_router_benchmark_proves_warm_selector_reuses_resident_target carries active profile through statuses into lambda args kwargs b next statuses. + {"activeProfile": "model-a", "activeRequests": 0, "activations": 3}, + # What: arrange the active profile field as model a; why: test_native_router_benchmark_proves_warm_selector_reuses_resident_target carries active profile through statuses into lambda args kwargs b next statuses. + {"activeProfile": "model-a", "activeRequests": 0, "activations": 3}, + # What: arrange the iter call with ordered positional inputs; why: test_native_router_benchmark_proves_warm_selector_reuses_resident_target groups the supplied clauses as one iter call before its value is consumed. + )) + # What: act by calling monkeypatch.setattr with native router qualifier and request json and next and statuses; why: the native router benchmark proves warm selector reuses resident target scenario observes the monkeypatch.setattr return value during native router qualifier request json. + monkeypatch.setattr( + # What: arrange the exact native router qualifier request json fixture fragment; why: the native router benchmark proves warm selector reuses resident target scenario feeds this byte-preserved fragment through native_router_qualifier, "request_json" before asserting its protocol or parser result. + native_router_qualifier, "request_json", + # What: arrange the args input for test_native_router_benchmark_proves_warm_selector_reuses_resident_target; why: test_native_router_benchmark_proves_warm_selector_reuses_resident_target consumes args during signature binding, so callers must bind it with the other signature inputs. + lambda *args, **kwargs: (b"{}", next(statuses)), + # What: arrange the monkeypatch.setattr call with native router qualifier and next; why: test_native_router_benchmark_proves_warm_selector_reuses_resident_target groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: act by calling monkeypatch.setattr with native router qualifier and canary and model and model and passed and true; why: the native router benchmark proves warm selector reuses resident target scenario observes the monkeypatch.setattr return value during native router qualifier canary. + monkeypatch.setattr( + # What: arrange the exact native router qualifier canary fixture fragment; why: the native router benchmark proves warm selector reuses resident target scenario feeds this byte-preserved fragment through native_router_qualifier, "canary" before asserting its protocol or parser result. + native_router_qualifier, "canary", + # What: arrange the base input for test_native_router_benchmark_proves_warm_selector_reuses_resident_target; why: test_native_router_benchmark_proves_warm_selector_reuses_resident_target consumes base during signature binding, so callers must bind it with the other signature inputs. + lambda base, model, direct: (b"data: private\n\n", { + # What: arrange the model field as model; why: test_native_router_benchmark_proves_warm_selector_reuses_resident_target sends this field through "model": model, "passed": True so the router selects the canonical model or alias for upstream dispatch. + "model": model, "passed": True, + # What: arrange the enclosing predicate collection with the named fixture input and model and model and passed and true; why: test_native_router_benchmark_proves_warm_selector_reuses_resident_target groups the supplied clauses as one }) collection before its value is consumed. + }), + # What: arrange the monkeypatch.setattr call with native router qualifier and model; why: test_native_router_benchmark_proves_warm_selector_reuses_resident_target groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: act by calling native_router_qualifier.selector_canary and capture result; why: the native router benchmark proves warm selector reuses resident target test asserts the response, state, or failure produced by this call. + result = native_router_qualifier.selector_canary("http://test", tmp_path) + + # What: assert the expected result == outcome; why: test swap qualification test native router benchmark proves warm selector reuses resident target protects its regression by requiring this observable result after the exercised behavior. + assert result == { + # What: arrange strategy warm resolvedProfile model a for the scenario; why: test swap qualification test native router benchmark proves warm selector reuses resident target requires this concrete input or helper state before exercising the behavior under test. + "strategy": "warm", "resolvedProfile": "model-a", + # What: arrange activationDelta 0 passed True for the scenario; why: test swap qualification test native router benchmark proves warm selector reuses resident target requires this concrete input or helper state before exercising the behavior under test. + "activationDelta": 0, "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router benchmark proves warm selector reuses resident target requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that tmp path warm selector sse read bytes equals b data private n n; why: this assertion protects the native router benchmark proves warm selector reuses resident target regression after the test's arranged inputs and exercised call. + assert (tmp_path / "warm-selector.sse").read_bytes() == b"data: private\n\n" + + +# What: define the test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap test around native router qualifier and monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the native router benchmark proves alias rewrites upstream without swap outcome. +def test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap( + # What: arrange native router qualifier monkeypatch tmp path for the scenario; why: test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. +): + # What: act by calling iter and capture statuses; why: the native router benchmark proves alias rewrites upstream without swap test asserts the response, state, or failure produced by this call. + statuses = iter(( + # What: arrange the active profile field as model a; why: test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap carries active profile through statuses into lambda args kwargs b next statuses. + {"activeProfile": "model-a", "activeRequests": 0, "activations": 3}, + # What: arrange the active profile field as model a; why: test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap carries active profile through statuses into lambda args kwargs b next statuses. + {"activeProfile": "model-a", "activeRequests": 0, "activations": 3}, + # What: arrange the iter call with ordered positional inputs; why: test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap groups the supplied clauses as one iter call before its value is consumed. + )) + # What: act by calling monkeypatch.setattr with native router qualifier and request json and next and statuses; why: the native router benchmark proves alias rewrites upstream without swap scenario observes the monkeypatch.setattr return value during native router qualifier request json. + monkeypatch.setattr( + # What: arrange the exact native router qualifier request json fixture fragment; why: the native router benchmark proves alias rewrites upstream without swap scenario feeds this byte-preserved fragment through native_router_qualifier, "request_json" before asserting its protocol or parser result. + native_router_qualifier, "request_json", + # What: arrange the args input for test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap; why: test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap consumes args during signature binding, so callers must bind it with the other signature inputs. + lambda *args, **kwargs: (b"{}", next(statuses)), + # What: arrange the monkeypatch.setattr call with native router qualifier and next; why: test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + # What: arrange monkeypatch setattr for the scenario; why: test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. + monkeypatch.setattr( + # What: arrange the exact native router qualifier canary fixture fragment; why: the native router benchmark proves alias rewrites upstream without swap scenario feeds this byte-preserved fragment through native_router_qualifier, "canary" before asserting its protocol or parser result. + native_router_qualifier, "canary", + # What: arrange the base input for test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap; why: test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap consumes base during signature binding, so callers must bind it with the other signature inputs. + lambda base, model, direct: (b"data: private-rewrite\n\n", { + # What: arrange the model field as model; why: test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap sends this field through "model": model, "responseModel": "model-a", "passed": True so the router selects the canonical model or alias for upstream dispatch. + "model": model, "responseModel": "model-a", "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. + }), + # What: arrange the monkeypatch.setattr call with native router qualifier and model; why: test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: act by calling native_router_qualifier.upstream_model_rewrite_canary and capture result; why: the native router benchmark proves alias rewrites upstream without swap test asserts the response, state, or failure produced by this call. + result = native_router_qualifier.upstream_model_rewrite_canary( + # What: arrange the http test tmp path portion of result; why: the native router benchmark proves alias rewrites upstream without swap scenario uses this clause to evaluate result as one grouped value. + "http://test", tmp_path + # What: arrange the native_router_qualifier.upstream_model_rewrite_canary call with tmp path; why: test_native_router_benchmark_proves_alias_rewrites_upstream_without_swap groups the supplied clauses as one native_router_qualifier.upstream_model_rewrite_canary call before its value is consumed. + ) + + # What: assert the expected result == outcome; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap protects its regression by requiring this observable result after the exercised behavior. + assert result == { + # What: arrange requestedModel compat model a for the scenario; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. + "requestedModel": "compat/model-a", + # What: arrange upstreamResponseModel model a for the scenario; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. + "upstreamResponseModel": "model-a", + # What: arrange residentProfile model a for the scenario; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. + "residentProfile": "model-a", + # What: arrange activationDelta 0 for the scenario; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. + "activationDelta": 0, + # What: arrange passed True for the scenario; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. + "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap requires this concrete input or. + } + # What: assert the expected tmp path upstream model rewrite sse read bytes == outcome; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap protects its regression by requiring this observable result after the exercised behavior. + assert (tmp_path / "upstream-model-rewrite.sse").read_bytes() == ( + # What: arrange b data private rewrite n n for the scenario; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap requires this concrete input or helper state before exercising the behavior under test. + b"data: private-rewrite\n\n" + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router benchmark proves alias rewrites upstream without swap requires this concrete input or. + ) + + +# What: define the test_native_router_benchmark_proves_profile_selector_composition_and_cleanup test around native router qualifier and monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the native router benchmark proves profile selector composition and cleanup outcome. +def test_native_router_benchmark_proves_profile_selector_composition_and_cleanup( + # What: arrange native router qualifier monkeypatch tmp path for the scenario; why: test native router benchmark proves profile selector composition and cleanup requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test native router benchmark proves profile selector composition and cleanup requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange calls as the fixture input; why: the native router benchmark proves profile selector composition and cleanup test consumes this named precondition before exercising the behavior. + calls = [] + # What: act by calling iter and capture statuses; why: the native router benchmark proves profile selector composition and cleanup test asserts the response, state, or failure produced by this call. + statuses = iter(( + # What: arrange activeProfile model a activeRequests 0 activations 3 for the scenario; why: test swap qualification test native router benchmark proves profile selector composition and cleanup requires this concrete input or helper state before exercising the behavior under test. + {"activeProfile": "model-a", "activeRequests": 0, "activations": 3}, + # What: arrange the statuses mapping with active profile and active routing profile and active requests and; why: test_native_router_benchmark_proves_profile_selector_composition_and_cleanup groups the supplied clauses as one statuses mapping before its. + { + # What: arrange activeProfile model a activeRoutingProfile coding for the scenario; why: test swap qualification test native router benchmark proves profile selector composition and cleanup requires this concrete input or helper state before exercising the behavior under test. + "activeProfile": "model-a", "activeRoutingProfile": "coding", + # What: arrange the active requests field as 0; why: test_native_router_benchmark_proves_profile_selector_composition_and_cleanup carries active requests through statuses into return b next statuses. + "activeRequests": 0, "activations": 3, + # What: arrange the statuses mapping with active profile and active routing profile and active requests and; why: test_native_router_benchmark_proves_profile_selector_composition_and_cleanup groups the supplied clauses as one statuses mapping before. + }, + # What: arrange the iter call with ordered positional inputs; why: test_native_router_benchmark_proves_profile_selector_composition_and_cleanup groups the supplied clauses as one iter call before its value is consumed. + )) + + # What: define the request_json test helper around url and body; why: the native router benchmark proves profile selector composition and cleanup scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request_json(url, body=None, **kwargs): + # What: act by calling calls.append with url and body and kwargs; why: the native router benchmark proves profile selector composition and cleanup scenario observes the calls.append return value during if url endswith router status. + calls.append((url, body, kwargs)) + # What: act on endswith and url before next and statuses; why: the native router benchmark proves profile selector composition and cleanup scenario admits next and statuses only for this predicate and excludes the opposite state. + if url.endswith("/router/status"): + # What: return next and statuses from the request_json test helper; why: the native router benchmark proves profile selector composition and cleanup scenario uses this helper result in its subsequent act or assertion. + return b"{}", next(statuses) + # What: act on endswith and url before body; why: the native router benchmark proves profile selector composition and cleanup scenario admits body only for this predicate and excludes the opposite state. + if url.endswith("/router/profiles/active"): + # What: arrange the active field as body and name; why: request_json carries active into return b"{}", {"active": body["name"]}. + return b"{}", {"active": body["name"]} + # What: act on endswith and url before the computed value; why: the native router benchmark proves profile selector composition and cleanup scenario admits the computed value only for this predicate and excludes the opposite state. + if url.endswith("/v1/models"): + # What: return data and id and profile model and id and model a from the request_json test helper; why: the native router benchmark proves profile selector composition and cleanup scenario uses this helper result in its subsequent act or assertion. + return b'{"data":[]}', { + # What: arrange the id field as profile model; why: request_json carries id into "data": [{"id": "profile-model"}, {"id": "model-a"}]. + "data": [{"id": "profile-model"}, {"id": "model-a"}], + # What: arrange the enclosing predicate collection with the named fixture input and data and id and profile model and id and model a; why: request_json groups the supplied clauses as one request_json expression collection before its value is consumed. + } + # What: raise AssertionError for the caller; why: request_json stops this rejected path before it can mutate state, dispatch work, or report success. + raise AssertionError(url) + + # What: arrange the exact monkeypatch setattr native router qualifier request json request json fixture fragment; why: the native router benchmark proves profile selector composition and cleanup scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "request_json", request. + monkeypatch.setattr(native_router_qualifier, "request_json", request_json) + # What: act by calling monkeypatch.setattr with native router qualifier and canary and model and model and passed and true; why: the native router benchmark proves profile selector composition and cleanup scenario observes the monkeypatch.setattr return value during native router qualifier canary. + monkeypatch.setattr( + # What: arrange the exact native router qualifier canary fixture fragment; why: the native router benchmark proves profile selector composition and cleanup scenario feeds this byte-preserved fragment through native_router_qualifier, "canary" before asserting its protocol or parser result. + native_router_qualifier, "canary", + # What: arrange the base input for test_native_router_benchmark_proves_profile_selector_composition_and_cleanup; why: test_native_router_benchmark_proves_profile_selector_composition_and_cleanup consumes base during signature binding, so callers must bind it with the other signature inputs. + lambda base, model, direct: (b"data: private-profile\n\n", { + # What: arrange the model field as model; why: test_native_router_benchmark_proves_profile_selector_composition_and_cleanup sends this field through "model": model, "passed": True so the router selects the canonical model or alias for upstream dispatch. + "model": model, "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test native router benchmark proves profile selector composition and cleanup requires this concrete input or helper state before exercising the behavior under test. + }), + # What: arrange the monkeypatch.setattr call with native router qualifier and model; why: test_native_router_benchmark_proves_profile_selector_composition_and_cleanup groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: act by calling native_router_qualifier.routing_profile_canary and capture result; why: the native router benchmark proves profile selector composition and cleanup test asserts the response, state, or failure produced by this call. + result = native_router_qualifier.routing_profile_canary("http://test", tmp_path) + + # What: assert the expected result == outcome; why: test swap qualification test native router benchmark proves profile selector composition and cleanup protects its regression by requiring this observable result after the exercised behavior. + assert result == { + # What: arrange profileActivated True profileCleared True for the scenario; why: test swap qualification test native router benchmark proves profile selector composition and cleanup requires this concrete input or helper state before exercising the behavior under test. + "profileActivated": True, "profileCleared": True, + # What: arrange selectorComposed True resolvedProfile model a for the scenario; why: test swap qualification test native router benchmark proves profile selector composition and cleanup requires this concrete input or helper state before exercising the behavior under test. + "selectorComposed": True, "resolvedProfile": "model-a", + # What: arrange activationDelta 0 passed True for the scenario; why: test swap qualification test native router benchmark proves profile selector composition and cleanup requires this concrete input or helper state before exercising the behavior under test. + "activationDelta": 0, "passed": True, + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router benchmark proves profile selector composition and cleanup requires this concrete input or helper state before exercising the behavior under test. + } + # What: act by calling call.endswith and capture profile calls; why: the native router benchmark proves profile selector composition and cleanup test asserts the response, state, or failure produced by this call. + profile_calls = [call for call in calls if call[0].endswith("/router/profiles/active")] + # What: assert that call 1 for call in profile calls equals name coding name; why: this assertion protects the native router benchmark proves profile selector composition and cleanup regression after the test's arranged inputs and exercised call. + assert [call[1] for call in profile_calls] == [{"name": "coding"}, {"name": None}] + # What: assert that all call 2 method equals put for call in profile calls; why: this assertion protects the native router benchmark proves profile selector composition and cleanup regression after the test's arranged inputs and exercised call. + assert all(call[2]["method"] == "PUT" for call in profile_calls) + # What: assert that tmp path routing profile sse read bytes equals b data private profile n n; why: this assertion protects the native router benchmark proves profile selector composition and cleanup regression after the test's arranged inputs and exercised call. + assert (tmp_path / "routing-profile.sse").read_bytes() == b"data: private-profile\n\n" + # What: assert that tmp path routing profile models json read bytes equals b data; why: this assertion protects the native router benchmark proves profile selector composition and cleanup regression after the test's arranged inputs and exercised call. + assert (tmp_path / "routing-profile-models.json").read_bytes() == b'{"data":[]}' + + +# What: define the test_native_router_benchmark_captures_private_hardware_observation test around native router qualifier and monkeypatch and tmp path; why: this test groups the arrange, act, and assertions that protect the native router benchmark captures private hardware observation outcome. +def test_native_router_benchmark_captures_private_hardware_observation( + # What: arrange native router qualifier monkeypatch tmp path for the scenario; why: test native router benchmark captures private hardware observation requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch, tmp_path +# What: arrange the grouped source fragment for the scenario; why: test native router benchmark captures private hardware observation requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange captured as the fixture input; why: the native router benchmark captures private hardware observation test consumes this named precondition before exercising the behavior. + captured = b'{"engine":{"running":true,"pid":7,"port":1234},"memory":{"ramBytes":3,"vramBytes":4,"ramAvailable":true,"vramAvailable":true,"ramSource":"proc-smaps-rollup-pss","vramSource":"amd-smi"}}' + # What: arrange the exact monkeypatch setattr native router qualifier request json lambda a k fixture fragment; why: the native router benchmark captures private hardware observation scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "request_json", lambda *a, before a. + monkeypatch.setattr(native_router_qualifier, "request_json", lambda *a, **k: (captured, { + # What: arrange the running field as true; why: test_native_router_benchmark_captures_private_hardware_observation carries running into "engine": {"running": True, "pid": 7, "port": 1234}. + "engine": {"running": True, "pid": 7, "port": 1234}, + # What: arrange the ram bytes field as 3; why: test_native_router_benchmark_captures_private_hardware_observation carries ram bytes into "memory": {"ramBytes": 3, "vramBytes": 4, "ramAvailable": True. + "memory": {"ramBytes": 3, "vramBytes": 4, "ramAvailable": True, + # What: arrange the vram available field as true; why: test_native_router_benchmark_captures_private_hardware_observation carries vram available into "vramAvailable": True, "ramSource": "proc-smaps-rollup-pss". + "vramAvailable": True, "ramSource": "proc-smaps-rollup-pss", + # What: arrange the vram source field as amd smi; why: test_native_router_benchmark_captures_private_hardware_observation carries vram source into "vramSource": "amd-smi"}. + "vramSource": "amd-smi"}, + # What: arrange the monkeypatch.setattr call with native router qualifier and captured; why: test_native_router_benchmark_captures_private_hardware_observation groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + })) + + # What: act by calling native_router_qualifier.capture_hardware and capture hardware; why: the native router benchmark captures private hardware observation test asserts the response, state, or failure produced by this call. + hardware = native_router_qualifier.capture_hardware("http://test", tmp_path, "warm-a") + + # What: assert that hardware engine pid equals 7; why: this assertion protects the native router benchmark captures private hardware observation regression after the test's arranged inputs and exercised call. + assert hardware["engine"]["pid"] == 7 + # What: assert that tmp path warm a hardware json read bytes equals captured; why: this assertion protects the native router benchmark captures private hardware observation regression after the test's arranged inputs and exercised call. + assert (tmp_path / "warm-a.hardware.json").read_bytes() == captured + + +# What: define the test_native_router_benchmark_validates_same_process_re_adoption test around native router qualifier; why: this test groups the arrange, act, and assertions that protect the native router benchmark validates same process re adoption outcome. +def test_native_router_benchmark_validates_same_process_re_adoption(native_router_qualifier): + # What: arrange before as running and pid and port and adopted and true; why: the native router benchmark validates same process re adoption test consumes this named precondition before exercising the behavior. + before = {"running": True, "pid": 41, "port": 24567, "adopted": False} + # What: arrange after as running and pid and port and adopted and true; why: the native router benchmark validates same process re adoption test consumes this named precondition before exercising the behavior. + after = {"running": True, "pid": 41, "port": 24567, "adopted": True} + # What: arrange router as active profile and active identity matches engine and activations and model a and true; why: the native router benchmark validates same process re adoption test consumes this named precondition before exercising the behavior. + router = { + # What: arrange the active profile field as model a; why: test_native_router_benchmark_validates_same_process_re_adoption carries active profile through router into assert native router qualifier validate re adoption before after router equals. + "activeProfile": "model-a", "activeIdentityMatchesEngine": True, + # What: arrange the activations field as 0; why: test_native_router_benchmark_validates_same_process_re_adoption carries activations through router into assert native router qualifier validate re adoption before after router equals. + "activations": 0, + # What: arrange the router mapping with active profile and active identity matches engine and activations; why: test_native_router_benchmark_validates_same_process_re_adoption groups the supplied clauses as one router mapping before its value is consumed. + } + + # What: assert the expected native router qualifier validate re adoption before after router == outcome; why: test swap qualification test protects its regression by requiring this observable result after the exercised behavior. + assert native_router_qualifier.validate_re_adoption(before, after, router) == { + # What: arrange profile model a samePid True samePort True for the scenario; why: test swap qualification test native router benchmark validates same process re adoption requires this concrete input or helper state before exercising the behavior under test. + "profile": "model-a", "samePid": True, "samePort": True, + # What: arrange managerAdopted True activationDelta 0 for the scenario; why: test swap qualification test native router benchmark validates same process re adoption requires this concrete input or helper state before exercising the behavior under test. + "managerAdopted": True, "activationDelta": 0, + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router benchmark validates same process re adoption requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert the pytest.raises failure context; why: the native router benchmark validates same process re adoption scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError, match="exact adopted residency"): + # What: act by calling native_router_qualifier.validate_re_adoption with before and after and pid and 42 and router; why: the native router benchmark validates same process re adoption scenario observes the native_router_qualifier.validate_re_adoption return value during before after pid router. + native_router_qualifier.validate_re_adoption( + # What: arrange the pid field as 42; why: test_native_router_benchmark_validates_same_process_re_adoption carries pid into before, {**after, "pid": 42}, router. + before, {**after, "pid": 42}, router + # What: arrange the native_router_qualifier.validate_re_adoption call with before and after and router; why: test_native_router_benchmark_validates_same_process_re_adoption groups the supplied clauses as one native_router_qualifier.validate_re_adoption call before its value is consumed. + ) + + +# What: parameterize test_native_router_benchmark_rejects_incomplete_hardware_observation with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test native router benchmark rejects incomplete hardware observation. +@pytest.mark.parametrize( + # What: arrange the hardware portion of the enclosing predicate; why: this clause remains in the native router benchmark rejects incomplete hardware observation scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "hardware", + # What: arrange the grouped source fragment for the scenario; why: test native router benchmark rejects incomplete hardware observation requires this concrete input or helper state before exercising the behavior under test. + [ + # What: arrange the engine field as running and pid and port and false and 7; why: test_native_router_benchmark_rejects_incomplete_hardware_observation carries engine into {"engine": {"running": False, "pid": 7, "port": 1234}, "memory": {"ramBy. + {"engine": {"running": False, "pid": 7, "port": 1234}, "memory": {"ramBytes": 3, "vramBytes": 4}}, + # What: arrange the engine field as running and pid and port and true and 1234; why: test_native_router_benchmark_rejects_incomplete_hardware_observation carries engine into {"engine": {"running": True, "pid": None, "port": 1234}, "memory": {"ram. + {"engine": {"running": True, "pid": None, "port": 1234}, "memory": {"ramBytes": 3, "vramBytes": 4}}, + # What: arrange the engine field as running and pid and port and true and 0; why: test_native_router_benchmark_rejects_incomplete_hardware_observation carries engine into {"engine": {"running": True, "pid": 0, "port": 1234}, "memory": {"ramByt. + {"engine": {"running": True, "pid": 0, "port": 1234}, "memory": {"ramBytes": 3, "vramBytes": 4}}, + # What: arrange engine running True pid 7 port 0 memory ramBytes 3 vramBytes 4 for the scenario; why: test swap qualification test native router benchmark rejects incomplete hardware observation requires this concrete input or helper state before exercising the behavior under test. + {"engine": {"running": True, "pid": 7, "port": 0}, "memory": {"ramBytes": 3, "vramBytes": 4}}, + # What: arrange engine running True pid 7 port 1234 memory ramBytes None vramBytes 4 for the scenario; why: test swap qualification test native router benchmark rejects incomplete hardware observation requires this concrete input or helper state before exercising the behavior under test. + {"engine": {"running": True, "pid": 7, "port": 1234}, "memory": {"ramBytes": None, "vramBytes": 4}}, + # What: arrange the grouped source fragment for the scenario; why: test native router benchmark rejects incomplete hardware observation requires this concrete input or helper state before exercising the behavior under test. + ], +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_native_router_benchmark_rejects_incomplete_hardware_observation groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +) +# What: define the test_native_router_benchmark_rejects_incomplete_hardware_observation test around native router qualifier and monkeypatch and tmp path and hardware; why: this test groups the arrange, act, and assertions that protect the native router benchmark rejects incomplete hardware observation outcome. +def test_native_router_benchmark_rejects_incomplete_hardware_observation( + # What: arrange native router qualifier monkeypatch tmp path hardware for the scenario; why: test native router benchmark rejects incomplete hardware observation requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch, tmp_path, hardware +# What: arrange the grouped source fragment for the scenario; why: test native router benchmark rejects incomplete hardware observation requires this concrete input. +): + # What: arrange the exact monkeypatch setattr native router qualifier request json lambda a k fixture fragment; why: the native router benchmark rejects incomplete hardware observation scenario feeds this byte-preserved fragment through monkeypatch.setattr(native_router_qualifier, "request_json", lambda *a, before. + monkeypatch.setattr(native_router_qualifier, "request_json", lambda *a, **k: (b"{}", hardware)) + # What: assert the pytest.raises failure context; why: the native router benchmark rejects incomplete hardware observation scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError): + # What: arrange the exact native router qualifier capture hardware http test tmp path bad fixture fragment; why: the native router benchmark rejects incomplete hardware observation scenario feeds this byte-preserved fragment through native_router_qualifier.capture_hardware("http://test", tmp_path, "bad") befor. + native_router_qualifier.capture_hardware("http://test", tmp_path, "bad") + + +# What: parameterize test_native_router_benchmark_rejects_unmeasured_hardware_values with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test native router benchmark rejects unmeasured hardware values. +@pytest.mark.parametrize( + # What: arrange the field value portion of the enclosing predicate; why: this clause remains in the native router benchmark rejects unmeasured hardware values scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "field,value", + # What: arrange the vram available ram bytes vram source portion of the enclosing predicate; why: this clause remains in the native router benchmark rejects unmeasured hardware values scenario\'s enclosing expression so its grouping and evaluation order stay intact. + [("vramAvailable", False), ("ramBytes", 0), ("vramSource", None)], +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_native_router_benchmark_rejects_unmeasured_hardware_values groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +) +# What: define the test_native_router_benchmark_rejects_unmeasured_hardware_values test around native router qualifier and monkeypatch and tmp path and field and value; why: this test groups the arrange, act, and assertions that protect the native router benchmark rejects unmeasured hardware values outcome. +def test_native_router_benchmark_rejects_unmeasured_hardware_values( + # What: arrange native router qualifier monkeypatch tmp path field value for the scenario; why: test native router benchmark rejects requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, monkeypatch, tmp_path, field, value +# What: arrange the grouped source fragment for the scenario; why: test native router benchmark rejects unmeasured hardware values requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange memory as ram bytes and vram bytes and ram available and vram available and ram source; why: the native router benchmark rejects unmeasured hardware values test consumes this named precondition before exercising the behavior. + memory = { + # What: arrange the ram bytes field as 3; why: test_native_router_benchmark_rejects_unmeasured_hardware_values carries ram bytes through memory into memory field value. + "ramBytes": 3, + # What: arrange the vram bytes field as 4; why: test_native_router_benchmark_rejects_unmeasured_hardware_values carries vram bytes through memory into memory field value. + "vramBytes": 4, + # What: arrange the ram available field as true; why: test_native_router_benchmark_rejects_unmeasured_hardware_values carries ram available through memory into memory field value. + "ramAvailable": True, + # What: arrange the vram available field as true; why: test_native_router_benchmark_rejects_unmeasured_hardware_values carries vram available through memory into memory field value. + "vramAvailable": True, + # What: arrange the ram source field as proc smaps rollup pss; why: test_native_router_benchmark_rejects_unmeasured_hardware_values carries ram source through memory into memory field value. + "ramSource": "proc-smaps-rollup-pss", + # What: arrange the vram source field as amd smi; why: test_native_router_benchmark_rejects_unmeasured_hardware_values carries vram source through memory into memory field value. + "vramSource": "amd-smi", + # What: arrange the memory mapping with ram bytes and vram bytes and ram available and vram available and ram source; why: test_native_router_benchmark_rejects_unmeasured_hardware_values groups the supplied clauses as one memory mapping before its value is consumed. + } + # What: arrange memory entry as value; why: the native router benchmark rejects unmeasured hardware values test consumes this named precondition before exercising the behavior. + memory[field] = value + # What: arrange hardware as memory and engine and memory and running and pid; why: the native router benchmark rejects unmeasured hardware values test consumes this named precondition before exercising the behavior. + hardware = { + # What: arrange the running field as true; why: test_native_router_benchmark_rejects_unmeasured_hardware_values carries running through hardware into native router qualifier request json lambda args kwargs b hardware. + "engine": {"running": True, "pid": 7, "port": 1234}, + # What: arrange the memory field as memory; why: test_native_router_benchmark_rejects_unmeasured_hardware_values carries memory through hardware into native router qualifier request json lambda args kwargs b hardware. + "memory": memory, + # What: arrange the hardware mapping with engine and memory; why: test_native_router_benchmark_rejects_unmeasured_hardware_values groups the supplied clauses as one hardware mapping before its value is consumed. + } + # What: act by calling monkeypatch.setattr with native router qualifier and request json and hardware; why: the native router benchmark rejects unmeasured hardware values scenario observes the monkeypatch.setattr return value during native router qualifier request json lambda args kwargs b. + monkeypatch.setattr( + # What: arrange the exact native router qualifier request json lambda args kwargs b fixture fragment; why: the native router benchmark rejects unmeasured hardware values scenario feeds this byte-preserved fragment through native_router_qualifier, "request_json", lambda *args, **kwargs: (b"{}", before asserting. + native_router_qualifier, "request_json", lambda *args, **kwargs: (b"{}", hardware) + # What: arrange the monkeypatch.setattr call with native router qualifier and hardware; why: test_native_router_benchmark_rejects_unmeasured_hardware_values groups the supplied clauses as one monkeypatch.setattr call before its value is consumed. + ) + + # What: assert the pytest.raises failure context; why: the native router benchmark rejects unmeasured hardware values scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError): + # What: arrange the exact native router qualifier capture hardware http test tmp path bad fixture fragment; why: the native router benchmark rejects unmeasured hardware values scenario feeds this byte-preserved fragment through native_router_qualifier.capture_hardware("http://test", tmp_path, "bad") before ass. + native_router_qualifier.capture_hardware("http://test", tmp_path, "bad") + + +# What: define the test_native_router_benchmark_requires_final_engine_listener_to_close test around native router qualifier; why: this test groups the arrange, act, and assertions that protect the native router benchmark requires final engine listener to close outcome. +def test_native_router_benchmark_requires_final_engine_listener_to_close(native_router_qualifier): + # What: enter the socket.socket managed context before listener bind; why: test_native_router_benchmark_requires_final_engine_listener_to_close releases this resource or lock after listener bind on both success and failure paths. + with socket.socket() as listener: + # What: arrange the exact listener bind fixture fragment; why: the native router benchmark requires final engine listener to close scenario feeds this byte-preserved fragment through listener.bind(("127.0.0.1", 0)) before asserting its protocol or parser result. + listener.bind(("127.0.0.1", 0)) + # What: act by calling listener.listen with the declared inputs; why: the native router benchmark requires final engine listener to close scenario observes the listener.listen return value during with pytest raises runtime error match listener. + listener.listen() + # What: assert the pytest.raises failure context; why: the native router benchmark requires final engine listener to close scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError, match="listener"): + # What: act by calling native_router_qualifier.require_listener_closed with getsockname and listener and 1; why: the native router benchmark requires final engine listener to close scenario observes the native_router_qualifier.require_listener_closed return value during port listener getsockname. + native_router_qualifier.require_listener_closed(listener.getsockname()[1]) + # What: act by calling listener.getsockname and capture port; why: the native router benchmark requires final engine listener to close test asserts the response, state, or failure produced by this call. + port = listener.getsockname()[1] + # What: act by calling native_router_qualifier.require_listener_closed with port; why: the native router benchmark requires final engine listener to close scenario observes the native_router_qualifier.require_listener_closed return value during the enclosing return. + native_router_qualifier.require_listener_closed(port) + + +# What: define the test_native_router_benchmark_generates_a_valid_dynamic_port_catalog test around native router qualifier and tmp path; why: this test groups the arrange, act, and assertions that protect the native router benchmark generates a valid dynamic port catalog outcome. +def test_native_router_benchmark_generates_a_valid_dynamic_port_catalog(native_router_qualifier, tmp_path): + # What: arrange catalog path as tmp path and models and toml; why: the native router benchmark generates a valid dynamic port catalog test consumes this named precondition before exercising the behavior. + catalog_path = tmp_path / "models.toml" + # What: arrange catalog path write text for the scenario; why: test native router benchmark generates a valid dynamic port catalog requires this concrete input or helper state before exercising the behavior under test. + catalog_path.write_text( + # What: act by calling native_router_qualifier.native_catalog_text with first and gguf and second and gguf; why: the native router benchmark generates a valid dynamic port catalog scenario observes the native_router_qualifier.native_catalog_text return value during first gguf second gguf api key private key. + native_router_qualifier.native_catalog_text( + # What: arrange the exact first gguf second gguf api key private key fixture fragment; why: the native router benchmark generates a valid dynamic port catalog scenario feeds this byte-preserved fragment through "first.gguf", "second.gguf", api_key="private-key" before asserting its protocol or parser resul. + "first.gguf", "second.gguf", api_key="private-key" + # What: arrange the native_router_qualifier.native_catalog_text call with api key; why: test_native_router_benchmark_generates_a_valid_dynamic_port_catalog groups the supplied clauses as one native_router_qualifier.native_catalog_text call before its value is consumed. + ), + # What: arrange the exact encoding utf 8 fixture fragment; why: the native router benchmark generates a valid dynamic port catalog scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the catalog_path.write_text call with encoding; why: test_native_router_benchmark_generates_a_valid_dynamic_port_catalog groups the supplied clauses as one catalog_path.write_text call before its value is consumed. + ) + + # What: act by calling ModelCatalog.load and capture catalog; why: the native router benchmark generates a valid dynamic port catalog test asserts the response, state, or failure produced by this call. + catalog = ModelCatalog.load(str(catalog_path)) + + # What: assert that catalog settings upstream timeout s equals 660; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.settings.upstream_timeout_s == 660 + # What: assert that catalog settings api keys equals private key; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.settings.api_keys == ("private-key",) + # What: assert that catalog settings include aliases in list is true; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.settings.include_aliases_in_list is True + # What: assert that catalog settings send loading state is true; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.settings.send_loading_state is True + # What: assert that catalog get model a model equals first gguf; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("model-a").model == "first.gguf" + # What: assert that catalog get model a port equals 0; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("model-a").port == 0 + # What: assert that catalog get model a ttl s equals 0; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("model-a").ttl_s == 0 + # What: assert that catalog get model a check endpoint equals ready; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("model-a").check_endpoint == "/ready" + # What: assert that catalog get model a proxy equals http 127 0 0 1 port; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("model-a").proxy == "http://127.0.0.1:${PORT}" + # What: assert that catalog get model a use model name equals model a; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("model-a").use_model_name == "model-a" + # What: assert that catalog get model a upstream timeout s equals 659; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("model-a").upstream_timeout_s == 659 + # What: assert that catalog get model a display name equals qualification model a; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("model-a").display_name == "Qualification model A" + # What: assert the expected catalog get model a metadata == outcome; why: test swap qualification test native router benchmark generates a valid dynamic port catalog protects its regression by requiring this observable result after the exercised behavior. + assert catalog.get("model-a").metadata() == { + # What: arrange tier qualification type operator for the scenario; why: test swap qualification test native router benchmark generates a valid dynamic port catalog requires this concrete input or helper state before exercising the behavior under test. + "tier": "qualification", "type": "operator", + # What: arrange the grouped source fragment for the scenario; why: test swap qualification test native router benchmark generates a valid dynamic port catalog requires this concrete input or helper state before exercising the behavior under test. + } + # What: assert that catalog get compat model a name equals model a; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("compat/model-a").name == "model-a" + # What: assert that model a is present in catalog get model a args; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert "model-a" in catalog.get("model-a").args + # What: assert that catalog get model b model equals second gguf; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert catalog.get("model-b").model == "second.gguf" + # What: act by calling catalog.selector and capture selector; why: the native router benchmark generates a valid dynamic port catalog test asserts the response, state, or failure produced by this call. + selector = catalog.selector("preferred-model") + # What: assert that selector is not group delimiter; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert selector is not None + # What: assert that selector strategy equals warm; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert selector.strategy == "warm" + # What: assert that selector targets equals model b model a; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert selector.targets == ("model-b", "model-a") + # What: act by calling catalog.routing_profile and capture routing profile; why: the native router benchmark generates a valid dynamic port catalog test asserts the response, state, or failure produced by this call. + routing_profile = catalog.routing_profile("coding") + # What: assert that routing profile is not group delimiter; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert routing_profile is not None + # What: assert that routing profile replacement profile model equals true preferred model; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert routing_profile.replacement("profile-model") == (True, "preferred-model") + # What: assert that routing profile replacement disabled model equals true; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert routing_profile.replacement("disabled-model") == (True, None) + + # What: arrange startup path as tmp path and startup models and toml; why: the native router benchmark generates a valid dynamic port catalog test consumes this named precondition before exercising the behavior. + startup_path = tmp_path / "startup-models.toml" + # What: arrange startup path write text for the scenario; why: test native router benchmark generates a valid dynamic port catalog requires this concrete input or helper state before exercising the behavior under test. + startup_path.write_text( + # What: act by calling native_router_qualifier.native_catalog_text with first and gguf and second and gguf; why: the native router benchmark generates a valid dynamic port catalog scenario observes the native_router_qualifier.native_catalog_text return value during first gguf second gguf startup. + native_router_qualifier.native_catalog_text( + # What: arrange the exact first gguf second gguf startup fixture fragment; why: the native router benchmark generates a valid dynamic port catalog scenario feeds this byte-preserved fragment through "first.gguf", "second.gguf", startup=True before asserting its protocol or parser result. + "first.gguf", "second.gguf", startup=True + # What: arrange the native_router_qualifier.native_catalog_text call with startup; why: test_native_router_benchmark_generates_a_valid_dynamic_port_catalog groups the supplied clauses as one native_router_qualifier.native_catalog_text call before its value is consumed. + ), + # What: arrange the exact encoding utf 8 fixture fragment; why: the native router benchmark generates a valid dynamic port catalog scenario feeds this byte-preserved fragment through encoding="utf-8" before asserting its protocol or parser result. + encoding="utf-8", + # What: arrange the startup_path.write_text call with encoding; why: test_native_router_benchmark_generates_a_valid_dynamic_port_catalog groups the supplied clauses as one startup_path.write_text call before its value is consumed. + ) + # What: act by calling ModelCatalog.load and capture startup; why: the native router benchmark generates a valid dynamic port catalog test asserts the response, state, or failure produced by this call. + startup = ModelCatalog.load(str(startup_path)) + # What: assert that startup settings preload model equals model a; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert startup.settings.preload_model == "model-a" + # What: assert that startup settings startup routing profile equals coding; why: this assertion protects the native router benchmark generates a valid dynamic port catalog regression after the test's arranged inputs and exercised call. + assert startup.settings.startup_routing_profile == "coding" + + +# What: parameterize test_native_router_benchmark_rejects_mislabeled_routed_trials with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test native router benchmark rejects mislabeled routed trials. +@pytest.mark.parametrize( + # What: arrange the status alias prior delta portion of the enclosing predicate; why: this clause remains in the native router benchmark rejects mislabeled routed trials scenario\'s enclosing expression so its grouping and evaluation order stay intact. + "status,alias,prior,delta", + # What: arrange the grouped source fragment for the scenario; why: test native router benchmark rejects mislabeled routed trials requires this concrete input or helper state before exercising the behavior under test. + [ + # What: arrange activeProfile model a activeRequests 1 activations 1 model a 1 0 for the scenario; why: test swap qualification test native router benchmark rejects mislabeled routed trials requires this concrete input or helper state before exercising the behavior under test. + ({"activeProfile": "model-a", "activeRequests": 1, "activations": 1}, "model-a", 1, 0), + # What: arrange the active profile field as model b; why: test_native_router_benchmark_rejects_mislabeled_routed_trials carries active profile into ({"activeProfile": "model-b", "activeRequests": 0, "activations": 1}, "m. + ({"activeProfile": "model-b", "activeRequests": 0, "activations": 1}, "model-a", 1, 0), + # What: arrange activeProfile model a activeRequests 0 activations 2 model a 1 0 for the scenario; why: test swap qualification test native router benchmark rejects mislabeled routed trials requires this concrete input or helper state before exercising the behavior under test. + ({"activeProfile": "model-a", "activeRequests": 0, "activations": 2}, "model-a", 1, 0), + # What: arrange the grouped source fragment for the scenario; why: test native router benchmark rejects mislabeled routed trials requires this concrete input or helper state before exercising the behavior under test. + ], +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_native_router_benchmark_rejects_mislabeled_routed_trials groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +) +# What: define the test_native_router_benchmark_rejects_mislabeled_routed_trials test around native router qualifier and status and alias and prior and delta; why: this test groups the arrange, act, and assertions that protect the native router benchmark rejects mislabeled routed trials outcome. +def test_native_router_benchmark_rejects_mislabeled_routed_trials( + # What: arrange native router qualifier status alias prior delta for the scenario; why: test native router benchmark rejects mislabeled routed trials requires this concrete input or helper state before exercising the behavior under test. + native_router_qualifier, status, alias, prior, delta +# What: arrange the grouped source fragment for the scenario; why: test native router benchmark rejects mislabeled routed trials requires this concrete input or helper state before exercising the behavior under test. +): + # What: assert the pytest.raises failure context; why: the native router benchmark rejects mislabeled routed trials scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(RuntimeError): + # What: act by calling native_router_qualifier.validate_routed_trial with status; why: the native router benchmark rejects mislabeled routed trials scenario observes the native_router_qualifier.validate_routed_trial return value during status alias alias prior activations prior expected delta. + native_router_qualifier.validate_routed_trial( + # What: arrange alias to native_router_qualifier.validate_routed_trial; why: the native router benchmark rejects mislabeled routed trials scenario binds this alias value to native_router_qualifier.validate_routed_trial's alias input. + status, alias=alias, prior_activations=prior, expected_delta=delta + # What: arrange the grouped source fragment for the scenario; why: test native router benchmark rejects mislabeled routed trials requires this concrete input or helper state before exercising the behavior. + ) diff --git a/tests/daemon/test_swap_regressions.py b/tests/daemon/test_swap_regressions.py new file mode 100644 index 000000000..3b9f497fd --- /dev/null +++ b/tests/daemon/test_swap_regressions.py @@ -0,0 +1,343 @@ +"""Swap boundary regressions, runnable without the GPU runtime.""" +# What: document swap boundary regressions runnable without the in the test_swap_regressions docstring; why: introspection and maintainers read this exact docstring fragment to understand test swap regressions behavior without executing it. + +# What: import ast for test readiness http contract using ast; why: test_readiness_http_contract uses ast parse, making that imported dependency available to its named operation. +import ast +# What: import json for test routing profile client uses atomic selection endpoint using json; why: test_routing_profile_client_uses_atomic_selection_endpoint uses json loads, making that imported dependency available to its named operation. +import json +# What: import threading for test profile readiness failure recovery end to end using threading; why: test_profile_readiness_failure_recovery_end_to_end uses threading event, making that imported dependency available to its named operation. +import threading +# What: import thread pool executor for test profile readiness failure recovery end to end using concurrent and futures and thread pool executor; why: test_profile_readiness_failure_recovery_end_to_end uses thread pool executor, making that imported dependency available to its named operation. +from concurrent.futures import ThreadPoolExecutor +# What: import path for test readiness http contract using pathlib and path; why: test_readiness_http_contract uses path, making that imported dependency available to its named operation. +from pathlib import Path + +# What: import pytest for module initialization using pytest; why: module initialization uses pytest mark parametrize, making that imported dependency available to its named operation. +import pytest +# What: import fast api for test readiness http contract using fastapi and fast api; why: test_readiness_http_contract uses fast api, making that imported dependency available to its named operation. +from fastapi import FastAPI +# What: import test client for test readiness http contract using fastapi and testclient and test client; why: test_readiness_http_contract uses test client, making that imported dependency available to its named operation. +from fastapi.testclient import TestClient + +# What: arrange from freetoken daemon catalog import CatalogError ModelCatalog for the scenario; why: test swap regressions requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon.catalog import CatalogError, ModelCatalog +# What: arrange from freetoken daemon import client as daemon client for the scenario; why: test swap regressions requires this concrete input or helper state before exercising the behavior under test. +from freetoken.daemon import client as daemon_client +# What: import wait for ready for test readiness rechecks generation after probe using freetoken and daemon and readiness and wait for ready; why: test_readiness_rechecks_generation_after_probe uses wait for ready, making that imported dependency available to its named operation. +from freetoken.daemon.readiness import wait_for_ready +# What: import serve probe for test fresh health does not reuse previous model cache using freetoken and daemon and proxy and serve probe; why: test_fresh_health_does_not_reuse_previous_model_cache uses serve probe, making that imported dependency available to its named operation. +from freetoken.daemon.proxy import ServeProbe +# What: import build app for test profile readiness failure recovery end to end using freetoken and daemon and app and build app; why: test_profile_readiness_failure_recovery_end_to_end uses build app, making that imported dependency available to its named operation. +from freetoken.daemon.app import build_app +# What: import log ring for test switch launch recovery is 503 not success using freetoken and daemon and logring and log ring; why: test_switch_launch_recovery_is_503_not_success uses log ring, making that imported dependency available to its named operation. +from freetoken.daemon.logring import LogRing +# What: import switch launch error for switch using freetoken and daemon and serve manager and switch launch error; why: switch uses switch launch error, making that imported dependency available to its named operation. +from freetoken.daemon.serve_manager import SwitchLaunchError +# What: arrange from tests daemon test daemon serve manager import Spawner make manager for the scenario; why: test daemon serve manager import spawner make manager in test requires this concrete input or helper state before exercising the behavior under test. +from tests.daemon.test_daemon_serve_manager import Spawner, make_manager + + +# What: parameterize test_profile_readiness_failure_recovery_end_to_end with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test profile readiness failure recovery end to end. +@pytest.mark.parametrize("failure", ["error", "timeout", "operator-stop", "recovery-error"]) +# What: define the test_profile_readiness_failure_recovery_end_to_end test around tmp path and failure; why: this test groups the arrange, act, and assertions that protect the profile readiness failure recovery end to end outcome. +def test_profile_readiness_failure_recovery_end_to_end(tmp_path, failure): + # What: act by calling Spawner and capture sp; why: the profile readiness failure recovery end to end test asserts the response, state, or failure produced by this call. + sp = Spawner() + # What: act by calling make_manager and capture manager and and ring; why: the profile readiness failure recovery end to end test asserts the response, state, or failure produced by this call. + manager, _, ring = make_manager(tmp_path, sp, + # What: act by evaluating signal fn lambda pid sig sp by pid pid die; why: test swap regressions test profile readiness failure recovery end to end captures the behavior or response that its following assertions inspect. + signal_fn=lambda pid, sig: sp.by_pid(pid).die()) + # What: arrange the exact manager start previous original fixture fragment; why: the profile readiness failure recovery end to end scenario feeds this byte-preserved fragment through manager.start("previous", 1922, ["--original"]) before asserting its protocol or parser result. + manager.start("previous", 1922, ["--original"]) + # What: arrange path as tmp path and models and toml; why: the profile readiness failure recovery end to end test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models bad nmodel replacement nport nready timeout s fixture fragment; why: the profile readiness failure recovery end to end scenario feeds this byte-preserved fragment through path.write_text("[models.bad]\nmodel = 'replacement'\nport = 1923\nready before asserting its p. + path.write_text("[models.bad]\nmodel = 'replacement'\nport = 1923\nready_timeout_s = 1\n", + # What: arrange the exact encoding utf 8 fixture fragment; why: the profile readiness failure recovery end to end scenario feeds this byte-preserved fragment through encoding="utf-8") before asserting its protocol or parser result. + encoding="utf-8") + # What: act by calling threading.Event and capture entered and release; why: the profile readiness failure recovery end to end test asserts the response, state, or failure produced by this call. + entered, release = threading.Event(), threading.Event() + + # What: define Probe as the owner of fresh_health; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: arrange the def fresh health self port test helper boundary; why: test swap regressions test profile readiness failure recovery end to end uses this local double to isolate the behavior checked by its assertions. + def fresh_health(self, port): + # What: act on port before status and failure; why: the profile readiness failure recovery end to end scenario admits status and failure only for this predicate and excludes the opposite state. + if port == 1922: + # What: arrange status as failure and error and ok and recovery error; why: the profile readiness failure recovery end to end test consumes this named precondition before exercising the behavior. + status = "error" if failure == "recovery-error" else "ok" + # What: act on failure before set and entered; why: the profile readiness failure recovery end to end scenario admits set and entered only for this predicate and excludes the opposite state. + elif failure == "operator-stop": + # What: act by calling entered.set with the declared inputs; why: the profile readiness failure recovery end to end scenario observes the entered.set return value during assert release wait. + entered.set() + # What: assert that release wait 5; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert release.wait(5) + # What: arrange status as error; why: the profile readiness failure recovery end to end test consumes this named precondition before exercising the behavior. + status = "error" + # What: select the remaining branch that performs status loading if failure timeout else; why: fresh_health covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: arrange status as failure and loading and error and timeout; why: the profile readiness failure recovery end to end test consumes this named precondition before exercising the behavior. + status = "loading" if failure == "timeout" else "error" + # What: arrange the helper response as reachable True status status maintenance serving; why: test swap regressions test profile readiness failure recovery end to end feeds this result into the behavior whose outcome is asserted. + return {"reachable": True, "status": status, "maintenance": "serving"} + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app manager manager ring ring; why: test_profile_readiness_failure_recovery_end_to_end releases this resource or lock after app build app manager manager ring ring on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the profile readiness failure recovery end to end test asserts the response, state, or failure produced by this call. + app = build_app(manager=manager, ring=ring, probe=Probe(), + # What: arrange footprint fn lambda pid lifecycle pool lifecycle for the scenario; why: test swap regressions test profile readiness failure recovery end to end requires this concrete input or helper state before exercising the behavior under test. + footprint_fn=lambda pid: {}, lifecycle_pool=lifecycle, + # What: arrange proxy pool to ModelCatalog.load; why: the profile readiness failure recovery end to end scenario binds this proxy value to ModelCatalog.load's proxy pool input. + proxy_pool=proxy, catalog=ModelCatalog.load(str(path))) + # What: arrange with TestClient app as client ThreadPoolExecutor 1 as requests for the scenario; why: test client app as client thread pool executor 1 as in test requires this concrete input or helper state before exercising the behavior under test. + with TestClient(app) as client, ThreadPoolExecutor(1) as requests: + # What: act by calling requests.submit and capture response task; why: the profile readiness failure recovery end to end test asserts the response, state, or failure produced by this call. + response_task = requests.submit(client.post, "/engine/switch-profile", json={"name": "bad"}) + # What: act on failure before wait and status code and set and entered and release; why: the profile readiness failure recovery end to end scenario admits wait and status code and set and entered and release only for this predicate and excludes the opposite state. + if failure == "operator-stop": + # What: establish the handler boundary for the protected operation; why: test_profile_readiness_failure_recovery_end_to_end routes failures to the unconditional cleanup block while preserving cleanup and success flow. + try: + # What: assert that entered wait 5; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert entered.wait(5) + # The only proxy worker is blocked, but lifecycle remains available. + # What: assert that client post engine stop json status code equals 200; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert client.post("/engine/stop", json={}).status_code == 200 + # What: run release set on every exit path; why: test_profile_readiness_failure_recovery_end_to_end performs this cleanup after success, rejection, or exception so resources and accounting cannot remain stranded. + finally: + # What: act by calling release.set with the declared inputs; why: the profile readiness failure recovery end to end scenario observes the release.set return value during response response task result timeout. + release.set() + # What: act by calling response_task.result and capture response; why: the profile readiness failure recovery end to end test asserts the response, state, or failure produced by this call. + response = response_task.result(timeout=10) + # What: assert that response status code equals 503; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert response.status_code == 503 + # What: act by calling response.json and capture doc; why: the profile readiness failure recovery end to end test asserts the response, state, or failure produced by this call. + doc = response.json() + # What: assert that doc readiness ready is false; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert not doc["readiness"]["ready"] + # What: act on failure before doc; why: the profile readiness failure recovery end to end scenario admits doc only for this predicate and excludes the opposite state. + if failure == "operator-stop": + # What: assert that doc rollback reason equals superseded; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert doc["rollback"]["reason"] == "superseded" + # What: assert that manager status running is false; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert not manager.status()["running"] + # What: select the remaining branch that performs assert doc rollback launched; why: test_profile_readiness_failure_recovery_end_to_end covers the state excluded by the preceding predicate without conflating the two outcomes. + else: + # What: assert that doc rollback launched; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert doc["rollback"]["launched"] + # What: assert that doc rollback readiness ready is failure differs from recovery error; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert doc["rollback"]["readiness"]["ready"] is (failure != "recovery-error") + # What: assert that manager status model equals previous; why: this assertion protects the profile readiness failure recovery end to end regression after the test's arranged inputs and exercised call. + assert manager.status()["model"] == "previous" + # What: act by calling manager.stop with the declared inputs; why: the profile readiness failure recovery end to end scenario observes the manager.stop return value during the enclosing return. + manager.stop() + + +# What: parameterize test_switch_launch_recovery_is_503_not_success with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test switch launch recovery is 503 not success. +@pytest.mark.parametrize("route,body", [ + # What: arrange the model field as bad; why: test_switch_launch_recovery_is_503_not_success sends this field through ("/engine/switch", {"model": "bad"}) so the router selects the canonical model or alias for upstream dispatch. + ("/engine/switch", {"model": "bad"}), + # What: arrange the name field as bad; why: test_switch_launch_recovery_is_503_not_success carries name into ("/engine/switch-profile", {"name": "bad"}). + ("/engine/switch-profile", {"name": "bad"}), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_switch_launch_recovery_is_503_not_success groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_switch_launch_recovery_is_503_not_success test around tmp path and route and body; why: this test groups the arrange, act, and assertions that protect the switch launch recovery is 503 not success outcome. +def test_switch_launch_recovery_is_503_not_success(tmp_path, route, body): + # What: arrange path as tmp path and models and toml; why: the switch launch recovery is 503 not success test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text models bad nmodel bad n encoding fixture fragment; why: the switch launch recovery is 503 not success scenario feeds this byte-preserved fragment through path.write_text("[models.bad]\nmodel = 'bad'\n", encoding="utf-8") before asserting its protocol or parser result. + path.write_text("[models.bad]\nmodel = 'bad'\n", encoding="utf-8") + + # What: define Manager as the owner of status and switch; why: daemon callers use this class boundary so those methods share one manager state invariant. + class Manager: + # What: define the status test helper around captured fixture state; why: the switch launch recovery is 503 not success scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def status(self): + # What: arrange the port field as 1922; why: Manager.status carries port into return {"port": 1922}. + return {"port": 1922} + + # What: define the switch test helper around captured fixture state; why: the switch launch recovery is 503 not success scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def switch(self, *args): + # What: raise SwitchLaunchError for the caller; why: Manager.switch stops this rejected path before it can mutate state, dispatch work, or report success. + raise SwitchLaunchError(OSError("failed"), + # What: arrange the attempted field as true; why: Manager.switch carries attempted into {"attempted": True, "launched": True, "pid": 42}, None). + {"attempted": True, "launched": True, "pid": 42}, None) + + # What: arrange switch for readiness as switch; why: the switch launch recovery is 503 not success test consumes this named precondition before exercising the behavior. + switch_for_readiness = switch + + # What: enter the ThreadPoolExecutor and ThreadPoolExecutor managed context before app build app manager manager ring log ring; why: test_switch_launch_recovery_is_503_not_success releases this resource or lock after app build app manager manager ring log ring on both success and failure paths. + with ThreadPoolExecutor(1) as lifecycle, ThreadPoolExecutor(1) as proxy: + # What: act by calling build_app and capture app; why: the switch launch recovery is 503 not success test asserts the response, state, or failure produced by this call. + app = build_app(manager=Manager(), ring=LogRing(), probe=None, + # What: arrange the pid input for test_switch_launch_recovery_is_503_not_success; why: test_switch_launch_recovery_is_503_not_success consumes pid during signature binding, so callers must bind it with the other signature inputs. + footprint_fn=lambda pid: {}, lifecycle_pool=lifecycle, + # What: arrange proxy pool to ModelCatalog.load; why: the switch launch recovery is 503 not success scenario binds this proxy value to ModelCatalog.load's proxy pool input. + proxy_pool=proxy, catalog=ModelCatalog.load(str(path))) + # What: enter the TestClient managed context before response client post route json body; why: test_switch_launch_recovery_is_503_not_success releases this resource or lock after response client post route json body on both success and failure paths. + with TestClient(app) as client: + # What: act by calling client.post and capture response; why: the switch launch recovery is 503 not success test asserts the response, state, or failure produced by this call. + response = client.post(route, json=body) + # What: assert that response status code equals 503; why: this assertion protects the switch launch recovery is 503 not success regression after the test's arranged inputs and exercised call. + assert response.status_code == 503 + # What: assert that response json code equals switch launch failed; why: this assertion protects the switch launch recovery is 503 not success regression after the test's arranged inputs and exercised call. + assert response.json()["code"] == "switch_launch_failed" + # What: assert that response json rollback launched is true; why: this assertion protects the switch launch recovery is 503 not success regression after the test's arranged inputs and exercised call. + assert response.json()["rollback"]["launched"] is True + # What: assert that ready is absent from response json rollback; why: this assertion protects the switch launch recovery is 503 not success regression after the test's arranged inputs and exercised call. + assert "ready" not in response.json()["rollback"] + + +# What: parameterize test_catalog_rejects_owned_option_aliases with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test catalog rejects owned option aliases. +@pytest.mark.parametrize("arg", ["--model-path", "--model-path=other", "--model-p", "--mod=other", "--por=8", "--"]) +# What: define the test_catalog_rejects_owned_option_aliases test around tmp path and arg; why: this test groups the arrange, act, and assertions that protect the catalog rejects owned option aliases outcome. +def test_catalog_rejects_owned_option_aliases(tmp_path, arg): + # What: arrange path as tmp path and models and toml; why: the catalog rejects owned option aliases test consumes this named precondition before exercising the behavior. + path = tmp_path / "models.toml" + # What: arrange the exact path write text f models bad nmodel m nargs fixture fragment; why: the catalog rejects owned option aliases scenario feeds this byte-preserved fragment through path.write_text(f"[models.bad]\nmodel = 'm'\nargs = ['{arg}']\n", encodi before asserting its protocol or parser result. + path.write_text(f"[models.bad]\nmodel = 'm'\nargs = ['{arg}']\n", encoding="utf-8") + # What: assert the pytest.raises failure context; why: the catalog rejects owned option aliases scenario rejects the unsafe input through this exact exception boundary. + with pytest.raises(CatalogError, match="must not set"): + # What: act by calling ModelCatalog.load with str and path; why: the catalog rejects owned option aliases scenario observes the ModelCatalog.load return value during the enclosing return. + ModelCatalog.load(str(path)) + + +# What: define the test_readiness_rechecks_generation_after_probe test around local fixtures; why: this test groups the arrange, act, and assertions that protect the readiness rechecks generation after probe outcome. +def test_readiness_rechecks_generation_after_probe(): + # What: define Manager as the owner of status; why: daemon callers use this class boundary so those methods share one manager state invariant. + class Manager: + # What: arrange pid as 44; why: the readiness rechecks generation after probe test consumes this named precondition before exercising the behavior. + pid = 44 + + # What: define the status test helper around captured fixture state; why: the readiness rechecks generation after probe scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def status(self): + # What: arrange the running field as true; why: Manager.status carries running into return {"running": True, "pid": self.pid}. + return {"running": True, "pid": self.pid} + + # What: act by calling Manager and capture manager; why: the readiness rechecks generation after probe test asserts the response, state, or failure produced by this call. + manager = Manager() + + # What: define Probe as the owner of fresh_health; why: daemon callers use this class boundary so those methods share one probe state invariant. + class Probe: + # What: define an uncached health probe for the active engine port; why: readiness checks must bypass a replaced generation's cached response before accepting the new process. + def fresh_health(self, port): + # What: arrange pid as 45; why: the readiness rechecks generation after probe test consumes this named precondition before exercising the behavior. + manager.pid = 45 + # What: arrange the reachable field as true; why: Probe.fresh_health carries reachable into return {"reachable": True, "status": "ok"}. + return {"reachable": True, "status": "ok"} + + # What: act by calling wait_for_ready and capture result; why: the readiness rechecks generation after probe test asserts the response, state, or failure produced by this call. + result = wait_for_ready(manager, Probe(), pid=44, port=1922, timeout_s=1) + # What: assert that result ready is false; why: this assertion protects the readiness rechecks generation after probe regression after the test's arranged inputs and exercised call. + assert result["ready"] is False + # What: assert that result reason equals superseded; why: this assertion protects the readiness rechecks generation after probe regression after the test's arranged inputs and exercised call. + assert result["reason"] == "superseded" + + +# What: define the test_profile_client_reports_legacy_readiness_failure test around monkeypatch; why: this test groups the arrange, act, and assertions that protect the profile client reports legacy readiness failure outcome. +def test_profile_client_reports_legacy_readiness_failure(monkeypatch): + # What: arrange seen as the fixture input; why: the profile client reports legacy readiness failure test consumes this named precondition before exercising the behavior. + seen = {} + + # What: define the request test helper around captured fixture state; why: the profile client reports legacy readiness failure scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request(*args, **kwargs): + # What: act by calling seen.update with kwargs; why: the profile client reports legacy readiness failure scenario observes the seen.update return value during return readiness ready reason engine error. + seen.update(kwargs) + # What: arrange the readiness field as ready and reason and false and engine error; why: request carries readiness into return {"readiness": {"ready": False, "reason": "engine-error"}}. + return {"readiness": {"ready": False, "reason": "engine-error"}} + + # What: arrange the exact monkeypatch setattr daemon client request json request fixture fragment; why: the profile client reports legacy readiness failure scenario feeds this byte-preserved fragment through monkeypatch.setattr(daemon_client, "_request_json", request) before asserting its protocol or parser result. + monkeypatch.setattr(daemon_client, "_request_json", request) + # What: assert that daemon client main start profile coding equals 1; why: this assertion protects the profile client reports legacy readiness failure regression after the test's arranged inputs and exercised call. + assert daemon_client.main(["start-profile", "coding"]) == 1 + # What: assert that seen timeout equals daemon client default profile timeout; why: this assertion protects the profile client reports legacy readiness failure regression after the test's arranged inputs and exercised call. + assert seen["timeout"] == daemon_client.DEFAULT_PROFILE_TIMEOUT + + +# What: parameterize test_routing_profile_client_uses_atomic_selection_endpoint with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test routing profile client uses atomic selection endpoint. +@pytest.mark.parametrize("argv,expected_body", [ + # What: arrange the name field as coding; why: test_routing_profile_client_uses_atomic_selection_endpoint carries name into (["activate-routing-profile", "coding"], {"name": "coding"}). + (["activate-routing-profile", "coding"], {"name": "coding"}), + # What: arrange the name field as the fixture input; why: test_routing_profile_client_uses_atomic_selection_endpoint carries name into (["clear-routing-profile"], {"name": None}). + (["clear-routing-profile"], {"name": None}), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_routing_profile_client_uses_atomic_selection_endpoint groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_routing_profile_client_uses_atomic_selection_endpoint test around monkeypatch and capsys and argv and expected body; why: this test groups the arrange, act, and assertions that protect the routing profile client uses atomic selection endpoint outcome. +def test_routing_profile_client_uses_atomic_selection_endpoint( + # What: arrange the monkeypatch input for test_routing_profile_client_uses_atomic_selection_endpoint; why: test_routing_profile_client_uses_atomic_selection_endpoint consumes monkeypatch during monkeypatch setattr daemon client request json request, so callers must bind it with the other signature inputs. + monkeypatch, capsys, argv, expected_body +# What: arrange the grouped source fragment for the scenario; why: test routing profile client uses atomic selection endpoint requires this concrete input or helper state before exercising the behavior under test. +): + # What: arrange seen as the fixture input; why: the routing profile client uses atomic selection endpoint test consumes this named precondition before exercising the behavior. + seen = {} + + # What: define the request test helper around method and url and path; why: the routing profile client uses atomic selection endpoint scenario calls this helper to produce or observe the exact behavior checked by its assertions. + def request(method, url, path, **kwargs): + # What: arrange method to seen.update; why: the routing profile client uses atomic selection endpoint scenario binds this method value to seen.update's method input. + seen.update(method=method, url=url, path=path, **kwargs) + # What: arrange the active field as expected body and name; why: request carries active into return {"active": expected_body["name"]}. + return {"active": expected_body["name"]} + + # What: arrange the exact monkeypatch setattr daemon client request json request fixture fragment; why: the routing profile client uses atomic selection endpoint scenario feeds this byte-preserved fragment through monkeypatch.setattr(daemon_client, "_request_json", request) before asserting its protocol or parser. + monkeypatch.setattr(daemon_client, "_request_json", request) + + # What: assert that daemon client main argv equals 0; why: this assertion protects the routing profile client uses atomic selection endpoint regression after the test's arranged inputs and exercised call. + assert daemon_client.main(argv) == 0 + # What: assert that seen method equals put; why: this assertion protects the routing profile client uses atomic selection endpoint regression after the test's arranged inputs and exercised call. + assert seen["method"] == "PUT" + # What: assert that seen path equals router profiles active; why: this assertion protects the routing profile client uses atomic selection endpoint regression after the test's arranged inputs and exercised call. + assert seen["path"] == "/router/profiles/active" + # What: assert that seen body equals expected body; why: this assertion protects the routing profile client uses atomic selection endpoint regression after the test's arranged inputs and exercised call. + assert seen["body"] == expected_body + # What: assert that json loads capsys readouterr out active equals expected body name; why: this assertion protects the routing profile client uses atomic selection endpoint regression after the test's arranged inputs and exercised call. + assert json.loads(capsys.readouterr().out)["active"] == expected_body["name"] + + +# What: define the test_fresh_health_does_not_reuse_previous_model_cache test around local fixtures; why: this test groups the arrange, act, and assertions that protect the fresh health does not reuse previous model cache outcome. +def test_fresh_health_does_not_reuse_previous_model_cache(): + # What: act by calling iter and capture docs; why: the fresh health does not reuse previous model cache test asserts the response, state, or failure produced by this call. + docs = iter([{"status": "ok", "instance_id": "old"}, {"status": "loading", "instance_id": "new"}]) + # What: act by calling ServeProbe and capture probe; why: the fresh health does not reuse previous model cache test asserts the response, state, or failure produced by this call. + probe = ServeProbe(opener=lambda *_: next(docs), ttl_s=100) + # What: assert that probe health 1922 status equals ok; why: this assertion protects the fresh health does not reuse previous model cache regression after the test's arranged inputs and exercised call. + assert probe.health(1922)["status"] == "ok" + # What: assert that probe fresh health 1922 status equals loading; why: this assertion protects the fresh health does not reuse previous model cache regression after the test's arranged inputs and exercised call. + assert probe.fresh_health(1922)["status"] == "loading" + + +# What: parameterize test_readiness_http_contract with the listed cases; why: pytest reruns the same arrange, act, and assertions for each input protecting test readiness http contract. +@pytest.mark.parametrize("status,maintenance,expected", [ + # What: arrange the loading error portion of the enclosing predicate; why: this clause remains in the readiness http contract scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("loading", None, 503), ("error", None, 503), + # What: arrange the ok draining ok serving portion of the enclosing predicate; why: this clause remains in the readiness http contract scenario\'s enclosing expression so its grouping and evaluation order stay intact. + ("ok", "draining", 503), ("ok", "serving", 200), +# What: arrange the pytest.mark.parametrize call with ordered positional inputs; why: test_readiness_http_contract groups the supplied clauses as one pytest.mark.parametrize call before its value is consumed. +]) +# What: define the test_readiness_http_contract test around status and maintenance and expected; why: this test groups the arrange, act, and assertions that protect the readiness http contract outcome. +def test_readiness_http_contract(status, maintenance, expected): + # Execute the actual handlers, excluding unrelated torch-dependent metrics + # imports. This is a CPU contract test, not a full serving integration test. + # What: act by calling Path and capture source; why: the readiness http contract test asserts the response, state, or failure produced by this call. + source = Path(__file__).parents[2] / "python/freetoken/server/control_api.py" + # What: act by calling ast.parse and capture module; why: the readiness http contract test asserts the response, state, or failure produced by this call. + module = ast.parse(source.read_text(encoding="utf-8")) + # What: act by calling next and capture register; why: the readiness http contract test asserts the response, state, or failure produced by this call. + register = next(n for n in module.body if isinstance(n, ast.FunctionDef) and n.name == "register_control_routes") + # What: act by calling isinstance and capture routes; why: the readiness http contract test asserts the response, state, or failure produced by this call. + routes = [n for n in register.body if isinstance(n, ast.AsyncFunctionDef) and n.name in {"health", "ready"}] + # What: act by calling FastAPI and capture app; why: the readiness http contract test asserts the response, state, or failure produced by this call. + app = FastAPI() + # What: arrange doc as status and maintenance and status and maintenance; why: the readiness http contract test consumes this named precondition before exercising the behavior. + doc = {"status": status, "maintenance": maintenance} + # What: arrange namespace as app and doc and app and build health and get state; why: the readiness http contract test consumes this named precondition before exercising the behavior. + namespace = {"app": app, "build_health": lambda *_: doc, "get_state": lambda: None} + # What: arrange the exact exec compile ast module body routes type ignores fixture fragment; why: the readiness http contract scenario feeds this byte-preserved fragment through exec(compile(ast.Module(body=routes, type_ignores=[]), str(source), "exe before asserting its protocol or parser result. + exec(compile(ast.Module(body=routes, type_ignores=[]), str(source), "exec"), namespace) + # What: enter the TestClient managed context before assert client get health status code; why: test_readiness_http_contract releases this resource or lock after assert client get health status code on both success and failure paths. + with TestClient(app) as client: + # What: assert that client get health status code equals 200; why: this assertion protects the readiness http contract regression after the test's arranged inputs and exercised call. + assert client.get("/health").status_code == 200 + # What: act by calling client.get and capture response; why: the readiness http contract test asserts the response, state, or failure produced by this call. + response = client.get("/ready") + # What: assert that response status code equals expected; why: this assertion protects the readiness http contract regression after the test's arranged inputs and exercised call. + assert response.status_code == expected + # What: assert that response json equals doc; why: this assertion protects the readiness http contract regression after the test's arranged inputs and exercised call. + assert response.json() == doc