@agent-custody/receipts 0.6.19 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,11 +7,11 @@ Two producers, one receipt format, one verifier.
7
7
  - **The gateway** is an MCP proxy between an agent and the systems it can affect: MCP servers, REST APIs described as tools, and other agents it delegates to over A2A. For every tool call, allowed or denied, it checks a delegation grant signed by the human principal, gathers the facts the policy needs by calling upstream itself, evaluates a Cedar policy that fails closed, forwards the call only on allow, and emits a signed receipt appended to a Merkle transparency log.
8
8
  - **The SDK** is an interceptor inside the agent's own process, hooked into the framework's tool-call callbacks: Claude Code, the Claude Agent SDK, the OpenAI Agents SDK, the Vercel AI SDK, OpenClaw, DeepSeek Harness, Hermes, Google ADK, LangChain, or any function you wrap. It reaches everything the gateway cannot see and issues the same receipts, labelled as self-reported.
9
9
 
10
- Anyone holding the public keys can verify a receipt offline. The agent is not trusted. The layer around it is, and the receipt says exactly how far that trust extends, starting with who issued it.
10
+ Anyone holding the public keys can verify a receipt offline. The agent is not trusted. The layer around it is, and the receipt says exactly how far that trust extends, starting with who issued it. What the receipt file carries of a call is a choice per tool: a field can be withheld as a commitment or encrypted under your key, and the verifier recomputes the digests when it is shown the values.
11
11
 
12
12
  - [Reference](https://docs.agent-custody.dev/reference/): every function, endpoint, MCP tool, and command with its request and response
13
- - [Tutorials](docs/tutorials.md): twenty-five runnable examples, one per aspect of the code, all executed by the test suite
14
- - [Usage guide](docs/usage.md): gateway setup, wiring into Claude Desktop, Claude Code, or your own agent loop, and the gateway as an A2A agent between agents that delegate to each other
13
+ - [Tutorials](docs/tutorials.md): twenty-seven runnable examples, one per aspect of the code, all executed by the test suite
14
+ - [Usage guide](docs/usage.md): gateway setup, wiring into Claude Desktop, Claude Code, or your own agent loop, and the gateway as an A2A agent between agents that delegate to each other, and private evidence: what the receipt file carries of each call
15
15
  - [The interceptor SDK](docs/sdk.md): Claude Code hooks, the Claude Agent SDK, adapters for the OpenAI Agents SDK, Vercel AI SDK, LangChain, OpenClaw and DeepSeek Harness, a Hermes Agent plugin and Google ADK callbacks through the Python package, and wrapping tool functions in anything else
16
16
  - [Writing policies](docs/policies.md): how a tool call becomes a Cedar request, with tested examples
17
17
  - [Verifying a receipt](docs/verification.md): what each check means and what a verified receipt does and does not prove
@@ -181,7 +181,7 @@ Every field carries a provenance label. This is the design decision that matters
181
181
  bun install # from the repository root, once for the workspace
182
182
  cd packages/receipts
183
183
  node scripts/demo.ts # gateway: keys, grant, policy, four tool calls, verification, a tampering attempt; then the SDK wrapping the same tool
184
- node examples/01-keys-and-signing.ts # first of twenty-five step-by-step examples, see docs/tutorials.md
184
+ node examples/01-keys-and-signing.ts # first of twenty-seven step-by-step examples, see docs/tutorials.md
185
185
  bun run test # this package; `bun run test` at the root runs every package
186
186
  ```
187
187
 
@@ -256,14 +256,15 @@ src/splunk.ts Splunk export: one HTTP Event Collector event per receipt, to
256
256
  src/rest.ts the REST connector: an HTTP API described as tools, standing where an MCP upstream stands
257
257
  src/a2a.ts agents delegating to agents: a remote A2A agent as an upstream, and the gateway serving A2A itself in front of it
258
258
  src/upstream.ts attested execution: an upstream signs its result for the receipt; the verifier checks it with the upstream key
259
+ src/capture.ts private evidence: per-tool capture rules applied by the issuer to the whole statement, withheld values as salted commitments or encrypted under the customer's key, restore and reveal
259
260
  vectors/ conformance vectors: receipts, keys, logs, proofs, and expected verdicts; `bun run vectors` regenerates them
260
261
  src/verify.ts offline verification, the human-readable report, and the audit that a later log extends an earlier one
261
- src/cli.ts keygen, grant, gateway, hook, serve, log, prune, verify, audit
262
+ src/cli.ts keygen, grant, gateway, hook, serve, log, prune, verify, reveal, audit
262
263
  src/retention.ts pruning the log: leaves become their hashes, bundles are removed, proofs survive
263
264
  src/index.ts the package's public surface; adapters are exported on ./sdk/<framework> subpaths
264
265
  tsconfig.build.json emits dist/ (JavaScript plus declarations) for consumers; the repo itself runs the .ts directly
265
266
  scripts/ fake Stripe upstream (signs its results with --key), a second fake upstream, fixture builders for gateway and SDK, demo
266
- examples/ twenty-five runnable tutorials, plus examples/languages/: Python, Go, Java, and Rust clients of the sidecar, run by the test suite, one per aspect; each is run by the test suite
267
+ examples/ twenty-seven runnable tutorials, plus examples/languages/: Python, Go, Java, and Rust clients of the sidecar, run by the test suite, one per aspect; each is run by the test suite
267
268
  test/ unit tests per module, end-to-end gateway test, SDK and hook tests,
268
269
  adapter tests against the real packages, and a test that runs every policy in docs/policies.md
269
270
  docs/ tutorials, usage (gateway), sdk, policies, verification
@@ -309,7 +310,9 @@ The design is two producers feeding one verifier. The SDK is the top of the funn
309
310
  - Splunk export: with `splunk` in either config, every receipt is also one event at the HTTP Event Collector, with the receipt id, tool, agent, principal, status, decision, and log position as searchable fields and the token from the environment; the same best-effort rule.
310
311
  - The REST connector: a plain HTTP API described as tools in the gateway config, credentials from the environment, so an agent's direct API calls become receipted, policy-checked tool calls through the gateway.
311
312
  - Pre-commit authorization for consequential tools: named in `precommit`, a call is signed and logged before it is forwarded, withheld if the log will not take it, and its receipt carries the committed authorization with proof that it precedes the execution.
313
+ - The grant on every call (issue #97): expiry and revocation are checked on every call and again before the forward, in every mode, on the MCP, HTTP, and A2A fronts; `agent-custody revoke` revokes a grant and every grant delegated from it. The remote log's answer is verified against pinned keys before a pre-committed call goes upstream (issue #99). The portal verifies an address only from the account's own session before a provider sign-in can link to it (issue #103). The verifier recomputes the execution result digest (issue #104). The Stripe webhook ignores a repeated or out-of-order event (issue #105).
312
314
  - Agent-to-agent delegation under custody: the A2A upstream and the gateway's A2A front; receipts per delegation with the delegate's card as a fact. A Google ADK `RemoteA2aAgent`, or any A2A client, is pointed at the gateway and a refused delegation comes back as a rejected task the delegate never saw. The delegate does not yet countersign its answer; the receipt attests what the gateway sent and observed, not what the delegate did with it.
315
+ - Private evidence: per-tool capture rules, applied by the issuer to the whole statement so nothing outside a fixed structural list is in clear under a private rule; withheld values as salted commitments whose salts never enter the receipt, encrypted under the customer's key, revealed and recomputed by the verifier; nothing in the receipt confirms a guess of a withheld value.
313
316
 
314
317
  **Next, in the order it pays off**
315
318
 
package/dist/a2a.js CHANGED
@@ -136,20 +136,20 @@ export function a2aUpstream(name, cfg, opts = {}) {
136
136
  const tools = [
137
137
  {
138
138
  name: sendTool,
139
- description: `Delegate a task to the agent "${c.name}"${c.description ? `: ${c.description}` : ""}${skills ? `. Skills: ${skills}` : ""}`,
139
+ description: `Delegate a task to the agent "${c.name}"${c.description ? `: ${c.description}` : ""}${skills ? `. Skills: ${skills}` : ""}. Send text for a plain task, or message for a full A2A message; one of the two is needed. Returns the agent's task or message as JSON text. A JSON-RPC error from the agent comes back as an error result.`,
140
140
  inputSchema: {
141
141
  type: "object",
142
142
  properties: {
143
143
  text: { type: "string", description: "the task, as text" },
144
144
  message: { type: "object", description: "a full A2A message, forwarded as is; text is derived from it" },
145
- taskId: { type: "string" },
146
- contextId: { type: "string" },
147
- configuration: { type: "object" },
148
- metadata: { type: "object" },
145
+ taskId: { type: "string", description: "the id of an earlier task to continue; used with text only, because a full message carries its own" },
146
+ contextId: { type: "string", description: "the id of the conversation context to continue; used with text only" },
147
+ configuration: { type: "object", description: "the A2A send configuration, forwarded as is" },
148
+ metadata: { type: "object", description: "A2A metadata for the request, forwarded as is" },
149
149
  },
150
150
  },
151
151
  },
152
- { name: cardTool, description: `The agent card of "${c.name}", fetched by the gateway`, inputSchema: { type: "object", properties: {} } },
152
+ { name: cardTool, description: `The agent card of "${c.name}", fetched afresh from the agent by the gateway: its name, description, skills, and the interfaces it serves. Call it to see what the agent can do before you delegate with ${sendTool}. A gateway config can also name it as a fact, so that a policy can read the card.`, inputSchema: { type: "object", properties: {} } },
153
153
  ];
154
154
  return { tools };
155
155
  },
@@ -316,8 +316,20 @@ export async function serveA2a(host, cfg, opts) {
316
316
  const method = restSend ?? (typeof parsed.method === "string" ? parsed.method : "");
317
317
  const id = restSend ? null : parsed.id ?? null;
318
318
  const params = (restSend ? parsed : parsed.params) ?? {};
319
- if (!SEND_METHODS.has(method) && !STREAM_METHODS.has(method))
319
+ if (!SEND_METHODS.has(method) && !STREAM_METHODS.has(method)) {
320
+ // reads and cancels carry no receipt, but they still need a grant in force (issue #97)
321
+ let problem;
322
+ try {
323
+ const p = session(req).grantProblem();
324
+ problem = p && `the grant ${p}`;
325
+ }
326
+ catch (e) {
327
+ problem = e instanceof Error ? e.message : String(e);
328
+ }
329
+ if (problem)
330
+ return sendBody(res, 403, "application/json", JSON.stringify(restSend ? { error: problem } : { jsonrpc: "2.0", id, error: { code: -32000, message: problem } }));
320
331
  return forward(req, res, body);
332
+ }
321
333
  const legacy = LEGACY_METHODS.has(method);
322
334
  const stream = STREAM_METHODS.has(method);
323
335
  const message = (params.message ?? {});
@@ -0,0 +1,153 @@
1
+ import type { AuthorizationPredicate } from "./receipt.ts";
2
+ export type CaptureMode = "clear" | "digest" | "encrypted";
3
+ /** One side of a call: the arguments, the result, or the facts. */
4
+ export interface CaptureSide {
5
+ /** clear: the side in the receipt as it was; digest: withheld fields leave a commitment; encrypted: withheld fields leave a commitment and the value under the customer's key */
6
+ mode: CaptureMode;
7
+ /** top-level fields kept in clear in digest or encrypted mode, e.g. the amount and the customer id of a refund */
8
+ clear?: string[];
9
+ }
10
+ /**
11
+ * A tool's rule. Once any side is private, the rule is private: a side it does not set is withheld whole, field by
12
+ * field, and so is every other value in the receipt that is not structural (policy errors, an error message, a denial
13
+ * reason, the result's metadata, a provider's delivery body). A side set to mode clear stays in clear.
14
+ */
15
+ export interface CaptureRule {
16
+ args?: CaptureSide;
17
+ result?: CaptureSide;
18
+ facts?: CaptureSide;
19
+ }
20
+ /** by tool name, or "*" for every tool without a rule of its own */
21
+ export type CaptureConfig = Record<string, CaptureRule>;
22
+ /** A path into the predicate, one segment per key: `["request", "args", "reason"]`. Segments are keys, so a field named "." or "*" is just a field. */
23
+ export type Path = string[];
24
+ /**
25
+ * On the receipt, inside the signed statement: how the call was captured and which paths are withheld. The verifier
26
+ * trusts the note, not the shape of the data: a path the note names must hold a commitment, and commitment-shaped data
27
+ * at a path the note does not name is ordinary data, in the digest like anything else.
28
+ */
29
+ export interface CaptureNote {
30
+ mode: "digest" | "encrypted";
31
+ withheld: Path[];
32
+ }
33
+ export interface Withheld {
34
+ $custody: "withheld";
35
+ /** sha256 over salt || canonical JSON of the value; the salt is not in the receipt */
36
+ sha256: string;
37
+ }
38
+ export interface Encrypted {
39
+ $custody: "encrypted";
40
+ alg: "A256GCM";
41
+ /** sha256 of the key, so a reviewer knows which key reveals it */
42
+ keyid: string;
43
+ iv: string;
44
+ /** AES-256-GCM over the canonical JSON of `{ salt, value }` */
45
+ ciphertext: string;
46
+ tag: string;
47
+ sha256: string;
48
+ }
49
+ export type Commitment = Withheld | Encrypted;
50
+ /** What a disclosure carries at a withheld path: the value and the salt the commitment was made with. Without `value` it is unfinished (`reveal --include-salts` writes it so for the holder to fill in) and fails verification. */
51
+ export interface Disclosed {
52
+ $custody: "disclosed";
53
+ salt: string;
54
+ value?: unknown;
55
+ }
56
+ /** A 32-byte key from hex (64 chars) or base64 (44 chars), as an environment variable holds it. */
57
+ export declare function captureKeyFrom(text: string): Buffer;
58
+ export declare function captureKeyId(key: Buffer): string;
59
+ /** A well-formed commitment: the sha256 a verifier checks, and for an encrypted one the fields the key needs. A `$custody` tag alone is not one. */
60
+ export declare function isCommitment(v: unknown): v is Commitment;
61
+ /** What an encrypted commitment is bound to: the receipt and the path, so a ciphertext moved to another receipt or path does not decrypt. */
62
+ export declare function aadFor(receiptId: unknown, path: Path): string;
63
+ /** A disclosed value as a disclosure carries it: the salt, and the value when the holder has filled it in. */
64
+ export declare function isDisclosed(v: unknown): v is Disclosed;
65
+ /** Replaces a value with its commitment, carrying the value and the salt encrypted when a key is given (bound to `aad`, see `aadFor`); the salt is returned to the caller, never written into the receipt. */
66
+ export declare function withhold(value: unknown, key?: Buffer, aad?: string): {
67
+ commitment: Commitment;
68
+ salt: string;
69
+ };
70
+ /** True when `value` under `salt` is what the commitment was made over. */
71
+ export declare function checkCommitment(c: Commitment, value: unknown, salt: string): boolean;
72
+ /** The value and the salt behind an encrypted commitment, under the key that made it; checked against the commitment too. */
73
+ export declare function reveal(c: Commitment, key: Buffer, aad?: string): {
74
+ value: unknown;
75
+ salt: string;
76
+ };
77
+ /**
78
+ * The salt of a digest over a part that holds withheld values: derived from the salts of those values, in note order,
79
+ * so whoever can restore the values can recompute the digest and nobody else can confirm a guess against it. No salt
80
+ * when nothing under the part is withheld: the values are public, and so may the digest be.
81
+ */
82
+ export declare function digestSalt(salts: string[]): string | undefined;
83
+ /** The tool's rule, else the "*" rule: by own property, so a tool named `constructor` or `toString` does not find an inherited member and fall through to clear. */
84
+ export declare function ruleFor(capture: CaptureConfig | undefined, tool: string): CaptureRule | undefined;
85
+ /**
86
+ * The rule for a call: the tool's own, else the wildcard. For a name no upstream offers, which is text the agent
87
+ * chose, both together, side by side: the more private mode, and only the fields both keep in clear, so that a rule
88
+ * left for a retired name can never be selected by an agent to widen what is in clear. A mode does not order which
89
+ * fields are public, so the clear lists are intersected, not taken from the more private rule. The gateway and the
90
+ * issuer both call this, so the fact arguments kept in clear and the view agree.
91
+ */
92
+ export declare function effectiveRule(capture: CaptureConfig | undefined, tool: string, unknownTool?: boolean): CaptureRule | undefined;
93
+ /** The one mode a rule withholds in: encrypted when any side is, else digest; clear when no side is private. */
94
+ export declare function ruleMode(rule: CaptureRule | undefined): CaptureMode;
95
+ export declare const pathKey: (p: Path) => string;
96
+ export declare const pathText: (p: Path) => string;
97
+ export declare const under: (p: Path, prefix: Path) => boolean;
98
+ /** The value at a path, by own properties only; undefined when the path leaves the data. */
99
+ export declare function getAt(root: unknown, path: Path): unknown;
100
+ /** A copy of `root` with `value` at `path`, every object on the way copied (null-prototype), the rest shared. Creates objects for missing segments. */
101
+ export declare function setAt<T>(root: T, path: Path, value: unknown): T;
102
+ /** the slot in a captured side's view that holds the commitment over every field not kept in clear */
103
+ export declare const REST = "$rest";
104
+ /** What the producer knows about this call that the view needs: which fact arguments copy a call argument that is in clear, and the full lookup results behind the fact digests. */
105
+ export interface CaptureContext {
106
+ /** per fact name: the lookup arguments that may stay in clear under a private args rule (constants, or copies of call arguments the rule keeps in clear) */
107
+ clearFactArgs?: Record<string, string[]>;
108
+ /** per fact name: the lookup's full result, which the receipt's `resultDigest` then commits to; without it the digest the producer computed is withheld */
109
+ factResults?: Record<string, unknown>;
110
+ /** the tool name is not one any upstream offers, so it is text the agent chose: under a private rule the view shows "(unknown tool)" in its place, the rule having been selected by the real name */
111
+ unknownTool?: boolean;
112
+ }
113
+ /** The view the issuer signs, and the salts behind its commitments by `pathKey`, which the issuer keeps and never signs. */
114
+ export interface PrivateView<P> {
115
+ predicate: P;
116
+ salts: Record<string, string>;
117
+ }
118
+ /**
119
+ * The statement a receipt carries under a tool's rule: the predicate with every non-structural value withheld as the
120
+ * rule says, the three digests salted from the field salts, and the note of what is withheld at `capture`. Under a
121
+ * clear rule, the predicate itself and no salts. Applied by the issuer to every statement it signs, the authorization
122
+ * included; given the authorization's view as `prior`, the receipt's view carries the same request, facts, and policy,
123
+ * so that the receipt still names the authorization and nothing is withheld twice.
124
+ */
125
+ export declare function privateView<P extends AuthorizationPredicate>(predicate: P, rule: CaptureRule | undefined, key?: Buffer, ctx?: CaptureContext, prior?: PrivateView<AuthorizationPredicate>): PrivateView<P>;
126
+ export type How = "disclosed" | "decrypted";
127
+ export interface Restoration<P> {
128
+ /** the predicate with every restorable path put back */
129
+ predicate: P;
130
+ /** how each restored path came back, by `pathKey` */
131
+ restored: Map<string, How>;
132
+ /** the salt of each restored path, by `pathKey`: what the digests over them are salted from */
133
+ salts: Map<string, string>;
134
+ /** the value of each restored path as it was committed, by `pathKey`; a `$rest` is folded into its parent in `predicate`, so this is where it stays whole */
135
+ values: Map<string, unknown>;
136
+ /** the withheld paths whose value could not be had */
137
+ missing: Path[];
138
+ /** paths where the note, the data, a disclosure, or the key contradict each other; each a reason a check must fail */
139
+ problems: {
140
+ path: Path;
141
+ error: string;
142
+ }[];
143
+ }
144
+ /**
145
+ * The full statement behind a view, under its note: a path the note names must hold a commitment; the disclosure, when
146
+ * it carries that path as `{ $custody: "disclosed", salt, value }`, must match the commitment; else the key reveals
147
+ * what was encrypted under it; else the path stays withheld. A disclosure is a partial predicate with those at the
148
+ * withheld paths, as `agent-custody reveal` writes it. Nothing here throws: the problems are reported, so a verifier
149
+ * fails the right check.
150
+ */
151
+ export declare function restorePaths<P>(predicate: P, note: CaptureNote | undefined, disclosed?: unknown, key?: Buffer): Restoration<P>;
152
+ /** The salt a digest over `prefix` was made with, once every withheld path under it is restored: see `digestSalt`. */
153
+ export declare function digestSaltFor<P>(r: Restoration<P>, note: CaptureNote | undefined, prefix: Path): string | undefined;