Skip to content

feat(client): versioned JSON output, validate --file, and associate/refs/parse/fold verbs (Phase 5 stage 2) - #326

Merged
ZacxDev merged 4 commits into
mainfrom
feat/client-json-contracts
Dec 4, 2026
Merged

ZacxDev merged 4 commits into
mainfrom
feat/client-json-contracts

Conversation

@ZacxDev

@ZacxDev ZacxDev commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

Stage 2 of the Python-oracle retirement (v1 plan Phase 5): give the Go client the answers devrc's last Python-lib consumers read, as a versioned JSON contract (schema: 1, README → Machine-readable output). Operator rule behind B5–B7: devrc keeps no local copies of cairn logic, so every question it used to answer itself is a verb here.

what consumer it unblocks (devrc)
B1 recall --json service_recon.py (/analyze-service)
B2 validate --file PATH [--ref R] — offline, via write.CheckEntryBytes (the pod's create/put predicate) and the store's body cap subsystem_touch.py --validate <draft>
B3 new verb associate --scope S --paths-from FILE|- [--min-paths N] [--json] over store.AssociatePaths subsystem_touch.py's touch report
B4 routes --json — each instance's cache root + the table cairn-ops/health.sh (replaces banner scraping)
B5 new verb refs (--scope S | --all-scopes) [--json] — every ref the cached entries make addressable, and every one store.ResolveRefTiered calls ambiguous. --all-scopes counts each scope once, in its routed instance; a cached scope with no route is listed under unrouted and exits 11; an instance whose cache is unreadable is a PARTIAL answer (unread_instances, exit 3, as search --all-scopes); a --scope the cache does not hold is status: scope-absent, exit 0 devrc's ref-collision census
B6 new verb parse --file PATH|- [--bullets] --json — the reader's parse of an entry (front matter, headings, sections, journal bullets) devrc's former local entry reader (cairn_entry.py)
B7 new verb fold --file PATH|- --json — input one JSON array of strings; output results: [{input, ref, path_refs: [{component, ref}]}] = store.NormalizeRef / store.PathRefs devrc's scripts/lib/cairn_ref.py (the last local copy)

doctor --json is deliberately untouched: the parity gate pins its bytes until Stage 4 deletes that gate.

Evidence

  • Tests: internal/client/jsonout_test.go (B1–B4: literal key sets, fixture-chosen values, exit-code equality with the text form, refusal ⇒ empty stdout) and internal/client/parserefs_test.go (B5–B7: literal collision fixtures, every level of the refs key set pinned, the two-instance unrouted and partial cases, the 7 fold rows of devrc's oracle-produced fixture plus path_refs rows from devrc's own copy).
  • Mutation sweeps, per commit and over that commit's guards only — not one cumulative count: c401fef 17/17 killed (B1–B4), b39c18a 9/9 (B5–B6), 510f6d2 23/24 (the round-1 follow-ups; the one survivor is equivalent — parse's read bound doubled while the length check still refuses at MaxEntryBytes+1).
  • Ledgers: associate, refs, parse, fold in capability_ledger (go_only), want-go-only-verbs.txt, and the read-ledger class probes (+ recall --json/routes --json probes).
  • Local: change-scoped go test ./internal/client ./internal/write ./internal/store (+ ./internal/report in earlier rounds), tests/test_capability_ledger.py, tests/test_go_client_ledgers.py, leakscan + self-test. The whole suite is CI's.

Audits: round 0 #326 (comment), round 1 (ladder ended, no 🔴) #326 (comment). Round-1 follow-ups (510f6d2) are not re-audited; round-0 D1 (keep text renderings) deliberately skipped.

🤖 Generated with Claude Code

@ZacxDev

ZacxDev commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

Recorded open after the audit: F4 — recall --json omits some report fields (e.g. ref-to / tag totals, featured basis). Additive under schema: 1; not done in this PR.

Note on F2: associate answers an unknown scope with status: scope-absent at exit 0 — recall --json's own convention — rather than a new exit code. A new code would change the printed exit-code ledger (cairn -exit-codes), which tests/test_go_client_ledgers.py still pins equal across both clients until #327 deletes the Python one; the status field is the branch point devrc reads.

🤖 Generated with Claude Code

ZacxDev added a commit that referenced this pull request Dec 4, 2026
…se onto 9c2d9c4)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
ZacxDev and others added 3 commits December 4, 2026 09:29
…e verb

The Go client gains the answers the Python client's last consumers read from its
in-process objects, as a stable contract (`schema: 1`, documented in README.md →
"Machine-readable output"), so packages.cairn can be retired (v1 plan Phase 5):

- `recall --json`: the reader's report — status, exit, warning, scope, instance,
  cache_root, state, candidates, entries with sections keyed by heading. Same exit
  code and stderr warning as the text form; a refusal prints no document.
- `validate --file PATH`: one draft, offline, judged by `write.CheckEntryBytes` —
  the predicate create/put run on the pod (loader parse + closed tag vocabulary) —
  then the write-protocol advisories. Exit 0 / 5 malformed / 2 unreadable.
- `associate --scope S --paths-from FILE|- [--min-paths N] [--json]`: which entries
  a list of repo-relative paths names, through store.AssociatePaths (the matcher
  recall's focus window already uses). Go-only verb, ledger class `resolved`.
- `routes --json`: every instance's cache root (via instanceCache, the read verbs'
  own resolver) and the scope->instance table (null when none). `--json --check`
  is refused.

doctor --json is unchanged (the parity gate pins its bytes until Phase 5 removes it).

Tests: internal/client/jsonout_test.go pins literal key sets and fixture values.
Mutation sweep, 17 mutants over the four features, 17 killed, each by the test
that names its feature (incl. M6: validate --file on the loader alone, no
vocabulary — killed only by the off-vocabulary-tag case).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…nswers scope-absent

Audit round 1 findings (no 🔴; ladder ended):

- F1: `validate --file` judged a draft under its OWN basename/directory while create
  uses NormalizeRef(--ref)+".md" and the folded scope, and ignored the 1 MiB body cap.
  New `--ref REF` derives (scope, filename) exactly as create does and refuses a
  scope/ref that is not a safe path component (the pod 400s those before reading the
  body); without `--ref` the path-based judgement stays. The body cap
  (write.MaxEntryBytes, which api.maxBodyBytes is now defined from) is checked in both
  modes. README/CHANGELOG/comment narrowed to what holds. Expectations measured against
  the real pod first: draft.md/widget-api 5->0 with --ref (create 201);
  widget-api.md/--ref other 0->5 (create 6 entry-shape); x.runbook.md/--ref x 5->0;
  --ref x.process -> 2 (create 400); >1 MiB -> 5 in both modes (create 400).
- F2: `associate` on a scope the cache does not hold printed nothing and exited 2 like a
  usage error. It now answers recall's way: a document with `status: scope-absent`
  (new `status` field, `associated` otherwise), exit 0 — the same answer for "never
  existed" and "not visible". Text form: one `scope-absent\t<scope>` line.
- F3: the text form prints `below-threshold` lines.
- F5: CheckEntryBytes's doc comment no longer splits validateEntryBytes's from its
  func; the caller-ledger sentence names CheckEntryBytes.
- F4 (recall --json omits some report fields) stays open, additive.

Red at the previous head, green here: the three F1 tests and the two associate tests
(TestValidateRefWithoutFileIsAUsageError passes at both — an invariant guard).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…e reader's entry parse)

Operator decision on devrc#3529: no local copies of cairn logic in devrc. Two generic
read verbs so consumers ask the reader instead of re-implementing it (schema 1,
README "Machine-readable output"):

- `refs (--scope S | --all-scopes) [--json]`: every ref the cached entries make
  addressable, and every one `store.ResolveRefTiered` calls ambiguous (tier +
  candidate filenames). `refs_tried` is the denominator. `--all-scopes` counts each
  scope once, in the instance the routing table sends it to; a copy cached on
  another instance is listed in that instance's `not_routed_here`.
- `parse --file PATH|- [--bullets] --json`: front matter, headings, sections and the
  nuance journal's bullets (start_line, date, openness, resolved_by, population,
  citation_id); `--bullets` parses any text as one journal body. Offline; not a
  well-formedness verdict.

Both Go-only; ledgers updated (capability_ledger, want-go-only-verbs, read-ledger
class probes). Tests: internal/client/parserefs_test.go — literal fixtures (alias-tier
and filename-tier collisions, the alias-equals-another-slug non-collision, the routed
instance count, all six populations). Mutation sweep 9/9 killed.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ZacxDev
ZacxDev force-pushed the feat/client-json-contracts branch from 9c2d9c4 to b39c18a Compare December 4, 2026 15:30
ZacxDev added a commit that referenced this pull request Dec 4, 2026
…se onto 9c2d9c4)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ZacxDev

ZacxDev commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

Audit round 0 — requirements & deletion (head b39c18a1)

Verdict: proceed to the checklist. One requirement questioned (below) — it is a gap against an operator decision that is already settled, so it is raised as a finding for round 1, not as an open premise.

Step 1 — requirements and their authors

Operator asks behind this PR (paraphrased, relayed by the dispatching session; the brief's own "operator asks" block held only an agent-written comment, so the session transcript side is UNKNOWN): retire cairn's Python oracle; devrc should consume the Go client (shipped as a nix flake) and keep no local copies of cairn logic; asked how devrc should get entry parsing and ref folding, the operator chose client verbs.

# requirement author devrc #3529 consumer (measured in its diff)
R1 recall --json operator (retire oracle / use the client) service_recon.py (recall --repo … --ref … --no-sync)
R2 validate --file [--ref] operator cairn-ops/write.sh create, subsystem_touch.run_validate
R3 associate verb operator subsystem_touch touch report (--paths-from - --min-paths N --json)
R4 routes --json operator cairn-ops / skills (routes --json → routes, instances[].cache_root)
R5 refs (--scope, --all-scopes) operator ("client verbs" for ref questions) subsystem_touch.census, subsystem-audit.py
R6 parse (+ --bullets) operator ("client verbs" for entry parsing) handoff_doc.durable_reason, census, subsystem_touch, subsystem-audit.py
R7 schema: 1 contract + refuse-unknown-schema rule UNATTRIBUTED-UNKNOWN (agent design; consumer cairn_cli.KNOWN_SCHEMAS={1} honours it) —
R8 text (non---json) renderings of associate / refs UNATTRIBUTED-UNKNOWN none: every devrc call passes --json; skills name the verbs only
R9 api.maxBodyBytes := write.MaxEntryBytes follows from R2 (same cap offline and on the pod) —

Requirement questioned (not a premise — the decision is already made): the "no local copies" ask is not fully served by this PR. devrc #3529 still carries scripts/lib/cairn_ref.py — a port of store.NormalizeRef and store.PathRefs — and labels it "the last local copy … pending a cairn verb". No verb in this PR folds an arbitrary string or path (the recorded Python reference's refs rows, 7 of them, have no verb to run against). Either this PR, or a named follow-up, owes that verb; the operator's decision already says which way.

Step 2 — delete

Examined every new verb/flag against devrc #3529's call sites: R1–R6 each have a consumer, so none is retired on question (a). Only:

  • R8 has no consumer; it is small (a few Fprintf loops, with tests) and gives an operator something to read at a terminal, so it is a candidate, not a recommendation.

Step 3 / 4

Simplify recommendation for the operator: /simplify over internal/client/{refs,parse,associate}.go. Accelerate note (devrc side, for awareness): census and durable_reason spawn one cairn parse subprocess per entry file / per bullet-shaped line; a multi-file parse would cut that, but nothing here should be accelerated before the fold-verb gap is settled.

Ledger: round 0 · requirements: 9 (unattributed: 0; UNATTRIBUTED-UNKNOWN: 2) · deletion candidates: 1

D1 — the text (non --json) renderings of `associate` and `refs`: no consumer in devrc #3529, every call passes --json

@ZacxDev

ZacxDev commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

Audit round 1 — first full adversarial audit (head b39c18a1, base main @ e72afdf)

🔴: none. This round ends the ladder; the 🟡/🟢 below are for a final un-re-audited commit or to be recorded open.

Evidence gathered

  • parse vs the retired Python reader: ran the PR-head client (go build ./cmd/cairn) over every row of devrc's recording of cairn's Python subsystem_resolver at ec1b756 (scripts/tests/fixtures/cairn_entry_reference.json, added in devrc defbea38, deleted in 7bfa8be6; it is not on devrc main): 40/40 rows identical — 6 section bodies, every single-line row (all six openness populations), every front-matter row, and the sections+headings row. Instrument control: two mutated expectations in a copy of the fixture → 2 mismatches reported. The fixture's 7 normalize_ref rows have no verb to run against (F1).
  • CI (gh pr checks 326, run 47365616764 on b39c18a1): all 29 checks pass, including nix, go, parity, tests, mutants-aggregate.
  • Local, dev-host tier (nix develop <worktree> -c go test -count=1): internal/{client,write,store,report,api} all ok.
  • Rebase onto feat(log): read-only SQL over the transcript index, single-user (v1 Phase 2) #328/feat(log): list and search default to the current repo's sessions (--all-repos to widen) #329/capture: unnamed scope ships owner-only to the default (not held); --reevaluate-held #330: the merge-base equals main's tip e72afdf. Read the merged regions of cli.go (verb table, needsValue list including --all-repos and --bullets), README.md (verb table rows next to the log/log query rows), flake.nix want-go-only-verbs.txt (keeps log reads), and CHANGELOG.md: no semantic conflict found. clientapi/server routes are untouched apart from maxBodyBytes := write.MaxEntryBytes.

🟡 should-fix

F1 — payload — the "no local copies of cairn logic in devrc" decision is not fully served: no verb folds an arbitrary string or path. devrc #3529 keeps scripts/lib/cairn_ref.py, a port of store.NormalizeRef and store.PathRefs, and labels it "pending a cairn verb". Scenario: if cairn changes the fold (for example, the [a-z0-9.-] class), devrc's subsystem_touch.derive_scope / service_recon.locate / handoff_register keep folding the old way, and no test in either repo goes red. The recorded reference's 7 fold rows cannot be checked against the client because no verb exists. Fix: add a fold verb here or in a named follow-up (for example cairn ref-fold --json over stdin strings and paths), then delete cairn_ref.py.

F2 — payload — refs --all-scopes files an UNROUTED scope under not_routed_here, silently. internal/client/refs.go:80-84: any AliasFor error, including "scope is not in the routing table", goes into elsewhere. Reproduced on a two-instance host whose table names only alpha-notes, with gamma-notes cached on secondary: exit 0, no stderr, secondary.scopes: [], not_routed_here: ["gamma-notes"], while recall --scope gamma-notes exits 11 (not in the routing table). README:408 defines not_routed_here as "a copy cached elsewhere", but this scope is the only copy and is routed nowhere. devrc's census and subsystem-audit read only scopes, so its entries drop out of the counts and the collision audit with no signal. Fix: keep "routed to another instance" and "routed nowhere / unreachable alias" separate (for example an unrouted list plus a stderr line), and say so in the README.

F3 — payload — refs --all-scopes is all-or-nothing where its sibling walker answers partially. refs.go:89-93: the first instance with ExitHint != 0 returns with no document, even after earlier instances answered. Reproduced: secondary has no cache, then refs --no-sync --all-scopes --json → exit 3, 0 bytes; search --no-sync --all-scopes on the same host prints the answering instance and a PARTIAL — 1 of 2 line. devrc always passes --no-sync, so one configured but never-synced instance blocks census and subsystem-audit for every instance. This fails closed and loudly, so it is not 🔴. Fix: emit the answering instances and mark the unread ones (with a non-zero exit), as searchEveryInstance does.

F4 — payload — refs --scope on a routed scope that has no entries is exit 2 "usage", while recall and associate answer scope-absent at exit 0. refs.go:133; README:409. Reproduced: with the table routing delta-notes and nothing cached for it, refs --scope delta-notes --json → exit 2, the cache holds no scope 'delta-notes', and recall --scope delta-notes --json → exit 0 with a document. This is the same inconsistency this PR's own F2 note resolved for associate. A pre-registered empty scope (the hollow-set case in instances.go) is a correct invocation, and calling it a usage error makes devrc raise ClientRefused(2).

F5 — scaffolding — the refs document's contract fields that devrc reads are not pinned. Renaming entries→entry_list, cache_root→cache_dir and the scope's malformed→bad in refs.go together leaves go test ./internal/client ok. Positive control: renaming collisions → FAIL. devrc's census reads exactly those three fields (entries[].filename, cache_root, malformed[].filename). The README says a rename must bump schema, but nothing makes a rename without a bump go red. Fix: a literal key-set assertion for the refs document at each level, as jsonout_test.go does for recall/associate/routes.

🟢 nits

  • F6 — payload — parse.go:14 maxParseBytes = 1 << 20 re-spells write.MaxEntryBytes (which validate --file and the server already share). If the store's cap moves, parse quietly keeps the old one.
  • F7 — payload/process — the PR title and body describe B1–B4 only. refs and parse (commit b39c18a, about 560 lines) are not mentioned, and the "17 mutants, 17 killed" figure predates them. A reviewer working from the body misses two of the five contract documents. Not re-verified here: the 17/17 sweep.

Not findings (one line each): the shared schema constant means a breaking change to one document makes devrc refuse all five — this over-refuses loudly and is acceptable. An older pinned client without parse/refs exits 2 with an empty stdout, and devrc's cairn_cli treats that as no answer (fail-safe in durable_reason, a raise in census).

Ledger: not measured (first full audit; starts at round 2). Round 0 carried: requirements: 9 (unattributed: 0; UNATTRIBUTED-UNKNOWN: 2) · deletion candidates: 1 (D1 open).

Verdict: safe to merge. F2–F5 are worth one final commit; F1 is owed before devrc's interim local copy can go.

1. none — round 1 audited only; this round made no fixes, so it claims nothing addressed

…, scope-absent

Round-1 follow-ups on #326 (ladder ended, 0 🔴; these are not re-audited):

- F1: new Go-only verb `fold --file PATH|- --json`: the reader's ref fold for strings that
  are not an entry's. Input is ONE JSON array of strings (any string carried exactly; a null
  element or non-array is 2). Output `{schema: 1, verb: "fold", results: [{input, ref,
  path_refs: [{component, ref}]}]}` — `ref` is store.NormalizeRef, `path_refs` store.PathRefs,
  in input order. Lets a consumer delete its local copy of the fold. Ledgers: cli Verbs,
  capability_ledger, want-go-only-verbs, read-ledger class probe; README + CHANGELOG.
- F2: `refs --all-scopes` no longer files a cached scope with NO route under
  `not_routed_here` at exit 0. It goes in the instance's new `unrouted` list, is named on
  stderr, and the run exits 11 (recall's refusal for the same scope); the document is still
  printed.
- F3: an instance whose cache cannot be read no longer aborts `refs --all-scopes` with empty
  stdout. As `search --all-scopes` does: the answered instances are printed, the unread ones
  are named in the new top-level `unread_instances` and a `PARTIAL` stderr line, exit 3 (the
  unread state's code — search's, not a new code). Exit is the highest that applies.
- F4: `refs --scope S` on a scope the cache does not hold answers `status: scope-absent`
  exit 0 (new top-level `status`, `answered` otherwise), like recall/associate; text form
  prints `scope-absent\t<scope>`.
- F5: every level of the `refs` document's key set is pinned literally.
- F6: `parse` bounds its read by write.MaxEntryBytes, not a private 1 MiB copy.

Red at b39c18a, green here: TestRefsOnAScopeTheCacheDoesNotHoldAnswersScopeAbsent (exit 2),
TestRefsAllScopesIsLoudAboutAnUnroutedScope (exit 0, want 11),
TestRefsAllScopesPrintsAPartialAnswerWhenAnInstanceIsUnread (empty stdout),
TestRefsDocumentFieldNamesArePinned, TestFoldIsTheReadersRefFold,
TestFoldRefusesAnythingButAJSONArrayOfStrings. TestParseCapIsTheStoresBodyCap is an
INVARIANT guard (green at both: the private constant had the same value).
Fold expectations are literal: the 7 folding rows of a consumer's oracle-produced fixture,
and path_refs rows produced by that consumer's own Python copy.

Mutation sweep 23/24 killed, each by the guard's own test; the survivor (LimitReader bound
doubled in parse) is equivalent — the length check still refuses at MaxEntryBytes+1.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@ZacxDev ZacxDev changed the title feat(client): versioned JSON output, validate --file, associate verb (Phase 5 stage 2) feat(client): versioned JSON output, validate --file, and associate/refs/parse/fold verbs (Phase 5 stage 2) Dec 4, 2026
@ZacxDev
ZacxDev marked this pull request as ready for review December 4, 2026 16:28
@ZacxDev
ZacxDev merged commit 4000d33 into main Dec 4, 2026
29 checks passed
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