agent-customs 0.0.0-stage → 1.0.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.
Files changed (48) hide show
  1. package/LICENSE.md +341 -0
  2. package/README.md +592 -2
  3. package/bin/agent-customs.mjs +194 -0
  4. package/package.json +77 -4
  5. package/site/.well-known/agent-customs-key.json +13 -0
  6. package/site/certificate.js +944 -0
  7. package/site/customs/analyze.js +1224 -0
  8. package/site/customs/auto.js +44 -0
  9. package/site/customs/canary.js +270 -0
  10. package/site/customs/canonical.js +123 -0
  11. package/site/customs/capabilities.js +387 -0
  12. package/site/customs/diff.js +236 -0
  13. package/site/customs/english-words.js +153 -0
  14. package/site/customs/guard-notice.js +226 -0
  15. package/site/customs/guard-ui.js +105 -0
  16. package/site/customs/guard.js +259 -0
  17. package/site/customs/inspect.js +446 -0
  18. package/site/customs/judge.js +208 -0
  19. package/site/customs/languages.js +826 -0
  20. package/site/customs/locks.js +967 -0
  21. package/site/customs/pins.js +119 -0
  22. package/site/customs/protocols.js +621 -0
  23. package/site/customs/receipts.js +154 -0
  24. package/site/customs/rules.js +1470 -0
  25. package/site/customs/unicode.js +361 -0
  26. package/site/runtime/LICENSE.txt +176 -0
  27. package/site/runtime/generated/graph.gen.mjs +83 -0
  28. package/site/runtime/generated/validation.gen.mjs +70 -0
  29. package/site/runtime/hocon.js +262 -0
  30. package/site/runtime/kernel.js +237 -0
  31. package/site/runtime/model.js +44 -0
  32. package/tests/fixtures/official/NOTICE +3 -0
  33. package/tests/fixtures/official/guesthouse_agentified/agent-manifest.json +86 -0
  34. package/tests/fixtures/official/guesthouse_agentified/links.json +6 -0
  35. package/tests/fixtures/official/guesthouse_agentified/tidewhistle_guesthouse.network.json +56 -0
  36. package/tests/fixtures/official/harbor_tours_agentified/agent-manifest.json +86 -0
  37. package/tests/fixtures/official/harbor_tours_agentified/links.json +6 -0
  38. package/tests/fixtures/official/harbor_tours_agentified/marrow_point_harbor_tours.network.json +56 -0
  39. package/tests/fixtures/official/pizza_restaurant_agentified/agent-manifest.json +78 -0
  40. package/tests/fixtures/official/pizza_restaurant_agentified/links.json +6 -0
  41. package/tests/fixtures/official/pizza_restaurant_agentified/pizza_restaurant_website_assistant.network.json +156 -0
  42. package/tools/bench.mjs +731 -0
  43. package/tools/external-heldout.mjs +337 -0
  44. package/tools/fs-fetch.mjs +19 -0
  45. package/tools/heldout-corpus.mjs +135 -0
  46. package/tools/inspect-local.mjs +88 -0
  47. package/tools/round2-corpus.mjs +395 -0
  48. package/tools/round3-corpus.mjs +192 -0
@@ -0,0 +1,154 @@
1
+ // receipts.js: signed inspection receipts, Agent Customs' stamp in a form anyone can check.
2
+ //
3
+ // A receipt says: Agent Customs inspected this network on this date, its fingerprint was
4
+ // this, and the verdict was this. It is signed with Agent Customs' private key (ECDSA P-256,
5
+ // "ES256"), so nobody can change the verdict, the date or the fingerprint without the
6
+ // signature failing. The public key is published at .well-known/agent-customs-key.json.
7
+ //
8
+ // A receipt covers one exact version. If the network changes, its fingerprint no longer
9
+ // matches, and the stamp no longer applies to it. Receipts expire after RECEIPT_DAYS, so an
10
+ // old stamp can't stand in for a fresh check.
11
+ //
12
+ // Every word in a receipt is Agent Customs' own: verdicts, rule titles and counts. Nothing
13
+ // written by the inspected network is copied into it.
14
+
15
+ import { canonicalJson } from "./canonical.js";
16
+
17
+ export const RECEIPT_TYPE = "agent-customs-receipt";
18
+ export const RECEIPT_DAYS = 35;
19
+
20
+ const KEY_ALGORITHM = { name: "ECDSA", namedCurve: "P-256" };
21
+ const SIGN_ALGORITHM = { name: "ECDSA", hash: "SHA-256" };
22
+
23
+ function subtle() {
24
+ const s = globalThis.crypto?.subtle;
25
+ if (!s) throw new Error("This browser cannot check signatures (Web Crypto is missing).");
26
+ return s;
27
+ }
28
+
29
+ const utf8 = (text) => new TextEncoder().encode(text);
30
+
31
+ function toBase64Url(bytes) {
32
+ let binary = "";
33
+ for (const b of bytes) binary += String.fromCharCode(b);
34
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
35
+ }
36
+
37
+ function fromBase64Url(text) {
38
+ const base64 = String(text).replace(/-/g, "+").replace(/_/g, "/");
39
+ const binary = atob(base64 + "=".repeat((4 - (base64.length % 4)) % 4));
40
+ return Uint8Array.from(binary, (c) => c.charCodeAt(0));
41
+ }
42
+
43
+ /**
44
+ * A short, stable name for a public key: the start of the SHA-256 of its JWK.
45
+ *
46
+ * :param publicJwk: The public key as a JWK.
47
+ * :return: The key id.
48
+ */
49
+ export async function keyIdOf(publicJwk) {
50
+ const { crv, kty, x, y } = publicJwk;
51
+ const digest = await subtle().digest("SHA-256", utf8(canonicalJson({ crv, kty, x, y })));
52
+ return toBase64Url(new Uint8Array(digest).slice(0, 12));
53
+ }
54
+
55
+ /**
56
+ * Make a new signing key pair.
57
+ *
58
+ * :return: {privateJwk, publicJwk, keyId}
59
+ */
60
+ export async function makeSigningKey() {
61
+ const pair = await subtle().generateKey(KEY_ALGORITHM, true, ["sign", "verify"]);
62
+ const privateJwk = await subtle().exportKey("jwk", pair.privateKey);
63
+ const { crv, kty, x, y } = await subtle().exportKey("jwk", pair.publicKey);
64
+ const publicJwk = { crv, kty, x, y };
65
+ return { privateJwk, publicJwk, keyId: await keyIdOf(publicJwk) };
66
+ }
67
+
68
+ /**
69
+ * The receipt for one inspection report, before it is signed.
70
+ *
71
+ * :param report: A report from analyzeNetwork or inspectNetworkUrl.
72
+ * :param options.keyId: The id of the key that will sign it.
73
+ * :param options.site: The Agent Customs site that publishes it.
74
+ * :param options.now: The time of signing.
75
+ * :return: The receipt.
76
+ */
77
+ export function receiptFromReport(report, { keyId, site, now = new Date() } = {}) {
78
+ const expires = new Date(now.getTime() + RECEIPT_DAYS * 86400000);
79
+ return {
80
+ type: RECEIPT_TYPE,
81
+ version: 1,
82
+ network: report.target.url,
83
+ fingerprint: report.identity?.hash || null,
84
+ verdict: report.verdict.code,
85
+ stamp: report.verdict.stamp,
86
+ summary: report.verdict.summary,
87
+ counts: report.counts || {},
88
+ findings: (report.findings || []).map((f) => ({ rule: f.rule, severity: f.severity, title: f.title })),
89
+ conformance: { passed: report.conformance?.passed ?? 0, total: report.conformance?.total ?? 0 },
90
+ inspectedAt: now.toISOString(),
91
+ expiresAt: expires.toISOString(),
92
+ inspector: { name: "Agent Customs", version: report.version || "", site: site || "", keyId: keyId || "" },
93
+ };
94
+ }
95
+
96
+ /**
97
+ * Sign a receipt.
98
+ *
99
+ * :param receipt: From receiptFromReport.
100
+ * :param privateJwk: The private key as a JWK.
101
+ * :return: {receipt, signature: {alg, keyId, value}}
102
+ */
103
+ export async function signReceipt(receipt, privateJwk) {
104
+ const key = await subtle().importKey("jwk", privateJwk, KEY_ALGORITHM, false, ["sign"]);
105
+ const signature = await subtle().sign(SIGN_ALGORITHM, key, utf8(canonicalJson(receipt)));
106
+ return { receipt, signature: { alg: "ES256", keyId: receipt.inspector.keyId, value: toBase64Url(new Uint8Array(signature)) } };
107
+ }
108
+
109
+ /**
110
+ * Check a signed receipt.
111
+ *
112
+ * :param signed: {receipt, signature}.
113
+ * :param publicJwk: Agent Customs' public key as a JWK.
114
+ * :param options.now: The time to check expiry against.
115
+ * :param options.fingerprint: The network's fingerprint now, to see whether the stamp applies.
116
+ * :return: {valid, reason, expired, matches, receipt}. valid means the signature is right;
117
+ * matches is true, false, or null when no fingerprint was given.
118
+ */
119
+ export async function verifyReceipt(signed, publicJwk, { now = new Date(), fingerprint = null } = {}) {
120
+ const receipt = signed?.receipt;
121
+ const fail = (reason) => ({ valid: false, reason, expired: false, matches: null, receipt: receipt || null });
122
+ if (!receipt || receipt.type !== RECEIPT_TYPE || receipt.version !== 1) return fail("This is not an Agent Customs receipt.");
123
+ if (signed.signature?.alg !== "ES256" || typeof signed.signature.value !== "string") return fail("The receipt has no signature.");
124
+ const keyId = await keyIdOf(publicJwk);
125
+ if (signed.signature.keyId !== keyId || receipt.inspector?.keyId !== keyId) return fail("The receipt was signed with another key.");
126
+ let ok = false;
127
+ try {
128
+ const key = await subtle().importKey("jwk", { ...publicJwk, ext: true }, KEY_ALGORITHM, false, ["verify"]);
129
+ ok = await subtle().verify(SIGN_ALGORITHM, key, fromBase64Url(signed.signature.value), utf8(canonicalJson(receipt)));
130
+ } catch {
131
+ ok = false;
132
+ }
133
+ if (!ok) return fail("The signature does not match: the receipt was changed after it was signed.");
134
+ return {
135
+ valid: true,
136
+ reason: "",
137
+ expired: now.getTime() > Date.parse(receipt.expiresAt),
138
+ matches: fingerprint ? receipt.fingerprint === fingerprint : null,
139
+ receipt,
140
+ };
141
+ }
142
+
143
+ /**
144
+ * Where a network's receipt is published, relative to the receipts folder:
145
+ * https://shop.example/agents/bakery becomes shop.example/agents/bakery.json.
146
+ *
147
+ * :param networkUrl: The network's address.
148
+ * :return: The relative path.
149
+ */
150
+ export function receiptPath(networkUrl) {
151
+ const u = new URL(networkUrl);
152
+ const parts = u.pathname.split("/").filter(Boolean).map((p) => p.replace(/[^\w.-]/g, "_"));
153
+ return `${u.host.replace(/[^\w.-]/g, "_")}/${parts.join("/") || "index"}.json`;
154
+ }