Skip to content

feat(schema): zod at tool+viewer boundaries; ISO ts at the store reader (closes #15) - #31

Merged
abogartz merged 2 commits into
mainfrom
feat/boundary-schemas
Oct 22, 2026
Merged

abogartz merged 2 commits into
mainfrom
feat/boundary-schemas

Conversation

@abogartz

Copy link
Copy Markdown
Owner

Closes #15. The zod tier from the 2026-10-21 review, kept exactly as scoped then: a shape gate at the input boundaries, nothing functional.

Design

One source of truth. The JSON Schema the model sees is now generated from the zod schemas (z.toJSONSchema, $schema stripped, empty required restored). zod-alongside-the-hand-builders would have been two descriptions of one contract — the drift surface we just buried in the artifact cleanup. The test proves the migration is invisible to the model: all 13 registered tool inputs deep-equal a byte-capture taken from main before any code changed.

Coercion where intent-preserving, strict where not (the trap that decides this):

  • z.coerce.number() on limit/olderThanDays/evidence-limit — limit: "2" means 2 and already worked by downstream accident; coercion cannot break a working flow.
  • Booleans stay strict on purpose: z.coerce.boolean() is JS Boolean(), so dryRun: "false" coerces to true — a dry-run call mutating the store. A rejected call returns a message the model can fix; a silent flip does not exist.
  • Strings stay strict — coercing content is precisely the garbage-write hole.

What lands

  • Tools: every execute() re-parses raw args via validated(); malformed calls answer invalid arguments to memory_X — nothing was done: <fields>. Verified against live main first: a tags string today fails or not depending on unrelated gate heuristics; zod makes the boundary deterministic. : any is gone from all tool bodies — tsc now checks them against the schema (types flow from z.infer).
  • Viewer: POST bodies schema-parsed → clean 400. Closes the restore-entry hole: on main, POST /api/restore-entry {} returned 200 and appended a row with empty type/scope/content (probe output in commit message); String(body.ts ?? "") made ts:{} the row identity "[object Object]". /api/restore keeps its basename+grammar containment (viewer /api/restore joins raw body.name — files outside the store can be promoted into memory #21) — different job, untouched.
  • Store reader (store: validate ts shape at read time (row identity must not be an arbitrary string) #15): toMemory rejects a ts that is not an ISO instant (regex + Date.parse for structurally-valid impossibilities like month 13). Malformed rows are skipped by every reader, counted (memory_status/TUI show N malformed, hidden at zero), warned once per file. logfmt.ts stays a dumb tokenizer — the shape check lives where the schema knowledge lives. Supersede chains, the blocklist, undo, and viewer buttons can no longer key off arbitrary identity strings.
  • zod is a runtime dependency — npm plugin installs fetch runtime deps only; devDep would fail closed at load.

Deliberately excluded

Semantics (a well-shaped lie still passes — the gates own that), /api/restore name grammar (containment, not shape), and any zod in the hot read path (a plain ISO check beats schemas over every row).

Gate

14 new tests (wire-equality x13, boundary rejects incl. the dryRun string trap, coercion non-breaking, viewer 400s + happy path, reader skip/tally/impossible-date/clean-store-silent). 235 pass / 4 skip / 0 fail, tsc --noEmit clean, biome lint 0. Probes of main's behavior are in the commit message, not a tracked doc.

…ation at the store reader

Shape gate, not a meaning gate (the 4-gate pipeline still owns semantics).

Single source of truth: the JSON Schema handed to the model is GENERATED
from the zod schemas (z.toJSONSchema, $schema stripped, empty required
restored) — hand-written wire shapes next to runtime validators would
recreate exactly the drift surface zod removes. test/boundaries.test.ts
deep-equals all 13 registered tool inputs against a byte-capture taken
from main: the model-facing contract did not move.

- Tools: every execute() re-parses raw args; malformed calls get
  "invalid arguments ... nothing was done" naming the fields, instead
  of a gate message by coincidence or junk reaching appendMemory.
  z.coerce.number() for numerics (limit:"2" still works — strictly
  non-breaking); booleans deliberately STRICT: z.coerce.boolean is JS
  Boolean() and would flip dryRun:"false" into TRUE — a dry run
  mutating. Verified main's behavior with live probes (restore-entry
  with an empty body appended a row with empty type/scope/content and
  got 200; ts=hello loaded as an addressable row; tags was unchecked
  except where unrelated heuristics caught it).
- Viewer: POST bodies are schema-parsed (400 on junk). Closes the
  restore-entry hole; String(body.ts) is gone, so an object ts can no
  longer become the row identity "[object Object]". /api/restore keeps
  its basename+grammar containment (#21) untouched — different job.
- Store reader (#15): toMemory rejects a ts that is not an ISO instant
  (regex + Date.parse for impossible dates); rows are skipped, counted
  (memory_status/TUI show "N malformed", hidden at 0), warned once per
  file. logfmt stays a dumb parser; supersede chains, blocklist, undo,
  and viewer buttons can no longer key off arbitrary strings.
- zod is a RUNTIME dependency (npm plugin installs fetch deps only).

14 new tests (wire contract x13 tools, tool-boundary rejects, coercion
non-breaking, viewer 400s, reader skip/tally/impossible-date). Suite
235 pass / 4 skip / 0 fail; tsc clean; biome lint 0.
Supply-chain policy: dependencies AND devDependencies are exact-pinned.
Consumers resolve our semver ranges at plugin-install time (a lockfile
is not their install instruction), so carets, >= ranges, and "latest"
were a standing window for any future — including compromised —
release to land in every user's process. Updates are now deliberate
acts (bun upgrade <pkg>, reviewed lockfile diff, CI).

peerDependencies stay ranges by design: they declare tolerance for the
host-provided environment (opencode ships the opentui/solid runtime
itself), not a fetch target; pinning them would only break hosts on
newer versions. optionalDeps codebase-memory-mcp was already exact.

Pinned to the versions currently resolved in bun.lock — no upgrades
sneaking in under the pin: zod 4.6.7, biome 2.6.2, @opencode/plugin
2.0.32, @OpenTui core+solid 0.6.6, @semantic-release/git 11.0.1,
@types/bun 1.4.2 (was "latest"), semantic-release 25.0.9, solid-js
1.9.17, typescript 7.0.2. Suite 235 pass / 0 fail.
@abogartz
abogartz force-pushed the feat/boundary-schemas branch from b0d1ebd to 65fa73b Compare October 22, 2026 15:59
@abogartz
abogartz merged commit 5de62ed into main Oct 22, 2026
2 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@abogartz
abogartz deleted the feat/boundary-schemas branch October 22, 2026 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

store: validate ts shape at read time (row identity must not be an arbitrary string)

1 participant