Skip to content

feat(server): response builders for acquire_rights, sync_accounts, sync_governance, report_usage - #725

Merged
bokelley merged 6 commits into
mainfrom
bokelley/sdk-utility-response-builders
Apr 21, 2026
Merged

bokelley merged 6 commits into
mainfrom
bokelley/sdk-utility-response-builders

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Shifts from patching skill examples to fixing the drift class once in the SDK — first step of the 'SDK utilities over hand-rolled skill examples' plan prompted by matrix-v5 findings.

New builders

  • acquireRightsResponse(data) — typed on the acquired | pending_approval | rejected discriminated union. Runtime-validates approval_webhook.authentication.credentials length (spec: ≥32 chars) with an actionable error message.
  • syncAccountsResponse(data) — asserts every accounts[i].account_id is present. The sync_accounts storyboard step requires platform-assigned IDs; fresh agents commonly echo request fields without stamping one.
  • syncGovernanceResponse(data) — typed passthrough.
  • reportUsageResponse(data) + reportUsageResponse.fromRequest(request) — auto-defaults accepted: 0; the .fromRequest(...) shortcut acknowledges every usage[] entry as accepted.
  • buildCreativeResponse now validates creative_manifest.format_id is { agent_url, id } (not bare string or undefined). Previously failed downstream with a cryptic oneOf error.

All four are auto-applied via createAdcpServer's TOOL_META so handlers return domain objects and the framework wraps. Also exported from @adcp/client and @adcp/client/server for manual use.

Why

Matrix v5 had four distinct SERVICE_UNAVAILABLE: Tool X encountered internal error signatures — fresh-built agents calling tools that returned objects missing required fields the schema now mandates. Rather than teach every skill reader to hand-roll correct shapes, encode the shape in the SDK once; correct code is the compile-time / first-call-time easy path.

Test plan

  • tsc --noEmit clean
  • npm run build:lib clean
  • CI
  • Rerun npm run compliance:skill-matrix after merge — expect the 4 SERVICE_UNAVAILABLE failures to shift to either pass or different signature

🤖 Generated with Claude Code

…report_usage

Review-driven follow-ups on PR #725:

- acquireRightsResponse now accepts the full AcquireRightsResponse union
  (including AcquireRightsError). Error payloads pass through as
  structuredContent + a minimal 'Rights acquisition error' summary.
- Same for syncAccountsResponse — accepts SyncAccountsResponse union,
  error variant passes through.
- syncGovernanceResponse already took the full union; no change.
- reportUsageResponse drops the accepted: 0 default; throws with an
  actionable pointer at reportUsageResponse.fromRequest(request) when
  the handler forgets to set accepted (consistent with the
  throw-on-missing-required-field stance in the rest of the PR).
- acquireRightsResponse now validates revocation_webhook.authentication
  credentials too, not just approval_webhook. Extracted
  assertWebhookCredentials helper so future webhook fields get the same
  ≥32-char check.
- Documented .fromRequest shortcut with an @example and explicit
  'sets accepted = request.usage?.length ?? 0' jsdoc.
…report_usage ack

Applies DX-expert feedback on PR #725:

1. Brand-rights skill example credential (line 419) was 31 chars and would
   trip the builder's own >=32 check. Swap to randomUUID().replace(/-/g,'')
   (32 hex) and annotate the spec constraint inline.
2. Remove dead revocation_webhook check on acquireRightsResponse — the
   field is on the request, not the response, and the 'as unknown' cast
   made that obvious on second read.
3. Per-variant constructors: acquireRightsAcquired, acquireRightsPendingApproval,
   acquireRightsRejected. Coding agents that type 'acquireRightsAcqu…'
   get the right variant's required fields directly without reading the
   4-variant union. Delegates to acquireRightsResponse so runtime checks
   still fire.
4. reportUsageResponse.fromRequest renamed to .acceptAll and reshaped to
   take opts.errors so the shortcut can't silently lie about ingest
   failures. The 'ack everything from request' case is one call; the
   'ack all minus errors' case is one call with an argument; the old
   fromRequest signature had no way to express honest failures.
5. report_usage error message now names the '@adcp/client/server' import.
Test-agent-team review called out the architecture issue: builders were
doing the schema validator's job (credential length, schemes count,
account_id presence, accepted requiredness, format_id shape). Those are
JSON Schema constraints that belong in wire-level Zod validation
(already available as createAdcpServer({ validation: responses: strict }),
not default yet). Putting them in builders:

1. Only fires for tools whose handlers reach the wrapper.
2. Misses manual-registration paths (server.registerTool).
3. Teaches the next contributor to add a sixth runtime guard instead
   of fixing the generator + validator.

Builders now do one job: wrap the domain object in { content,
structuredContent }. Schema enforcement lives one layer up. Per-variant
constructors (acquireRightsAcquired, etc.) stay — they tighten the
type surface, not the runtime.

Follow-ups tracked separately:
- Default validation.responses to warn/strict in dev mode.
- Tighten generated TS discriminated unions so sparse shapes fail tsc.
@bokelley
bokelley merged commit 65740a1 into main Apr 21, 2026
14 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