Skip to content

feat(runtime): typed final output contract for qq run (HC3) - #33

Merged
lg2m merged 5 commits into
mainfrom
feat/hc3-typed-final-output
Sep 13, 2026
Merged

lg2m merged 5 commits into
mainfrom
feat/hc3-typed-final-output

Conversation

@lg2m

@lg2m lg2m commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator

HC3: typed final output for qq run

Spec: docs/plans/speed-first-extensible-agent-harness.md#phase-5b--headless-contract-for-supervisors · docs/design/headless-contract.md (gap row "Typed final output") · ADR: docs/adr/0014-typed-final-output.md

What changed

  • CLI. qq run --output-schema PATH [--output-repair-turns N] (N 0–8, default 2, requires the schema). The file is read (bounded to the 64 KiB ceiling + 1) and compiled in prepare_headless before configuration loads; unreadable, oversized, malformed, $ref-bearing, or unsupported-keyword schemas exit 2 naming the path. Text format prints the pretty-printed validated document instead of the raw answer.
  • Protocol 18 → 19. SubmitPrompt.output: Option<Box<OutputContract>>; RunFinished.final_output and RunSnapshot.final_output: Option<Box<FinalOutput>> (valid { value, repair_turns } | invalid { errors, repair_turns }); LimitCapabilities declares the four bounds. All optional and omitted when absent; every default-path v18 golden is byte-identical to v19 apart from the version field (capabilities gains the four bounds). v19/ goldens added (+3 new fixtures); v17/, v18/ retained decode-only.
  • Core. New qq_core::output: a closed JSON Schema subset (type, enum, const, properties, required, additionalProperties, items, length/count/range bounds, anyOf/oneOf/allOf/not; no references, no regex) with byte/depth/value ceilings, linear-time validation, RFC 6901 pointer errors, and 8 KiB / 16-entry payload caps. No validator crate added.
    • Admission compiles the contract (SessionRuntimeError::InvalidOutputContract → HTTP invalid / exit 2) and persists it (runs.output_contract_json, store schema 27); the claim recompiles it so a restart enforces the accepted contract.
    • The run loop validates at the completion boundary after the audit and steering. Within the allowance it pushes the assistant message plus a runtime notice carrying the bounded errors and continues; repairs are ordinary turns against ordinary budgets and the allowance is per run (an audit revision or steering never resets it). Past the allowance the run completes with FinalOutput::Invalid — no new RunOutcome variant.
    • RuntimeEvent::Completed { final_output } → settle_run writes runs.final_output_json in the settlement transaction and publishes it on RunFinished. No verdict for cancelled/failed/budget-exhausted runs.
    • The model sees the compact schema as an "Output contract" section of the system prompt (prompt identity differs for contract runs, as it should).
  • Headless records. trial gains output_schema_sha256 and output_repair_turns; outcome gains final_output. A completed-but-invalid answer maps to task_failed (exit 1) with a message naming the errors; exit codes are unchanged.

How verified

  • cargo test --workspace — 1,329 passed / 4 ignored (one new ignored measurement test)
  • cargo fmt --all -- --check; cargo clippy --workspace --all-targets --all-features -- -D warnings clean
  • cargo build --workspace; cargo test -p qq-provider --no-default-features --features test-support (149 passed)
  • New tests: 8 output::tests (semantics, pointers, fence, bounds, payload caps); 9 sessions::tests — valid first answer; repaired within allowance with notice contents; never-validates → typed failure and exactly N repairs; zero repairs; no contract → no verdict/no prompt change; unenforceable refused at admission with no run row; audited revision is what the contract judges and does not reset repairs; repair turn as budget-final response → budget_exhausted, no verdict; contract survives abandon_for_test restart and is enforced by the recovering runtime; schema-27 migration keeps history null; 6 headless::tests (JSONL valid/repaired/invalid records, text prints the document, no fields without a contract); 2 cli::tests; 1 main::tests (every exit-2 path before config loads); 3 protocol goldens.

Measurements

Gate Before (b0a18be) After Budget
Default path read_tool_loop med / p95 (15 interleaved A/B pairs × 3000 iters, release, I/O pressure ~28%) 54.8 / 76.8 µs 52.2 / 65.0 µs ≤ +5%
Enabled: compile 64-property schema (3.5 KiB) — 37 µs opt-in
Enabled: validate 7.3 KiB answer — 11 µs opt-in

Evidence: target/qq-perf/hc3-2026-09-12/ (untracked) in the main checkout.

Docs

  • docs/adr/0014-typed-final-output.md (Accepted), docs/adr/README.md, docs/plans/progress/root.md
  • docs/design/headless-contract.md — invocation, record tables, exit table, gap row → Shipped, bounds & keyword subset, compatibility policy
  • docs/design/protocol.md — version 19 history, run_finished row, submit_prompt.output, new "Typed final output" section
  • docs/design/architecture.md § Structured Input And Steering
  • Plan status block / task index; ledger receipt in docs/plans/progress/speed-first.md

Compatibility

  • PROTOCOL_VERSION 18 → 19 (strict event/snapshot decoders; additive fields). Harbor trace fixtures re-pinned to 19.
  • Store schema 26 → 27: two nullable TEXT columns on runs; historical rows read as "no contract".
  • DESCRIPTOR_VERSION, CAPABILITIES_VERSION unchanged.

Follow-ups / decisions needed

  • HC4 (headless golden JSONL) should pin trial/outcome both with and without final_output.
  • H21.2 (mechanical sessions.rs split) is unblocked.
  • The design doc said schemas are rejected "before session creation"; the CLI does that, and the protocol path rejects at prompt admission — the earliest point a command-level contract can be checked. Recorded in the ledger.
  • Provider-native structured output could later sit behind the same contract as an optimization.

Review note: touches sessions.rs settlement → independent second review required per docs/plans/workflow.md § 4.

Add OutputContract on SubmitPrompt, FinalOutput on RunFinished and
RunSnapshot, and their bounds; bump PROTOCOL_VERSION to 19. Add the
bounded qq-core output module: a reference-free JSON Schema subset
compiled once per run with byte, depth, value, and repair-turn ceilings,
and a validator whose error payloads are bounded to 8 KiB.

Slice: HC3
…ith bounded repair turns (HC3)

Store schema 27 adds runs.output_contract_json and runs.final_output_json.
SubmitPrompt.output is compiled at admission (an unenforceable schema is
InvalidOutputContract, not a run failure), persisted with the run row,
recompiled at claim so a restart enforces the accepted contract, and
carried into the run loop. At the completion boundary — after audit and
steering — the answer is validated; a failure within the contract's
repair allowance pushes the bounded errors as a runtime notice and
continues; otherwise Completed carries the FinalOutput verdict, which
settle_run writes with the outcome and publishes on RunFinished. Repair
turns share the ordinary turn budget; exhaustion publishes no verdict.

Slice: HC3
The schema file is read (bounded to the ceiling plus one byte) and
compiled in prepare_headless before configuration loads, so every
defect is invalid_configuration (exit 2) naming the path. The contract
rides SubmitPrompt; the trial record gains output_schema_sha256 and
output_repair_turns; the outcome record gains final_output. A completed
run whose answer never validated exits 1 (task_failed) with the typed
verdict; text format prints the validated document instead of the raw
answer. PROTOCOL_VERSION 19 goldens under fixtures/v19, v18 retained
decode-only; LimitCapabilities advertises the schema and repair bounds.

Slice: HC3
@lg2m
lg2m merged commit 24b6e5c into main Sep 13, 2026
4 checks passed
@lg2m
lg2m deleted the feat/hc3-typed-final-output branch September 13, 2026 21:53
lg2m added a commit that referenced this pull request Sep 14, 2026
…lden streams (HC4) (#34)

* docs(ledger): start HC4, close HC3 (#33), reserve ADR-0023

Slice: HC4

* feat(protocol): headless JSONL record types shared by qq run (HC4)

HeadlessRecord/HeadlessTrial/HeadlessOutcome/HeadlessStatus/HeadlessApproval
move into qq-protocol so the trial-stream shapes are protocol vocabulary a
consumer can decode strictly. The binary emits through the borrowing
HeadlessRecordRef; encodings are unchanged.

Slice: HC4

* test(protocol): golden qq run JSONL streams per PROTOCOL_VERSION (HC4)

tests/fixtures/headless/v19/: ten complete trial streams covering every
exit status, the default payload, every optional trial field, and both
final_output verdicts; byte-for-byte checked and framing-validated.
v18/ holds the same default-path streams re-pinned to 18, decode-only,
which is the compatibility statement: additive fields, older streams
still decode. The headless tests now decode every stdout line as a strict
HeadlessRecord and require it to re-encode identically.

Slice: HC4

* docs(hc4): ADR-0023 headless records as protocol; contract, protocol, architecture, plan, and ledger updates

Slice: HC4

* docs(ledger): link HC4 PR #34
@lg2m lg2m mentioned this pull request Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant