Skip to content

Decode the runner API's responses whole, against TypeBox schemas - #105

Merged
agorischek merged 4 commits into
mainfrom
pride-f11-runner-api-schemas
Nov 21, 2026
Merged

agorischek merged 4 commits into
mainfrom
pride-f11-runner-api-schemas

Conversation

@agorischek

Copy link
Copy Markdown
Collaborator

Code review, finding 11: the response decoders certified nested shapes they didn't check.

Evidence

In packages/host/inspector/src/client/responses.ts (lines 13-18, 25-28, 36-42 and 50-59 before this change), parseWrite, parseExplore, parseSweep and parsePairs checked an outer object and array and then asserted the full type with as unknown as. parseWrite accepted files: [null], and FilesPreview in explore.tsx (29-48) then dereferenced file.path on each entry. Explore candidates and sweep cells had the same gap between the envelope and its elements. A malformed or version-skewed response therefore failed in unrelated UI code rather than as an actionable error at the boundary. This is about robustness and the type contract. It doesn't treat the authenticated local runner as an adversary.

Strategy

The user approved the proposal ("Sure that proposal sounds good"):

  1. TypeBox in packages/schemas is the one schema system for the contracts we own. It already produces the published JSON Schemas and TS types. No Zod, no tRPC (the server is Rust).
  2. The runner's HTTP API responses that the Inspector decodes now have schemas, in packages/schemas/src/runner/api.ts, exported as @githubnext/projections-schemas/runner. The Inspector's types.ts now re-exports their Static types in place of the hand-written interfaces: ExploreCandidate(s), OnboardingFile/OnboardingWrite, Sweep*, InspectorPair(s), InspectorDefinitionSave and InspectorWorkspaceChanges. The names are unchanged, and a scratch check confirmed the new and old types are mutually assignable.
  3. Decoding at the client boundary: responses.ts calls decodeRunnerResponse(name, value). It returns the decoded value or throws a RunnerResponseError with request, pointer and reason, for example The local runner's response to GET /api/sweep is invalid at /sweep/cells/0/status: must be one of "changed", "unchanged", "failed".
    • It uses TypeBox's interpreting checker (Value.Check/Value.Errors), not Compile. The JIT needs unsafe-eval, which the Inspector's CSP (served page and VS Code webview) forbids. Probing for it would log CSP violations.
    • The root @githubnext/projections-schemas entry still loads no TypeBox at run time; only the ./runner subpath does.
  4. Rust conformance: packages/cli/tests/service/conformance.rs validates every response the service tests receive for these endpoints against the generated packages/schemas/internal/runner-api.json. The covered endpoints are adopt, explore, both sweep forms, pairs, definition save, changes, fixtures changes and document. Generating Rust types is out of scope.
    • The check is stricter than the clients: an undeclared property fails it. It supports exactly the keywords the generator writes and refuses any other.
    • It already found two fields the hand-written TS types lacked: /api/pairs suggestions' backwards and /api/changes' tree. Sweep cells' stderr and explore basis.skipped were added from reading the Rust.

Where the schemas live, and why

The schemas are in packages/schemas/src/runner, kept apart from the published src/typebox, and the generated file is packages/schemas/internal/runner-api.json, outside schemas/. It has no $id on the schemas host, isn't in the publication set and gets no reference pages.

The runner API is internal: the CLI and its Inspector ship from the same commit, and the editor and hosted clients follow the CLI. Publishing it as a versioned document would promise outside readers a stability it doesn't have. It still sits in packages/schemas so there is one schema system and one generator: npm run generate writes it, and the package's --check (and tools generated) fail when it's stale.

Validated vs deliberately opaque

  • Validated whole: every field the decoded types declare, nested entries included. The runner may still add fields: the clients accept them, and only the conformance check requires them to be declared.
  • Opaque past the envelope: /api/document's document (only format and version; the CLI's artifact::read_document validated it when loading) and an explore candidate's trial definition (an object; the runner loads and validates definitions).
  • Not touched: frame validation between host and renderer (host/core/src/frame/admission.ts, renderers/sdk/src/protocol/frame.ts), which is asymmetric on purpose. Decoders already written by hand in other files (definitions, revisions, scenarios, jobs, fixtures catalog, renderers, comparator) stay as they are and could move to schemas later.

Docs

  • docs/contracts.md#schema-owned-contracts states which contracts TypeBox owns, the internal runner API, the opaque documents and the Rust conformance rule.
  • packages/schemas/README.md and docs/runner/stdio-backend.md point to that section.

Tests

  • packages/schemas/test/runner.test.mjs covers decoding with malformed nested entries: files: [null] → /files/0, a candidate missing basis → /candidates/1, a sweep cell with changes: 'one' and with an unknown status, a document with the wrong format, a non-object root, and opaque documents passing through. The whole schemas package passes (22 tests).
  • cargo test -p projections-cli --test service, filtered to the conformance test and the pairs, sweeps, adopting, saving, working-tree changes and fixtures tests that now check conformance: 8 of 9 pass.
    • sweeps_run_projections_across_recent_commits failed locally only because that build didn't embed the default comparator Wasm. Its responses conformed, including the failed cells; its later unchanged assertion needs the comparator. CI builds it.
  • tools check --fast passes.
  • Left to CI:
    • The Inspector's typecheck and its node and browser suites, which need the engine/Wasm build.
    • docs:check.

Follow-up (private)

After the pin is bumped, the private src/github/projections-cli.ts should adopt decodeRunnerResponse from @githubnext/projections-schemas/runner and replace its own guards for these endpoints. The VS Code extension's separate parseWrite/parsePairs (integrations/vscode/src/writes.ts, projections/pair-list.ts) could do the same.

The Inspector's response decoders checked an outer object and array, then
asserted the full type with `as unknown as`: an adopt preview with
`files: [null]`, an explore candidate missing a field or a sweep cell of the
wrong type reached UI code, which failed there instead of at the boundary.

The local runner's API responses the Inspector decodes now have TypeBox
schemas in `packages/schemas/src/runner` (exported as
`@githubnext/projections-schemas/runner`; internal and unversioned), which
give the Inspector's types and `decodeRunnerResponse`, whose
`RunnerResponseError` names the request and the JSON pointer. The generated
`internal/runner-api.json` holds the Rust service to them: the service tests
check each response they receive against it, refusing undeclared fields.
Saved documents and trial definitions stay opaque past their envelope.

Code review, finding 11.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI 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.

🟡 Changes recommended

The schema omits the runner’s existing basis.file field, causing the strict Rust conformance checker to reject legitimate per-file explore candidates.

1 open issue
What changed in this PR

This PR moves selected runner API response contracts into the shared schemas package, replacing shallow Inspector checks with nested validation and adding Rust response conformance checks.

Changes:

  • Adds TypeBox response schemas, derived types, and a CSP-compatible decoder with structured errors.
  • Migrates Inspector response parsing to the shared decoder while retaining deliberately opaque document payloads.
  • Adds generated-schema tracking, decoder tests, Rust conformance checks, and contract documentation.
File Description
tools/​cli/​src/​lib/​generated.ts Tracks the generated runner API schema.
packages/​schemas/​test/​runner.test.mjs Tests valid responses and nested validation failures.
packages/​schemas/​src/​runner/​index.ts Exports runner schemas and decoding APIs.
packages/​schemas/​src/​runner/​decode.ts Implements validation and structured errors.
packages/​schemas/​src/​runner/​api.ts Defines response schemas and derived types.
packages/​schemas/​scripts/​generate.mjs Generates and checks the internal API schema.
packages/​schemas/​README.md Documents the internal runner schema entry point.
packages/​schemas/​package.json Exposes the ./runner subpath.
packages/​schemas/​internal/​runner-api.json Supplies generated schemas for Rust conformance checks.
packages/​host/​inspector/​src/​types.ts Re-exports schema-derived response types.
packages/​host/​inspector/​src/​client/​responses.ts Uses shared decoding for selected responses.
packages/​host/​inspector/​src/​client/​documents.ts Removes the replaced document-envelope guard.
packages/​cli/​tests/​service/​main.rs Registers the conformance test module.
packages/​cli/​tests/​service/​history.rs Checks pairs and sweep responses for conformance.
packages/​cli/​tests/​service/​fixtures.rs Checks document and fixture-generation responses.
packages/​cli/​tests/​service/​conformance.rs Adds strict response-schema validation for tests.
packages/​cli/​tests/​service/​changes.rs Checks working-tree change responses.
packages/​cli/​tests/​service/​authoring.rs Checks save, explore, and adopt responses.
docs/​runner/​stdio-backend.md Links response documentation to schema ownership.
docs/​contracts.md Defines schema ownership, opacity, and conformance rules.

Review effort: Balanced

Comment thread packages/schemas/src/runner/api.ts Resolved

Copilot AI 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.

🔵 Needs a closer look

The schema omits the runner’s existing basis.file field, causing the strict conformance checker to reject valid workflow candidates, and current tests do not cover this case.

1 open issue

Review effort: Balanced

agorischek and others added 2 commits November 20, 2026 14:44
Explore's per-file candidates (one per workflow) name their file in
basis.file, which the schema omitted, so the strict conformance check
refused them. Declare it, regenerate internal/runner-api.json, and check
a workflow's candidate against the schema.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@agorischek
agorischek merged commit f7fb9fa into main Nov 21, 2026
52 checks passed
@agorischek
agorischek deleted the pride-f11-runner-api-schemas branch November 21, 2026 02:45
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.

2 participants