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,418 @@
1
+ /**
2
+ * Production implementation of the workflow-store pre-commit verifier.
3
+ *
4
+ * The verifier runs after bundle/engine validation and before the install
5
+ * transaction swaps an object or writes the index. It therefore refuses by
6
+ * throwing, while warnings are sent through the injected diagnostic stream.
7
+ * No verdict or sidecar is written into the immutable object directory.
8
+ */
9
+ import { lstatSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync, } from 'node:fs';
10
+ import { dirname, isAbsolute, join } from 'node:path';
11
+ import { defDigest, parseWorkflowCoordinate } from "./types.js";
12
+ import { readOwenloopSettingsRaw, owenloopSettingsPath } from "../work-settings.js";
13
+ import { resolveAllowedSigners, } from "../crypto/trust-roots.js";
14
+ import { isDefPolicy, verifyPublication, } from "../crypto/verify-publication.js";
15
+ import { mergePolicyFloorWithLocal } from "../crypto/policy-floor.js";
16
+ import { evaluateOriginRule, matchOriginRule, parseOriginRules, } from "../crypto/origin-rules.js";
17
+ import { verifyOrigin } from "../crypto/verify-origin.js";
18
+ import { persistOriginEvidence, readOriginEvidence } from "./origin-evidence.js";
19
+ import { StoreDefinitionVerificationError, StoreOriginPolicyError } from "./types.js";
20
+ function resolvePolicyValue(env, explicit, envKey, settingsKey, label) {
21
+ if (explicit !== undefined) {
22
+ if (!isDefPolicy(explicit))
23
+ throw new Error(`invalid ${label} '${String(explicit)}'`);
24
+ return explicit;
25
+ }
26
+ const envValue = env[envKey];
27
+ if (envValue !== undefined && envValue.trim() !== '') {
28
+ if (!isDefPolicy(envValue)) {
29
+ throw new Error(`invalid ${envKey} '${envValue}': expected enforce, warn, or off`);
30
+ }
31
+ return envValue;
32
+ }
33
+ // A missing HOME/XDG_CONFIG_HOME means there is no settings file to read;
34
+ // the built-in default remains usable. Any other settings read/parse error is
35
+ // surfaced rather than weakened into the least restrictive policy.
36
+ let path;
37
+ try {
38
+ path = owenloopSettingsPath(env);
39
+ }
40
+ catch {
41
+ return 'warn';
42
+ }
43
+ const raw = readOwenloopSettingsRaw(path);
44
+ const value = raw?.[settingsKey];
45
+ if (value === undefined)
46
+ return 'warn';
47
+ if (!isDefPolicy(value)) {
48
+ throw new Error(`invalid settings file at ${path}: '${settingsKey}' must be 'enforce', 'warn', or 'off', got ${JSON.stringify(value)}`);
49
+ }
50
+ return value;
51
+ }
52
+ /** Resolve publication policy with explicit > env > settings > default precedence. */
53
+ export function resolveDefPolicy(env, explicit) {
54
+ return resolvePolicyValue(env, explicit, 'OWENLOOP_DEF_POLICY', 'defPolicy', 'defPolicy');
55
+ }
56
+ /** Resolve origin policy with the same four-layer precedence as defPolicy. */
57
+ export function resolveOriginPolicy(env, explicit) {
58
+ return resolvePolicyValue(env, explicit, 'OWENLOOP_ORIGIN_POLICY', 'originPolicy', 'originPolicy');
59
+ }
60
+ /** Resolve consumed-artifact policy with the same four-layer precedence. */
61
+ export function resolveArtifactPolicy(env, explicit) {
62
+ return resolvePolicyValue(env, explicit, 'OWENLOOP_ARTIFACT_POLICY', 'artifactPolicy', 'artifactPolicy');
63
+ }
64
+ /** Resolve namespace rules with explicit > settings > empty precedence. */
65
+ export function resolveOriginRules(env, explicit) {
66
+ if (explicit !== undefined)
67
+ return parseOriginRules(explicit);
68
+ let path;
69
+ try {
70
+ path = owenloopSettingsPath(env);
71
+ }
72
+ catch {
73
+ return parseOriginRules(undefined);
74
+ }
75
+ const raw = readOwenloopSettingsRaw(path);
76
+ return parseOriginRules(raw?.['originRules']);
77
+ }
78
+ function filesystemSourcePath(source, cwd) {
79
+ if (source.kind !== 'file')
80
+ return undefined;
81
+ return isAbsolute(source.path) || cwd === undefined ? source.path : join(cwd, source.path);
82
+ }
83
+ function sidecarPath(source, cwd, suffix) {
84
+ const bundlePath = filesystemSourcePath(source, cwd);
85
+ return bundlePath === undefined ? undefined : `${bundlePath}${suffix}`;
86
+ }
87
+ function readSidecars(source, cwd) {
88
+ if (source.kind !== 'file')
89
+ return { unsigned: false, contradiction: false };
90
+ const dssePath = sidecarPath(source, cwd, '.dsse');
91
+ const unsignedPath = sidecarPath(source, cwd, '.unsigned');
92
+ const originPath = sidecarPath(source, cwd, '.origin.dsse');
93
+ const dsseStat = lstatSync(dssePath, { throwIfNoEntry: false });
94
+ const unsignedStat = lstatSync(unsignedPath, { throwIfNoEntry: false });
95
+ const originStat = lstatSync(originPath, { throwIfNoEntry: false });
96
+ const dssePresent = dsseStat !== undefined;
97
+ const unsignedPresent = unsignedStat !== undefined;
98
+ const originPresent = originStat !== undefined;
99
+ if (dssePresent && unsignedPresent) {
100
+ return {
101
+ unsigned: true,
102
+ contradiction: true,
103
+ invalidReason: `both publication sidecars are present ('${dssePath}' and '${unsignedPath}')`,
104
+ };
105
+ }
106
+ let originDsseBytes;
107
+ let originInvalidReason;
108
+ if (originPresent) {
109
+ if (originStat.isSymbolicLink() || !originStat.isFile()) {
110
+ originInvalidReason = `origin sidecar is not a regular file: ${originPath}`;
111
+ }
112
+ else {
113
+ try {
114
+ originDsseBytes = readFileSync(originPath);
115
+ }
116
+ catch (error) {
117
+ originInvalidReason = `origin sidecar could not be read: ${error instanceof Error ? error.message : String(error)}`;
118
+ }
119
+ }
120
+ }
121
+ if (unsignedPresent) {
122
+ if (unsignedStat.isSymbolicLink() || !unsignedStat.isFile()) {
123
+ return {
124
+ unsigned: true,
125
+ contradiction: false,
126
+ originDsseBytes,
127
+ originInvalidReason,
128
+ invalidReason: `unsigned publication marker is not a regular file: ${unsignedPath}`,
129
+ };
130
+ }
131
+ // The marker's existence is the producer's explicit unsigned declaration.
132
+ // The policy layer intentionally treats it exactly like a missing .dsse.
133
+ return { unsigned: true, contradiction: false, originDsseBytes, originInvalidReason };
134
+ }
135
+ if (!dssePresent)
136
+ return { unsigned: true, contradiction: false, originDsseBytes, originInvalidReason };
137
+ if (dsseStat.isSymbolicLink() || !dsseStat.isFile()) {
138
+ return {
139
+ unsigned: false,
140
+ contradiction: false,
141
+ originDsseBytes,
142
+ originInvalidReason,
143
+ invalidReason: `publication sidecar is not a regular file: ${dssePath}`,
144
+ };
145
+ }
146
+ try {
147
+ return { unsigned: false, contradiction: false, originDsseBytes, originInvalidReason, dsseBytes: readFileSync(dssePath) };
148
+ }
149
+ catch (error) {
150
+ return {
151
+ unsigned: false,
152
+ contradiction: false,
153
+ originDsseBytes,
154
+ originInvalidReason,
155
+ invalidReason: `publication sidecar could not be read: ${error instanceof Error ? error.message : String(error)}`,
156
+ };
157
+ }
158
+ }
159
+ /** Verify supplied publication bytes against the injected trust roots. */
160
+ async function verifyPublicationBytesAsync(digest, dsseBytes, env, optionsForPublication) {
161
+ let roots;
162
+ try {
163
+ roots = resolveAllowedSigners(env);
164
+ }
165
+ catch (error) {
166
+ return { kind: 'unverifiable', reason: error instanceof Error ? error.message : String(error) };
167
+ }
168
+ if (roots.kind === 'absent') {
169
+ return {
170
+ kind: 'unverifiable',
171
+ reason: `allowed_signers trust root is absent at ${roots.path}; create that file with an authorized signer entry`,
172
+ };
173
+ }
174
+ return verifyPublication({ bundleDigest: digest, dsseBytes, allowedSignersText: roots.text }, {
175
+ ...(optionsForPublication.signerForPrincipal !== undefined ? { signerForPrincipal: optionsForPublication.signerForPrincipal } : {}),
176
+ ...(optionsForPublication.namespace !== undefined ? { namespace: optionsForPublication.namespace } : {}),
177
+ });
178
+ }
179
+ /** Resolve a source sidecar and trust roots before invoking the verifier. */
180
+ async function verifyPublicationAsync(source, digest, env, optionsForPublication, sidecars) {
181
+ let resolvedSidecars = sidecars;
182
+ if (resolvedSidecars === undefined) {
183
+ try {
184
+ resolvedSidecars = readSidecars(source, optionsForPublication.cwd);
185
+ }
186
+ catch (error) {
187
+ return { kind: 'unverifiable', reason: `publication sidecar could not be inspected: ${error instanceof Error ? error.message : String(error)}` };
188
+ }
189
+ }
190
+ if (resolvedSidecars.contradiction || resolvedSidecars.invalidReason !== undefined) {
191
+ return { kind: 'invalid', reason: resolvedSidecars.invalidReason ?? 'publication sidecars contradict one another' };
192
+ }
193
+ if (resolvedSidecars.unsigned || resolvedSidecars.dsseBytes === undefined)
194
+ return { kind: 'unsigned' };
195
+ return verifyPublicationBytesAsync(digest, resolvedSidecars.dsseBytes, env, optionsForPublication);
196
+ }
197
+ async function verifyOriginBytesAsync(digest, dsseBytes, env, optionsForOrigin) {
198
+ if (dsseBytes === undefined)
199
+ return { kind: 'absent' };
200
+ let roots;
201
+ try {
202
+ roots = resolveAllowedSigners(env);
203
+ }
204
+ catch (error) {
205
+ return { kind: 'unverifiable', reason: error instanceof Error ? error.message : String(error) };
206
+ }
207
+ if (roots.kind === 'absent') {
208
+ return {
209
+ kind: 'unverifiable',
210
+ reason: `allowed_signers trust root is absent at ${roots.path}; create that file with an authorized signer entry`,
211
+ };
212
+ }
213
+ return verifyOrigin({ bundleDigest: digest, dsseBytes, allowedSignersText: roots.text }, {
214
+ ...(optionsForOrigin.signerForPrincipal !== undefined ? { signerForPrincipal: optionsForOrigin.signerForPrincipal } : {}),
215
+ ...(optionsForOrigin.namespace !== undefined ? { namespace: optionsForOrigin.namespace } : {}),
216
+ });
217
+ }
218
+ async function verifyOriginAsync(source, digest, env, optionsForOrigin, sidecars) {
219
+ let resolvedSidecars = sidecars;
220
+ if (resolvedSidecars === undefined) {
221
+ try {
222
+ resolvedSidecars = readSidecars(source, optionsForOrigin.cwd);
223
+ }
224
+ catch (error) {
225
+ return { kind: 'unverifiable', reason: `origin sidecar could not be inspected: ${error instanceof Error ? error.message : String(error)}` };
226
+ }
227
+ }
228
+ if (resolvedSidecars.originInvalidReason !== undefined) {
229
+ return { kind: 'invalid', reason: resolvedSidecars.originInvalidReason };
230
+ }
231
+ if (resolvedSidecars.unsigned) {
232
+ return resolvedSidecars.originDsseBytes === undefined
233
+ ? { kind: 'absent' }
234
+ : { kind: 'invalid', reason: 'definition was published unsigned and cannot carry an origin' };
235
+ }
236
+ return verifyOriginBytesAsync(digest, resolvedSidecars.originDsseBytes, env, optionsForOrigin);
237
+ }
238
+ function ensureDirectory(path) {
239
+ const existing = lstatSync(path, { throwIfNoEntry: false });
240
+ if (existing !== undefined) {
241
+ if (existing.isSymbolicLink() || !existing.isDirectory()) {
242
+ throw new Error(`publication evidence directory is not a real directory: ${path}`);
243
+ }
244
+ return;
245
+ }
246
+ mkdirSync(path, { recursive: true, mode: 0o700 });
247
+ }
248
+ /** Root of the workflow store for an install staging path (`root/.owenloop-staging/id`). */
249
+ function storeRootFromStaging(objectDir) {
250
+ return dirname(dirname(objectDir));
251
+ }
252
+ /** Root of the workflow store for an installed object (`root/objects/sha256/digest`). */
253
+ function storeRootFromObject(objectPath) {
254
+ return dirname(dirname(dirname(objectPath)));
255
+ }
256
+ function publicationEvidencePath(root, digest) {
257
+ return join(root, '.owenloop', 'publications', `${digest}.dsse`);
258
+ }
259
+ /**
260
+ * Retain the exact signed sidecar outside the immutable object. The verdict is
261
+ * never persisted: execution re-verifies these bytes against current trust
262
+ * roots. Existing evidence is left intact when a later unsigned reinstall
263
+ * deduplicates the same digest.
264
+ */
265
+ function persistPublicationEvidence(objectDir, digest, dsseBytes) {
266
+ const root = storeRootFromStaging(objectDir);
267
+ const stateDir = join(root, '.owenloop');
268
+ const evidenceDir = join(stateDir, 'publications');
269
+ ensureDirectory(stateDir);
270
+ ensureDirectory(evidenceDir);
271
+ const target = publicationEvidencePath(root, digest);
272
+ const temporary = `${target}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
273
+ try {
274
+ writeFileSync(temporary, dsseBytes, { mode: 0o600 });
275
+ renameSync(temporary, target);
276
+ }
277
+ finally {
278
+ rmSync(temporary, { force: true });
279
+ }
280
+ }
281
+ export function createExecutionDefinitionVerifier(options = {}) {
282
+ const env = options.env ?? process.env;
283
+ return async (input) => {
284
+ let digest;
285
+ try {
286
+ digest = defDigest(input.bundleDigest);
287
+ }
288
+ catch (error) {
289
+ return { kind: 'unverifiable', reason: error instanceof Error ? error.message : String(error) };
290
+ }
291
+ const target = publicationEvidencePath(storeRootFromObject(input.objectPath), input.bundleDigest);
292
+ const stat = lstatSync(target, { throwIfNoEntry: false });
293
+ if (stat === undefined)
294
+ return { kind: 'unsigned' };
295
+ if (stat.isSymbolicLink() || !stat.isFile()) {
296
+ return { kind: 'invalid', reason: `stored publication evidence is not a regular file: ${target}` };
297
+ }
298
+ let bytes;
299
+ try {
300
+ bytes = readFileSync(target);
301
+ }
302
+ catch (error) {
303
+ return { kind: 'unverifiable', reason: `stored publication evidence could not be read: ${error instanceof Error ? error.message : String(error)}` };
304
+ }
305
+ return verifyPublicationBytesAsync(digest, bytes, env, options);
306
+ };
307
+ }
308
+ /**
309
+ * Build the execution-time origin verifier. Installed origin evidence is read
310
+ * from mutable store state and re-verified against the current allowed_signers
311
+ * trust root; no cached origin verdict is trusted.
312
+ */
313
+ export function createExecutionOriginVerifier(options = {}) {
314
+ const env = options.env ?? process.env;
315
+ return async (input) => {
316
+ let digest;
317
+ try {
318
+ digest = defDigest(input.bundleDigest);
319
+ }
320
+ catch (error) {
321
+ return { kind: 'unverifiable', reason: error instanceof Error ? error.message : String(error) };
322
+ }
323
+ let bytes;
324
+ try {
325
+ bytes = readOriginEvidence(input.objectPath, digest);
326
+ }
327
+ catch (error) {
328
+ return { kind: 'invalid', reason: error instanceof Error ? error.message : String(error) };
329
+ }
330
+ return verifyOriginBytesAsync(digest, bytes, env, options);
331
+ };
332
+ }
333
+ function warningText(source, coordinate, verdict) {
334
+ const origin = source.kind === 'file' ? source.path : source.url;
335
+ if (verdict.kind === 'unsigned') {
336
+ return `workflow definition '${coordinate}' from ${origin} is unsigned; defPolicy=warn allows installation`;
337
+ }
338
+ if (verdict.kind === 'unverifiable') {
339
+ return `workflow definition '${coordinate}' from ${origin} is unverifiable; defPolicy=warn allows installation: ${verdict.reason}`;
340
+ }
341
+ return `workflow definition '${coordinate}' from ${origin} was not verified`;
342
+ }
343
+ function absentOriginDetail(source, sidecars) {
344
+ if (source.kind !== 'file') {
345
+ return 'cannot carry an origin: installed from a non-file source, where no sidecar is available';
346
+ }
347
+ if (sidecars.unsigned) {
348
+ return 'cannot carry an origin: the definition was published unsigned';
349
+ }
350
+ return 'no origin was recorded';
351
+ }
352
+ function evaluateInstalledOrigin(source, sidecars, match, verdict) {
353
+ if (match === undefined)
354
+ return { ok: true };
355
+ const evaluation = evaluateOriginRule(match.value, verdict);
356
+ if (!evaluation.ok && evaluation.kind === 'absent') {
357
+ return { ok: false, kind: 'absent', detail: absentOriginDetail(source, sidecars) };
358
+ }
359
+ return evaluation;
360
+ }
361
+ function originWarningText(coordinate, namespace, match, evaluation) {
362
+ return `workflow definition '${coordinate}' was not admitted by originRules=${match.key} for namespace '${namespace}'; originPolicy=warn allows installation: ${evaluation.kind} — ${evaluation.detail}`;
363
+ }
364
+ /** Production adapter bound by the root CLI's `defaultIO()`. */
365
+ export function createPreCommitVerifier(options = {}) {
366
+ const env = options.env ?? process.env;
367
+ return {
368
+ async verify(input) {
369
+ const namespace = parseWorkflowCoordinate(input.coordinate).namespace;
370
+ const originRules = resolveOriginRules(env, options.originRules);
371
+ const originMatch = matchOriginRule(originRules, namespace);
372
+ const merged = mergePolicyFloorWithLocal(resolveDefPolicy(env, options.policy), options.policyFloor, resolveOriginPolicy(env, options.originPolicy), resolveArtifactPolicy(env, options.artifactPolicy));
373
+ const policy = merged.effective;
374
+ const originPolicy = merged.originPolicy;
375
+ let sidecars;
376
+ try {
377
+ sidecars = readSidecars(input.source, options.cwd);
378
+ }
379
+ catch (error) {
380
+ const detail = `publication sidecar could not be inspected: ${error instanceof Error ? error.message : String(error)}`;
381
+ throw new StoreDefinitionVerificationError('unverifiable', policy, input.coordinate, detail);
382
+ }
383
+ const verdict = await verifyPublicationAsync(input.source, input.digest, env, options, sidecars);
384
+ if (verdict.kind === 'invalid') {
385
+ throw new StoreDefinitionVerificationError('invalid', policy, input.coordinate, verdict.reason);
386
+ }
387
+ if (policy === 'enforce' && verdict.kind !== 'verified') {
388
+ throw new StoreDefinitionVerificationError(verdict.kind, policy, input.coordinate, verdict.kind === 'unverifiable' ? verdict.reason : 'definition is unsigned');
389
+ }
390
+ if (policy === 'warn' && verdict.kind !== 'verified') {
391
+ (options.warn ?? ((line) => void console.error(line)))(warningText(input.source, input.coordinate, verdict));
392
+ }
393
+ const originVerdict = await verifyOriginAsync(input.source, input.digest, env, options, sidecars);
394
+ if (originVerdict.kind === 'invalid') {
395
+ throw new StoreOriginPolicyError(input.coordinate, namespace, originMatch?.key ?? '<none>', 'invalid', originVerdict.reason);
396
+ }
397
+ const originEvaluation = evaluateInstalledOrigin(input.source, sidecars, originMatch, originVerdict);
398
+ if (!originEvaluation.ok) {
399
+ if (originPolicy === 'enforce') {
400
+ throw new StoreOriginPolicyError(input.coordinate, namespace, originMatch.key, originEvaluation.kind, originEvaluation.detail);
401
+ }
402
+ if (originPolicy === 'warn') {
403
+ (options.warn ?? ((line) => void console.error(line)))(originWarningText(input.coordinate, namespace, originMatch, originEvaluation));
404
+ }
405
+ }
406
+ // Keep only exact signed evidence, never the derived verdict. Existing
407
+ // evidence is replaced only by a newly verified statement for this digest.
408
+ if (sidecars.dsseBytes !== undefined) {
409
+ persistPublicationEvidence(input.objectDir, input.digest, sidecars.dsseBytes);
410
+ }
411
+ if (sidecars.originDsseBytes !== undefined && originVerdict.kind === 'verified') {
412
+ persistOriginEvidence(input.objectDir, input.digest, sidecars.originDsseBytes);
413
+ }
414
+ },
415
+ };
416
+ }
417
+ /** Alias emphasizing the adapter's role in the install transaction. */
418
+ export const createProductionPreCommitVerifier = createPreCommitVerifier;
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Two-level store roots and fail-closed resolution.
3
+ *
4
+ * Two SEPARATE resolution APIs, so execution code cannot accidentally call
5
+ * human-name resolution:
6
+ *
7
+ * - {@link resolveWorkflowDigest} — execution path: digest only, no index,
8
+ * no workflow name. Project first, fall-through to global when the
9
+ * project object is ABSENT; a corrupt/invalid project hit is a hard error
10
+ * (never masked by falling back to a valid global copy).
11
+ *
12
+ * - {@link resolveWorkflowCoordinate} — human/CLI path: reads the project
13
+ * index first and uses the global index only as a fallback. When both
14
+ * levels name the coordinate, the project definition wins deterministically.
15
+ *
16
+ * Every successful resolution verifies the object it returns through the
17
+ * ingest adapter's `verifyInstalledObject` before handing back a path — the
18
+ * permissions/object shape on disk are defense in depth, not an integrity
19
+ * proof. There is no bare-name resolution anywhere in this module.
20
+ */
21
+ import type { DefDigest, ResolutionLevel, WorkflowCoordinate } from './types.ts';
22
+ import type { BundleIngestor } from './install.ts';
23
+ /**
24
+ * The PROJECT store root: the resolved defs directory itself — project pins
25
+ * stay reviewable in git, and the nested `objects/sha256/<digest>/` path
26
+ * stays below the def loader's scan depth.
27
+ */
28
+ export declare function projectStoreRoot(defsDir: string): string;
29
+ /**
30
+ * The GLOBAL store root: `<home>/.owenloop/workflows`. `home` is required so
31
+ * library callers and tests must pin a fixture HOME from the first write and
32
+ * never fall back to the ambient process home.
33
+ */
34
+ export declare function globalStoreRoot(home: string): string;
35
+ /** The index file path at a store root. */
36
+ export declare function storeIndexPath(root: string): string;
37
+ /** Canonical state paths for one workflow store root. */
38
+ export interface WorkflowStoreStatePaths {
39
+ stateDir: string;
40
+ lockPath: string;
41
+ journalPath: string;
42
+ }
43
+ export declare function workflowStoreStatePaths(root: string): WorkflowStoreStatePaths;
44
+ export interface DigestRepairWaitOptions {
45
+ /** Total bounded wait. A stale/crashed transaction fails closed after this. */
46
+ timeoutMs?: number;
47
+ /** Poll interval while the journal is in a replacement phase. */
48
+ retryMs?: number;
49
+ }
50
+ /** Bounded asynchronous coordination for digest-backed async readers. */
51
+ export declare function waitForDigestRepair(root: string, digest: DefDigest, options?: DigestRepairWaitOptions): Promise<void>;
52
+ /** Bounded synchronous coordination for `openCtx`/definition discovery readers. */
53
+ export declare function waitForDigestRepairSync(root: string, digest: DefDigest, options?: DigestRepairWaitOptions): void;
54
+ /**
55
+ * Run one asynchronous digest-backed read outside every active replacement
56
+ * phase. A replacement can start after the initial journal check, so a failed
57
+ * read checks the journal again and retries after the transaction stabilizes.
58
+ * One immediate retry is also allowed when the transaction completed quickly
59
+ * enough for both journal checks to observe absence; a genuinely missing or
60
+ * corrupt object therefore keeps its normal error after two immediate reads,
61
+ * rather than waiting for the full timeout.
62
+ */
63
+ export declare function coordinateDigestRead<T>(root: string, digest: DefDigest, read: () => Promise<T> | T, options?: DigestRepairWaitOptions): Promise<T>;
64
+ /** Synchronous counterpart of {@link coordinateDigestRead}. */
65
+ export declare function coordinateDigestReadSync<T>(root: string, digest: DefDigest, read: () => T, options?: DigestRepairWaitOptions): T;
66
+ /**
67
+ * Probe a store root: `'absent'` (nothing installed here — NOT an error), or
68
+ * a real directory. A symlink or non-directory squatting at the root is a
69
+ * hard {@link StorePathError}: resolution must never read through a link that
70
+ * redirects it outside the intended tree (SEC-3). `lstat`, never `stat`.
71
+ */
72
+ export declare function probeStoreRoot(root: string): 'dir' | 'absent';
73
+ /**
74
+ * Probe an object directory (`objects/sha256/<digest>`): `'dir'` or
75
+ * `'absent'`. Anything else — a symlink, a regular file, any non-directory —
76
+ * is a hard {@link StoreIntegrityError} naming the digest and level: a
77
+ * wrong-typed or linked object must never be returned, and (at project level)
78
+ * must never be papered over by a global fall-through.
79
+ */
80
+ export declare function probeObjectDir(objectDir: string, digest: DefDigest, level: ResolutionLevel): 'dir' | 'absent';
81
+ /** One resolution result: where the object lives and which levels hold it. */
82
+ export interface ResolvedWorkflowObject {
83
+ digest: DefDigest;
84
+ /** The object directory to read executable content from (verified). */
85
+ objectPath: string;
86
+ /** The level whose objectPath is returned (project wins a same-digest pair). */
87
+ level: ResolutionLevel;
88
+ /** Presence metadata: which levels hold this digest's object directory. */
89
+ presentAt: {
90
+ project: boolean;
91
+ global: boolean;
92
+ };
93
+ }
94
+ export interface ResolveWorkflowDigestArgs {
95
+ /** The digest to resolve (execution identity — no index, no name). */
96
+ digest: DefDigest;
97
+ /** The project store root; ABSENT (undefined) means global-only resolution. */
98
+ projectRoot?: string;
99
+ /** The global store root (always consulted). */
100
+ globalRoot: string;
101
+ /** The ingest adapter that verifies installed object bytes. */
102
+ verifier: BundleIngestor;
103
+ }
104
+ /**
105
+ * Digest-only resolution for EXECUTION. Tests the project object path first
106
+ * when a project root exists, then the global path. An ABSENT project object
107
+ * may fall through to global; a PRESENT project object that fails type
108
+ * probing or verification is a hard integrity error — project tampering is
109
+ * never hidden by falling back to a valid global copy. When both levels hold
110
+ * the same digest, ONE logical result is returned (level `project`, with
111
+ * presence metadata), not two candidates. No index and no workflow name
112
+ * participate. Throws {@link StoreIntegrityError} (`object-missing`) when the
113
+ * digest is present nowhere, or (`object-corrupt`) on any invalid hit.
114
+ */
115
+ export declare function resolveWorkflowDigest(args: ResolveWorkflowDigestArgs): Promise<ResolvedWorkflowObject>;
116
+ export interface ResolveWorkflowCoordinateArgs {
117
+ /** The full `namespace/name@version` coordinate (human identity). */
118
+ coordinate: WorkflowCoordinate;
119
+ /** The project store root; ABSENT (undefined) means global-only resolution. */
120
+ projectRoot?: string;
121
+ /** The global store root (always consulted). */
122
+ globalRoot: string;
123
+ /** The ingest adapter that verifies installed object bytes. */
124
+ verifier: BundleIngestor;
125
+ }
126
+ /**
127
+ * Human/CLI coordinate resolution. Reads BOTH indexes (fail-closed — a
128
+ * corrupt index is a {@link StoreIndexError}, never silently empty). No
129
+ * entries for the coordinate ⇒ {@link StoreNotFoundError}. A project entry is
130
+ * authoritative when present, even when the global index maps the same
131
+ * coordinate to a different digest; the global entry is the fallback only
132
+ * when the project has no match. The selected object's presence metadata is
133
+ * still reported at both levels. An index entry whose selected object is
134
+ * missing or corrupt is a {@link StoreIntegrityError}, never a returned path.
135
+ * This API is for humans/CLI only — execution resolves digests via
136
+ * {@link resolveWorkflowDigest}.
137
+ */
138
+ export declare function resolveWorkflowCoordinate(args: ResolveWorkflowCoordinateArgs): Promise<ResolvedWorkflowObject>;