@coreguard/agent-commerce 0.2.0 → 0.3.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 +1 -1
- package/bin/trust-card.mjs +12 -0
- package/mcp.mjs +35 -1
- package/observations.mjs +24 -3
- package/package.json +13 -6
- package/server.json +3 -3
- package/trust-card-cli.mjs +289 -0
- package/trust-card.mjs +252 -0
- package/wave3.mjs +97 -11
- package/wave4.mjs +593 -0
package/trust-card.mjs
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CoreGuard trust cards (CG-TC/1) — turn a server record and a tools/list into
|
|
3
|
+
* one publishable page.
|
|
4
|
+
*
|
|
5
|
+
* This module is the product surface behind `coreguard_audit_server_manifest`
|
|
6
|
+
* and `coreguard_scan_tool_descriptions`: a deterministic composition of both
|
|
7
|
+
* checks plus the inventory, digests and the honesty boundary a listing needs.
|
|
8
|
+
*
|
|
9
|
+
* It performs no I/O, reads no wall clock and calls no network. The CLI wraps
|
|
10
|
+
* it; the MCP tools stay available for agents that want the raw structures.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { auditServerManifest, scanToolDescriptions, digestOf } from "./wave3.mjs";
|
|
14
|
+
|
|
15
|
+
export const TRUST_CARD_SPEC = "CG-TC/1";
|
|
16
|
+
|
|
17
|
+
const str = (v) => (typeof v === "string" && v.trim() ? v.trim() : null);
|
|
18
|
+
|
|
19
|
+
/** Accept an array, {tools}, a JSON-RPC tools/list response, or {result:{tools}}. */
|
|
20
|
+
export function parseToolsList(text) {
|
|
21
|
+
let parsed;
|
|
22
|
+
try {
|
|
23
|
+
parsed = typeof text === "string" ? JSON.parse(text) : text;
|
|
24
|
+
} catch (err) {
|
|
25
|
+
throw new Error(`tools/list is not valid JSON: ${err?.message ?? err}`);
|
|
26
|
+
}
|
|
27
|
+
const tools = Array.isArray(parsed)
|
|
28
|
+
? parsed
|
|
29
|
+
: Array.isArray(parsed?.tools)
|
|
30
|
+
? parsed.tools
|
|
31
|
+
: Array.isArray(parsed?.result?.tools)
|
|
32
|
+
? parsed.result.tools
|
|
33
|
+
: null;
|
|
34
|
+
if (!tools) throw new Error("tools/list must be an array, {tools:[...]} or a JSON-RPC tools/list response");
|
|
35
|
+
if (tools.some((t) => !t || typeof t !== "object" || !str(t.name))) {
|
|
36
|
+
throw new Error("every tool needs a non-empty name");
|
|
37
|
+
}
|
|
38
|
+
return tools;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Detect the JSON-RPC envelope so the card can state what the bytes were. */
|
|
42
|
+
export function toolsListShape(text) {
|
|
43
|
+
let parsed;
|
|
44
|
+
try {
|
|
45
|
+
parsed = typeof text === "string" ? JSON.parse(text) : text;
|
|
46
|
+
} catch {
|
|
47
|
+
return "unknown";
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(parsed)) return "array";
|
|
50
|
+
if (Array.isArray(parsed?.tools)) return "object";
|
|
51
|
+
if (Array.isArray(parsed?.result?.tools)) return "json-rpc";
|
|
52
|
+
return "unknown";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Structural verdict. Deliberately conservative:
|
|
57
|
+
* BLOCKED — a HIGH manifest finding or a DANGEROUS description scan.
|
|
58
|
+
* REVIEW — anything medium/suspicious, plus any manifest-only card.
|
|
59
|
+
* PASS — no structural finding at all. PASS is not SAFE.
|
|
60
|
+
*/
|
|
61
|
+
export function verdictFor(manifestAudit, scan, coverage) {
|
|
62
|
+
const reasons = [];
|
|
63
|
+
if (!coverage.tools) reasons.push("TOOLS_LIST_MISSING: descriptions were not scanned, so the surface is not covered");
|
|
64
|
+
if (manifestAudit.risk === "high") reasons.push("MANIFEST_HIGH: at least one HIGH-severity manifest finding");
|
|
65
|
+
if (scan.verdict === "DANGEROUS") reasons.push("TOOLS_DANGEROUS: at least one HIGH-severity description finding");
|
|
66
|
+
if (reasons.length > 0) return { verdict: "BLOCKED", reasons };
|
|
67
|
+
if (manifestAudit.risk === "medium" || scan.verdict === "SUSPICIOUS" || !coverage.manifest) reasons.push("REVIEW_REQUIRED: medium-severity findings or missing manifest");
|
|
68
|
+
if (manifestAudit.findings.length > 0) reasons.push(`${manifestAudit.findings.length} informational/low manifest finding(s)`);
|
|
69
|
+
return { verdict: reasons.length > 0 ? "REVIEW" : "PASS", reasons };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const esc = (s) => String(s ?? "").replace(/\|/g, "\\|").replace(/\n/g, " ");
|
|
73
|
+
const short = (s, n = 96) => {
|
|
74
|
+
const v = String(s ?? "");
|
|
75
|
+
return v.length <= n ? v : v.slice(0, n - 1) + "…";
|
|
76
|
+
};
|
|
77
|
+
const code = (v) => "`" + String(v).replace(/`/g, "'") + "`";
|
|
78
|
+
|
|
79
|
+
function findingsTable(findings) {
|
|
80
|
+
if (findings.length === 0) return "_No manifest findings._";
|
|
81
|
+
const rows = findings.map(
|
|
82
|
+
(f) => `| ${code(f.id)} | ${f.severity} | ${esc(f.detail)}${f.evidence === undefined || f.evidence === null ? "" : ` — ${code(short(JSON.stringify(f.evidence), 120))}`} |`,
|
|
83
|
+
);
|
|
84
|
+
return ["| Finding | Severity | Detail |", "|---|---|---|", ...rows].join("\n");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Build the card.
|
|
89
|
+
*
|
|
90
|
+
* @param {object} input
|
|
91
|
+
* @param {object} input.manifest server.json record
|
|
92
|
+
* @param {Array} [input.tools] tools/list entries
|
|
93
|
+
* @param {object} [input.sources] { manifest: label, tools: label }
|
|
94
|
+
* @param {string} [input.date] YYYY-MM-DD, caller-supplied (no wall clock here)
|
|
95
|
+
* @param {string} [input.url] published URL, if any
|
|
96
|
+
* @param {string} [input.toolsShape] array | object | json-rpc
|
|
97
|
+
*/
|
|
98
|
+
export function buildTrustCard(input = {}) {
|
|
99
|
+
const manifest = input.manifest;
|
|
100
|
+
if (!manifest || typeof manifest !== "object" || Array.isArray(manifest)) {
|
|
101
|
+
throw new Error("server.json must be a JSON object");
|
|
102
|
+
}
|
|
103
|
+
const tools = Array.isArray(input.tools) ? input.tools : null;
|
|
104
|
+
if (!tools) throw new Error("tools/list is required: a card that has not scanned the tool surface is not a trust card");
|
|
105
|
+
if (input.date !== undefined && input.date !== null && !/^\d{4}-\d{2}-\d{2}$/.test(String(input.date))) {
|
|
106
|
+
throw new Error("--date must be YYYY-MM-DD");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const coverage = { manifest: true, tools: true };
|
|
110
|
+
const manifestAudit = auditServerManifest({ manifest });
|
|
111
|
+
const scan = scanToolDescriptions({ tools });
|
|
112
|
+
const { verdict, reasons } = verdictFor(manifestAudit, scan, coverage);
|
|
113
|
+
|
|
114
|
+
const serverName = str(input.name) ?? manifestAudit.serverName ?? str(manifest.name) ?? "unnamed server";
|
|
115
|
+
const version = manifest.version ?? "unpinned";
|
|
116
|
+
const toolsDigest = digestOf(tools);
|
|
117
|
+
const sources = { manifest: input.sources?.manifest ?? "server.json", tools: input.sources?.tools ?? "tools/list" };
|
|
118
|
+
const cardDigest = digestOf({
|
|
119
|
+
spec: TRUST_CARD_SPEC,
|
|
120
|
+
server: serverName,
|
|
121
|
+
version,
|
|
122
|
+
verdict,
|
|
123
|
+
manifestDigest: manifestAudit.manifestDigest,
|
|
124
|
+
toolsDigest,
|
|
125
|
+
toolsCount: tools.length,
|
|
126
|
+
date: input.date ?? null,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const byTool = new Map();
|
|
130
|
+
for (const f of scan.findings) {
|
|
131
|
+
const list = byTool.get(f.tool) ?? [];
|
|
132
|
+
list.push(f);
|
|
133
|
+
byTool.set(f.tool, list);
|
|
134
|
+
}
|
|
135
|
+
const inventory = tools
|
|
136
|
+
.map((t, i) => {
|
|
137
|
+
const hits = byTool.get(String(t.name)) ?? [];
|
|
138
|
+
const status = hits.length === 0 ? "clean" : hits.some((h) => h.severity === "HIGH") ? "**FLAGGED**" : "review";
|
|
139
|
+
return `| ${i + 1} | ${code(short(t.name, 48))} | ${short(t.description, 80) || "—"} | ${status} |`;
|
|
140
|
+
})
|
|
141
|
+
.join("\n");
|
|
142
|
+
|
|
143
|
+
const flagged = scan.findings.length === 0
|
|
144
|
+
? "_No hidden characters, HTML comments or imperative phrases were found in any tool description._"
|
|
145
|
+
: scan.findings
|
|
146
|
+
.map((f) => `- ${code(f.tool)} — ${f.kind}${f.codepoint ? ` ${f.codepoint}` : f.phrase ? ` "${esc(f.phrase)}"` : ""} at offset ${f.index} (${f.severity})`)
|
|
147
|
+
.join("\n");
|
|
148
|
+
|
|
149
|
+
const lines = [
|
|
150
|
+
`# Trust card — ${serverName} ${version}`,
|
|
151
|
+
"",
|
|
152
|
+
`> **Verdict: ${verdict}** · manifest risk \`${manifestAudit.risk}\` · tool descriptions \`${scan.verdict}\` · ${tools.length} tools`,
|
|
153
|
+
">",
|
|
154
|
+
"> Generated by CoreGuard (`CG-TC/1`) from the published server record and a live `tools/list`. Structural audit only: it executes nothing and contacts no network. **PASS ≠ SAFE; absence of findings is not proof of safety.**",
|
|
155
|
+
"",
|
|
156
|
+
"| Field | Value |",
|
|
157
|
+
"|---|---|",
|
|
158
|
+
`| Server | ${esc(serverName)} |`,
|
|
159
|
+
`| Version | ${code(version)} |`,
|
|
160
|
+
`| Transport | ${manifestAudit.transport} |`,
|
|
161
|
+
`| Tools | ${tools.length} — ${code(toolsDigest)} |`,
|
|
162
|
+
`| Manifest digest | ${code(manifestAudit.manifestDigest)} |`,
|
|
163
|
+
`| Card digest | ${code(cardDigest)} |`,
|
|
164
|
+
`| Sources | record: ${code(sources.manifest)} · tools: ${code(sources.tools)} |`,
|
|
165
|
+
...(input.date ? [`| Card date | ${code(input.date)} |`] : []),
|
|
166
|
+
"",
|
|
167
|
+
"## Why this verdict",
|
|
168
|
+
"",
|
|
169
|
+
...(reasons.length === 0 ? ["_No structural finding was raised. That is all this card can say._"] : reasons.map((r) => `- ${r}`)),
|
|
170
|
+
"",
|
|
171
|
+
"## Server-record findings",
|
|
172
|
+
"",
|
|
173
|
+
findingsTable(manifestAudit.findings),
|
|
174
|
+
"",
|
|
175
|
+
"## Tool-description scan",
|
|
176
|
+
"",
|
|
177
|
+
`Scanned ${tools.length} descriptions. Findings: ${scan.counts.findings} (${scan.counts.high} high, ${scan.counts.medium} medium).`,
|
|
178
|
+
"",
|
|
179
|
+
flagged,
|
|
180
|
+
"",
|
|
181
|
+
"## Tool inventory",
|
|
182
|
+
"",
|
|
183
|
+
"| # | Tool | Description (truncated) | Scan |",
|
|
184
|
+
"|---|---|---|---|",
|
|
185
|
+
inventory,
|
|
186
|
+
"",
|
|
187
|
+
"## What was checked",
|
|
188
|
+
"",
|
|
189
|
+
"- Server record: pinned version and package, declared distribution, transport, repository provenance, secret-shaped environment variables.",
|
|
190
|
+
"- Tool descriptions: bidi overrides, Unicode tag/smuggling characters, zero-width and soft-hyphen characters, HTML comments, and a fixed imperative-phrase list.",
|
|
191
|
+
"- Binding: both digests are embedded above, so a changed record or a single changed description changes the card digest.",
|
|
192
|
+
"",
|
|
193
|
+
"## What this does not prove",
|
|
194
|
+
"",
|
|
195
|
+
"- It does not execute the server, its package, or its remote endpoint.",
|
|
196
|
+
"- It does not review source code, permissions, data handling, licensing, or the payment behaviour of any tool.",
|
|
197
|
+
"- It does not detect every poisoning technique; the phrase list is fixed and the scanner is intentionally narrow.",
|
|
198
|
+
"- A clean card is a starting point for review, not an approval.",
|
|
199
|
+
"",
|
|
200
|
+
"## Reproduce",
|
|
201
|
+
"",
|
|
202
|
+
"```bash",
|
|
203
|
+
`coreguard-trust-card \\`,
|
|
204
|
+
` --server-json ${sources.manifest === "server.json" ? "server.json" : sources.manifest} \\`,
|
|
205
|
+
` --tools-list ${sources.tools === "tools/list" ? "tools.json" : sources.tools} \\`,
|
|
206
|
+
` --out trust-card.md`,
|
|
207
|
+
"```",
|
|
208
|
+
"",
|
|
209
|
+
"Same inputs and same `--date` produce the same card digest. Verify it by re-running and comparing the digest line.",
|
|
210
|
+
"",
|
|
211
|
+
"<!-- paste-ready summary for a listing page -->",
|
|
212
|
+
"",
|
|
213
|
+
`> **Trust card:** ${verdict} · manifest risk \`${manifestAudit.risk}\` · tool descriptions \`${scan.verdict}\` · ${tools.length} tools · card \`${cardDigest}\``,
|
|
214
|
+
`> Structural audit by CoreGuard (CG-TC/1) from the server record and a live \`tools/list\`. PASS ≠ SAFE.${input.url ? ` Full card: ${input.url}` : ""}`,
|
|
215
|
+
"",
|
|
216
|
+
];
|
|
217
|
+
|
|
218
|
+
const json = {
|
|
219
|
+
spec: TRUST_CARD_SPEC,
|
|
220
|
+
server: { name: serverName, version, transport: manifestAudit.transport },
|
|
221
|
+
verdict,
|
|
222
|
+
reasons,
|
|
223
|
+
coverage,
|
|
224
|
+
manifest: {
|
|
225
|
+
digest: manifestAudit.manifestDigest,
|
|
226
|
+
risk: manifestAudit.risk,
|
|
227
|
+
findings: manifestAudit.findings,
|
|
228
|
+
counts: manifestAudit.counts,
|
|
229
|
+
},
|
|
230
|
+
tools: {
|
|
231
|
+
count: tools.length,
|
|
232
|
+
digest: toolsDigest,
|
|
233
|
+
shape: input.toolsShape ?? "unknown",
|
|
234
|
+
scan: { verdict: scan.verdict, counts: scan.counts, findings: scan.findings },
|
|
235
|
+
},
|
|
236
|
+
sources,
|
|
237
|
+
cardDigest,
|
|
238
|
+
...(input.date ? { date: input.date } : {}),
|
|
239
|
+
...(input.url ? { url: input.url } : {}),
|
|
240
|
+
boundary: "structural audit only; executes nothing; PASS ≠ SAFE",
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
return { markdown: lines.join("\n"), json, verdict, cardDigest, manifestAudit, scan };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Exit status for the gate: 0 below the threshold, 1 at/above it. */
|
|
247
|
+
export function exitCodeFor(verdict, failOn = "high") {
|
|
248
|
+
const rank = { PASS: 0, REVIEW: 1, BLOCKED: 2 };
|
|
249
|
+
const threshold = { none: 99, high: 2, medium: 1 }[failOn];
|
|
250
|
+
if (threshold === undefined) throw new Error("--fail-on must be none, medium or high");
|
|
251
|
+
return rank[verdict] >= threshold ? 1 : 0;
|
|
252
|
+
}
|
package/wave3.mjs
CHANGED
|
@@ -80,10 +80,12 @@ export function compilePolicy(args = {}) {
|
|
|
80
80
|
rules.push({ ruleId: "RECIPIENT_001", type: "RECIPIENT_ALLOWLIST", params: { addresses: spec.allowRecipients.map(String) }, severity: "CRITICAL" });
|
|
81
81
|
}
|
|
82
82
|
if (Array.isArray(spec.allowSelectors) && spec.allowSelectors.length) {
|
|
83
|
-
|
|
83
|
+
// Selectors are bytes, not prose: normalise at compile time so a spec written
|
|
84
|
+
// as "0xA9059CBB" and an intent written as "0xa9059cbb" are the same rule.
|
|
85
|
+
rules.push({ ruleId: "SELECTOR_001", type: "SELECTOR_ALLOWLIST", params: { selectors: spec.allowSelectors.map((s) => String(s).toLowerCase()) }, severity: "CRITICAL" });
|
|
84
86
|
}
|
|
85
87
|
if (spec.currency) {
|
|
86
|
-
rules.push({ ruleId: "CURRENCY_001", type: "CURRENCY_ALLOWLIST", params: { currencies: [String(spec.currency)] }, severity: "HIGH" });
|
|
88
|
+
rules.push({ ruleId: "CURRENCY_001", type: "CURRENCY_ALLOWLIST", params: { currencies: [String(spec.currency).toUpperCase()] }, severity: "HIGH" });
|
|
87
89
|
}
|
|
88
90
|
const policy = {
|
|
89
91
|
version: "CGEP/1",
|
|
@@ -358,11 +360,12 @@ export function scanToolDescriptions(args = {}) {
|
|
|
358
360
|
const name = String(tool?.name ?? `tool-${toolIndex}`);
|
|
359
361
|
const description = String(tool?.description ?? "");
|
|
360
362
|
for (let i = 0; i < description.length; i++) {
|
|
363
|
+
const start = i; // astral code points consume two UTF-16 units; report where the character STARTS
|
|
361
364
|
const cp = description.codePointAt(i);
|
|
362
|
-
if (cp > 0xffff) i++; //
|
|
365
|
+
if (cp > 0xffff) i++; // consume the low surrogate
|
|
363
366
|
const kind = HIDDEN_CHAR_KINDS.find(([lo, hi]) => cp >= lo && cp <= hi)?.[2];
|
|
364
367
|
if (kind) {
|
|
365
|
-
findings.push({ tool: name, toolIndex, index:
|
|
368
|
+
findings.push({ tool: name, toolIndex, index: start, codepoint: "U+" + cp.toString(16).toUpperCase().padStart(4, "0"), kind, severity: kind === "unicode_tag" || kind === "bidi_override" ? "HIGH" : "MEDIUM" });
|
|
366
369
|
}
|
|
367
370
|
}
|
|
368
371
|
const lower = description.toLowerCase();
|
|
@@ -397,13 +400,80 @@ const CONFUSABLES = Object.freeze({
|
|
|
397
400
|
"5": "s", "3": "e", "8": "b", "|": "l", "!": "i",
|
|
398
401
|
});
|
|
399
402
|
|
|
403
|
+
/**
|
|
404
|
+
* Minimal RFC 3492 decoder for a single `xn--` label.
|
|
405
|
+
*
|
|
406
|
+
* Without this, an attacker writes the homoglyph in punycode and the string
|
|
407
|
+
* comparison sees two unrelated ASCII names — the exact trick the check exists
|
|
408
|
+
* to catch. Returns null when the label is not decodable (the caller then keeps
|
|
409
|
+
* the raw label and the `punycode` signal still fires).
|
|
410
|
+
*/
|
|
411
|
+
function decodePunycodeLabel(label) {
|
|
412
|
+
const prefix = "xn--";
|
|
413
|
+
if (!label.startsWith(prefix)) return label;
|
|
414
|
+
const input = label.slice(prefix.length);
|
|
415
|
+
const base = 36, tMin = 1, tMax = 26, skew = 38, damp = 700, initialBias = 72, initialN = 128;
|
|
416
|
+
const adapt = (delta, numPoints, firstTime) => {
|
|
417
|
+
let d = firstTime ? Math.floor(delta / damp) : delta >> 1;
|
|
418
|
+
d += Math.floor(d / numPoints);
|
|
419
|
+
let k = 0;
|
|
420
|
+
while (d > ((base - tMin) * tMax) >> 1) {
|
|
421
|
+
d = Math.floor(d / (base - tMin));
|
|
422
|
+
k += base;
|
|
423
|
+
}
|
|
424
|
+
return Math.floor(k + ((base - tMin + 1) * d) / (d + skew));
|
|
425
|
+
};
|
|
426
|
+
const digitOf = (code) => {
|
|
427
|
+
if (code >= 0x30 && code <= 0x39) return code - 22;
|
|
428
|
+
if (code >= 0x41 && code <= 0x5a) return code - 0x41;
|
|
429
|
+
if (code >= 0x61 && code <= 0x7a) return code - 0x61;
|
|
430
|
+
return -1;
|
|
431
|
+
};
|
|
432
|
+
const output = [];
|
|
433
|
+
let n = initialN, i = 0, bias = initialBias;
|
|
434
|
+
const basic = input.lastIndexOf("-");
|
|
435
|
+
if (basic > 0) {
|
|
436
|
+
for (let j = 0; j < basic; j++) {
|
|
437
|
+
const code = input.charCodeAt(j);
|
|
438
|
+
if (code >= 0x80) return null;
|
|
439
|
+
output.push(String.fromCharCode(code));
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
let idx = basic > 0 ? basic + 1 : 0;
|
|
443
|
+
while (idx < input.length) {
|
|
444
|
+
const oldi = i;
|
|
445
|
+
let w = 1;
|
|
446
|
+
for (let k = base; ; k += base) {
|
|
447
|
+
if (idx >= input.length) return null;
|
|
448
|
+
const digit = digitOf(input.charCodeAt(idx++));
|
|
449
|
+
if (digit < 0) return null;
|
|
450
|
+
i += digit * w;
|
|
451
|
+
const t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias;
|
|
452
|
+
if (digit < t) break;
|
|
453
|
+
w *= base - t;
|
|
454
|
+
}
|
|
455
|
+
const total = output.length + 1;
|
|
456
|
+
bias = adapt(i - oldi, total, oldi === 0);
|
|
457
|
+
n += Math.floor(i / total);
|
|
458
|
+
i %= total;
|
|
459
|
+
if (n > 0x10ffff || (n >= 0xd800 && n <= 0xdfff)) return null;
|
|
460
|
+
output.splice(i, 0, String.fromCodePoint(n));
|
|
461
|
+
i++;
|
|
462
|
+
}
|
|
463
|
+
return output.join("");
|
|
464
|
+
}
|
|
465
|
+
|
|
400
466
|
function skeleton(domain) {
|
|
401
|
-
|
|
467
|
+
const labels = String(domain ?? "")
|
|
402
468
|
.toLowerCase()
|
|
403
469
|
.trim()
|
|
404
470
|
.replace(/^https?:\/\//, "")
|
|
405
471
|
.replace(/\/.*$/, "")
|
|
406
472
|
.replace(/\.$/, "")
|
|
473
|
+
.split(".")
|
|
474
|
+
.map((label) => decodePunycodeLabel(label) ?? label);
|
|
475
|
+
return labels
|
|
476
|
+
.join(".")
|
|
407
477
|
.replace(/[^a-z0-9.\-\u0080-\uffff]/g, "")
|
|
408
478
|
.split("")
|
|
409
479
|
.map((ch) => CONFUSABLES[ch] ?? ch)
|
|
@@ -664,9 +734,16 @@ export function freezeAgent(args = {}) {
|
|
|
664
734
|
const pinned = args.nowSeconds === undefined ? null : Number(args.nowSeconds);
|
|
665
735
|
const cp = createControlPlane({ storePath, create: true, ...(pinned === null ? {} : { now: () => new Date(pinned * 1000) }) });
|
|
666
736
|
const state = cp.getState();
|
|
667
|
-
const
|
|
737
|
+
const wasRevoked = state.agents?.[agentId]?.status === "revoked";
|
|
738
|
+
const pending = Object.values(state.approvals ?? {}).filter((a) => a.agentId === agentId && a.status === "pending");
|
|
739
|
+
// The ledger only transitions pending → approved/rejected. An approval that was
|
|
740
|
+
// already approved cannot be re-decided, so the freeze must not pretend it did:
|
|
741
|
+
// revocation already makes it fail closed at evaluate() time. Report it as
|
|
742
|
+
// superseded instead of counting a silent "rejection" that never happened.
|
|
743
|
+
// A second freeze must not re-report approvals the first one already superseded.
|
|
744
|
+
const approved = wasRevoked ? [] : Object.values(state.approvals ?? {}).filter((a) => a.agentId === agentId && a.status === "approved");
|
|
668
745
|
const revoked = cp.revokeAgent(agentId, reason);
|
|
669
|
-
const rejected =
|
|
746
|
+
const rejected = pending.map((a) => {
|
|
670
747
|
try {
|
|
671
748
|
return cp.decideApproval(a.id, { decision: "rejected", decidedBy: decidedBy ?? "freeze", reason: `agent frozen: ${reason}` });
|
|
672
749
|
} catch (err) {
|
|
@@ -674,14 +751,23 @@ export function freezeAgent(args = {}) {
|
|
|
674
751
|
}
|
|
675
752
|
});
|
|
676
753
|
const report = cp.auditReport({ limit: 1 });
|
|
754
|
+
const rejectedOk = rejected.filter((r) => !r.error);
|
|
677
755
|
return {
|
|
678
756
|
type: "agent_freeze",
|
|
679
757
|
spec: "CG-FZ/1",
|
|
680
758
|
agentId,
|
|
681
759
|
reason,
|
|
682
760
|
revoked,
|
|
683
|
-
approvals: {
|
|
684
|
-
|
|
761
|
+
approvals: {
|
|
762
|
+
open: pending.length + approved.length,
|
|
763
|
+
rejected: rejectedOk.length,
|
|
764
|
+
failures: rejected.filter((r) => r.error).length,
|
|
765
|
+
approvedSuperseded: approved.length,
|
|
766
|
+
},
|
|
767
|
+
openApprovalIds: [...pending, ...approved].map((a) => a.id),
|
|
768
|
+
rejectedApprovalIds: rejectedOk.map((r) => r.id),
|
|
769
|
+
approvedSupersededIds: approved.map((a) => a.id),
|
|
770
|
+
note: "pending approvals are rejected in this operation; approved-but-unexecuted approvals cannot be re-decided by the ledger and are superseded by the agent revocation (evaluate() denies them)",
|
|
685
771
|
chain: { ok: report.chain?.ok ?? null, length: report.chain?.length ?? null, headHash: report.chain?.headHash ?? null },
|
|
686
772
|
boundary: "revocation and rejection are recorded in the local hash-chained ledger; they do not reach a chain, a merchant or a payment network. " + WAVE3_BOUNDARY,
|
|
687
773
|
};
|
|
@@ -984,9 +1070,9 @@ export const WAVE3_TOOLS = Object.freeze([
|
|
|
984
1070
|
},
|
|
985
1071
|
{
|
|
986
1072
|
name: "coreguard_freeze_agent",
|
|
987
|
-
description: "Kill switch over a CGCP/1 control plane: revoke the agent
|
|
1073
|
+
description: "Kill switch over a CGCP/1 control plane: revoke the agent, reject every pending approval it holds in one audited operation, and report approved-but-unexecuted approvals as superseded by the revocation. Returns the chain head and every affected id.",
|
|
988
1074
|
inputSchema: schema({ storePath: { type: "string" }, agentId: { type: "string" }, reason: { type: "string" }, decidedBy: { type: "string" }, nowSeconds: { type: "string", pattern: "^[0-9]+$", description: "optional pinned epoch seconds; omit to let the local ledger stamp call time" } }, ["storePath", "agentId"], "Control-plane path and the agent to freeze."),
|
|
989
|
-
outputSchema: out("Revocation record, rejected
|
|
1075
|
+
outputSchema: out("Revocation record, rejected and superseded approval ids, failure count and chain status."),
|
|
990
1076
|
},
|
|
991
1077
|
{
|
|
992
1078
|
name: "coreguard_budget_runway",
|