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,211 @@
1
+ /**
2
+ * Concrete `.wnlp` bundle ingestion and on-read object verification.
3
+ *
4
+ * The bundle format owns archive validation and the canonical content digest;
5
+ * this adapter connects that byte-oriented API to the workflow store's staged
6
+ * object contract. Installed objects are verified from their manifest integrity
7
+ * map every time instruction content is resolved.
8
+ */
9
+ import { lstatSync, mkdirSync, readFileSync, readdirSync } from 'node:fs';
10
+ import { dirname, join, relative, resolve, sep } from 'node:path';
11
+ import { canonicalBundlePathViolation } from "../archive.js";
12
+ import { unpackBundle } from "../bundle/index.js";
13
+ import { parseManifestBytes, sha256Hex } from "../bundle/manifest.js";
14
+ import { buildCanonicalTar, compareUtf8Paths } from "../bundle/tar.js";
15
+ import { StorePathError, workflowCoordinate } from "./types.js";
16
+ import { coordinateDigestReadSync } from "./resolve.js";
17
+ function posixRelative(root, path) {
18
+ return relative(root, path).split(sep).join('/');
19
+ }
20
+ function refuse(path, detail) {
21
+ throw new StorePathError(`refusing workflow object '${path}': ${detail}`);
22
+ }
23
+ function assertDirectory(path) {
24
+ const st = lstatSync(path, { throwIfNoEntry: false });
25
+ if (st === undefined)
26
+ refuse(path, 'path does not exist');
27
+ if (st.isSymbolicLink())
28
+ refuse(path, 'path is a symlink');
29
+ if (!st.isDirectory())
30
+ refuse(path, 'path is not a directory');
31
+ }
32
+ function coordinateFor(manifest, namespace) {
33
+ return workflowCoordinate({
34
+ namespace,
35
+ name: manifest.package.name,
36
+ version: manifest.package.version,
37
+ });
38
+ }
39
+ function modeString(mode) {
40
+ return `0${(mode & 0o7777).toString(8)}`;
41
+ }
42
+ function assertHardenedDirectory(path, mode) {
43
+ const actual = mode & 0o7777;
44
+ if (actual !== 0o555) {
45
+ refuse(path, `directory mode is ${modeString(actual)}, expected hardened store mode 0555`);
46
+ }
47
+ }
48
+ function assertHardenedFile(path, mode) {
49
+ const actual = mode & 0o7777;
50
+ const expected = (actual & 0o111) === 0 ? 0o444 : 0o555;
51
+ if (actual !== expected) {
52
+ refuse(path, `regular file mode is ${modeString(actual)}, expected hardened store mode ${modeString(expected)}`);
53
+ }
54
+ }
55
+ /** Verify exact immutable store modes independently from canonical identity. */
56
+ export function verifyHardenedWorkflowObjectModesSync(objectDir) {
57
+ assertDirectory(objectDir);
58
+ const walk = (directory) => {
59
+ const directoryStat = lstatSync(directory);
60
+ assertHardenedDirectory(directory, directoryStat.mode);
61
+ for (const entry of readdirSync(directory)) {
62
+ const full = join(directory, entry);
63
+ const st = lstatSync(full, { throwIfNoEntry: false });
64
+ if (st === undefined)
65
+ refuse(full, 'path disappeared during hardened-mode verification');
66
+ if (st.isSymbolicLink())
67
+ refuse(full, 'object contains a symlink');
68
+ if (st.isDirectory())
69
+ walk(full);
70
+ else if (st.isFile())
71
+ assertHardenedFile(full, st.mode);
72
+ else
73
+ refuse(full, 'object contains a non-regular filesystem node');
74
+ }
75
+ };
76
+ walk(objectDir);
77
+ }
78
+ function verifyWorkflowObjectOnce(objectDir, digest, options) {
79
+ assertDirectory(objectDir);
80
+ const requireHardenedModes = options.requireHardenedModes !== false;
81
+ const readDir = options.readDir ?? ((directory) => readdirSync(directory));
82
+ const manifestPath = join(objectDir, 'bundle.yaml');
83
+ const manifestStat = lstatSync(manifestPath, { throwIfNoEntry: false });
84
+ if (manifestStat === undefined)
85
+ refuse(manifestPath, `missing manifest for digest ${digest}`);
86
+ if (manifestStat.isSymbolicLink())
87
+ refuse(manifestPath, 'manifest is a symlink');
88
+ if (!manifestStat.isFile())
89
+ refuse(manifestPath, 'manifest is not a regular file');
90
+ let manifest;
91
+ try {
92
+ manifest = parseManifestBytes(readFileSync(manifestPath));
93
+ }
94
+ catch (error) {
95
+ refuse(manifestPath, `invalid bundle.yaml for digest ${digest}: ${error.message}`);
96
+ }
97
+ const actual = new Map();
98
+ const walk = (directory) => {
99
+ const entries = readDir(directory);
100
+ for (const entry of entries) {
101
+ const full = join(directory, entry);
102
+ const rel = posixRelative(objectDir, full);
103
+ options.onVisit?.(rel);
104
+ const violation = canonicalBundlePathViolation(rel);
105
+ if (violation !== undefined)
106
+ refuse(full, `unsafe installed path '${rel}': ${violation}`);
107
+ const st = lstatSync(full, { throwIfNoEntry: false });
108
+ if (st === undefined)
109
+ refuse(full, 'path disappeared during verification');
110
+ if (st.isSymbolicLink())
111
+ refuse(full, 'object contains a symlink');
112
+ if (st.isDirectory()) {
113
+ walk(full);
114
+ }
115
+ else if (st.isFile()) {
116
+ try {
117
+ const bytes = readFileSync(full);
118
+ actual.set(rel, {
119
+ bytes,
120
+ sha256: sha256Hex(bytes),
121
+ mode: (st.mode & 0o111) === 0 ? 0o644 : 0o755,
122
+ });
123
+ }
124
+ catch (error) {
125
+ refuse(full, `could not read regular file: ${error.message}`);
126
+ }
127
+ }
128
+ else {
129
+ refuse(full, 'object contains a non-regular filesystem node');
130
+ }
131
+ }
132
+ };
133
+ walk(objectDir);
134
+ for (const [path, expected] of Object.entries(manifest.integrity.files)) {
135
+ const found = actual.get(path);
136
+ if (found === undefined) {
137
+ refuse(join(objectDir, path), `integrity map lists missing file '${path}' for digest ${digest}`);
138
+ }
139
+ if (found.sha256 !== expected) {
140
+ refuse(join(objectDir, path), `integrity mismatch for '${path}' (expected ${expected}, got ${found.sha256})`);
141
+ }
142
+ }
143
+ for (const [path] of actual) {
144
+ if (path === 'bundle.yaml')
145
+ continue;
146
+ if (!Object.prototype.hasOwnProperty.call(manifest.integrity.files, path)) {
147
+ refuse(join(objectDir, path), `file '${path}' is not listed in bundle.yaml integrity.files`);
148
+ }
149
+ }
150
+ let canonicalTar;
151
+ try {
152
+ const canonicalFiles = [...actual.entries()]
153
+ .map(([path, file]) => ({ path, bytes: file.bytes, mode: file.mode }))
154
+ .sort((a, b) => compareUtf8Paths(a.path, b.path));
155
+ canonicalTar = buildCanonicalTar(canonicalFiles);
156
+ }
157
+ catch (error) {
158
+ refuse(objectDir, `could not reconstruct canonical bundle bytes: ${error.message}`);
159
+ }
160
+ const actualDigest = sha256Hex(canonicalTar);
161
+ if (actualDigest !== digest) {
162
+ refuse(objectDir, `canonical bundle digest mismatch (expected content-addressed digest ${digest}, got ${actualDigest})`);
163
+ }
164
+ if (requireHardenedModes)
165
+ verifyHardenedWorkflowObjectModesSync(objectDir);
166
+ }
167
+ export function verifyWorkflowObjectSync(objectDir, digest, options = {}) {
168
+ const resolvedObjectDir = resolve(objectDir);
169
+ const storeRoot = dirname(dirname(dirname(resolvedObjectDir)));
170
+ const isDigestPath = resolvedObjectDir === join(storeRoot, 'objects', 'sha256', digest);
171
+ const verify = () => verifyWorkflowObjectOnce(objectDir, digest, options);
172
+ if (options.coordinateRepair === false || !isDigestPath) {
173
+ verify();
174
+ return;
175
+ }
176
+ coordinateDigestReadSync(storeRoot, digest, verify);
177
+ }
178
+ /** Create the real store adapter used by the default CLI and driver paths. */
179
+ export function createBundleIngestor(options = {}) {
180
+ const namespaceOverride = options.namespace;
181
+ if (namespaceOverride !== undefined && namespaceOverride.trim() === '') {
182
+ throw new StorePathError('bundle ingestor namespace must not be empty');
183
+ }
184
+ return {
185
+ async ingest(input) {
186
+ // installWorkflowBundle removes the complete staging root immediately
187
+ // before this call. unpackBundle requires an existing, real parent and a
188
+ // destination that does not exist, so create only the parent here.
189
+ mkdirSync(dirname(input.stagingDir), { recursive: true });
190
+ const result = unpackBundle(input.bytes, input.stagingDir);
191
+ const namespace = namespaceOverride ?? result.manifest.package.name;
192
+ return {
193
+ coordinate: coordinateFor(result.manifest, namespace),
194
+ digest: result.digest,
195
+ workflows: Object.keys(result.manifest.workflows).sort(),
196
+ };
197
+ },
198
+ async verifyInstalledObject(input) {
199
+ verifyWorkflowObjectSync(input.objectDir, input.digest);
200
+ },
201
+ async verifyStagedObject(input) {
202
+ verifyWorkflowObjectSync(input.objectDir, input.digest, {
203
+ coordinateRepair: false,
204
+ requireHardenedModes: false,
205
+ });
206
+ },
207
+ async verifyInstalledObjectAfterCoordination(input) {
208
+ verifyWorkflowObjectSync(input.objectDir, input.digest, { coordinateRepair: false });
209
+ },
210
+ };
211
+ }
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Workflow-bundle installation into the content-addressed store — its trust
3
+ * boundary. This module OWNS the install orchestration (staging → engine
4
+ * validation → pre-commit verification → hardening → atomic swap → atomic
5
+ * index commit → finalize), but deliberately does NOT own bundle
6
+ * parsing/digest (the `BundleIngestor` port) or pre-commit verification (the
7
+ * `PreCommitVerifier` port). Neither port has a permissive fallback: with
8
+ * either adapter missing, installation fails closed BEFORE any
9
+ * object/index/journal commit — there is no default accepting parser, digest
10
+ * algorithm, or no-op verifier.
11
+ *
12
+ * The transaction rides the host-neutral machinery in `src/install.ts`
13
+ * (staging, swap+retained backup, journal, lock, recovery). CAS specifics:
14
+ * metadata = `index.json` at the store root; destination = the validated
15
+ * three-segment object path `objects/sha256/<digest>`; journal = v2 (identity
16
+ * is a metadata-hash match, no route-specific fields); lock/staging/journal
17
+ * live PER ROOT (project installs share the project add lock path; global
18
+ * installs keep equivalent state below the global root).
19
+ *
20
+ * Engine core: this module imports Node builtins, defs/model validation
21
+ * helpers, and the generic transaction — never `cli.ts`, `add.ts`, `untar.ts`,
22
+ * or a hub module.
23
+ */
24
+ import type { LedgerLookup, RecoveryOutcome } from '../install.ts';
25
+ import type { DefDigest, StoreLevel, WorkflowCoordinate } from './types.ts';
26
+ /**
27
+ * Where a bundle came from — origin DATA only (diagnostics, error messages,
28
+ * and the A1/A2 adapter call sites). NEVER identity: identity is the A1
29
+ * manifest's coordinate + canonical digest, and a source string is never
30
+ * joined into a filesystem path or hashed as identity.
31
+ */
32
+ export type BundleSource = {
33
+ kind: 'file';
34
+ path: string;
35
+ } | {
36
+ kind: 'url';
37
+ url: string;
38
+ };
39
+ /**
40
+ * The bundle-ingestion port: bundle unpacking, canonical digest computation,
41
+ * manifest integrity, safe extraction, and the full `namespace/name@version`
42
+ * coordinate. The concrete implementation lands with the bundle-format
43
+ * module; until then callers MUST inject an implementation — this module
44
+ * fails closed ({@link BundleIngestorUnavailableError}) without one.
45
+ *
46
+ * `ingest` unpacks `bytes` into the supplied `stagingDir` and returns ONLY
47
+ * after bundle manifest, per-file integrity, canonical digest, and coordinate
48
+ * checks pass. `verifyInstalledObject` re-checks an already-installed object
49
+ * directory against its expected digest — called on every dedupe and on every
50
+ * successful resolution before executable content is returned.
51
+ */
52
+ export interface BundleIngestor {
53
+ ingest(input: {
54
+ source: BundleSource;
55
+ bytes: Uint8Array;
56
+ stagingDir: string;
57
+ }): Promise<{
58
+ coordinate: WorkflowCoordinate;
59
+ digest: DefDigest;
60
+ workflows: string[];
61
+ }>;
62
+ verifyInstalledObject(input: {
63
+ objectDir: string;
64
+ digest: DefDigest;
65
+ }): Promise<void>;
66
+ /** Optional canonical-only check for a staged pre-swap reconstruction. */
67
+ verifyStagedObject?(input: {
68
+ objectDir: string;
69
+ digest: DefDigest;
70
+ }): Promise<void>;
71
+ /** Optional installed check used after the caller already coordinated the repair journal. */
72
+ verifyInstalledObjectAfterCoordination?(input: {
73
+ objectDir: string;
74
+ digest: DefDigest;
75
+ }): Promise<void>;
76
+ }
77
+ /**
78
+ * The pre-commit verification port, called after content/engine validation
79
+ * and BEFORE any destination swap or index write. A rejection removes
80
+ * staging and leaves objects/index/journal unchanged. The concrete
81
+ * implementation is a separate module; without one this module fails closed
82
+ * ({@link PreCommitVerifierUnavailableError}) — there is NO default
83
+ * accepting verifier.
84
+ */
85
+ export interface PreCommitVerifier {
86
+ verify(input: {
87
+ source: BundleSource;
88
+ coordinate: WorkflowCoordinate;
89
+ digest: DefDigest;
90
+ objectDir: string;
91
+ }): Promise<void>;
92
+ }
93
+ /** What a successful bundle install reports. No signature contents or keys — ever. */
94
+ export interface BundleInstallResult {
95
+ source: BundleSource;
96
+ level: StoreLevel;
97
+ coordinate: WorkflowCoordinate;
98
+ digest: DefDigest;
99
+ /** Sorted workflow names carried by the installed bundle. */
100
+ workflows: string[];
101
+ objectPath: string;
102
+ /** True when the digest object was newly installed; false when an existing same-digest object was deduplicated or repaired. */
103
+ installed: boolean;
104
+ }
105
+ export interface InstallWorkflowBundleArgs {
106
+ /** The raw bundle bytes (already fetched/read by the caller). */
107
+ bytes: Uint8Array;
108
+ /** Origin data for diagnostics and the adapter call sites — never identity. */
109
+ source: BundleSource;
110
+ /** The store root to install into (project: the resolved defs dir; global: `~/.owenloop/workflows`). */
111
+ root: string;
112
+ /** Which level `root` is (result metadata only). */
113
+ level: StoreLevel;
114
+ /** Path of the per-root install lock (project installs share the project add lock). */
115
+ lockPath: string;
116
+ /** Path of the per-root crash-recovery journal. */
117
+ journalPath: string;
118
+ /** External marker directory for fresh-install corroboration and repair directory identities. */
119
+ recoveryMarkerDir: string;
120
+ /** Bundle-ingest adapter — REQUIRED (fail-closed without it). */
121
+ ingestor: BundleIngestor;
122
+ /** Pre-commit verifier — REQUIRED (fail-closed without it). */
123
+ verifier: PreCommitVerifier;
124
+ /**
125
+ * Project-level installs ONLY: the installed.json ledger lookup so the
126
+ * inline recovery can roll back/forward a legacy v1 (GitHub-route) journal
127
+ * left at the shared project journal path — project bundle installs share
128
+ * the existing project add lock/recovery ordering. A v2 journal never needs
129
+ * this; without it a leftover v1 journal is refused (fail-closed), exactly
130
+ * as the generic recovery documents. Global installs pass nothing (v1 is
131
+ * never legal under the global root — there is no ledger there to vouch).
132
+ */
133
+ readLedger?: () => LedgerLookup;
134
+ }
135
+ /**
136
+ * The CAS destination relative path for a validated digest: the single
137
+ * relative path `objects/sha256/<digest>`, re-validated as a safe
138
+ * multi-segment path before any join/rename (reject-don't-normalize, same as
139
+ * every other destination path in the transaction).
140
+ */
141
+ export declare function objectDestRelPath(digest: DefDigest): string;
142
+ /**
143
+ * Set non-executable regular files read-only (0o444), executable regular
144
+ * files read/execute-only (0o555), and directories non-writable (0o555) under
145
+ * `dir`, recursively, INCLUDING `dir` itself. Preserving the executable bit is
146
+ * required to reconstruct the canonical 0644/0755 bundle identity. Defense in
147
+ * depth only —
148
+ * permissions are NOT the integrity proof (verification is). Any chmod
149
+ * failure propagates: an object that cannot be hardened is not committed
150
+ * (the caller rolls staging back), so a partially-hardened object never
151
+ * lands in the store. A symlink anywhere in the object is refused outright —
152
+ * an extracted object must never contain links.
153
+ */
154
+ export declare function hardenObjectModes(dir: string): void;
155
+ /** Concrete recovery actions for v2 content-addressed replacement journals. */
156
+ export declare const workflowStoreReplacementRecovery: {
157
+ harden: typeof hardenObjectModes;
158
+ verify(objectDir: string, rawDigest: string): void;
159
+ };
160
+ /**
161
+ * Install one `.wnlp` bundle into a store root as one recoverable operation.
162
+ *
163
+ * Fixed commit order:
164
+ * 1. acquire the root's install lock;
165
+ * 2. recover a prior interrupted install (before stale-stage cleanup — the
166
+ * backups a rollback needs live under the staging root);
167
+ * 3. clear stale staging debris;
168
+ * 4. reread + validate the index INSIDE the lock (ownership/conflict
169
+ * decisions must see the post-lock state — TOCTOU);
170
+ * 5. stage via the A1 ingestor (unpack + manifest + digest + coordinate);
171
+ * 6. validate the staged tree with the engine's strict pass (loadDefsRaw +
172
+ * lint + validate + bounded modelCheck(assumeProvided) + strict loadDefs);
173
+ * 7. run the A2 pre-commit verifier (after content validation, before ANY
174
+ * destination swap or index write);
175
+ * 8. when the digest object exists, verify the installed object; if that
176
+ * verification fails, completely verify the staged reconstruction and
177
+ * mark the transaction as a repair; a verified object remains a dedupe;
178
+ * 9. compute the post-install index and write the `applying` journal (v2,
179
+ * with the hash of that exact index state);
180
+ * 10. atomically install or repair the object — a fresh install or repair
181
+ * swaps the staged directory into place (retaining displaced content on
182
+ * the handle), THEN hardens file/directory modes at the destination; a
183
+ * repair verifies that exact hardened destination before the commit point,
184
+ * while a dedupe commits no object rewrite;
185
+ * 11. atomically write the index — the DURABLE COMMIT POINT;
186
+ * 12. write the `finalizing` journal;
187
+ * 13. discard the retained backup + staging;
188
+ * 14. remove the journal; release the lock.
189
+ *
190
+ * Fetching/reading the source bytes may happen before the lock (the caller's
191
+ * job); index ownership/conflict decisions happen inside it.
192
+ *
193
+ * Failure semantics: any rejection before the swap (A1 tamper refusal, A2
194
+ * refusal, engine validation, digest mismatch) leaves objects/index/journal
195
+ * untouched and staging is cleared by the `finally`. A swap failure, a
196
+ * post-swap hardening or repair-verification failure, or an index-write failure
197
+ * rolls the directory state back in-process so the previous object + index are
198
+ * left exactly as they were. A crash anywhere leaves the journal behind for the
199
+ * next recovery.
200
+ *
201
+ * Conflict semantics: an existing coordinate at a DIFFERENT digest is a
202
+ * {@link StoreConflictError} (no implicit retarget). An existing coordinate at
203
+ * the SAME digest deduplicates when the installed object verifies. When only
204
+ * installed-object verification fails, the already-admitted supplied archive
205
+ * is verified again from staging and atomically replaces the broken object;
206
+ * bytes from the broken object are never reused. The index keeps the same
207
+ * coordinate-to-digest meaning.
208
+ */
209
+ export declare function installWorkflowBundle(args: InstallWorkflowBundleArgs): Promise<BundleInstallResult>;
210
+ export interface RecoverWorkflowStoreArgs {
211
+ /** The store root to recover (global: `~/.owenloop/workflows`; project: the defs dir). */
212
+ root: string;
213
+ /** Path of the root's install lock. */
214
+ lockPath: string;
215
+ /** Path of the root's crash-recovery journal. */
216
+ journalPath: string;
217
+ /** External marker directory for install/repair transaction recovery. */
218
+ recoveryMarkerDir?: string;
219
+ }
220
+ /**
221
+ * Offline crash recovery for a workflow-store root — the `add --recover`
222
+ * counterpart for bundle installs. Acquires the root lock, runs the generic
223
+ * two-version recovery (v2 metadata-hash commit point), releases. No network,
224
+ * no store open. A v2 journal recovers forward or back exactly as the inline
225
+ * path would; a v1 journal is refused (the GitHub route owns v1 recovery).
226
+ * Refusals throw and leave the journal, staging, and objects untouched.
227
+ */
228
+ export declare function recoverWorkflowStore(args: RecoverWorkflowStoreArgs): Promise<RecoveryOutcome>;