Files
callstack__agent-device/test/wire-compat/wire-mutations.test.ts
Michał Pierzchała 05a1d76f2e test: add daemon RPC wire-surface compatibility gate (#1717)
* test: gate daemon RPC wire compatibility against the last released tag (#1432)

ADR 0006 fixes exactly when DAEMON_RPC_PROTOCOL_VERSION must be bumped, and
nothing checked that it was. The runtime guard (readRemoteDaemonHealth) refuses
a mismatched peer, but only fires when someone remembered the bump — a wire
change that skipped it left both sides advertising protocol 2 while parsing
different payloads, which is the failure ADR 0006 exists to prevent.

Local daemons cannot skew (isReusableDaemonInfo takes over on any package
version mismatch). Cross-machine is skewed by design — proxy, cloud/limrun, a
remote macOS host — and ADR 0006 explicitly rules package version out as the
compatibility gate there, so the one boundary where skew is intended was the
one boundary with no gate.

test/wire-compat/surface.ts declares the wire surface grouped by the ADR bullet
each group serves, quoting it, with an `uncovered` note where a bullet is only
partly digestible (the /health and /rpc literals inside http-server.ts stay
reviewer-owned: a moved route 404s at connect time rather than misparsing).
ledger.json records what each declaration hashes to, at which protocol version.

Two gates, split for the same reason the replay-compat corpus splits:
- unit-core holds the ledger to its source and prints the digest to paste;
- Released-Surface Compatibility reads the ledger at the last RELEASED tag and
  requires the drift since then to carry a bump or a compatibleChanges ack.

From one commit a bumped ledger and an unbumped one are both just an edited
file, so only a released baseline can tell them apart. Acks are keyed by the
digest they cover, so one "added an optional field" cannot launder later
changes. Digests ignore comments and formatting; the manifest's closure is
derived from the AST, so a field typed by an unlisted sibling fails rather than
sitting outside the gate.

CI cost: one added job (checkout + toolchain + two node scripts, ~1 min),
mirroring the existing full-history replay-compat job.

* test: close wire-surface overclaim and make the closure fail closed (#1432)

Addresses both review P1s on #1717.

P1 — the manifest materially overclaimed ADR 0006 coverage. It quoted all four
bullets while digesting only the payload TYPES, so the producer and consumer
seams could break a skewed peer without moving a listed digest. Now listed on
both sides of every boundary: JSON-RPC method sets and the projections that
turn each method's params into a DaemonRequest, createRpcError/sendJson/
writeRpcResponseEnvelope, resolveToken and the auth-hook types, upload
preflight/finalize/308 handlers and the resumable ticket shape, artifact route
and download/inventory framing, REST error mapping, and the client's own
payload builder, lease-method mapping, response parser and error projection.
57 -> 117 declarations.

What stays out is now named rather than implied: createDaemonHttpServer's
dispatch wiring and the /health and /rpc literals inside it. Everything it
dispatches WITH is digested individually, and a moved route 404s at connect
time rather than misparsing — the loud failure, not the silent one.

P1 — imported and re-exported payload shapes escaped the closure.
declarationHomes() scanned only the manifest's own files and the walk
continued silently when a name could not be placed, so a listed type could
gain foo?: ImportedShape from a new module and stay green. Resolution is now
explicit and fails closed: relative imports, workspace specifiers (through the
owning package's own exports map, so a re-pointed export cannot drop a type),
and facade re-export chains. Every referenced name must land on a listed
declaration, a waiver with a written reason, a declared external module, or the
TS/Node global set. Fixed two extractor blind spots the walk exposed: a
declaration's own generic parameters and `as const` were being reported as
references.

Planted-red proofs (wire-mutations.test.ts): 13 cases independently mutate
method naming, response serialization, response parsing, auth projection,
upload ticket shape, 308 framing, artifact framing, REST error mapping, and
progress framing, each asserting the digest moves; 3 probes prove the closure
really reaches across a package boundary, a facade re-export, and a plain
relative import. Mutations apply inside the declaration's own span — a
whole-file replace silently hit a sibling sharing the substring, which is how
the first draft of one case passed vacuously.

The largest waiver pair (InternalRequestOptions, CommandFlags) rests on ADR
0006's own additive rule: they reach the peer inside DaemonRequest's untyped
flags/input bags, and the decision says a new flag needs no bump. Digesting
them would fire the gate on every new CLI flag and train reviewers to
rubber-stamp acks.

* test: list the consumer half of the auxiliary HTTP boundaries (#1432)

Addresses the remaining review P1 on #1717. The manifest claimed both sides of
response/upload/artifact framing while listing nothing from upload-client.ts,
daemon-artifacts.ts, or the health consumer in daemon-client-transport.ts, so
those parsers could narrow without moving a listed digest or protocol 2.

Now listed (117 -> 141 declarations):

- /health consumer: RemoteDaemonHealth, readHealthPayload, readDaemonHttpHealth,
  readRemoteDaemonHealth. This is the sharpest of the three — narrowing the
  reader or the comparison disables the very refusal ADR 0006 exists to
  guarantee, and nothing else in the repo would notice.
- /upload consumer: UploadResponse, UploadPreflightResponse, UploadPreflightResult,
  parseUploadPreflightResult, requestUploadPreflight, uploadDirectArtifact,
  tryDirectUploadWithResume, shouldRetryDirectUpload, finalizeDirectUpload,
  uploadLegacyArtifact, ARTIFACT_HASH_ALGORITHM, isStringRecord, and
  PreparedUploadArtifact — whose sha256/sizeBytes/fileName/artifactType/
  contentType fields ARE the preflight body the daemon parses.
- /artifacts/* consumer: DaemonArtifactEndpoint, buildDaemonArtifactUrl,
  isRemoteDaemon, DownloadRemoteArtifactParams, downloadRemoteArtifact,
  materializeRemoteArtifacts, resolveMaterializedArtifactPath.

Running the closure fail-closed over the new files surfaced three more stops,
each decided rather than skipped: PreparedUploadArtifact listed (it is payload),
UploadProgressSink waived (client-local rendering, never leaves the process),
and src/daemon/types.ts#DaemonArtifact waived as a re-export alias of the listed
kernel type, matching its DaemonRequest/DaemonResponse siblings.

10 more planted-red mutations cover the new seams: health version-read and
mismatch-refusal defeated, RemoteDaemonHealth field dropped, preflight parser
narrowed, preflight/legacy response shapes narrowed, finalize body key renamed,
ticket field renamed, artifact tenant header dropped, artifact URL moved. A
fourth closure probe proves the upload-consumer files are genuinely reached by
the walk rather than merely listed. 22 -> 33 tests.

The README now states the coverage as a producer/consumer table per boundary,
so the claim is checkable at a glance instead of asserted in prose.

* test: list the client half of the resumable 308 contract (#1432)

Addresses the third review P1 on #1717. Listing the daemon's
handleResumableUpload proved it still PRODUCES 308; nothing proved the client
still CONSUMES the released one. src/remote/upload-stream.ts owns that half and
was entirely outside the manifest, so a newer client could stop accepting
`upload-offset`, change how it reads `Range: bytes=0-N`, or emit a different
resumed `Content-Range` without moving one of the 141 listed digests.

Now listed (141 -> 151): UploadStreamResponse, streamFileToHttpRequest,
streamFileToHttpRequestAttempt, buildUploadRequestHeaders, isUploadResumeStatus,
isUploadRedirectStatus, parseUploadResumeOffset, parseNonNegativeIntegerHeader,
firstHeaderValue, MAX_UPLOAD_REDIRECTS.

streamFileToHttpRequestAttempt is listed despite its size, unlike
createDaemonHttpServer which stays in `uncovered`. The distinction is stated at
the declaration: the HTTP server only dispatches to handlers that are each
digested, while the attempt loop IS the resume state machine — it decides
whether a 308 continues the upload and what the next request carries, so its
sequencing alone can break a released daemon while every helper keeps its digest.

6 new planted-red mutations prove the client half moves the ledger: a dropped
`upload-offset` fallback, narrowed Range parsing, a changed resumed
Content-Range, 308 no longer treated as continue, a narrowed UploadStreamResponse,
and dropped header-value coercion. 33 -> 39 tests.

Closure fail-closed surfaced two more stops: UploadStreamProgressOptions waived
(local byte-progress rendering) and URL/URLSearchParams added to the global set.

README now carries a `/upload` resume row in the producer/consumer table, and
names the pattern behind three rounds of review: the coverage sentence kept
getting written ahead of the coverage, so the table and the `uncovered` notes
are the claims to trust — they are checkable against surface.ts, prose is not.

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-08-10 20:52:29 +02:00

354 lines
13 KiB
TypeScript

/**
* Planted-red proofs: does the gate actually catch each class of wire break?
* (#1432, review P1.)
*
* The ledger and closure tests prove the manifest describes today's source.
* They cannot prove the manifest describes the RIGHT source — a gate can list
* 117 declarations, pass every check, and still miss the seam that breaks a
* skewed peer. So each case below mutates one real listed declaration the way
* a real break would, and asserts the digest moves.
*
* Two guards keep these from going vacuous, which is the standing failure mode
* for regression tests here (AGENTS.md: "a green check is evidence only once
* you have seen it red"):
*
* 1. every mutation must actually change the source it is applied to, so a
* renamed field silently turning the replacement into a no-op fails loudly
* rather than passing on an unmutated digest;
* 2. the unmutated digest must equal the ledger's, so the case is pinned to
* the declaration the gate really watches rather than to a stale copy.
*
* They run against source text in memory. Nothing on disk is modified.
*/
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { test } from 'vitest';
import { isExternalWireSpecifier, WIRE_CLOSURE_WAIVERS } from './closure-policy.ts';
import { findClosureGaps } from './closure.ts';
import { digestDeclaration, replaceInDeclaration } from './declaration-digest.ts';
import { readWireLedger } from './ledger.ts';
import { WIRE_DECLARATIONS, wireDeclarationKey } from './surface.ts';
const repoRoot = path.resolve(import.meta.dirname, '..', '..');
const ledger = readWireLedger(repoRoot);
function readSource(file: string): string {
return fs.readFileSync(path.join(repoRoot, file), 'utf8');
}
type WireMutation = {
/** The ADR 0006 break class this case stands for. */
breakClass: string;
file: string;
name: string;
/** Substring to replace, and what a real break would replace it with. */
from: string;
to: string;
};
const MUTATIONS: readonly WireMutation[] = [
{
breakClass: 'method naming: a released client keeps sending the old method name',
file: 'src/daemon/server/http-server.ts',
name: 'COMMAND_RPC_METHODS',
from: "'agent-device.command'",
to: "'agent-device.cmd'",
},
{
breakClass: 'method naming: the lease method table drops a released alias',
file: 'src/daemon/server/http-server.ts',
name: 'LEASE_RPC_METHOD_TO_COMMAND',
from: "'agent-device.lease.allocate': 'lease_allocate',",
to: '',
},
{
breakClass: 'response serialization: the RPC envelope stops being newline-framed',
file: 'src/daemon/request-progress-protocol.ts',
name: 'serializeDaemonRpcResponseEnvelope',
from: '}\\n`',
to: '}`',
},
{
breakClass: 'response parsing: the client narrows what a daemon may return',
file: 'src/daemon/client/daemon-client-rpc.ts',
name: 'parseDaemonHttpResponseBody',
from: 'error?: { message?: string; data?: Record<string, unknown> }',
to: 'error?: { message: string }',
},
{
breakClass: 'auth projection: the header a released client authenticates with is dropped',
file: 'src/daemon/server/http-server.ts',
name: 'resolveToken',
from: "typeof headers['x-agent-device-token'] === 'string'",
to: 'false',
},
{
breakClass: 'auth projection: the client stops sending the bearer form',
file: 'src/daemon/http-contract.ts',
name: 'buildDaemonHttpAuthHeaders',
from: 'authorization: `Bearer ${normalizedToken}`,',
to: '',
},
{
breakClass: 'upload ticket shape: a required preflight field is renamed',
file: 'src/daemon/resumable-upload.ts',
name: 'BeginResumableUploadOptions',
from: 'sha256: string;',
to: 'checksum: string;',
},
{
breakClass: 'upload ticket shape: the preflight body parser requires a new field',
file: 'src/daemon/upload-http.ts',
name: 'readUploadPreflightBody',
from: "readRequiredText(record, 'uploadAttemptId')",
to: "readRequiredText(record, 'attemptId')",
},
{
breakClass: '308 framing: resumable upload stops signalling continuation',
file: 'src/daemon/upload-http.ts',
name: 'handleResumableUpload',
from: 'res.statusCode = 308;',
to: 'res.statusCode = 200;',
},
{
breakClass: 'artifact framing: the direct-upload route prefix moves',
file: 'src/daemon/upload-http.ts',
name: 'DIRECT_UPLOAD_PATH_PREFIX',
from: "'/upload/direct/'",
to: "'/uploads/direct/'",
},
{
breakClass: 'artifact framing: the artifact inventory route stops answering its released path',
file: 'src/daemon/downloadable-artifact-http.ts',
name: 'resolveDownloadableArtifactHttpRoute',
from: "pathname === '/artifacts' || pathname === '/artifacts/'",
to: "pathname === '/artifacts'",
},
{
breakClass: 'REST error framing: a released status mapping changes',
file: 'src/daemon/http-errors.ts',
name: 'statusCodeForNormalizedError',
from: 'INVALID_ARGS',
to: 'BAD_ARGS',
},
{
breakClass: 'progress framing: the envelope discriminant is renamed',
file: 'src/daemon/request-progress-protocol.ts',
name: 'DaemonProgressEnvelope',
from: "type: 'progress';",
to: "kind: 'progress';",
},
// Consumer half of the auxiliary boundaries. A client-only narrowing here
// rejects a released daemon's response with no server change at all, which is
// why claiming "both sides" required these to be listed and proved.
{
breakClass: 'health consumer: the client stops reading the advertised protocol version',
file: 'src/daemon/client/daemon-client-transport.ts',
name: 'readHealthPayload',
from: "typeof parsed.rpcProtocolVersion === 'number' ? parsed.rpcProtocolVersion : undefined",
to: 'undefined',
},
{
breakClass: 'health consumer: the mismatch refusal ADR 0006 built is weakened',
file: 'src/daemon/client/daemon-client-transport.ts',
name: 'readRemoteDaemonHealth',
from: 'health.rpcProtocolVersion !== DAEMON_RPC_PROTOCOL_VERSION',
to: 'false',
},
{
breakClass: 'health consumer: the parsed health shape drops a released field',
file: 'src/daemon/client/daemon-client-transport.ts',
name: 'RemoteDaemonHealth',
from: 'rpcProtocolVersion?: number;',
to: '',
},
{
breakClass: 'upload consumer: the preflight parser stops accepting a released response',
file: 'src/remote/upload-client.ts',
name: 'parseUploadPreflightResult',
from: "preflight.ok !== true || typeof preflight.uploadId !== 'string'",
to: 'preflight.ok !== true',
},
{
breakClass: 'upload consumer: the preflight response shape narrows',
file: 'src/remote/upload-client.ts',
name: 'UploadPreflightResponse',
from: 'cacheHit?: boolean;',
to: '',
},
{
breakClass: 'upload consumer: the legacy/finalize response shape narrows',
file: 'src/remote/upload-client.ts',
name: 'UploadResponse',
from: 'uploadId: string;',
to: 'id: string;',
},
{
breakClass: 'upload consumer: the finalize request body key is renamed',
file: 'src/remote/upload-client.ts',
name: 'finalizeDirectUpload',
from: 'JSON.stringify({ uploadId: options.uploadId })',
to: 'JSON.stringify({ id: options.uploadId })',
},
{
breakClass: 'upload consumer: the preflight ticket fields the daemon parses are renamed',
file: 'src/remote/upload-client-artifact.ts',
name: 'PreparedUploadArtifact',
from: 'sha256: string;',
to: 'checksum: string;',
},
{
breakClass: 'artifact consumer: the download request drops its tenant header',
file: 'src/remote/daemon-artifacts.ts',
name: 'downloadRemoteArtifact',
from: '...buildDaemonHttpTenantHeaders(params.requestScope?.tenantId),',
to: '',
},
{
breakClass: 'artifact consumer: the artifact URL the client requests moves',
file: 'src/remote/daemon-artifacts.ts',
name: 'buildDaemonArtifactUrl',
from: 'artifacts/',
to: 'artifact/',
},
// The client half of the resumable 308 contract. Listing the daemon's
// handleResumableUpload proves it still PRODUCES 308; these prove the client
// still consumes the released one — which offset headers it accepts, how it
// reads `Range`, what `Content-Range` a resumed PUT emits, and that 308 is
// still the status it treats as "continue".
{
breakClass: '308 consumer: the client stops accepting a released offset header',
file: 'src/remote/upload-stream.ts',
name: 'parseUploadResumeOffset',
from: "headers['x-upload-offset'] ?? headers['upload-offset']",
to: "headers['x-upload-offset']",
},
{
breakClass: '308 consumer: Range-header offset parsing narrows',
file: 'src/remote/upload-stream.ts',
name: 'parseUploadResumeOffset',
from: '/^bytes=0-(\\d+)$/',
to: '/^bytes 0-(\\d+)$/',
},
{
breakClass: '308 consumer: the resumed request emits a different Content-Range',
file: 'src/remote/upload-stream.ts',
name: 'buildUploadRequestHeaders',
from: "'content-range': `bytes ${startOffset}-${payloadSize - 1}/${payloadSize}`,",
to: "'content-range': `bytes=${startOffset}-${payloadSize - 1}/${payloadSize}`,",
},
{
breakClass: '308 consumer: the client stops treating 308 as resume-and-continue',
file: 'src/remote/upload-stream.ts',
name: 'isUploadResumeStatus',
from: 'statusCode === 308',
to: 'statusCode === 208',
},
{
breakClass: '308 consumer: the streamed response shape a caller parses narrows',
file: 'src/remote/upload-stream.ts',
name: 'UploadStreamResponse',
from: 'statusMessage?: string;',
to: '',
},
{
breakClass: '308 consumer: header value coercion drops a released form',
file: 'src/remote/upload-stream.ts',
name: 'parseNonNegativeIntegerHeader',
from: 'firstHeaderValue(value)',
to: 'undefined',
},
];
for (const mutation of MUTATIONS) {
test(`gate catches — ${mutation.breakClass}`, () => {
const key = `${mutation.file}#${mutation.name}`;
const source = readSource(mutation.file);
assert.equal(
digestDeclaration(mutation.file, source, mutation.name),
ledger.declarations[key],
`${key} is not pinned at its ledger digest, so this case would prove nothing about the gate.`,
);
// Scoped to the declaration's own span: a whole-file replace would silently
// mutate a sibling that happens to share the substring, and throws rather
// than no-oping when the text has moved on.
const mutated = replaceInDeclaration(
mutation.file,
source,
mutation.name,
mutation.from,
mutation.to,
);
assert.notEqual(mutated, source, `The mutation for ${key} did not change the source.`);
assert.notEqual(
digestDeclaration(mutation.file, mutated, mutation.name),
ledger.declarations[key],
`${key} absorbed a ${mutation.breakClass} without moving its digest, so the gate would let ` +
`that break ship. The declaration listed in surface.ts is not the one carrying this ` +
`contract — list the one that is.`,
);
});
}
// The closure's own red proof. Dropping a listed declaration from the claimed
// set stands in for never having listed it: if the walk still reports nothing,
// import/re-export resolution is not actually reaching that type and the
// closure claim is hollow — which is precisely the defect review P1 found.
const CLOSURE_PROBES: readonly { reachedFrom: string; omit: string }[] = [
{
// Crosses a package boundary AND a façade re-export
// (`@agent-device/contracts/progress` → `../request-progress.ts`).
reachedFrom: 'DaemonProgressEnvelope',
omit: 'packages/contracts/src/request-progress.ts#RequestProgressEvent',
},
{
// Plain relative import inside src/.
reachedFrom: 'buildLeaseRpcParams',
omit: 'src/core/lease-scope.ts#LeaseRpcCommand',
},
{
// Workspace specifier resolved through the package's own exports map.
reachedFrom: 'DaemonResponse',
omit: 'packages/kernel/src/errors.ts#DaemonError',
},
{
// The consumer half of the auxiliary upload boundary, added after review:
// proves those files are genuinely wired into the walk, not just listed.
reachedFrom: 'requestUploadPreflight',
omit: 'src/remote/upload-client-artifact.ts#PreparedUploadArtifact',
},
];
for (const probe of CLOSURE_PROBES) {
test(`closure catches an unlisted wire type reached from ${probe.reachedFrom}`, () => {
const claimed = new Set(WIRE_DECLARATIONS.map(wireDeclarationKey));
assert.ok(
claimed.delete(probe.omit),
`${probe.omit} must be listed for this probe to mean anything.`,
);
const { omitted, unresolved } = findClosureGaps({
repoRoot,
readSource,
declarations: WIRE_DECLARATIONS,
claimed,
waivers: WIRE_CLOSURE_WAIVERS,
isExternalSpecifier: isExternalWireSpecifier,
});
assert.deepEqual(unresolved, [], 'The closure must place every name even while probing.');
assert.ok(
omitted.some((entry) => entry.startsWith(`${probe.omit} (`)),
`Removing ${probe.omit} from the claimed set did not make the closure report it. The walk is ` +
`not reaching that declaration, so listing it proves nothing.\nReported: ${
omitted.join(', ') || '(nothing)'
}`,
);
});
}