Skip to content

feat(protocol): headless JSONL records as protocol types pinned by golden streams (HC4) - #34

Merged
lg2m merged 5 commits into
mainfrom
feat/hc4-headless-goldens
Sep 14, 2026
Merged

lg2m merged 5 commits into
mainfrom
feat/hc4-headless-goldens

Conversation

@lg2m

@lg2m lg2m commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

HC4: headless JSONL records as protocol types, pinned by golden streams

Spec: docs/plans/speed-first-extensible-agent-harness.md#phase-5b--headless-contract-for-supervisors · docs/design/headless-contract.md (gap row "Pinning the contract") · ADR: docs/adr/0023-headless-records-as-protocol.md

Closes Phase 5b (HC1 #30, HC2 893e582, HC3 #33, HC4 this PR).

What changed

  • qq_protocol::headless (new). HeadlessRecord { Trial | Event { envelope } | Outcome }, HeadlessTrial, HeadlessOutcome, HeadlessStatus, HeadlessApproval. These were a private serde enum in src/headless.rs; a supervisor had to read QQ source to learn the shapes. Now they are protocol vocabulary with strict decoding (deny_unknown_fields; unknown type/status fail). Identifiers and hashes are protocol types (WorkspaceId, ContentHash); HeadlessStatus::code is the one exit table; HeadlessOutcome::is_well_formed checks exit_code == status.code().
  • The binary emits through HeadlessRecordRef<'_>, a borrowing mirror, so the streaming path still serializes each SessionEventEnvelope by reference — no clone per event. A unit test pins that the owned and borrowed views encode identically. src/headless.rs re-exports HeadlessStatus/HeadlessApproval and owns no record shape.
  • Golden streams. crates/qq-protocol/tests/fixtures/headless/v19/ — ten complete .jsonl trial streams: completed, completed_with_every_option (every optional trial field + an audit record), completed_final_output_valid, task_failed_final_output_invalid, task_failed, timed_out, budget_exhausted (exit 3 shared; status disambiguates), interrupted, harness_failure, invalid_configuration (outcome-only: startup failed before a session). tests/headless_fixtures.rs decodes each line as a strict HeadlessRecord, requires byte-exact re-encoding, and checks framing (one trailing outcome whose exit code matches; one leading trial unless startup failed; only events between; strictly increasing cursors). QQ_UPDATE_FIXTURES=1 rewrites the current directory only.
  • v18/ decode-only. The eight default-path streams re-pinned to protocol_version: 18 — they are byte-identical to v19 apart from that field, which is the compatibility statement the contract makes: additive fields only, older streams still decode.
  • Binary tests tightened. parse_records in src/headless.rs tests now decodes every stdout line as a HeadlessRecord and asserts identical re-encoding, so the binary cannot drift from the crate.

No PROTOCOL_VERSION or store-schema bump: the emitted bytes are unchanged (verified by the 38 existing headless tests passing under the strict round-trip).

How verified

  • cargo test --workspace — 1,347 passed / 4 ignored
  • 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 — 162 passed
  • New tests: 4 qq_protocol::headless::tests (owned/borrowed parity; fail-closed on unknown type, field, status; optionals omitted not null; exit table + wire spellings); 3 tests/headless_fixtures.rs (current_version_streams_match_their_goldens, historical_streams_still_decode, the_exit_table_matches_the_contract).

Measurements

None named. No hot-path change: the event record is still serialized by reference and the encoding is byte-identical.

Docs

  • docs/adr/0023-headless-records-as-protocol.md (Accepted); docs/adr/README.md (0022 → Accepted, 0023 added); docs/plans/progress/root.md
  • docs/design/headless-contract.md — records intro names the types and goldens; gap row → Shipped; compatibility policy (goldens per version, omitted-not-null, constructed-not-recorded)
  • docs/design/protocol.md — new "Headless Records" section
  • docs/design/architecture.md § Hosting Boundary
  • Plan status block (Now → HC4 in review, closes Phase 5b; Next → H18), task index, Phase 5b section; ledger receipt in docs/plans/progress/speed-first.md (also closes HC3 as Shipped (24b6e5c, #33))

Compatibility

  • qq-protocol gains a CLI-facing module. Bounded to the three record shapes and two enums; invocation options, text rendering, and approval behavior stay in the binary.
  • HeadlessRecord boxes its payloads (Clippy large_enum_variant); the binary never constructs the owned enum.
  • Root src/headless.rs: HeadlessStatus::exit_code() became headless::exit_code(status) (the protocol type cannot depend on std::process::ExitCode semantics of the binary). HeadlessApproval::approval_mode() became a private free fn.

Follow-ups

  • The harbor adapter's Python fixtures (benchmarks/harbor/tests/make_fixtures.py, pinned at protocol 16) can be regenerated from the goldens when it next changes.
  • Next slice per plan: H18 (shared transcript / precompiled prompt prefix).

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
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
… architecture, plan, and ledger updates

Slice: HC4
@lg2m
lg2m merged commit 43caaea into main Sep 14, 2026
4 checks passed
@lg2m
lg2m deleted the feat/hc4-headless-goldens branch September 14, 2026 16:50
lg2m added a commit that referenced this pull request Sep 14, 2026
…ompt prefix (H18) (#38)

* docs(ledger): start H18, close HC4 (#34)

Slice: H18

* perf(provider): provider_encode bench with a counting allocator (H18)

One MiB transcript plus 32 schemas encoded for each HTTP protocol; reports
encode time, body size, and the request's peak heap above the transcript
along the adapter path (build, per-attempt clone, body). Baseline at
43caaea: 4.4-4.7x payload, 335-555 us. test_support::encode_body exposes
the wire structs without a transport.

Slice: H18

* perf(provider): share the transcript and keep tool JSON as raw text (H18)

ModelRequest.messages is Arc<Vec<Message>>: the per-attempt clone every
adapter performs inside with_restart is a refcount bump. ToolSpec.input_schema
and ContentBlock::ToolCall.arguments are Box<RawValue>: every codec embeds
the compact text verbatim (or sends it as the string Responses and Chat
want) instead of walking a Value tree or re-stringifying per request.
Bedrock alone parses, because the Converse SDK wants a Document tree.

provider_encode, 6 interleaved A/B pairs, release profile, separate target
dirs: request heap 4.4-4.7x -> 2.7-2.9x payload; encode openai_responses
430 -> 305 us, openai_chat 479 -> 326, anthropic 342 -> 333, google 570 -> 504.

Slice: H18

* perf(runtime): append to a shared transcript; schemas and arguments stay text (H18)

The run holds Arc<Vec<Message>>, hands it to each turn's request by
reference count, drops the provider stream before appending, and appends
through Arc::make_mut, which copies nothing once the stream is gone (a test
pins the same allocation across turns). Tool-call arguments keep the
canonical string the run already produced; PendingToolCall loses its
duplicate parsed Value and measure_message stops re-serializing. The
catalog reads schema text straight from each spec instead of serializing
on compile and carrying a parallel Arc<str> list.

Slice: H18

* perf(runtime): build the plan-constant prompt prefix once and continue its digest (H18)

CompiledAgentPlan holds a PromptPrefix per capability set (header, tool
index, skill index, workspace instructions, persona) with its SHA-256 state;
the common set is built at compile, others on first use. A run appends only
its suffix (selected guidance, context blocks, output contract) to a copy
and finalizes the cloned hasher over the suffix, so system_prompt_hash is
unchanged from a whole-prompt digest; a test pins that across four
capability sets and against the single-pass builder.

Slice: H18

* perf(provider): escape transcript strings with a word-parallel scanner (H18)

With RawValue fields in the request structs serde_json's per-byte escape
loop lost 30-60% on a one MiB body, and the loss flipped with unrelated
build changes (same instruction count, different loop alignment). Text<'a>
and OwnedOrBorrowedText<'a> wrap every bulk string (message text, tool
results, system prompt): a SWAR scan finds the next byte needing escape
eight at a time, the clean run is copied once, and the literal reaches
serde_json through its raw-value hook. Byte-identical output, pinned by an
exhaustive per-lane test. The bench now also reports request heap from a
shared Arc, the run loop's case.

provider_encode vs 43caaea, 5 interleaved triples (base, cand, cand with
debuginfo): openai_responses 429 -> 191 us, openai_chat 466 -> 212,
anthropic 339 -> 194, google 559 -> 406; both candidate builds agree.

Slice: H18

* docs(h18): ADR-0024 shared transcript, raw tool JSON, prompt prefix; architecture, plan, and ledger

Slice: H18

* docs(ledger): link H18 PR #38
@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