fix(server): tighten handler return types so schema drift fails tsc#764
Merged
Conversation
`AdcpToolMap` brand-rights result slots were typed `Record<string,
unknown>` (stale scaffold) and `DomainHandler`'s return union
carried a `| Record<string, unknown>` escape hatch. Together these
let sparse handler returns like `{ rights_id, status: 'acquired' }`
pass tsc against `AcquireRightsResponse` — drift only failed at
wire-level validation.
Swaps brand-rights slots to the generated success types, drops the
escape hatch from `DomainHandler`, and adds a regression test that
asserts neither loosening can return. Test agents updated alongside:
`seller-agent.ts` uses `DEFAULT_REPORTING_CAPABILITIES`, and the
signed-MCP agent now mints `crypto.randomUUID()` for `package_id`
instead of reading it from an untyped request field (latent bug —
buyers don't send `package_id`).
Closes #727 (B).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #727 (B). Makes sparse handler returns like
{ rights_id, status: 'acquired' }failtscat compile time againstAcquireRightsResponse, instead of only failing at wire-level validation.Two hand-written type bindings in
create-adcp-server.tswere routing around the (already-tight) generated discriminated unions:AdcpToolMapbrand-rights slots were typedresult: Record<string, unknown>— stale scaffold from before the response types were code-generated. Swapped for the generated success types (AcquireRightsAcquired | AcquireRightsPendingApproval | AcquireRightsRejected,GetRightsSuccess,GetBrandIdentitySuccess).DomainHandler's return union included| Record<string, unknown>as a general escape hatch for every tool. Removed. Handler returns are nowAdcpToolMap[K]['result'] | McpToolResponse—adcpError(...)still works unchanged.No upstream schema or generator changes needed. The looseness was purely in SDK plumbing; the generated types were already correct discriminated unions with proper
requiredarrays.What this surfaced
Removing the escape hatch caught two real latent bugs in the reference test agents:
test-agents/seller-agent.ts:PRODUCTSwere missingreporting_capabilities(required onProduct). Strict response validation (default in dev/test per feat(server): strict response validation default in dev/test (#727) #757) would have caught this at runtime; now it fails at build. Fixed withDEFAULT_REPORTING_CAPABILITIES.test-agents/seller-agent-signed-mcp.ts:createMediaBuywas readingpkg.package_idfrom the request, butPackageRequesthas no such field per spec — buyers sendbuyer_refand the seller mintspackage_id. Fixed to mintcrypto.randomUUID().Regression guard
test/handler-return-tightness.test.jsasserts (a) no brand-rights slot inAdcpToolMapis typedRecord<string, unknown>and (b)DomainHandler's return union does not containRecord<string, unknown>. The runtime suite alone can't catch a reintroduced compile-time escape hatch.Test plan
npm run build:lib— cleannpm run build:test-agents— clean (was 2 errors before the test-agent fixes)npm run test:lib— 4391 pass, 0 fail@ts-expect-errorharness: sparse shapes, empty objects, and{ anything: 'goes' }all fail tsc atBrandRightsHandlers['acquireRights']return position; valid shapes compileMigration
Minor bump. If a handler returns a plain object literal without the full success shape, tsc will now flag it. Either fill in the missing required fields (use
DEFAULT_REPORTING_CAPABILITIESforProduct.reporting_capabilities) or wrap with a response builder (productsResponse({ ... }),acquireRightsResponse({ ... })) — details in the changeset.🤖 Generated with Claude Code