owenloop 0.4.1 → 0.5.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.
Files changed (282) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/README.md +182 -53
  3. package/bin/owenloop.mjs +50 -2
  4. package/dist/packages/work/src/agent/brief.d.ts +176 -0
  5. package/dist/packages/work/src/agent/brief.js +197 -0
  6. package/dist/packages/work/src/agent/loop.d.ts +141 -0
  7. package/dist/packages/work/src/agent/loop.js +669 -0
  8. package/dist/packages/work/src/agent/model-policy.d.ts +49 -0
  9. package/dist/packages/work/src/agent/model-policy.js +138 -0
  10. package/dist/packages/work/src/agent/workdir.d.ts +210 -0
  11. package/dist/packages/work/src/agent/workdir.js +319 -0
  12. package/dist/packages/work/src/bundle/cache.d.ts +92 -0
  13. package/dist/packages/work/src/bundle/cache.js +338 -0
  14. package/dist/packages/work/src/bundle/fetch.d.ts +87 -0
  15. package/dist/packages/work/src/bundle/fetch.js +345 -0
  16. package/dist/packages/work/src/bundle/types.d.ts +164 -0
  17. package/dist/packages/work/src/bundle/types.js +1 -0
  18. package/dist/packages/work/src/consumed-verifier.d.ts +45 -0
  19. package/dist/packages/work/src/consumed-verifier.js +205 -0
  20. package/dist/packages/work/src/credentials/owenloop.d.ts +11 -0
  21. package/dist/packages/work/src/credentials/owenloop.js +28 -0
  22. package/dist/packages/work/src/credentials/reader.d.ts +36 -0
  23. package/dist/packages/work/src/credentials/reader.js +32 -0
  24. package/dist/packages/work/src/credentials/resolve.d.ts +19 -0
  25. package/dist/packages/work/src/credentials/resolve.js +56 -0
  26. package/dist/packages/work/src/exec/instructions.d.ts +99 -0
  27. package/dist/packages/work/src/exec/instructions.js +323 -0
  28. package/dist/packages/work/src/exec/loop.d.ts +40 -0
  29. package/dist/packages/work/src/exec/loop.js +332 -0
  30. package/dist/packages/work/src/exec/payload.d.ts +29 -0
  31. package/dist/packages/work/src/exec/payload.js +54 -0
  32. package/dist/packages/work/src/exec/receipt.d.ts +41 -0
  33. package/dist/packages/work/src/exec/receipt.js +32 -0
  34. package/dist/packages/work/src/exec/runner.d.ts +71 -0
  35. package/dist/packages/work/src/exec/runner.js +256 -0
  36. package/dist/packages/work/src/harness/child-env.d.ts +100 -0
  37. package/dist/packages/work/src/harness/child-env.js +134 -0
  38. package/dist/packages/work/src/harness/claude.d.ts +140 -0
  39. package/dist/packages/work/src/harness/claude.js +837 -0
  40. package/dist/packages/work/src/harness/codex.d.ts +111 -0
  41. package/dist/packages/work/src/harness/codex.js +1024 -0
  42. package/dist/packages/work/src/harness/contract.d.ts +308 -0
  43. package/dist/packages/work/src/harness/contract.js +32 -0
  44. package/dist/packages/work/src/harness/fake.d.ts +59 -0
  45. package/dist/packages/work/src/harness/fake.js +91 -0
  46. package/dist/packages/work/src/harness/jsonrpc-stdio.d.ts +108 -0
  47. package/dist/packages/work/src/harness/jsonrpc-stdio.js +351 -0
  48. package/dist/packages/work/src/harness/permissions.d.ts +75 -0
  49. package/dist/packages/work/src/harness/permissions.js +182 -0
  50. package/dist/packages/work/src/harness/registry.d.ts +47 -0
  51. package/dist/packages/work/src/harness/registry.js +45 -0
  52. package/dist/packages/work/src/harness/session-store.d.ts +181 -0
  53. package/dist/packages/work/src/harness/session-store.js +523 -0
  54. package/dist/packages/work/src/harness/types.d.ts +29 -0
  55. package/dist/packages/work/src/harness/types.js +14 -0
  56. package/dist/packages/work/src/harnesses.d.ts +38 -0
  57. package/dist/packages/work/src/harnesses.js +38 -0
  58. package/dist/packages/work/src/hold/loop.d.ts +36 -0
  59. package/dist/packages/work/src/hold/loop.js +23 -0
  60. package/dist/packages/work/src/hold/mcp.d.ts +75 -0
  61. package/dist/packages/work/src/hold/mcp.js +264 -0
  62. package/dist/packages/work/src/hub/client.d.ts +23 -0
  63. package/dist/packages/work/src/hub/client.js +91 -0
  64. package/dist/packages/work/src/hub/types.d.ts +326 -0
  65. package/dist/packages/work/src/hub/types.js +26 -0
  66. package/dist/packages/work/src/lease/loop.d.ts +94 -0
  67. package/dist/packages/work/src/lease/loop.js +277 -0
  68. package/dist/packages/work/src/main.d.ts +1 -0
  69. package/dist/packages/work/src/main.js +48 -0
  70. package/dist/packages/work/src/mcp/server.d.ts +125 -0
  71. package/dist/packages/work/src/mcp/server.js +320 -0
  72. package/dist/packages/work/src/owenloop-bin.d.ts +1 -0
  73. package/dist/packages/work/src/owenloop-bin.js +18 -0
  74. package/dist/packages/work/src/roles/agent-run.d.ts +124 -0
  75. package/dist/packages/work/src/roles/agent-run.js +529 -0
  76. package/dist/packages/work/src/roles/exec.d.ts +48 -0
  77. package/dist/packages/work/src/roles/exec.js +228 -0
  78. package/dist/packages/work/src/roles/hold.d.ts +72 -0
  79. package/dist/packages/work/src/roles/hold.js +363 -0
  80. package/dist/packages/work/src/roles/join.d.ts +18 -0
  81. package/dist/packages/work/src/roles/join.js +204 -0
  82. package/dist/packages/work/src/roles/lint.d.ts +58 -0
  83. package/dist/packages/work/src/roles/lint.js +234 -0
  84. package/dist/packages/work/src/roles/prepare.d.ts +1 -0
  85. package/dist/packages/work/src/roles/prepare.js +249 -0
  86. package/dist/packages/work/src/roles/release.d.ts +48 -0
  87. package/dist/packages/work/src/roles/release.js +137 -0
  88. package/dist/packages/work/src/roles/sessions.d.ts +61 -0
  89. package/dist/packages/work/src/roles/sessions.js +180 -0
  90. package/dist/packages/work/src/roles/settings.d.ts +6 -0
  91. package/dist/packages/work/src/roles/settings.js +71 -0
  92. package/dist/packages/work/src/roles/shift.d.ts +13 -0
  93. package/dist/packages/work/src/roles/shift.js +250 -0
  94. package/dist/packages/work/src/roles/signals.d.ts +55 -0
  95. package/dist/packages/work/src/roles/signals.js +62 -0
  96. package/dist/packages/work/src/settings/provision.d.ts +21 -0
  97. package/dist/packages/work/src/settings/provision.js +47 -0
  98. package/dist/packages/work/src/settings/settings.d.ts +136 -0
  99. package/dist/packages/work/src/settings/settings.js +155 -0
  100. package/dist/packages/work/src/shift/client.d.ts +24 -0
  101. package/dist/packages/work/src/shift/client.js +124 -0
  102. package/dist/packages/work/src/shift/loop.d.ts +131 -0
  103. package/dist/packages/work/src/shift/loop.js +751 -0
  104. package/dist/packages/work/src/shift/protocol.d.ts +95 -0
  105. package/dist/packages/work/src/shift/protocol.js +24 -0
  106. package/dist/packages/work/src/shift/routing.d.ts +35 -0
  107. package/dist/packages/work/src/shift/routing.js +50 -0
  108. package/dist/packages/work/src/shift/runtime.d.ts +45 -0
  109. package/dist/packages/work/src/shift/runtime.js +337 -0
  110. package/dist/packages/work/src/shift/server.d.ts +23 -0
  111. package/dist/packages/work/src/shift/server.js +716 -0
  112. package/dist/packages/work/src/shift/spawn.d.ts +109 -0
  113. package/dist/packages/work/src/shift/spawn.js +158 -0
  114. package/dist/packages/work/src/shift/state.d.ts +79 -0
  115. package/dist/packages/work/src/shift/state.js +385 -0
  116. package/dist/packages/work/src/submit-proof.d.ts +36 -0
  117. package/dist/packages/work/src/submit-proof.js +93 -0
  118. package/dist/packages/work/src/usage.d.ts +6 -0
  119. package/dist/packages/work/src/usage.js +175 -0
  120. package/dist/src/add.d.ts +189 -0
  121. package/dist/src/add.js +300 -0
  122. package/dist/src/archive.d.ts +97 -0
  123. package/dist/src/archive.js +591 -0
  124. package/dist/src/bundle/index.d.ts +64 -0
  125. package/dist/src/bundle/index.js +508 -0
  126. package/dist/src/bundle/manifest.d.ts +50 -0
  127. package/dist/src/bundle/manifest.js +435 -0
  128. package/dist/src/bundle/runtime.d.ts +26 -0
  129. package/dist/src/bundle/runtime.js +64 -0
  130. package/dist/src/bundle/tar.d.ts +62 -0
  131. package/dist/src/bundle/tar.js +238 -0
  132. package/dist/src/bundle/types.d.ts +105 -0
  133. package/dist/src/bundle/types.js +21 -0
  134. package/dist/{cli.d.ts → src/cli.d.ts} +92 -7
  135. package/dist/src/cli.js +5968 -0
  136. package/dist/{credentials.d.ts → src/credentials.d.ts} +22 -15
  137. package/dist/{credentials.js → src/credentials.js} +35 -31
  138. package/dist/src/crypto/allowed-signers.d.ts +68 -0
  139. package/dist/src/crypto/allowed-signers.js +307 -0
  140. package/dist/src/crypto/canonical.d.ts +8 -0
  141. package/dist/src/crypto/canonical.js +92 -0
  142. package/dist/src/crypto/chain.d.ts +65 -0
  143. package/dist/src/crypto/chain.js +377 -0
  144. package/dist/src/crypto/dsse.d.ts +209 -0
  145. package/dist/src/crypto/dsse.js +282 -0
  146. package/dist/src/crypto/enrollment.d.ts +78 -0
  147. package/dist/src/crypto/enrollment.js +186 -0
  148. package/dist/src/crypto/index.d.ts +48 -0
  149. package/dist/src/crypto/index.js +44 -0
  150. package/dist/src/crypto/keys.d.ts +258 -0
  151. package/dist/src/crypto/keys.js +908 -0
  152. package/dist/src/crypto/org-root.d.ts +33 -0
  153. package/dist/src/crypto/org-root.js +78 -0
  154. package/dist/src/crypto/origin-rules.d.ts +56 -0
  155. package/dist/src/crypto/origin-rules.js +143 -0
  156. package/dist/src/crypto/policy-floor.d.ts +98 -0
  157. package/dist/src/crypto/policy-floor.js +353 -0
  158. package/dist/src/crypto/records.d.ts +316 -0
  159. package/dist/src/crypto/records.js +157 -0
  160. package/dist/src/crypto/scope.d.ts +49 -0
  161. package/dist/src/crypto/scope.js +83 -0
  162. package/dist/src/crypto/ssh.d.ts +171 -0
  163. package/dist/src/crypto/ssh.js +396 -0
  164. package/dist/src/crypto/submission.d.ts +33 -0
  165. package/dist/src/crypto/submission.js +47 -0
  166. package/dist/src/crypto/trust-roots.d.ts +29 -0
  167. package/dist/src/crypto/trust-roots.js +39 -0
  168. package/dist/src/crypto/verify-consumed.d.ts +67 -0
  169. package/dist/src/crypto/verify-consumed.js +259 -0
  170. package/dist/src/crypto/verify-origin.d.ts +50 -0
  171. package/dist/src/crypto/verify-origin.js +151 -0
  172. package/dist/src/crypto/verify-publication.d.ts +60 -0
  173. package/dist/src/crypto/verify-publication.js +150 -0
  174. package/dist/{defs.d.ts → src/defs.d.ts} +24 -1
  175. package/dist/{defs.js → src/defs.js} +181 -61
  176. package/dist/{engine.d.ts → src/engine.d.ts} +105 -9
  177. package/dist/{engine.js → src/engine.js} +251 -47
  178. package/dist/{factory.d.ts → src/factory.d.ts} +16 -0
  179. package/dist/{factory.js → src/factory.js} +16 -4
  180. package/dist/src/global-config.d.ts +59 -0
  181. package/dist/src/global-config.js +84 -0
  182. package/dist/{hub.d.ts → src/hub.d.ts} +280 -31
  183. package/dist/{hub.js → src/hub.js} +493 -52
  184. package/dist/src/index.d.ts +47 -0
  185. package/dist/src/index.js +41 -0
  186. package/dist/src/install.d.ts +538 -0
  187. package/dist/src/install.js +1981 -0
  188. package/dist/src/lock.d.ts +38 -0
  189. package/dist/src/lock.js +325 -0
  190. package/dist/src/mcp/serve.d.ts +109 -0
  191. package/dist/{mcp → src/mcp}/serve.js +335 -75
  192. package/dist/{mcp → src/mcp}/server.d.ts +6 -9
  193. package/dist/{mcp → src/mcp}/server.js +18 -10
  194. package/dist/{model.d.ts → src/model.d.ts} +15 -0
  195. package/dist/{model.js → src/model.js} +112 -2
  196. package/dist/src/order-resolver.d.ts +184 -0
  197. package/dist/src/order-resolver.js +252 -0
  198. package/dist/src/package-version.d.ts +10 -0
  199. package/dist/src/package-version.js +42 -0
  200. package/dist/{paths.d.ts → src/paths.d.ts} +14 -1
  201. package/dist/{paths.js → src/paths.js} +18 -0
  202. package/dist/src/schemas/enrollment-grant.v1.schema.json +90 -0
  203. package/dist/src/schemas/index.d.ts +1416 -0
  204. package/dist/src/schemas/index.js +31 -0
  205. package/dist/src/schemas/order.v1.schema.json +106 -0
  206. package/dist/src/schemas/origin.v1.schema.json +68 -0
  207. package/dist/src/schemas/policy-floor.v1.schema.json +29 -0
  208. package/dist/src/schemas/publication.v1.schema.json +31 -0
  209. package/dist/src/schemas/revocation.v1.schema.json +39 -0
  210. package/dist/src/schemas/submission.v1.schema.json +61 -0
  211. package/dist/src/store/def-source.d.ts +52 -0
  212. package/dist/src/store/def-source.js +335 -0
  213. package/dist/src/store/index-file.d.ts +69 -0
  214. package/dist/src/store/index-file.js +151 -0
  215. package/dist/src/store/index.d.ts +27 -0
  216. package/dist/src/store/index.js +22 -0
  217. package/dist/src/store/ingestor.d.ts +43 -0
  218. package/dist/src/store/ingestor.js +211 -0
  219. package/dist/src/store/install.d.ts +228 -0
  220. package/dist/src/store/install.js +555 -0
  221. package/dist/src/store/instruction-source.d.ts +49 -0
  222. package/dist/src/store/instruction-source.js +298 -0
  223. package/dist/src/store/origin-evidence.d.ts +21 -0
  224. package/dist/src/store/origin-evidence.js +66 -0
  225. package/dist/src/store/pre-commit-verifier.d.ts +68 -0
  226. package/dist/src/store/pre-commit-verifier.js +418 -0
  227. package/dist/src/store/resolve.d.ts +138 -0
  228. package/dist/src/store/resolve.js +363 -0
  229. package/dist/src/store/types.d.ts +288 -0
  230. package/dist/src/store/types.js +356 -0
  231. package/dist/{types.d.ts → src/types.d.ts} +58 -24
  232. package/dist/{untar.d.ts → src/untar.d.ts} +8 -23
  233. package/dist/src/untar.js +38 -0
  234. package/dist/{util.d.ts → src/util.d.ts} +3 -2
  235. package/dist/{util.js → src/util.js} +3 -2
  236. package/dist/{owenwork.d.ts → src/work-settings.d.ts} +22 -22
  237. package/dist/{owenwork.js → src/work-settings.js} +27 -27
  238. package/docs/agent-runner.md +86 -0
  239. package/docs/authoring.md +150 -59
  240. package/docs/bundles.md +250 -0
  241. package/docs/cli.md +1758 -163
  242. package/docs/crypto.md +816 -0
  243. package/docs/design.md +374 -52
  244. package/docs/embedding.md +135 -27
  245. package/docs/wire-contracts.md +380 -0
  246. package/examples/workflows/{command-worker.yaml → command-executor.yaml} +15 -15
  247. package/examples/workflows/compile-dev-playbook.yaml +338 -0
  248. package/examples/workflows/judges/gate-sharpness.md +39 -0
  249. package/examples/workflows/judges/security.md +34 -0
  250. package/package.json +22 -8
  251. package/plugins/claude-code/.claude-plugin/marketplace.json +14 -0
  252. package/plugins/claude-code/plugin/.claude-plugin/plugin.json +8 -0
  253. package/plugins/claude-code/plugin/.mcp.json +14 -0
  254. package/plugins/claude-code/plugin/hooks/hooks.json +26 -0
  255. package/plugins/claude-code/plugin/hooks/session-end.sh +40 -0
  256. package/plugins/claude-code/plugin/hooks/session-start.sh +52 -0
  257. package/plugins/claude-code/plugin/skills/author/SKILL.md +181 -0
  258. package/plugins/claude-code/plugin/skills/conduct/SKILL.md +106 -0
  259. package/plugins/claude-code/plugin/skills/shift/SKILL.md +105 -0
  260. package/plugins/codex/.agents/plugins/marketplace.json +21 -0
  261. package/plugins/codex/plugins/owenloop/.codex-plugin/plugin.json +7 -0
  262. package/plugins/codex/plugins/owenloop/.mcp.json +20 -0
  263. package/plugins/codex/plugins/owenloop/hooks/hooks.json +26 -0
  264. package/plugins/codex/plugins/owenloop/hooks/session-end.sh +40 -0
  265. package/plugins/codex/plugins/owenloop/hooks/session-start.sh +52 -0
  266. package/plugins/codex/plugins/owenloop/skills/author/SKILL.md +181 -0
  267. package/plugins/codex/plugins/owenloop/skills/conduct/SKILL.md +106 -0
  268. package/plugins/codex/plugins/owenloop/skills/shift/SKILL.md +105 -0
  269. package/dist/add.d.ts +0 -384
  270. package/dist/add.js +0 -834
  271. package/dist/cli.js +0 -3270
  272. package/dist/index.d.ts +0 -32
  273. package/dist/index.js +0 -23
  274. package/dist/lock.d.ts +0 -80
  275. package/dist/lock.js +0 -217
  276. package/dist/mcp/serve.d.ts +0 -84
  277. package/dist/untar.js +0 -164
  278. /package/dist/{schema.d.ts → src/schema.d.ts} +0 -0
  279. /package/dist/{schema.js → src/schema.js} +0 -0
  280. /package/dist/{store.d.ts → src/store.d.ts} +0 -0
  281. /package/dist/{store.js → src/store.js} +0 -0
  282. /package/dist/{types.js → src/types.js} +0 -0
@@ -0,0 +1,92 @@
1
+ import { createHash } from 'node:crypto';
2
+ /**
3
+ * Serialize a JSON value deterministically for value-level trust records.
4
+ * Object keys are sorted recursively; array order is preserved. The output is
5
+ * separator-tight JSON encoded as UTF-8 bytes.
6
+ */
7
+ export function canonicalValueBytes(value) {
8
+ const json = canonicalValueJson(value);
9
+ if (json === undefined)
10
+ throw new TypeError('cannot canonicalize a value that serializes to undefined');
11
+ return new TextEncoder().encode(json);
12
+ }
13
+ /** Compute the lowercase SHA-256 digest of a canonical JSON value. */
14
+ export function valueDigestHex(value) {
15
+ return createHash('sha256').update(canonicalValueBytes(value)).digest('hex');
16
+ }
17
+ /**
18
+ * Apply the same `toJSON(key)` hook that JSON.stringify applies before a value
19
+ * crosses the submit transport. Objects without JSON-compatible prototypes are
20
+ * rejected instead of being treated as empty records.
21
+ */
22
+ function canonicalValueJson(value, key = '', stack = new Set(), position = 'root') {
23
+ if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
24
+ const toJSON = value.toJSON;
25
+ if (typeof toJSON === 'function') {
26
+ if (stack.has(value))
27
+ throw new TypeError('cannot canonicalize a circular value');
28
+ stack.add(value);
29
+ try {
30
+ const replacement = Reflect.apply(toJSON, value, [key]);
31
+ return canonicalValueJson(replacement, key, stack, position);
32
+ }
33
+ finally {
34
+ stack.delete(value);
35
+ }
36
+ }
37
+ }
38
+ if (value === null)
39
+ return 'null';
40
+ switch (typeof value) {
41
+ case 'string':
42
+ return JSON.stringify(value);
43
+ case 'boolean':
44
+ return value ? 'true' : 'false';
45
+ case 'number': {
46
+ if (!Number.isFinite(value))
47
+ throw new TypeError('cannot canonicalize a non-finite number');
48
+ return JSON.stringify(value);
49
+ }
50
+ case 'undefined':
51
+ case 'function':
52
+ case 'symbol':
53
+ if (position === 'object')
54
+ return undefined;
55
+ if (position === 'array')
56
+ return 'null';
57
+ throw new TypeError(`cannot canonicalize ${typeof value}`);
58
+ case 'bigint':
59
+ throw new TypeError('cannot canonicalize bigint');
60
+ case 'object':
61
+ break;
62
+ default:
63
+ throw new TypeError(`cannot canonicalize ${typeof value}`);
64
+ }
65
+ if (stack.has(value))
66
+ throw new TypeError('cannot canonicalize a circular value');
67
+ stack.add(value);
68
+ try {
69
+ if (Array.isArray(value)) {
70
+ const entries = [];
71
+ for (let index = 0; index < value.length; index += 1) {
72
+ entries.push(canonicalValueJson(value[index], String(index), stack, 'array') ?? 'null');
73
+ }
74
+ return `[${entries.join(',')}]`;
75
+ }
76
+ const prototype = Object.getPrototypeOf(value);
77
+ if (prototype !== Object.prototype && prototype !== null) {
78
+ throw new TypeError('cannot canonicalize a non-plain object without toJSON');
79
+ }
80
+ const object = value;
81
+ const entries = [];
82
+ for (const property of Object.keys(object).sort()) {
83
+ const serialized = canonicalValueJson(object[property], property, stack, 'object');
84
+ if (serialized !== undefined)
85
+ entries.push(`${JSON.stringify(property)}:${serialized}`);
86
+ }
87
+ return `{${entries.join(',')}}`;
88
+ }
89
+ finally {
90
+ stack.delete(value);
91
+ }
92
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Enrollment-chain verification for locally anchored principal keys.
3
+ *
4
+ * The validator consumes raw DSSE envelopes and an explicit validation instant.
5
+ * Each grant link is verified with a signer whose allowed-signers text contains
6
+ * exactly that link's parent key, so a remote roster assertion cannot substitute
7
+ * for the local org-root anchor. Scope attenuation and revocation authority are
8
+ * checked after schema decoding and cryptographic verification. This module is
9
+ * deliberately pure: it performs no filesystem or network I/O; org-root.ts
10
+ * supplies the optional on-disk loader for callers that need one.
11
+ */
12
+ import type { Signer } from './ssh.ts';
13
+ import type { GrantScope, PrincipalReference, RevocationRecord } from './records.ts';
14
+ /** Inputs to the pure enrollment-chain validator. */
15
+ export interface ChainInput {
16
+ targetKeyId: string;
17
+ orgRootPublicKey: string;
18
+ grants: Uint8Array[];
19
+ revocations?: Uint8Array[];
20
+ at: number;
21
+ }
22
+ /** Injectable cryptographic and policy options for chain verification. */
23
+ export interface ChainOptions {
24
+ signerForPrincipal?: (args: {
25
+ principal: string;
26
+ allowedSignersText: string;
27
+ }) => Pick<Signer, 'verify'> & {
28
+ dispose?: () => void;
29
+ };
30
+ /** SSHSIG namespace used by the DSSE envelopes. */
31
+ namespace?: string;
32
+ /** Maximum number of grant links before the walk is refused. */
33
+ maxChainDepth?: number;
34
+ /** Sink for the conspicuous, non-secret accepted-backdating note. */
35
+ onBackdatedRevocation?: (note: string) => void;
36
+ }
37
+ /** The three possible chain-validation decisions. */
38
+ export type ChainVerdict = {
39
+ kind: 'verified';
40
+ keyid: string;
41
+ principal: PrincipalReference;
42
+ effectiveScope: GrantScope;
43
+ depth: number;
44
+ } | {
45
+ kind: 'unverifiable';
46
+ reason: string;
47
+ } | {
48
+ kind: 'invalid';
49
+ reason: string;
50
+ };
51
+ /** The canonical cross-field revocation timestamp rule. */
52
+ export declare function revocationTemporalConsistency(record: RevocationRecord): boolean;
53
+ /**
54
+ * Validate that `targetKeyId` is rooted at the supplied org anchor, with all
55
+ * grant links attenuating scope and all effective revocations applied at `at`.
56
+ */
57
+ export declare function validateEnrollmentChain(input: ChainInput, options?: ChainOptions): Promise<ChainVerdict>;
58
+ /** Validate a chain first, then apply the requested producer scope demand. */
59
+ export declare function validateProducer(input: ChainInput & {
60
+ demand: {
61
+ pool?: string;
62
+ label?: string;
63
+ namespace?: string;
64
+ };
65
+ }, options?: ChainOptions): Promise<ChainVerdict>;
@@ -0,0 +1,377 @@
1
+ /**
2
+ * Enrollment-chain verification for locally anchored principal keys.
3
+ *
4
+ * The validator consumes raw DSSE envelopes and an explicit validation instant.
5
+ * Each grant link is verified with a signer whose allowed-signers text contains
6
+ * exactly that link's parent key, so a remote roster assertion cannot substitute
7
+ * for the local org-root anchor. Scope attenuation and revocation authority are
8
+ * checked after schema decoding and cryptographic verification. This module is
9
+ * deliberately pure: it performs no filesystem or network I/O; org-root.ts
10
+ * supplies the optional on-disk loader for callers that need one.
11
+ */
12
+ import { decodeBase64Strict, DSSE_SSH_NAMESPACE, dsseVerifyEnrollmentGrant, dsseVerifyRevocation, DsseEnvelopeError } from "./dsse.js";
13
+ import { publicKeyDescriptor } from "./keys.js";
14
+ import { assertEd25519PubText, SshSignerError, createSshSigner } from "./ssh.js";
15
+ import { enrollmentGrantSchema, revocationSchema } from "../schemas/index.js";
16
+ import { summarizeIssues, validateValue } from "../schema.js";
17
+ import { attenuate, ORG_ROOT_SCOPE, scopePermits } from "./scope.js";
18
+ function invalid(reason) {
19
+ return { kind: 'invalid', reason };
20
+ }
21
+ function unverifiable(reason) {
22
+ return { kind: 'unverifiable', reason };
23
+ }
24
+ function isRecord(value) {
25
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
26
+ }
27
+ /** Decode and schema-check one signed record without invoking any signer. */
28
+ function decodeRecord(bytes, label, schema) {
29
+ let envelope;
30
+ try {
31
+ envelope = JSON.parse(Buffer.from(bytes).toString('utf8'));
32
+ }
33
+ catch {
34
+ return invalid(`${label} envelope is not valid JSON`);
35
+ }
36
+ if (!isRecord(envelope))
37
+ return invalid(`${label} envelope is not a JSON object`);
38
+ const payload = envelope.payload;
39
+ if (typeof payload !== 'string')
40
+ return invalid(`${label} envelope is missing a string 'payload'`);
41
+ let payloadBytes;
42
+ try {
43
+ payloadBytes = decodeBase64Strict(payload, { allowEmpty: true });
44
+ }
45
+ catch (error) {
46
+ return invalid(`${label} payload is not valid Base64: ${error.message}`);
47
+ }
48
+ let decoded;
49
+ try {
50
+ decoded = JSON.parse(payloadBytes.toString('utf8'));
51
+ }
52
+ catch {
53
+ return invalid(`${label} payload is not valid JSON`);
54
+ }
55
+ if (!isRecord(decoded))
56
+ return invalid(`${label} payload is not a JSON object`);
57
+ const shape = validateValue(schema, decoded);
58
+ if (!shape.valid) {
59
+ return invalid(`${label} record does not match schema: ${summarizeIssues(shape.issues)}`);
60
+ }
61
+ return { envelope, record: decoded };
62
+ }
63
+ function parseGrants(bytes) {
64
+ const grants = new Map();
65
+ for (const raw of bytes) {
66
+ const parsed = decodeRecord(raw, 'enrollment grant', enrollmentGrantSchema);
67
+ if ('kind' in parsed)
68
+ return parsed;
69
+ const keyid = parsed.record.newKey.keyid;
70
+ if (grants.has(keyid)) {
71
+ return invalid(`roster contains more than one enrollment grant for '${keyid}'`);
72
+ }
73
+ grants.set(keyid, parsed);
74
+ }
75
+ return grants;
76
+ }
77
+ function parseRevocations(bytes) {
78
+ const revocations = [];
79
+ for (const raw of bytes) {
80
+ const parsed = decodeRecord(raw, 'revocation', revocationSchema);
81
+ if ('kind' in parsed)
82
+ return parsed;
83
+ revocations.push(parsed);
84
+ }
85
+ return revocations;
86
+ }
87
+ function principalText(principal) {
88
+ return `${principal.kind}:${principal.id}`;
89
+ }
90
+ function describePublicKey(publicKey, label) {
91
+ try {
92
+ assertEd25519PubText(publicKey, label);
93
+ const descriptor = publicKeyDescriptor(publicKey);
94
+ return { info: { keyid: descriptor.keyid, publicKey: descriptor.openSshPublicKey } };
95
+ }
96
+ catch (error) {
97
+ return invalid(`${label}: ${error instanceof Error ? error.message : String(error)}`);
98
+ }
99
+ }
100
+ function signerText(info) {
101
+ // The descriptor contains one canonical OpenSSH key line. No second roster
102
+ // key is ever concatenated here: OpenSSH must authorize this parent only.
103
+ return `${info.principal} ${info.publicKey}\n`;
104
+ }
105
+ function makeSigner(options, parent) {
106
+ const factory = options.signerForPrincipal ?? ((args) => createSshSigner({
107
+ namespace: options.namespace ?? DSSE_SSH_NAMESPACE,
108
+ verify: args,
109
+ }));
110
+ return factory({ principal: parent.principal, allowedSignersText: signerText(parent) });
111
+ }
112
+ async function verifyGrant(parsed, childKeyId, parent, options) {
113
+ let signer;
114
+ try {
115
+ signer = makeSigner(options, parent);
116
+ }
117
+ catch (error) {
118
+ if (error instanceof SshSignerError)
119
+ return unverifiable(error.message);
120
+ return unverifiable(`enrollment grant verification setup failed: ${error instanceof Error ? error.message : String(error)}`);
121
+ }
122
+ try {
123
+ const result = await dsseVerifyEnrollmentGrant(parsed.envelope, signer, { threshold: 1 });
124
+ const verified = result.signers[0];
125
+ if (verified === undefined)
126
+ return invalid(`enrollment grant for '${childKeyId}' produced no verified signer`);
127
+ if (verified.keyid !== parent.keyid) {
128
+ return invalid(`enrollment grant for '${childKeyId}' was verified by '${verified.keyid}', expected parent '${parent.keyid}'`);
129
+ }
130
+ return { kind: 'ok' };
131
+ }
132
+ catch (error) {
133
+ if (error instanceof SshSignerError)
134
+ return unverifiable(error.message);
135
+ if (error instanceof DsseEnvelopeError) {
136
+ return invalid(`enrollment grant for '${childKeyId}' signature verification failed: ${error.message}`);
137
+ }
138
+ return invalid(`enrollment grant for '${childKeyId}' signature verification failed: ${error instanceof Error ? error.message : String(error)}`);
139
+ }
140
+ finally {
141
+ signer.dispose?.();
142
+ }
143
+ }
144
+ async function verifyRevocation(parsed, authority, options) {
145
+ let signer;
146
+ try {
147
+ signer = makeSigner(options, authority);
148
+ }
149
+ catch (error) {
150
+ if (error instanceof SshSignerError)
151
+ return unverifiable(error.message);
152
+ return unverifiable(`revocation verification setup failed: ${error instanceof Error ? error.message : String(error)}`);
153
+ }
154
+ try {
155
+ const result = await dsseVerifyRevocation(parsed.envelope, signer, { threshold: 1 });
156
+ const verified = result.signers[0];
157
+ if (verified === undefined)
158
+ return invalid(`revocation for '${parsed.record.revokedKey}' produced no verified signer`);
159
+ if (verified.keyid !== authority.keyid) {
160
+ return invalid(`revocation for '${parsed.record.revokedKey}' was verified by '${verified.keyid}', expected signer '${authority.keyid}'`);
161
+ }
162
+ return { kind: 'ok' };
163
+ }
164
+ catch (error) {
165
+ if (error instanceof SshSignerError)
166
+ return unverifiable(error.message);
167
+ if (error instanceof DsseEnvelopeError) {
168
+ return invalid(`revocation for '${parsed.record.revokedKey}' signature verification failed: ${error.message}`);
169
+ }
170
+ return invalid(`revocation for '${parsed.record.revokedKey}' signature verification failed: ${error instanceof Error ? error.message : String(error)}`);
171
+ }
172
+ finally {
173
+ signer.dispose?.();
174
+ }
175
+ }
176
+ /** The canonical cross-field revocation timestamp rule. */
177
+ export function revocationTemporalConsistency(record) {
178
+ return record.backdated === (record.effectiveFrom < record.issuedAt);
179
+ }
180
+ function isAncestor(revokedKey, possibleAncestor, parentOf, rootKeyId) {
181
+ let current = revokedKey;
182
+ const seen = new Set();
183
+ while (current !== rootKeyId) {
184
+ if (seen.has(current))
185
+ return false;
186
+ seen.add(current);
187
+ const parent = parentOf.get(current);
188
+ if (parent === undefined)
189
+ return false;
190
+ if (parent === possibleAncestor)
191
+ return true;
192
+ current = parent;
193
+ }
194
+ return possibleAncestor === rootKeyId;
195
+ }
196
+ /**
197
+ * Validate that `targetKeyId` is rooted at the supplied org anchor, with all
198
+ * grant links attenuating scope and all effective revocations applied at `at`.
199
+ */
200
+ export async function validateEnrollmentChain(input, options = {}) {
201
+ if (!Number.isInteger(input.at) || input.at < 0) {
202
+ return invalid(`validation instant '${String(input.at)}' is not a non-negative integer`);
203
+ }
204
+ const maxDepth = options.maxChainDepth ?? 32;
205
+ if (!Number.isInteger(maxDepth) || maxDepth < 0) {
206
+ return invalid(`maxChainDepth '${String(maxDepth)}' is not a non-negative integer`);
207
+ }
208
+ const rawRevocations = input.revocations ?? [];
209
+ const parsedRevocations = parseRevocations(rawRevocations);
210
+ if ('kind' in parsedRevocations)
211
+ return parsedRevocations;
212
+ const anchor = describePublicKey(input.orgRootPublicKey, 'org root public key');
213
+ if ('kind' in anchor)
214
+ return unverifiable(`cannot use org root public key: ${anchor.reason}`);
215
+ const rootKeyId = anchor.info.keyid;
216
+ // The anchor has no grant record. A self-revocation is always refused rather
217
+ // than silently making every otherwise-valid chain unverifiable.
218
+ if (parsedRevocations.some((entry) => entry.record.revokedKey === rootKeyId)) {
219
+ return invalid(`revocation of the org root '${rootKeyId}' is not permitted`);
220
+ }
221
+ if (input.targetKeyId === rootKeyId) {
222
+ return {
223
+ kind: 'verified',
224
+ keyid: rootKeyId,
225
+ principal: { kind: 'human', id: 'org-root' },
226
+ effectiveScope: ORG_ROOT_SCOPE,
227
+ depth: 0,
228
+ };
229
+ }
230
+ if (!Array.isArray(input.grants))
231
+ return invalid('enrollment roster is not an array');
232
+ const grants = parseGrants(input.grants);
233
+ if ('kind' in grants)
234
+ return grants;
235
+ const rootInfo = {
236
+ keyid: rootKeyId,
237
+ publicKey: anchor.info.publicKey,
238
+ principal: 'org-root',
239
+ scope: ORG_ROOT_SCOPE,
240
+ };
241
+ const chainKeys = new Map([[rootKeyId, rootInfo]]);
242
+ const parentOf = new Map();
243
+ const visited = new Set();
244
+ let current = input.targetKeyId;
245
+ let depth = 0;
246
+ let effectiveScope;
247
+ let targetPrincipal;
248
+ while (current !== rootKeyId) {
249
+ if (visited.has(current)) {
250
+ return invalid(`enrollment chain for '${input.targetKeyId}' contains a cycle at '${current}'`);
251
+ }
252
+ visited.add(current);
253
+ if (depth >= maxDepth) {
254
+ return invalid(`enrollment chain for '${input.targetKeyId}' exceeds maxChainDepth ${maxDepth} at '${current}'`);
255
+ }
256
+ const parsed = grants.get(current);
257
+ if (parsed === undefined) {
258
+ return invalid(`enrollment chain for '${input.targetKeyId}' does not terminate at the org root: no grant for '${current}'`);
259
+ }
260
+ const grant = parsed.record;
261
+ if (grant.validFrom > input.at) {
262
+ return invalid(`enrollment grant for '${current}' is not valid until ${grant.validFrom}`);
263
+ }
264
+ const childPublic = describePublicKey(grant.newKey.openSshPublicKey, `enrollment grant for '${current}'`);
265
+ if ('kind' in childPublic)
266
+ return childPublic;
267
+ if (childPublic.info.keyid !== current) {
268
+ return invalid(`enrollment grant for '${current}' carries public key '${childPublic.info.keyid}' in newKey, not '${current}'`);
269
+ }
270
+ if (grant.newKey.keyType !== 'ssh-ed25519') {
271
+ return invalid(`enrollment grant for '${current}' declares non-Ed25519 key type '${grant.newKey.keyType}'`);
272
+ }
273
+ const parentId = grant.grantedBy;
274
+ const parentGrant = parentId === rootKeyId ? undefined : grants.get(parentId);
275
+ if (parentId !== rootKeyId && parentGrant === undefined) {
276
+ return invalid(`enrollment chain for '${input.targetKeyId}' does not terminate at the org root: parent '${parentId}' has no grant`);
277
+ }
278
+ if (parentId === current) {
279
+ return invalid(`enrollment chain for '${input.targetKeyId}' contains a cycle at '${current}'`);
280
+ }
281
+ let parentInfo;
282
+ if (parentId === rootKeyId) {
283
+ parentInfo = rootInfo;
284
+ }
285
+ else {
286
+ const parentRecord = parentGrant.record;
287
+ const parentPublic = describePublicKey(parentRecord.newKey.openSshPublicKey, `enrollment grant for '${parentId}'`);
288
+ if ('kind' in parentPublic)
289
+ return parentPublic;
290
+ if (parentPublic.info.keyid !== parentId) {
291
+ return invalid(`enrollment grant for '${parentId}' carries public key '${parentPublic.info.keyid}' in newKey, not '${parentId}'`);
292
+ }
293
+ if (parentRecord.newKey.keyType !== 'ssh-ed25519') {
294
+ return invalid(`enrollment grant for '${parentId}' declares non-Ed25519 key type '${parentRecord.newKey.keyType}'`);
295
+ }
296
+ parentInfo = {
297
+ keyid: parentId,
298
+ publicKey: parentPublic.info.publicKey,
299
+ principal: principalText(parentRecord.principal),
300
+ scope: parentRecord.scope,
301
+ };
302
+ }
303
+ const attenuation = attenuate(parentInfo.scope, grant.scope);
304
+ if (!attenuation.ok) {
305
+ return invalid(`enrollment grant for '${current}' widens its parent scope: ${attenuation.reason}`);
306
+ }
307
+ const verified = await verifyGrant(parsed, current, { keyid: parentInfo.keyid, principal: parentInfo.principal, publicKey: parentInfo.publicKey }, options);
308
+ if (verified.kind !== 'ok')
309
+ return verified;
310
+ chainKeys.set(current, {
311
+ keyid: current,
312
+ publicKey: childPublic.info.publicKey,
313
+ principal: principalText(grant.principal),
314
+ scope: grant.scope,
315
+ });
316
+ parentOf.set(current, parentId);
317
+ if (effectiveScope === undefined) {
318
+ effectiveScope = grant.scope;
319
+ targetPrincipal = grant.principal;
320
+ }
321
+ current = parentId;
322
+ depth += 1;
323
+ }
324
+ const finalScope = effectiveScope;
325
+ const finalPrincipal = targetPrincipal;
326
+ if (finalScope === undefined || finalPrincipal === undefined) {
327
+ return invalid(`enrollment chain for '${input.targetKeyId}' contains no grant`);
328
+ }
329
+ for (const parsed of parsedRevocations) {
330
+ const record = parsed.record;
331
+ if (!chainKeys.has(record.revokedKey))
332
+ continue;
333
+ if (record.revokedKey === rootKeyId) {
334
+ return invalid(`revocation of the org root '${rootKeyId}' is not permitted`);
335
+ }
336
+ if (!revocationTemporalConsistency(record)) {
337
+ return invalid(`revocation for '${record.revokedKey}' has inconsistent backdated flag for issuedAt ${record.issuedAt} and effectiveFrom ${record.effectiveFrom}`);
338
+ }
339
+ const authority = chainKeys.get(record.revokedBy);
340
+ if (authority === undefined || !isAncestor(record.revokedKey, record.revokedBy, parentOf, rootKeyId)) {
341
+ return invalid(`revocation for '${record.revokedKey}' is signed by non-ancestor '${record.revokedBy}'`);
342
+ }
343
+ if (record.revokedBy !== rootKeyId && !authority.scope.delegation.allowed) {
344
+ return invalid(`revocation for '${record.revokedKey}' is signed by '${record.revokedBy}', whose delegation scope does not permit revocation`);
345
+ }
346
+ if (record.backdated && record.revokedBy !== rootKeyId) {
347
+ return invalid(`backdated revocation for '${record.revokedKey}' must be signed by the org root`);
348
+ }
349
+ const verified = await verifyRevocation(parsed, { keyid: authority.keyid, principal: authority.principal, publicKey: authority.publicKey }, options);
350
+ if (verified.kind !== 'ok')
351
+ return verified;
352
+ if (record.backdated && record.revokedBy === rootKeyId) {
353
+ (options.onBackdatedRevocation ?? (() => undefined))(`accepted backdated revocation for '${record.revokedKey}' effective from ${record.effectiveFrom}`);
354
+ }
355
+ if (record.effectiveFrom <= input.at) {
356
+ return invalid(`enrollment chain for '${input.targetKeyId}' contains revoked key '${record.revokedKey}' effective from ${record.effectiveFrom}`);
357
+ }
358
+ }
359
+ return {
360
+ kind: 'verified',
361
+ keyid: input.targetKeyId,
362
+ principal: finalPrincipal,
363
+ effectiveScope: finalScope,
364
+ depth,
365
+ };
366
+ }
367
+ /** Validate a chain first, then apply the requested producer scope demand. */
368
+ export async function validateProducer(input, options = {}) {
369
+ const chain = await validateEnrollmentChain(input, options);
370
+ if (chain.kind !== 'verified')
371
+ return chain;
372
+ const permitted = scopePermits(chain.effectiveScope, input.demand);
373
+ if (!permitted.ok) {
374
+ return invalid(`producer demand is outside the effective scope for '${chain.keyid}': ${permitted.reason}`);
375
+ }
376
+ return chain;
377
+ }