Skip to content

feat: finish client-side idempotency gaps from #568 - #604

Merged
bokelley merged 4 commits into
mainfrom
bokelley/issue-568
Apr 19, 2026
Merged

bokelley merged 4 commits into
mainfrom
bokelley/issue-568

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Closes the remaining acceptance criteria from #568 that were left open after PR #590. Four concrete additions, all additive / non-breaking.

  1. Typed error instances surface on TaskResult. When the seller returns IDEMPOTENCY_CONFLICT / IDEMPOTENCY_EXPIRED, the result now carries an errorInstance populated with the corresponding ADCPError subclass. Callers who want `if (result.errorInstance instanceof IdempotencyConflictError)` get it without switching on code strings. The SDK pulls the sent `idempotency_key` from tracked task state — the server intentionally omits it from error bodies (read-oracle).
  2. `getIdempotencyReplayTtlSeconds()` on `SingleAgentClient` / `AgentClient`. Reads `adcp.idempotency.replay_ttl_seconds` from cached capabilities. Returns `undefined` on v2, the declared number on compliant v3, and throws when a v3 seller omits the declaration. No silent 24h default — spec requires it, and defaulting would mislead BYOK callers about their replay window.
  3. `useIdempotencyKey(key)` BYOK helper. Validates against `IDEMPOTENCY_KEY_PATTERN` up front and returns `{ idempotency_key }` for spreading into params. Catches persisted-key drift before the round-trip.
  4. Key logging hygiene. MCP and A2A debug logs truncate `idempotency_key` values to the first 8 chars + `…` by default. Set `ADCP_LOG_IDEMPOTENCY_KEYS=1` for full logging. Retry-pattern oracle within TTL — shouldn't land in debug transcripts.

Plus `adcpErrorToTypedError(adcpError, key)` exported for callers that already hold an `AdcpErrorInfo` and want to type-narrow themselves.

Public API additions

```ts
import {
adcpErrorToTypedError,
useIdempotencyKey,
redactIdempotencyKey,
type IdempotencyCapabilities,
} from '@adcp/client';

const ttl = await client.getIdempotencyReplayTtlSeconds(); // throws on non-compliant v3
```

Not a breaking change

`TaskResult.errorInstance` is additive — existing code switching on `adcpError.code` still works. The new method only throws when callers explicitly ask for it against a non-compliant seller.

Test plan

  • `npm run build:lib` — clean compile
  • New unit tests for `adcpErrorToTypedError`, `useIdempotencyKey`, `redactIdempotencyKey`, `errorInstance` wiring through `TaskExecutor`, and `getIdempotencyReplayTtlSeconds` (fail-closed on v3, undefined on v2, pass-through on declared)
  • All 44 tests in `test/lib/idempotency{,-client,-capabilities}.test.js` pass
  • Full `npm test` matches baseline (governance-e2e pre-existing fails; no new regressions)
  • `npx eslint` clean for the touched files (no new warnings introduced)

🤖 Generated with Claude Code

bokelley and others added 4 commits April 18, 2026 23:55
Finishes the remaining #568 work left open after #590: typed errors are
now surfaced on TaskResult, `replay_ttl_seconds` is exposed on the client
with fail-closed semantics, `useIdempotencyKey()` validates BYOK input
up front, and idempotency keys are redacted from debug logs by default.

- TaskResult.errorInstance: IdempotencyConflictError / IdempotencyExpiredError
  attached automatically when the seller returns those codes. The SDK pulls
  the sent idempotency_key from task state — the server intentionally omits
  it from error bodies (it's a read-oracle).
- adcpErrorToTypedError helper lets callers that already hold an AdcpErrorInfo
  type-narrow it themselves.
- getIdempotencyReplayTtlSeconds() on SingleAgentClient / AgentClient reads
  adcp.idempotency.replay_ttl_seconds from cached capabilities. Throws on
  v3 sellers that omit the (required) declaration rather than defaulting
  to 24h, which would mislead retry-sensitive flows.
- useIdempotencyKey(key) validates against IDEMPOTENCY_KEY_PATTERN up front.
- redactIdempotencyKey / redactIdempotencyKeyInArgs truncate keys to 8 chars
  in MCP and A2A debug logs. ADCP_LOG_IDEMPOTENCY_KEYS=1 opts into full
  logging for local debugging.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Format-only change to satisfy the Code Quality CI job.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Security (security-reviewer):
- Redact idempotency_key in Activity.payload.params — this is the
  observability hook buyers typically pipe into Datadog/Sentry, so the
  key was leaving the process in the clear.
- Tighten useIdempotencyKey() invalid-key preview from 32 chars to 8
  to match the redactIdempotencyKey() policy — near-valid 16-char keys
  were flowing verbatim into exception messages.
- Make idempotencyKey non-enumerable on IdempotencyConflictError /
  IdempotencyExpiredError so console.log(err) and JSON.stringify
  don't leak the key (parameter properties are enumerable by default
  in TS; explicit Object.defineProperty fixes it).

DX (dx-expert):
- Teach docs/llms.txt the errorInstance instanceof pattern, the
  getIdempotencyReplayTtlSeconds() fail-closed behaviour, and the
  useIdempotencyKey() BYOK helper — previously only mentioned in the
  changeset.
- Add an Idempotency section to README with the four-line happy path.

Code quality (code-reviewer):
- Add tests for redactIdempotencyKeyInArgs covering the referential-
  equality contract the A2A rebuild-only-when-changed logic depends on,
  plus JSON-serialization leak tests for the non-enumerable key fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
docs/llms.txt is generated by scripts/generate-agent-docs.ts — editing
the output directly lost on CI regenerate. Move the errorInstance
instanceof, useIdempotencyKey, and getIdempotencyReplayTtlSeconds
sections into the generator so they survive regeneration.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit 7a31d75 into main Apr 19, 2026
12 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