agent-customs 0.0.0-stage → 1.0.1
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/LICENSE.md +341 -0
- package/README.md +592 -2
- package/bin/agent-customs.mjs +194 -0
- package/package.json +77 -4
- package/site/.well-known/agent-customs-key.json +13 -0
- package/site/certificate.js +944 -0
- package/site/customs/analyze.js +1224 -0
- package/site/customs/auto.js +83 -0
- package/site/customs/canary.js +270 -0
- package/site/customs/canonical.js +123 -0
- package/site/customs/capabilities.js +387 -0
- package/site/customs/diff.js +236 -0
- package/site/customs/english-words.js +153 -0
- package/site/customs/guard-notice.js +226 -0
- package/site/customs/guard-ui.js +105 -0
- package/site/customs/guard.js +259 -0
- package/site/customs/inspect.js +446 -0
- package/site/customs/judge.js +208 -0
- package/site/customs/languages.js +826 -0
- package/site/customs/locks.js +967 -0
- package/site/customs/pins.js +119 -0
- package/site/customs/protocols.js +621 -0
- package/site/customs/receipts.js +154 -0
- package/site/customs/rules.js +1470 -0
- package/site/customs/unicode.js +361 -0
- package/site/runtime/LICENSE.txt +176 -0
- package/site/runtime/generated/graph.gen.mjs +83 -0
- package/site/runtime/generated/validation.gen.mjs +70 -0
- package/site/runtime/hocon.js +262 -0
- package/site/runtime/kernel.js +237 -0
- package/site/runtime/model.js +44 -0
- package/tests/fixtures/official/NOTICE +3 -0
- package/tests/fixtures/official/guesthouse_agentified/agent-manifest.json +86 -0
- package/tests/fixtures/official/guesthouse_agentified/links.json +6 -0
- package/tests/fixtures/official/guesthouse_agentified/tidewhistle_guesthouse.network.json +56 -0
- package/tests/fixtures/official/harbor_tours_agentified/agent-manifest.json +86 -0
- package/tests/fixtures/official/harbor_tours_agentified/links.json +6 -0
- package/tests/fixtures/official/harbor_tours_agentified/marrow_point_harbor_tours.network.json +56 -0
- package/tests/fixtures/official/pizza_restaurant_agentified/agent-manifest.json +78 -0
- package/tests/fixtures/official/pizza_restaurant_agentified/links.json +6 -0
- package/tests/fixtures/official/pizza_restaurant_agentified/pizza_restaurant_website_assistant.network.json +156 -0
- package/tools/bench.mjs +731 -0
- package/tools/external-heldout.mjs +337 -0
- package/tools/fs-fetch.mjs +19 -0
- package/tools/heldout-corpus.mjs +135 -0
- package/tools/inspect-local.mjs +88 -0
- package/tools/round2-corpus.mjs +395 -0
- package/tools/round3-corpus.mjs +192 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// guard.js: Agent Customs as a drop-in guard for any open agentic website.
|
|
2
|
+
//
|
|
3
|
+
// import { installCustomsGuard } from "./customs/guard.js";
|
|
4
|
+
// const { engine } = await boot("#agent-app", {...});
|
|
5
|
+
// installCustomsGuard(engine);
|
|
6
|
+
//
|
|
7
|
+
// Every partner network the runtime loads is inspected after the visitor's consent
|
|
8
|
+
// card (so consent still comes before any contact) and before any of its agents run.
|
|
9
|
+
// The guard inspects the exact objects the runtime compiled, not a second download,
|
|
10
|
+
// so a partner cannot show one file to the inspector and run another.
|
|
11
|
+
//
|
|
12
|
+
// DENIED / INVALID -> refused. The calling agent gets an error that says why,
|
|
13
|
+
// and nothing from the partner runs (spec section 5, fail closed).
|
|
14
|
+
// HOLD (or changed -> the visitor sees the findings and decides. Approving pins
|
|
15
|
+
// since approved) the new version.
|
|
16
|
+
// CONDITIONS/CLEARED-> admitted; the first version seen is pinned.
|
|
17
|
+
//
|
|
18
|
+
// After every decision the visitor gets a notice (guard-notice.js): what was decided, the
|
|
19
|
+
// full list of findings and a link to the full report.
|
|
20
|
+
//
|
|
21
|
+
// It wraps engine._compile on this engine instance only. The runtime files are not
|
|
22
|
+
// edited, and uninstall() restores the original.
|
|
23
|
+
//
|
|
24
|
+
// It also puts on two locks that hold whatever a scam says (locks.js): a real card number
|
|
25
|
+
// or IBAN never goes to a partner, and after a partner answers, website actions that change
|
|
26
|
+
// something wait for the visitor's click.
|
|
27
|
+
|
|
28
|
+
import { analyzeNetwork } from "./analyze.js";
|
|
29
|
+
import { applyJudge, createEngineJudge } from "./judge.js";
|
|
30
|
+
import { installLocks } from "./locks.js";
|
|
31
|
+
import { PinStore, pinFromReport } from "./pins.js";
|
|
32
|
+
|
|
33
|
+
export const DEFAULT_POLICY = Object.freeze({
|
|
34
|
+
deny: ["DENIED", "INVALID"],
|
|
35
|
+
ask: ["HOLD"],
|
|
36
|
+
pinOnFirstUse: true,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export const STRICT_POLICY = Object.freeze({
|
|
40
|
+
deny: ["DENIED", "INVALID"],
|
|
41
|
+
ask: ["HOLD", "CONDITIONS", "CLEARED"],
|
|
42
|
+
pinOnFirstUse: false,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Install the guard on a runtime engine.
|
|
47
|
+
*
|
|
48
|
+
* :param engine: The Engine from the runtime's boot().
|
|
49
|
+
* :param options.policy: Override DEFAULT_POLICY fields.
|
|
50
|
+
* :param options.strict: Zero-trust mode: ask before every partner, never pin on first use,
|
|
51
|
+
* ignore trustedActions once a partner answers, block PII outbound and require
|
|
52
|
+
* declarative facts-only partner replies.
|
|
53
|
+
* :param options.store: A PinStore (defaults to this origin's localStorage).
|
|
54
|
+
* :param options.confirmEntry: async (report) => boolean, for held networks. Defaults to
|
|
55
|
+
* a built-in dialog in a browser and to "refuse" anywhere else.
|
|
56
|
+
* :param options.onReport: Called with every report, for logs and UI.
|
|
57
|
+
* :param options.notice: false shows the visitor no notice; a function (report) => boolean
|
|
58
|
+
* shows your own. By default, in a browser, a notice appears in the chat (guard-notice.js).
|
|
59
|
+
* :param options.reportPage: The Agent Customs page that "See the full report" opens, with
|
|
60
|
+
* the network's address filled in. Defaults to the public Agent Customs site.
|
|
61
|
+
* :param options.openReport: (url) => void. Shows the full report on this page instead.
|
|
62
|
+
* :param options.locks: false turns off the runtime locks and reply quarantine (locks.js).
|
|
63
|
+
* They are on by default.
|
|
64
|
+
* :param options.canary: false disables the synthetic Lock 3 canary tripwire (on by default).
|
|
65
|
+
* :param options.canaryToken: Optional fixed canary token for tests or interactive demos.
|
|
66
|
+
* :param options.onCanaryTrip: Called with {encoding, where, partners, revoked} when tripped.
|
|
67
|
+
* :param options.semanticJudge: The AI double-check, off by default so checks stay
|
|
68
|
+
* deterministic and use no tokens. true turns it on. A function () => boolean is asked
|
|
69
|
+
* before each check, so a switch on the page can turn it on and off. When on, the site's
|
|
70
|
+
* AI also reads partners the rules cleared, their MCP tool lists and their replies, and it
|
|
71
|
+
* can only make a verdict stricter.
|
|
72
|
+
* :param options.aiJudge: Your own judge, async ({kind, text, host}) => {checked, flagged,
|
|
73
|
+
* reason}, used in place of the site's AI. semanticJudge, when a function, still turns it
|
|
74
|
+
* on and off.
|
|
75
|
+
* :param options.trustedActions: Names of website actions that only change harmless things
|
|
76
|
+
* on the page, or ask the visitor themselves, so the action lock lets them run.
|
|
77
|
+
* :param options.onLock: Called with {lock, kind, where, reason} whenever a lock blocks
|
|
78
|
+
* something.
|
|
79
|
+
* :return: {store, log, policy, locks, uninstall()}
|
|
80
|
+
*/
|
|
81
|
+
export function installCustomsGuard(engine, options = {}) {
|
|
82
|
+
if (!engine || typeof engine._compile !== "function") {
|
|
83
|
+
throw new Error("Agent Customs: this runtime has no _compile step to guard.");
|
|
84
|
+
}
|
|
85
|
+
if (engine.__agentCustoms) return engine.__agentCustoms;
|
|
86
|
+
const strict = Boolean(options.strict);
|
|
87
|
+
const policy = { ...(strict ? STRICT_POLICY : DEFAULT_POLICY), ...(options.policy || {}) };
|
|
88
|
+
const store = options.store || new PinStore();
|
|
89
|
+
const log = [];
|
|
90
|
+
const partnerInfo = new Map();
|
|
91
|
+
const original = engine._compile;
|
|
92
|
+
const switchable = typeof options.semanticJudge === "function";
|
|
93
|
+
const judgeOn = switchable
|
|
94
|
+
? () => { try { return options.semanticJudge() === true; } catch { return false; } }
|
|
95
|
+
: () => true;
|
|
96
|
+
const baseJudge = typeof options.aiJudge === "function"
|
|
97
|
+
? options.aiJudge
|
|
98
|
+
: (options.semanticJudge === true || switchable ? createEngineJudge(engine) : null);
|
|
99
|
+
// Replies and tool lists are checked through this one, so turning the switch off stops
|
|
100
|
+
// every AI call at once.
|
|
101
|
+
const aiJudge = baseJudge
|
|
102
|
+
? async (request) => (judgeOn() ? baseJudge(request) : { checked: false, flagged: false })
|
|
103
|
+
: null;
|
|
104
|
+
|
|
105
|
+
let locks = null;
|
|
106
|
+
|
|
107
|
+
engine._compile = async function customsCompile(base, manifest, entry, opts = {}) {
|
|
108
|
+
const compiled = await original.call(this, base, manifest, entry, opts);
|
|
109
|
+
if (opts && opts.isTop) {
|
|
110
|
+
if (compiled?.network) engine.top = { ...(engine.top || {}), network: compiled.network, base, manifest };
|
|
111
|
+
locks?.syncTopCanary?.();
|
|
112
|
+
return compiled; // the site trusts its own network
|
|
113
|
+
}
|
|
114
|
+
const name = entry?.name || compiled?.network?.name || "";
|
|
115
|
+
const networkUrl = `${String(base).replace(/\/$/, "")}/${name}`;
|
|
116
|
+
const network = compiled.network;
|
|
117
|
+
let frontMan = null;
|
|
118
|
+
try { frontMan = network.findFrontMan(); } catch { frontMan = null; }
|
|
119
|
+
const pin = store.get(networkUrl);
|
|
120
|
+
let report = await analyzeNetwork({
|
|
121
|
+
networkUrl, manifest, entry, config: network.config, frontMan,
|
|
122
|
+
linkSlots: [...(network.linkSlots || [])],
|
|
123
|
+
validation: { errors: compiled.validation?.errors || [], warnings: compiled.validation?.warnings || [] },
|
|
124
|
+
pin,
|
|
125
|
+
});
|
|
126
|
+
// Layer 2, the AI double-check: when it is on and the rules cleared the partner, the
|
|
127
|
+
// isolated zero-tool judge reads it too. It can raise the verdict to Held, never lower it.
|
|
128
|
+
if (baseJudge && judgeOn()) {
|
|
129
|
+
report = await applyJudge(report, baseJudge, { config: network.config });
|
|
130
|
+
}
|
|
131
|
+
report.guard = { decision: "pending", pinned: false };
|
|
132
|
+
log.push(report);
|
|
133
|
+
partnerInfo.set(networkUrl, {
|
|
134
|
+
allowLanguage: report.findings.some((f) => f.rule === "C-LANGUAGE"),
|
|
135
|
+
});
|
|
136
|
+
// Each decision goes to the site's log and, in a notice, to the visitor.
|
|
137
|
+
const tell = async () => {
|
|
138
|
+
options.onReport?.(report);
|
|
139
|
+
return showNotice(report, options);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const code = report.verdict.code;
|
|
143
|
+
if (policy.deny.includes(code)) {
|
|
144
|
+
report.guard.decision = "refused";
|
|
145
|
+
throw refusal(report, false, await tell());
|
|
146
|
+
}
|
|
147
|
+
if (policy.ask.includes(code) || report.drift?.changed) {
|
|
148
|
+
const confirm = options.confirmEntry || defaultConfirm;
|
|
149
|
+
let approved = false;
|
|
150
|
+
try { approved = await confirm(report); } catch { approved = false; }
|
|
151
|
+
if (!approved) {
|
|
152
|
+
report.guard.decision = "declined";
|
|
153
|
+
throw refusal(report, true, await tell());
|
|
154
|
+
}
|
|
155
|
+
store.set(networkUrl, pinFromReport(report, "reviewed"));
|
|
156
|
+
report.guard.decision = "admitted after review";
|
|
157
|
+
report.guard.pinned = true;
|
|
158
|
+
} else {
|
|
159
|
+
if (!pin && policy.pinOnFirstUse) {
|
|
160
|
+
store.set(networkUrl, pinFromReport(report, "first-use"));
|
|
161
|
+
report.guard.pinned = true;
|
|
162
|
+
}
|
|
163
|
+
report.guard.decision = "admitted";
|
|
164
|
+
}
|
|
165
|
+
await tell();
|
|
166
|
+
return compiled;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
locks = options.locks === false ? null : installLocks(engine, {
|
|
170
|
+
trustedActions: options.trustedActions || [],
|
|
171
|
+
strict,
|
|
172
|
+
aiJudge,
|
|
173
|
+
store,
|
|
174
|
+
canary: options.canary,
|
|
175
|
+
canaryToken: options.canaryToken,
|
|
176
|
+
onCanaryTrip: (event) => {
|
|
177
|
+
const revoked = [];
|
|
178
|
+
for (const url of event.partners || []) {
|
|
179
|
+
if (store.delete(url)) revoked.push(url);
|
|
180
|
+
}
|
|
181
|
+
for (const r of log) {
|
|
182
|
+
if (r?.target?.url && (event.partners || []).includes(r.target.url)) {
|
|
183
|
+
r.guard.canaryTripped = true;
|
|
184
|
+
if (revoked.includes(r.target.url)) r.guard.pinned = false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
options.onCanaryTrip?.({ ...event, revoked });
|
|
188
|
+
},
|
|
189
|
+
isForeignPartner: (base, name) => Boolean(partnerInfo.get(`${String(base).replace(/\/$/, "")}/${name || ""}`)?.allowLanguage),
|
|
190
|
+
notify: (event) => {
|
|
191
|
+
options.onLock?.(event);
|
|
192
|
+
showLockNoticeSafe(event, options);
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const guard = {
|
|
197
|
+
store, log, policy, locks, canary: locks?.canary || null,
|
|
198
|
+
uninstall() {
|
|
199
|
+
engine._compile = original;
|
|
200
|
+
locks?.uninstall();
|
|
201
|
+
delete engine.__agentCustoms;
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
engine.__agentCustoms = guard;
|
|
205
|
+
return guard;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The error the calling agent receives. It names the verdict and our own rule
|
|
210
|
+
* titles only: quoted text from the refused network never reaches the model.
|
|
211
|
+
* It also says plainly what to tell the visitor, because a model that only reads
|
|
212
|
+
* "error" may describe the partner as if it had answered. When the visitor got a
|
|
213
|
+
* notice, it says so, so the agent can point to it.
|
|
214
|
+
*/
|
|
215
|
+
export function refusal(report, declinedByVisitor = false, noticeShown = false) {
|
|
216
|
+
const titles = [...new Set(report.findings.filter((f) => ["critical", "high"].includes(f.severity)).map((f) => f.title))].slice(0, 3);
|
|
217
|
+
const failed = report.conformance.checks.filter((c) => c.status === "fail").map((c) => c.title).slice(0, 2);
|
|
218
|
+
const reasons = titles.concat(failed).join("; ") || report.verdict.summary;
|
|
219
|
+
const who = `"${report.target.network}" from ${report.target.host}`;
|
|
220
|
+
const seen = noticeShown ? " The visitor sees the full list in a notice on the page." : "";
|
|
221
|
+
const message = declinedByVisitor
|
|
222
|
+
? `Agent Customs held ${who} for review and the visitor turned it away (${reasons}). Nothing from that network ran, so it gave no answer. Tell the visitor it was not used because they turned it away, and do not call it again in this turn.${seen}`
|
|
223
|
+
: `Agent Customs refused entry to ${who}: ${report.verdict.title.toLowerCase()} (${reasons}). Nothing from that network ran, so it gave no answer. Tell the visitor that Agent Customs blocked this partner's assistant for safety, and do not call it again in this turn.${seen}`;
|
|
224
|
+
const error = new Error(message);
|
|
225
|
+
error.code = "AGENT_CUSTOMS_REFUSED";
|
|
226
|
+
error.report = report;
|
|
227
|
+
return error;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function defaultConfirm(report) {
|
|
231
|
+
if (typeof document === "undefined" || typeof HTMLDialogElement === "undefined") return false;
|
|
232
|
+
const { confirmEntryDialog } = await import("./guard-ui.js");
|
|
233
|
+
return confirmEntryDialog(report);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Show the visitor what the guard decided. A notice never changes a decision.
|
|
238
|
+
*
|
|
239
|
+
* :return: true when a notice is on the page.
|
|
240
|
+
*/
|
|
241
|
+
async function showLockNoticeSafe(event, options) {
|
|
242
|
+
if (options.notice === false || typeof options.notice === "function" || typeof document === "undefined") return;
|
|
243
|
+
try {
|
|
244
|
+
const { showLockNotice } = await import("./guard-notice.js");
|
|
245
|
+
showLockNotice(event);
|
|
246
|
+
} catch { /* a notice never changes a decision */ }
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function showNotice(report, options) {
|
|
250
|
+
if (options.notice === false) return false;
|
|
251
|
+
try {
|
|
252
|
+
if (typeof options.notice === "function") return Boolean(await options.notice(report));
|
|
253
|
+
if (typeof document === "undefined") return false;
|
|
254
|
+
const { showGuardNotice } = await import("./guard-notice.js");
|
|
255
|
+
return showGuardNotice(report, { reportPage: options.reportPage, openReport: options.openReport });
|
|
256
|
+
} catch {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
}
|