Skip to content

Add trace schema v1, replay and dataset export - #211

Merged
tzhouam merged 1 commit into
mainfrom
kb/trace-schema
Sep 28, 2026
Merged

tzhouam merged 1 commit into
mainfrom
kb/trace-schema

Conversation

@tzhouam

@tzhouam tzhouam commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

PR 8 of the knowledge-service redesign: trace schema v1 (shared by the copilot and the review bot), plus replay and dataset export.

  • trace_store (trace/1):
    • append-only JSONL, content-addressed gzip blobs (hash re-verified on read), and a SQLite index (by kind, run, repo, change set, PR, rule, role, model, outcome) that can be rebuilt from the JSONL;
    • context bound through contextvars;
    • every record string (keys included) and every blob is redacted before it is written;
    • stdlib only, exported via sdk.v1.
  • Knowledge service records:
    • every model call, with its final verdict (a truncated, empty, unparseable or schema-failing reply is recorded as a failure);
    • gate decisions;
    • outcomes (merged / closed_unmerged / head_changed / rule_retired);
    • generator calls, linked to their change set through the accepted attempt only.
  • kb replay: re-asks a recorded call with a substitute model and compares the decision fields. It makes no GitHub, outbox or ledger writes.
  • kb export: dataset rows of (input, output, decision, outcomes). Calibration and replay calls, failed calls, and prompts citing calibration evidence are dropped.

Commit hook approved; full suite and SPEC freshness pass locally.

🤖 Generated with Claude Code

Every model call, decision and later outcome is now kept durably, so the
expensive models' work can be replayed and turned into datasets for cheaper
models plus workflows. The copilot and the review bot share one schema.

- trace_store (trace/1): append-only JSONL records (the source of truth),
  content-addressed gzip blobs for prompts and replies (records hold only
  sha256 refs; reads re-verify the hash), and a SQLite index by kind, run,
  repo, change set, PR, rule ID, role, model and outcome. The index can be
  rebuilt from the JSONL. trace_context binds run/playbook/repo/change-set
  context through contextvars. Every string of a record (keys, model and
  usage included) and every blob is redacted before it is hashed or written: known token shapes, private-key blocks, and the value of every
  environment variable whose name marks it as a secret. It uses only the
  standard library and is exported through sdk.v1 (TraceStore, TRACE_SCHEMA,
  trace_context, redact) without loading private modules.
- knowledge service:
  - ModelGateway records every call with the pinned provider/model/effort
    and the call's final verdict: transport failures and truncated, empty,
    unparseable or schema-failing replies are recorded as failures.
  - gate_and_stage allocates the change set ID first, judges under its
    context, then writes a decision record.
  - merge.advance writes outcomes (merged, closed_unmerged, head_changed).
  - retirements write rule_retired.
  - intake, sweep and calibration are bound to their playbooks. Generator
    calls precede their change set, so each drafting has a unique draft key
    (event, or release + page) and each call its attempt number; the
    decision lists only the ACCEPTED attempt, so rejected replies and earlier
    tries never inherit its labels.
  - Tracing never breaks the service.
- replay: re-asks a recorded call with a substitute model and compares the
  decision-bearing fields. It touches no GitHub, outbox or ledger; the
  substitute call and the comparison are traced too.
- export: judge or generator rows of (input, expensive output, gate decision,
  outcomes, later rule outcomes), generator rows joined through draft keys.
  Calibration and replay calls, failed calls, and any prompt that cites a
  calibration case's evidence reference anywhere or its title are dropped.
- CLI: kb traces, kb replay --record ID --model P:M[:E], kb export --out F.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tzhouam
tzhouam merged commit deb2b98 into main Sep 28, 2026
5 checks passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 074a27ada7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +73 to +74
with trace_context(playbook="kb-calibrate", run_id=f"calibrate-{Path(directory).name}"):
return _run_calibration(directory, gateway=gateway, judge=judge)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Attach the trace recorder to CLI calibration calls

When calibration is run through infermatrix-copilot kb calibrate, cli.py passes a plain ModelGateway(Settings()) with no recorder, so this context manager does not produce any model_call records. Consequently, all calibration model activity is absent from trace/1 despite the new every-call audit contract; construct the gateway with the state directory's TraceStore and trace_recorder before invoking run_calibration.

Useful? React with 👍 / 👎.

Comment on lines +107 to +108
identity = {"role": role.name, "requested": role.label(), "provider": role.provider,
"model": role.model, "effort": role.effort}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Record failures raised while acquiring the transport

When a configured provider CLI is missing or unauthenticated, _transport() raises ModelUnavailable before the new identity and exception-recording path is reached. These common unavailable-model failures therefore leave no model_call record at all, contradicting the promised inclusion of failed calls and preventing operators from replaying or auditing them; move transport acquisition inside the recorded failure path.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-28T13:44:21.453992Z 074a27a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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