@agent-custody/receipts 0.6.15 → 0.6.17
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 +4 -3
- package/dist/config.d.ts +8 -0
- package/dist/config.js +9 -0
- package/dist/gateway.js +12 -3
- package/dist/policy.d.ts +2 -0
- package/dist/sdk/claude.js +2 -2
- package/dist/sdk/deepseek-harness.d.ts +69 -0
- package/dist/sdk/deepseek-harness.js +74 -0
- package/dist/sdk/index.d.ts +4 -0
- package/dist/sdk/index.js +11 -2
- package/dist/sdk/openai-agents.js +2 -2
- package/dist/sdk/openclaw.js +2 -2
- package/dist/sdk/vercel-ai.js +2 -2
- package/dist/sidecar.js +1 -1
- package/dist/verify.js +4 -3
- package/docs/sdk.md +19 -0
- package/docs/tutorials.md +1 -0
- package/docs/usage.md +2 -0
- package/docs/verification.md +1 -1
- package/package.json +5 -1
- package/vectors/audit.json +27 -27
- package/vectors/canonical.json +5 -5
- package/vectors/receipts.json +342 -241
package/README.md
CHANGED
|
@@ -5,14 +5,14 @@ Chain of custody for AI agents: a signed receipt for every tool call, checkable
|
|
|
5
5
|
Two producers, one receipt format, one verifier.
|
|
6
6
|
|
|
7
7
|
- **The gateway** is an MCP proxy between an agent and the systems it can affect. 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
|
-
- **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, Hermes, LangChain, or any function you wrap. It reaches everything the gateway cannot see and issues the same receipts, labelled as self-reported.
|
|
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, 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
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.
|
|
11
11
|
|
|
12
12
|
- [Reference](https://docs.agent-custody.dev/reference/): every function, endpoint, MCP tool, and command with its request and response
|
|
13
13
|
- [Tutorials](docs/tutorials.md): twenty runnable examples, one per aspect of the code, all executed by the test suite
|
|
14
14
|
- [Usage guide](docs/usage.md): gateway setup, wiring into Claude Desktop, Claude Code, or your own agent loop
|
|
15
|
-
- [The interceptor SDK](docs/sdk.md): Claude Code hooks, the Claude Agent SDK, adapters for the OpenAI Agents SDK, Vercel AI SDK and
|
|
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 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
|
|
18
18
|
- [What the evidence satisfies](docs/compliance.md): the receipts, packs, and certificates mapped to SOC 2, ISO 27001, the EU AI Act, and UK GDPR, with what none of them claims
|
|
@@ -47,7 +47,7 @@ const bundle = JSON.parse(readFileSync("receipts/<id>.json", "utf8"));
|
|
|
47
47
|
verifyBundle(bundle, { issuerKeys: [loadPublicKey("keys/app.pub")], logFile: "log.jsonl" }).ok; // true
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
Framework hooks and adapters, including Claude Code, the OpenAI Agents SDK, the Vercel AI SDK and
|
|
50
|
+
Framework hooks and adapters, including Claude Code, the OpenAI Agents SDK, the Vercel AI SDK, LangChain, OpenClaw, DeepSeek Harness and Hermes, are in [docs/sdk.md](docs/sdk.md). To enforce rather than record, put the gateway between the agent and its tools: `npx agent-custody gateway --config gateway.json`, set up in [docs/usage.md](docs/usage.md). The gateway is listed in the [MCP Registry](https://registry.modelcontextprotocol.io/) as `io.github.svayatta/agent-custody`; its entry is [server.json](server.json), published after each npm release by running the `mcp-registry` workflow from the Actions tab (GitHub OIDC, no token).
|
|
51
51
|
|
|
52
52
|
## How it fits together
|
|
53
53
|
|
|
@@ -108,6 +108,7 @@ Every receipt names its issuer, and the verifier prints what that issuer kind is
|
|
|
108
108
|
| Vercel AI SDK | SDK | `wrapTools` | | a real `generateText` loop over the SDK's mock model |
|
|
109
109
|
| LangChain / LangGraph (JS) | SDK | `tool(issuer.wrap(fn))` | `ReceiptCallbackHandler` | real `StructuredTool` invocations |
|
|
110
110
|
| OpenClaw | SDK | `registerOpenClaw` / `openclawHooks`: `before_tool_call` deny | `after_tool_call` | OpenClaw's documented hook contract, in the plugin's process |
|
|
111
|
+
| DeepSeek Harness | SDK | the module is the plugin: `tools/pre-execute` deny | `tools/post-execute` | the harness's tool waterfall types, driven as the harness drives them |
|
|
111
112
|
| Hermes Agent | sidecar + [Python package](../python/README.md) | `agent_custody.hermes`: `pre_tool_call` block | `post_tool_call` | the documented hook contract, against a live sidecar |
|
|
112
113
|
| anything else | SDK | `issuer.wrap(name, fn)` | `issuer.record` | plain functions |
|
|
113
114
|
| Python: LangChain, OpenAI Agents SDK, CrewAI, Claude Agent SDK | sidecar + [Python package](../python/README.md) | `wrap_tools` (OpenAI Agents, CrewAI), `claude_hook` PreToolUse deny, `client.wrap` | `ReceiptCallbackHandler` | the real Python packages, receipts checked by this verifier |
|
package/dist/config.d.ts
CHANGED
|
@@ -169,6 +169,10 @@ export declare const GatewayConfigSchema: z.ZodObject<{
|
|
|
169
169
|
optional: z.ZodDefault<z.ZodBoolean>;
|
|
170
170
|
}, z.core.$strip>>>;
|
|
171
171
|
precommit: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
172
|
+
mode: z.ZodOptional<z.ZodEnum<{
|
|
173
|
+
enforce: "enforce";
|
|
174
|
+
observe: "observe";
|
|
175
|
+
}>>;
|
|
172
176
|
receiptsDir: z.ZodString;
|
|
173
177
|
logFile: z.ZodOptional<z.ZodString>;
|
|
174
178
|
log: z.ZodOptional<z.ZodObject<{
|
|
@@ -202,6 +206,10 @@ export declare const SdkConfigSchema: z.ZodObject<{
|
|
|
202
206
|
keyFile: z.ZodString;
|
|
203
207
|
}, z.core.$strip>;
|
|
204
208
|
policyFile: z.ZodOptional<z.ZodString>;
|
|
209
|
+
mode: z.ZodOptional<z.ZodEnum<{
|
|
210
|
+
enforce: "enforce";
|
|
211
|
+
observe: "observe";
|
|
212
|
+
}>>;
|
|
205
213
|
receiptsDir: z.ZodString;
|
|
206
214
|
logFile: z.ZodOptional<z.ZodString>;
|
|
207
215
|
log: z.ZodOptional<z.ZodObject<{
|
package/dist/config.js
CHANGED
|
@@ -63,6 +63,13 @@ export const GatewayConfigSchema = z.object({
|
|
|
63
63
|
* authorization to the log and refuses the call if the log will not take it. Evidence then precedes the side effect.
|
|
64
64
|
*/
|
|
65
65
|
precommit: z.array(z.string().min(1)).default([]),
|
|
66
|
+
/**
|
|
67
|
+
* "enforce" (the default when absent) denies what the policy denies. "observe" forwards every call the grant allows and records the
|
|
68
|
+
* decision the policy would have made, with `enforced: false` on the receipt: the way to run a new policy against
|
|
69
|
+
* real traffic before turning it on. A log that refuses a consequential call's authorization does not withhold the
|
|
70
|
+
* call in observe mode; it is printed.
|
|
71
|
+
*/
|
|
72
|
+
mode: z.enum(["enforce", "observe"]).optional(),
|
|
66
73
|
receiptsDir: z.string(),
|
|
67
74
|
logFile: z.string().optional(),
|
|
68
75
|
log: LogSchema.optional(),
|
|
@@ -91,6 +98,8 @@ export const SdkConfigSchema = z.object({
|
|
|
91
98
|
identity: z.object({ keyFile: z.string() }),
|
|
92
99
|
/** optional Cedar policy; when present, wrapped tools and PreToolUse hooks can deny */
|
|
93
100
|
policyFile: z.string().optional(),
|
|
101
|
+
/** "observe" records the policy's decision with `enforced: false` and never blocks; "enforce" (default) blocks a deny */
|
|
102
|
+
mode: z.enum(["enforce", "observe"]).optional(),
|
|
94
103
|
receiptsDir: z.string(),
|
|
95
104
|
logFile: z.string().optional(),
|
|
96
105
|
log: LogSchema.optional(),
|
package/dist/gateway.js
CHANGED
|
@@ -189,6 +189,11 @@ export async function createGatewayHost(cfg, options = {}) {
|
|
|
189
189
|
policy = { decision: "deny", reasons: [], errors: [String(e instanceof Error ? e.message : e)], policyDigest: pDigest };
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
|
+
// Observe mode: the decision is recorded, never enforced. The receipt says so on every call, so a verifier can
|
|
193
|
+
// tell a policy that was being tested from one that was in force.
|
|
194
|
+
if (cfg.mode === "observe")
|
|
195
|
+
policy = { ...policy, enforced: false };
|
|
196
|
+
const forward = policy.decision === "allow" || policy.enforced === false;
|
|
192
197
|
const head = {
|
|
193
198
|
receiptId,
|
|
194
199
|
timestamp,
|
|
@@ -203,18 +208,22 @@ export async function createGatewayHost(cfg, options = {}) {
|
|
|
203
208
|
};
|
|
204
209
|
if (policy.decision === "allow" && consequential(tool)) {
|
|
205
210
|
// A consequential call is committed to the log before it goes out, so that evidence of the side effect exists
|
|
206
|
-
// before the side effect does. If the log will not take the authorization, the call is not forwarded
|
|
211
|
+
// before the side effect does. If the log will not take the authorization, the call is not forwarded; in
|
|
212
|
+
// observe mode nothing is withheld, and the missing authorization is printed instead.
|
|
207
213
|
try {
|
|
208
214
|
authorization = await issuer.authorize({ ...head, policy: { ...policy, provenance: "observed" } });
|
|
209
215
|
}
|
|
210
216
|
catch (e) {
|
|
211
|
-
|
|
217
|
+
if (cfg.mode === "observe")
|
|
218
|
+
console.error(`agent-custody gateway (observe mode): the log did not commit the authorization for ${tool}, receipt ${receiptId}; the call was forwarded without it: ${String(e instanceof Error ? e.message : e)}`);
|
|
219
|
+
else
|
|
220
|
+
execution = { status: "withheld", reason: `the log did not commit the authorization, so the call was not forwarded: ${String(e instanceof Error ? e.message : e)}`, provenance: "observed" };
|
|
212
221
|
}
|
|
213
222
|
}
|
|
214
223
|
if (execution) {
|
|
215
224
|
// withheld: nothing was forwarded
|
|
216
225
|
}
|
|
217
|
-
else if (
|
|
226
|
+
else if (forward) {
|
|
218
227
|
try {
|
|
219
228
|
// The upstream learns which receipt this call is, and who the grant says is calling. An upstream that keeps
|
|
220
229
|
// state, such as the memory server, cites the receipt as the source of what it stores.
|
package/dist/policy.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export interface PolicyDecision {
|
|
|
5
5
|
/** evaluation errors; non-empty always yields deny */
|
|
6
6
|
errors: string[];
|
|
7
7
|
policyDigest: string;
|
|
8
|
+
/** false when the issuer ran in observe mode: the decision was recorded but a deny was not enforced. Absent means enforced. */
|
|
9
|
+
enforced?: boolean;
|
|
8
10
|
}
|
|
9
11
|
export interface PolicyRequest {
|
|
10
12
|
agentId: string;
|
package/dist/sdk/claude.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Adapter for Claude Code hooks (command hooks over stdin/stdout) and the Claude Agent SDK (in-process hooks).
|
|
2
2
|
// Both use the same input and output JSON, so one handler serves both.
|
|
3
|
-
import { createSdkIssuer, receiptIdOf } from "./index.js";
|
|
3
|
+
import { createSdkIssuer, receiptIdOf, denies } from "./index.js";
|
|
4
4
|
function toEvent(input) {
|
|
5
5
|
const raw = input.tool_input;
|
|
6
6
|
const args = raw && typeof raw === "object" && !Array.isArray(raw) ? raw : { input: raw ?? null };
|
|
@@ -16,7 +16,7 @@ export async function handleHookEvent(issuer, input) {
|
|
|
16
16
|
switch (input.hook_event_name) {
|
|
17
17
|
case "PreToolUse": {
|
|
18
18
|
const policy = issuer.decide(ev);
|
|
19
|
-
if (policy
|
|
19
|
+
if (denies(policy)) {
|
|
20
20
|
const reason = [...policy.reasons, ...policy.errors].join("; ") || "no permit policy matched";
|
|
21
21
|
const bundle = await issuer.record(ev, { status: "denied", reason }, policy);
|
|
22
22
|
return {
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type SdkIssuer } from "./index.ts";
|
|
3
|
+
/** The fields of the harness's ToolExecution this adapter reads. */
|
|
4
|
+
export interface DshToolExecution {
|
|
5
|
+
name: string;
|
|
6
|
+
arguments?: unknown;
|
|
7
|
+
callId?: string;
|
|
8
|
+
agent?: {
|
|
9
|
+
session?: {
|
|
10
|
+
header?: {
|
|
11
|
+
id?: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
} | undefined;
|
|
15
|
+
[k: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
/** ToolExecutionResult: success carries `content` blocks and a `value`; failure carries `error`. */
|
|
18
|
+
export interface DshToolExecutionResult {
|
|
19
|
+
isError: boolean;
|
|
20
|
+
content?: unknown[];
|
|
21
|
+
value?: unknown;
|
|
22
|
+
error?: unknown;
|
|
23
|
+
[k: string]: unknown;
|
|
24
|
+
}
|
|
25
|
+
export type DshPreToolDecision = {
|
|
26
|
+
kind: "allow";
|
|
27
|
+
} | {
|
|
28
|
+
kind: "deny";
|
|
29
|
+
reason: string;
|
|
30
|
+
} | {
|
|
31
|
+
kind: "cancel";
|
|
32
|
+
} | {
|
|
33
|
+
kind: "ask";
|
|
34
|
+
reason?: string;
|
|
35
|
+
};
|
|
36
|
+
export type DshPostToolDecision = {
|
|
37
|
+
kind: "accept";
|
|
38
|
+
[k: string]: unknown;
|
|
39
|
+
} | {
|
|
40
|
+
kind: "block";
|
|
41
|
+
feedback: unknown[];
|
|
42
|
+
[k: string]: unknown;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* The two handlers. `preExecute` evaluates the policy: on an enforced deny it issues the denial receipt and returns
|
|
46
|
+
* `{ kind: "deny" }` with the reason and the receipt id, which the model sees; otherwise it delegates with `next()`,
|
|
47
|
+
* so the harness's own approvals and other plugins still apply. It never returns `allow` itself. If the denial
|
|
48
|
+
* receipt cannot be issued the call is denied all the same. `postExecute` issues the receipt for the executed or
|
|
49
|
+
* failed call and delegates; a refused leaf is thrown, which the harness reports.
|
|
50
|
+
*/
|
|
51
|
+
export declare function deepseekHarnessHooks(issuer: SdkIssuer): {
|
|
52
|
+
preExecute: (exec: DshToolExecution, next: () => Promise<DshPreToolDecision>) => Promise<DshPreToolDecision>;
|
|
53
|
+
postExecute: (exec: DshToolExecution, result: DshToolExecutionResult, next: () => Promise<DshPostToolDecision>) => Promise<DshPostToolDecision>;
|
|
54
|
+
};
|
|
55
|
+
/** Registers both handlers on a Cordis context (`ctx.on(event, handler)`), for a plugin's `apply(ctx)`. */
|
|
56
|
+
export declare function registerDeepSeekHarness(ctx: {
|
|
57
|
+
on: (event: string, handler: (...args: any[]) => unknown) => unknown;
|
|
58
|
+
}, issuer: SdkIssuer): void;
|
|
59
|
+
export declare const name = "agent-custody";
|
|
60
|
+
/** the tool runtime must be up before the two events exist */
|
|
61
|
+
export declare const inject: string[];
|
|
62
|
+
/** Cordis validates the plugin's settings with this (zod implements Standard Schema): the SDK config file, else AGENT_CUSTODY_CONFIG. */
|
|
63
|
+
export declare const Config: z.ZodObject<{
|
|
64
|
+
config: z.ZodOptional<z.ZodString>;
|
|
65
|
+
}, z.core.$strip>;
|
|
66
|
+
export type Config = z.infer<typeof Config>;
|
|
67
|
+
export declare function apply(ctx: {
|
|
68
|
+
on: (event: string, handler: (...args: any[]) => unknown) => unknown;
|
|
69
|
+
}, config?: Config): void;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Adapter for DeepSeek Harness (dsh), where everything is a plugin. The harness runs two awaited waterfalls around
|
|
2
|
+
// every tool call: `tools/pre-execute`, whose handler returns `{ kind: "allow" }`, `{ kind: "deny", reason }` (the
|
|
3
|
+
// reason reaches the model), or delegates with `next()`; and `tools/post-execute`, which sees the result. This module
|
|
4
|
+
// is itself a harness plugin: it exports `name`, `inject`, `Config`, and `apply`, so it is listed by its package path
|
|
5
|
+
// with the SDK config as its only setting.
|
|
6
|
+
//
|
|
7
|
+
// # cordis.yml (an overlay; `dsh web --patch ./cordis.yml`), or the plugin list the dsh CLI manages
|
|
8
|
+
// - insert:
|
|
9
|
+
// - id: agent-custody
|
|
10
|
+
// name: "@agent-custody/receipts/sdk/deepseek-harness"
|
|
11
|
+
// config:
|
|
12
|
+
// config: /abs/path/sdk.json
|
|
13
|
+
//
|
|
14
|
+
// Typed loosely from the harness's `packages/core/tools/src/index.ts` on purpose, so this package does not depend on
|
|
15
|
+
// `@deepseek-ai/dsh-tools`; `deepseekHarnessHooks(issuer)` gives the two handlers for a plugin that registers them
|
|
16
|
+
// itself. The harness also ships `@deepseek-ai/dsh-hooks-claude-code`, which runs a Claude Code hooks.json: the
|
|
17
|
+
// `agent-custody hook` command works through it unchanged, one process per call. This plugin is the in-process path.
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
import { loadSdkConfig } from "../config.js";
|
|
20
|
+
import { createSdkIssuer, denies, receiptIdOf } from "./index.js";
|
|
21
|
+
function toEvent(exec) {
|
|
22
|
+
const raw = exec.arguments;
|
|
23
|
+
const args = raw && typeof raw === "object" && !Array.isArray(raw) ? raw : { input: raw ?? null };
|
|
24
|
+
return { tool: exec.name, args, session: { id: exec.agent?.session?.header?.id ?? null, toolUseId: exec.callId ?? null } };
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The two handlers. `preExecute` evaluates the policy: on an enforced deny it issues the denial receipt and returns
|
|
28
|
+
* `{ kind: "deny" }` with the reason and the receipt id, which the model sees; otherwise it delegates with `next()`,
|
|
29
|
+
* so the harness's own approvals and other plugins still apply. It never returns `allow` itself. If the denial
|
|
30
|
+
* receipt cannot be issued the call is denied all the same. `postExecute` issues the receipt for the executed or
|
|
31
|
+
* failed call and delegates; a refused leaf is thrown, which the harness reports.
|
|
32
|
+
*/
|
|
33
|
+
export function deepseekHarnessHooks(issuer) {
|
|
34
|
+
return {
|
|
35
|
+
preExecute: async (exec, next) => {
|
|
36
|
+
const ev = toEvent(exec);
|
|
37
|
+
const policy = issuer.decide(ev);
|
|
38
|
+
if (!denies(policy))
|
|
39
|
+
return next();
|
|
40
|
+
const reason = [...policy.reasons, ...policy.errors].join("; ") || "no permit policy matched";
|
|
41
|
+
try {
|
|
42
|
+
const bundle = await issuer.record(ev, { status: "denied", reason }, policy);
|
|
43
|
+
return { kind: "deny", reason: `agent-custody: ${reason} (receipt ${receiptIdOf(bundle)})` };
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
return { kind: "deny", reason: `agent-custody: ${reason}; the denial receipt could not be issued (${e instanceof Error ? e.message : String(e)})` };
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
postExecute: async (exec, result, next) => {
|
|
50
|
+
const ev = toEvent(exec);
|
|
51
|
+
const outcome = result.isError ? { status: "failed", result: result.error ?? result.content ?? null } : { status: "executed", result: result.content ?? result.value ?? null };
|
|
52
|
+
await issuer.record(ev, outcome, issuer.decide(ev));
|
|
53
|
+
return next();
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** Registers both handlers on a Cordis context (`ctx.on(event, handler)`), for a plugin's `apply(ctx)`. */
|
|
58
|
+
export function registerDeepSeekHarness(ctx, issuer) {
|
|
59
|
+
const hooks = deepseekHarnessHooks(issuer);
|
|
60
|
+
ctx.on("tools/pre-execute", hooks.preExecute);
|
|
61
|
+
ctx.on("tools/post-execute", hooks.postExecute);
|
|
62
|
+
}
|
|
63
|
+
// ---- the module as a plugin ----
|
|
64
|
+
export const name = "agent-custody";
|
|
65
|
+
/** the tool runtime must be up before the two events exist */
|
|
66
|
+
export const inject = ["tools"];
|
|
67
|
+
/** Cordis validates the plugin's settings with this (zod implements Standard Schema): the SDK config file, else AGENT_CUSTODY_CONFIG. */
|
|
68
|
+
export const Config = z.object({ config: z.string().optional() });
|
|
69
|
+
export function apply(ctx, config = {}) {
|
|
70
|
+
const path = config.config ?? process.env.AGENT_CUSTODY_CONFIG;
|
|
71
|
+
if (!path)
|
|
72
|
+
throw new Error("agent-custody: set `config` in the plugin's settings to the SDK config file, or AGENT_CUSTODY_CONFIG in the environment");
|
|
73
|
+
registerDeepSeekHarness(ctx, createSdkIssuer(loadSdkConfig(path)));
|
|
74
|
+
}
|
package/dist/sdk/index.d.ts
CHANGED
|
@@ -34,6 +34,10 @@ export interface SdkIssuer {
|
|
|
34
34
|
/** Wraps a tool function: decide, run, record. Throws PolicyDeniedError on deny, after issuing the denial receipt. */
|
|
35
35
|
wrap<A extends Record<string, unknown>, R>(tool: string, fn: (args: A) => R | Promise<R>, meta?: Omit<ToolEvent, "tool" | "args">): (args: A) => Promise<R>;
|
|
36
36
|
}
|
|
37
|
+
/** True when a decision is a deny that must be enforced: a deny made in observe mode (`enforced: false`) is recorded, not acted on. */
|
|
38
|
+
export declare function denies(policy: PolicyDecision | null | undefined): policy is PolicyDecision & {
|
|
39
|
+
decision: "deny";
|
|
40
|
+
};
|
|
37
41
|
export declare class PolicyDeniedError extends Error {
|
|
38
42
|
readonly tool: string;
|
|
39
43
|
readonly reason: string;
|
package/dist/sdk/index.js
CHANGED
|
@@ -8,6 +8,10 @@ import { openExporter } from "../otel.js";
|
|
|
8
8
|
import { openLog } from "../log-sink.js";
|
|
9
9
|
import { evaluate } from "../policy.js";
|
|
10
10
|
export const SDK_VERSION = "0.1.0";
|
|
11
|
+
/** True when a decision is a deny that must be enforced: a deny made in observe mode (`enforced: false`) is recorded, not acted on. */
|
|
12
|
+
export function denies(policy) {
|
|
13
|
+
return !!policy && policy.decision === "deny" && policy.enforced !== false;
|
|
14
|
+
}
|
|
11
15
|
export class PolicyDeniedError extends Error {
|
|
12
16
|
tool;
|
|
13
17
|
reason;
|
|
@@ -24,7 +28,12 @@ export function createSdkIssuer(cfg) {
|
|
|
24
28
|
const key = loadPrivateKey(cfg.identity.keyFile);
|
|
25
29
|
const issuer = createIssuer(key, cfg.receiptsDir, openLog(cfg, key), { exporter: openExporter(cfg) });
|
|
26
30
|
const policyText = cfg.policyFile ? readFileSync(cfg.policyFile, "utf8") : null;
|
|
27
|
-
const decide = (ev) =>
|
|
31
|
+
const decide = (ev) => {
|
|
32
|
+
if (policyText === null)
|
|
33
|
+
return null;
|
|
34
|
+
const d = evaluate(policyText, { agentId: cfg.agentId, tool: ev.tool, context: { args: ev.args, facts: {} } });
|
|
35
|
+
return cfg.mode === "observe" ? { ...d, enforced: false } : d;
|
|
36
|
+
};
|
|
28
37
|
function record(ev, outcome, policy = null) {
|
|
29
38
|
const execution = outcome.status === "denied"
|
|
30
39
|
? { status: "denied", reason: outcome.reason, provenance: "claimed" }
|
|
@@ -56,7 +65,7 @@ export function createSdkIssuer(cfg) {
|
|
|
56
65
|
return async (args) => {
|
|
57
66
|
const ev = { tool, args, ...meta };
|
|
58
67
|
const policy = decide(ev);
|
|
59
|
-
if (policy
|
|
68
|
+
if (denies(policy)) {
|
|
60
69
|
const reason = [...policy.reasons, ...policy.errors].join("; ") || "no permit policy matched";
|
|
61
70
|
const bundle = await record(ev, { status: "denied", reason }, policy);
|
|
62
71
|
throw new PolicyDeniedError(tool, reason, receiptIdOf(bundle));
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// observeRunner: receipts only, from the runner's agent_tool_start / agent_tool_end events. Cannot block, so it
|
|
5
5
|
// evaluates no policy. Use one or the other per tool, not both.
|
|
6
6
|
// Typed structurally so this file does not import the package.
|
|
7
|
-
import { receiptIdOf } from "./index.js";
|
|
7
|
+
import { receiptIdOf, denies } from "./index.js";
|
|
8
8
|
function parseArgs(input) {
|
|
9
9
|
if (!input)
|
|
10
10
|
return {};
|
|
@@ -31,7 +31,7 @@ export function wrapTools(issuer, tools) {
|
|
|
31
31
|
const invoke = async (ctx, input, details) => {
|
|
32
32
|
const ev = { tool: t.name, args: parseArgs(input), session: { id: null, toolUseId: details?.toolCall?.callId ?? null } };
|
|
33
33
|
const policy = issuer.decide(ev);
|
|
34
|
-
if (policy
|
|
34
|
+
if (denies(policy)) {
|
|
35
35
|
const reason = [...policy.reasons, ...policy.errors].join("; ") || "no permit policy matched";
|
|
36
36
|
const bundle = await issuer.record(ev, { status: "denied", reason }, policy);
|
|
37
37
|
return `Denied by policy: ${reason} (receipt ${receiptIdOf(bundle)})`;
|
package/dist/sdk/openclaw.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// import { registerOpenClaw } from "@agent-custody/receipts/sdk/openclaw";
|
|
9
9
|
// export default definePluginEntry({ id: "agent-custody", name: "agent-custody", description: "A receipt for every tool call",
|
|
10
10
|
// register(api) { registerOpenClaw(api, createSdkIssuer(loadSdkConfig(process.env.AGENT_CUSTODY_CONFIG!))); } });
|
|
11
|
-
import { receiptIdOf } from "./index.js";
|
|
11
|
+
import { denies, receiptIdOf } from "./index.js";
|
|
12
12
|
function toEvent(event, ctx) {
|
|
13
13
|
const raw = event.params;
|
|
14
14
|
const args = raw && typeof raw === "object" && !Array.isArray(raw) ? raw : { input: raw ?? null };
|
|
@@ -26,7 +26,7 @@ export function openclawHooks(issuer) {
|
|
|
26
26
|
before_tool_call: async (event, ctx) => {
|
|
27
27
|
const ev = toEvent(event, ctx);
|
|
28
28
|
const policy = issuer.decide(ev);
|
|
29
|
-
if (!policy
|
|
29
|
+
if (!denies(policy))
|
|
30
30
|
return undefined;
|
|
31
31
|
const reason = [...policy.reasons, ...policy.errors].join("; ") || "no permit policy matched";
|
|
32
32
|
try {
|
package/dist/sdk/vercel-ai.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Vercel AI SDK adapter: wraps every tool's execute() in a ToolSet. Decides, runs, records.
|
|
2
2
|
// Typed structurally so this file does not import the `ai` package.
|
|
3
|
-
import { PolicyDeniedError, receiptIdOf } from "./index.js";
|
|
3
|
+
import { PolicyDeniedError, receiptIdOf, denies } from "./index.js";
|
|
4
4
|
/** Returns a new ToolSet. Tools without execute (client-side or provider-executed) pass through unchanged. */
|
|
5
5
|
export function wrapTools(issuer, tools) {
|
|
6
6
|
const out = {};
|
|
@@ -14,7 +14,7 @@ export function wrapTools(issuer, tools) {
|
|
|
14
14
|
const args = input && typeof input === "object" && !Array.isArray(input) ? input : { input };
|
|
15
15
|
const ev = { tool: name, args, session: { id: null, toolUseId: options?.toolCallId ?? null } };
|
|
16
16
|
const policy = issuer.decide(ev);
|
|
17
|
-
if (policy
|
|
17
|
+
if (denies(policy)) {
|
|
18
18
|
const reason = [...policy.reasons, ...policy.errors].join("; ") || "no permit policy matched";
|
|
19
19
|
const bundle = await issuer.record(ev, { status: "denied", reason }, policy);
|
|
20
20
|
throw new PolicyDeniedError(name, reason, receiptIdOf(bundle));
|
package/dist/sidecar.js
CHANGED
|
@@ -37,7 +37,7 @@ function parsePolicy(v) {
|
|
|
37
37
|
return null;
|
|
38
38
|
if (!isRecord(v) || (v.decision !== "allow" && v.decision !== "deny") || !Array.isArray(v.reasons) || !Array.isArray(v.errors) || typeof v.policyDigest !== "string")
|
|
39
39
|
throw new Error("policy must be a PolicyDecision from /decide");
|
|
40
|
-
return { decision: v.decision, reasons: v.reasons.map(String), errors: v.errors.map(String), policyDigest: v.policyDigest };
|
|
40
|
+
return { decision: v.decision, reasons: v.reasons.map(String), errors: v.errors.map(String), policyDigest: v.policyDigest, ...(typeof v.enforced === "boolean" ? { enforced: v.enforced } : {}) };
|
|
41
41
|
}
|
|
42
42
|
export function sidecarHandler(issuer) {
|
|
43
43
|
return async (req, res) => {
|
package/dist/verify.js
CHANGED
|
@@ -64,8 +64,9 @@ export function verifyBundle(bundle, opts) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
if (p.policy) {
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
// a deny that was not enforced (observe mode, enforced: false on the receipt) may sit beside an executed call; an enforced one never does
|
|
68
|
+
const consistent = p.policy.decision === "allow" ? p.execution.status !== "denied" : p.policy.enforced === false ? p.execution.status !== "denied" : p.execution.status === "denied";
|
|
69
|
+
add("policy decision consistent with execution", consistent, `${p.policy.decision}${p.policy.enforced === false ? " (observe mode, not enforced)" : ""} -> ${p.execution.status}`);
|
|
69
70
|
add("no policy errors on an allow", !(p.policy.decision === "allow" && p.policy.errors.length > 0));
|
|
70
71
|
}
|
|
71
72
|
const logKeys = opts.logKeys ?? [];
|
|
@@ -174,7 +175,7 @@ export function formatReport(r) {
|
|
|
174
175
|
for (const [k, f] of Object.entries(p.facts))
|
|
175
176
|
row(`fact.${k}`, f.provenance, f.value);
|
|
176
177
|
if (p.policy)
|
|
177
|
-
row("policy", p.policy.provenance, `${p.policy.decision} [${p.policy.reasons.join(",")}] policy ${short(p.policy.policyDigest)}`);
|
|
178
|
+
row("policy", p.policy.provenance, `${p.policy.decision}${p.policy.enforced === false ? " (observe mode, not enforced)" : ""} [${p.policy.reasons.join(",")}] policy ${short(p.policy.policyDigest)}`);
|
|
178
179
|
else
|
|
179
180
|
row("policy", "-", "(none evaluated)");
|
|
180
181
|
if (p.consumed)
|
package/docs/sdk.md
CHANGED
|
@@ -34,6 +34,8 @@ Use the SDK for reach. Use the gateway for anything that moves money, touches pr
|
|
|
34
34
|
|
|
35
35
|
Policies see `context.args` and an empty `context.facts`. A policy that reads `context.facts` or `context.grant` errors, which is a deny. That is intended: an SDK policy cannot pretend it checked something outside the agent's process.
|
|
36
36
|
|
|
37
|
+
`"mode": "observe"` in the SDK config records the policy's decision without acting on it: `decide` returns it with `enforced: false`, `wrap` runs the function on a deny and records it as executed, and every hook adapter below returns no block. Run a policy that way first, read the receipts that say "would have been denied", then remove the line.
|
|
38
|
+
|
|
37
39
|
## Claude Code
|
|
38
40
|
|
|
39
41
|
Register the hook command in `.claude/settings.json`. The same command handles all three events.
|
|
@@ -155,6 +157,23 @@ export default definePluginEntry({
|
|
|
155
157
|
|
|
156
158
|
Then `openclaw plugins install --link ./agent-custody-plugin && openclaw plugins enable agent-custody`. `before_tool_call` evaluates the policy: on deny it issues a denial receipt and returns `{ block: true, blockReason: "agent-custody: … (receipt <id>)" }`; on allow, or with no policy, it returns nothing, so OpenClaw's own approvals and other plugins still apply. It never auto-approves, and if the denial receipt cannot be issued the call is blocked all the same. `after_tool_call` issues the receipt for the executed or failed call, with OpenClaw's `sessionId` and `toolCallId` as the receipt's session. `openclawHooks(issuer)` returns the two handlers for a plugin that registers them itself. Typed loosely on purpose, mirroring OpenClaw's `hook-types.ts`, so the package does not depend on `openclaw`; exercised against that contract in [example 21](../examples/21-openclaw.ts) and the test.
|
|
157
159
|
|
|
160
|
+
## DeepSeek Harness
|
|
161
|
+
|
|
162
|
+
[src/sdk/deepseek-harness.ts](../src/sdk/deepseek-harness.ts). DeepSeek Harness (dsh) is Node, everything in it is a plugin, and it runs two awaited waterfalls around every tool call: `tools/pre-execute`, whose handler returns `{ kind: "deny", reason }` (the reason reaches the model) or delegates with `next()`, and `tools/post-execute`, which sees the result. This module is a harness plugin itself: it exports `name`, `inject`, `Config`, and `apply`, so it is listed by its package path with the SDK config file as its one setting:
|
|
163
|
+
|
|
164
|
+
```yaml
|
|
165
|
+
# cordis.yml overlay (dsh web --patch ./cordis.yml), or the plugin list the dsh CLI manages
|
|
166
|
+
- insert:
|
|
167
|
+
- id: agent-custody
|
|
168
|
+
name: "@agent-custody/receipts/sdk/deepseek-harness"
|
|
169
|
+
config:
|
|
170
|
+
config: /abs/path/sdk.json
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
`AGENT_CUSTODY_CONFIG` in the environment works instead of the setting. `tools/pre-execute` evaluates the policy: on an enforced deny it issues the denial receipt and denies with `agent-custody: … (receipt <id>)`; otherwise it delegates, so the harness's own approvals and other plugins still apply, and it never decides `allow` itself. `tools/post-execute` records the executed or failed call with the harness's session and call ids and delegates. `deepseekHarnessHooks(issuer)` returns the two handlers, `registerDeepSeekHarness(ctx, issuer)` registers them, for a plugin of your own. Typed loosely from the harness's `packages/core/tools/src/index.ts`, no dependency on `@deepseek-ai/dsh-tools`; driven with the harness's event shapes in [example 22](../examples/22-deepseek-harness.ts) and the test.
|
|
174
|
+
|
|
175
|
+
The harness also ships `@deepseek-ai/dsh-hooks-claude-code`, a bridge that runs a Claude Code `hooks.json` on the same seams. The [`agent-custody hook`](#claude-code) command works through it unchanged, one process per call; this plugin is the in-process path.
|
|
176
|
+
|
|
158
177
|
## Hermes Agent
|
|
159
178
|
|
|
160
179
|
[packages/python/agent_custody/hermes.py](../../python/agent_custody/hermes.py), through the sidecar like every Python adapter. Hermes runs Python plugins in the agent's process and fires `pre_tool_call(tool_name, args, task_id, **kwargs)`, which may return `{"action": "block", "message": …}`, and `post_tool_call(tool_name, args, result, task_id, duration_ms, **kwargs)`. The plugin is the directory [packages/python/hermes-plugin](../../python/hermes-plugin/): a `plugin.yaml` declaring both hooks and a `sidecar_url` setting, and an `__init__.py` that is one line, `from agent_custody.hermes import register`. Copy it to `~/.hermes/plugins/agent-custody/`, `pip install agent-custody`, start the sidecar, `hermes plugins enable agent-custody`. A denied call is blocked before it runs with the receipt id in the message; an allowed one gets no action, so Hermes's own guardrails and approvals still apply; every completed call is recorded with the Hermes `task_id` as the receipt's session. `hermes_hooks(client)` returns the two callables; `register_hermes(ctx, client)` registers them on a plugin context.
|
package/docs/tutorials.md
CHANGED
|
@@ -31,6 +31,7 @@ Suggested reading order is the numbering. Output lands in `examples-out/`, which
|
|
|
31
31
|
| 19 | Splunk export | [19-splunk.ts](../examples/19-splunk.ts) | a stand-in HTTP Event Collector, `splunk` in the config with the token from the environment, one event per receipt with the receipt id and log position as fields, the collector going away and the next receipt still issued | `src/splunk.ts`, `src/otel.ts` |
|
|
32
32
|
| 20 | one gateway for many agents, over HTTP | [20-http-gateway.ts](../examples/20-http-gateway.ts) | a gateway host served over Streamable HTTP, two agents connecting with their own grants and seeing their own tools, receipts naming the right agent, a refusal by grant, and a stranger's grant getting no session | `src/gateway-http.ts`, `src/gateway.ts` |
|
|
33
33
|
| 21 | OpenClaw plugin hooks | [21-openclaw.ts](../examples/21-openclaw.ts) | the two hooks driven with OpenClaw's event shapes: an allowed call gets no result, a denied one is blocked with the receipt id, executed and failed calls are recorded, every receipt verifies | `src/sdk/openclaw.ts` |
|
|
34
|
+
| 22 | DeepSeek Harness plugin | [22-deepseek-harness.ts](../examples/22-deepseek-harness.ts) | the module as a plugin, applied by a stand-in context; pre-execute delegating an allowed call and denying one with the receipt id; post-execute recording executed and failed calls; every receipt verifying with the harness's session and call ids | `src/sdk/deepseek-harness.ts` |
|
|
34
35
|
|
|
35
36
|
## How policies are defined, in one paragraph
|
|
36
37
|
|
package/docs/usage.md
CHANGED
|
@@ -115,6 +115,8 @@ Exactly one of the two. The bearer token comes from the named environment variab
|
|
|
115
115
|
|
|
116
116
|
`facts` tells the gateway which upstream tool to call before evaluating policy for a given tool. `$args.<key>` copies a value from the intercepted call. The result appears in Cedar as `context.facts.<name>` and in the receipt with its own digest, labelled `observed`. If a fact lookup fails, the call is denied and the receipt says why. A lookup with `"optional": true` is skipped when a `$args.<key>` it needs is absent from the call, and the fact is then simply not present, which a policy tests with `context.facts has <name>`; this is how a policy sees the fact a `memory.write` is about to supersede without denying every write that supersedes nothing.
|
|
117
117
|
|
|
118
|
+
`mode` is `"enforce"` by default. Set it to `"observe"` to run a new policy against real traffic before turning it on: every call the grant allows is forwarded, the decision the policy would have made is recorded on the receipt with `enforced: false`, and nothing is withheld. The verifier accepts such a receipt and names the mode; strip the flag and the same receipt fails. Switch to `"enforce"` when the receipts stop saying "would have been denied" for calls you meant to allow.
|
|
119
|
+
|
|
118
120
|
`precommit` names the consequential tools, or `["*"]` for all of them. For every other tool the gateway forwards the call and then records it, so if the log is unreachable at that moment the side effect exists before its evidence does. For a tool in `precommit` the gateway first signs an authorization statement, everything the receipt will say except the outcome, and appends it to the log. Only if the log took it does the call go upstream. The receipt then embeds that authorization with its own inclusion proof, and a verifier checks that it names this call and sits in the log before the receipt does. If the log will not take it, the call is not forwarded, the agent is told `Not executed`, and the receipt records `execution.status: "withheld"` with the policy's `allow` beside it. The authorization is also written on its own as `receipts/<receiptId>.authorization.json`, which is the evidence that survives if the gateway dies between forwarding and the receipt. Use it for money, for anything irreversible, and for anything a counterparty could later dispute; the cost is one extra log append per call.
|
|
119
121
|
|
|
120
122
|
**5. Run the gateway.** It speaks MCP on stdin/stdout and logs to stderr only.
|
package/docs/verification.md
CHANGED
|
@@ -76,7 +76,7 @@ execution observed executed
|
|
|
76
76
|
| executed tool within delegated scope | if the tool ran, the grant covered it. Denied calls pass this check by construction | a gateway that forwarded out of scope |
|
|
77
77
|
| principal is claimed, not attested | SDK receipts only: there is no delegation, so the principal is a config string and is labelled as such | an SDK receipt pretending to an attested principal |
|
|
78
78
|
| request args digest | the args in the predicate hash to the digest in the subject | edited arguments |
|
|
79
|
-
| policy decision consistent with execution | allow went with executed or failed; deny went with denied. Skipped when no policy was evaluated | an issuer that executed after a deny |
|
|
79
|
+
| policy decision consistent with execution | allow went with executed or failed; an enforced deny went with denied; a deny carrying `enforced: false` (observe mode) went with anything but denied, and the detail says "observe mode, not enforced". Skipped when no policy was evaluated | an issuer that executed after a deny |
|
|
80
80
|
| no policy errors on an allow | an allow was not produced while Cedar reported errors | broken fail-closed behaviour |
|
|
81
81
|
| authorization signature (issuer key) | pre-commit receipts only: the embedded authorization statement was signed by the same issuer key as the receipt | an authorization from another gateway, or none the issuer signed |
|
|
82
82
|
| authorization names this call | the committed authorization names this receipt id, tool, argument digest, agent, and principal, and recorded an allow | an authorization spliced in from a different call |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-custody/receipts",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.17",
|
|
4
4
|
"mcpName": "io.github.svayatta/agent-custody",
|
|
5
5
|
"description": "Chain of custody for AI agents: signed, independently verifiable receipts for tool calls. MCP gateway + Cedar policy + Merkle transparency log",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -45,6 +45,10 @@
|
|
|
45
45
|
"types": "./dist/sdk/openclaw.d.ts",
|
|
46
46
|
"default": "./dist/sdk/openclaw.js"
|
|
47
47
|
},
|
|
48
|
+
"./sdk/deepseek-harness": {
|
|
49
|
+
"types": "./dist/sdk/deepseek-harness.d.ts",
|
|
50
|
+
"default": "./dist/sdk/deepseek-harness.js"
|
|
51
|
+
},
|
|
48
52
|
"./cli": {
|
|
49
53
|
"types": "./dist/cli.d.ts",
|
|
50
54
|
"default": "./dist/cli.js"
|