owenloop 0.5.0 → 0.5.2

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 (229) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +144 -35
  3. package/bin/owenloop.mjs +49 -1
  4. package/dist/packages/work/src/agent/brief.d.ts +29 -4
  5. package/dist/packages/work/src/agent/brief.js +49 -4
  6. package/dist/packages/work/src/agent/capability-model.d.ts +123 -0
  7. package/dist/packages/work/src/agent/capability-model.js +148 -0
  8. package/dist/packages/work/src/agent/loop.d.ts +56 -7
  9. package/dist/packages/work/src/agent/loop.js +166 -9
  10. package/dist/packages/work/src/bundle/cache.d.ts +7 -5
  11. package/dist/packages/work/src/bundle/cache.js +20 -6
  12. package/dist/packages/work/src/bundle/fetch.d.ts +7 -5
  13. package/dist/packages/work/src/bundle/fetch.js +27 -7
  14. package/dist/packages/work/src/bundle/types.d.ts +8 -4
  15. package/dist/packages/work/src/consumed-verifier.d.ts +45 -0
  16. package/dist/packages/work/src/consumed-verifier.js +205 -0
  17. package/dist/packages/work/src/exec/instructions.d.ts +99 -0
  18. package/dist/packages/work/src/exec/instructions.js +323 -0
  19. package/dist/packages/work/src/exec/loop.d.ts +29 -1
  20. package/dist/packages/work/src/exec/loop.js +313 -35
  21. package/dist/packages/work/src/exec/payload.d.ts +29 -0
  22. package/dist/packages/work/src/exec/payload.js +54 -0
  23. package/dist/packages/work/src/exec/receipt.d.ts +9 -6
  24. package/dist/packages/work/src/exec/receipt.js +12 -1
  25. package/dist/packages/work/src/exec/runner.d.ts +7 -1
  26. package/dist/packages/work/src/exec/runner.js +84 -3
  27. package/dist/packages/work/src/harness/child-env.js +7 -0
  28. package/dist/packages/work/src/harness/claude.d.ts +20 -8
  29. package/dist/packages/work/src/harness/claude.js +260 -58
  30. package/dist/packages/work/src/harness/codex.d.ts +5 -4
  31. package/dist/packages/work/src/harness/codex.js +169 -41
  32. package/dist/packages/work/src/harness/contract.d.ts +36 -11
  33. package/dist/packages/work/src/harness/fake.js +1 -0
  34. package/dist/packages/work/src/harness/jsonrpc-stdio.js +7 -0
  35. package/dist/packages/work/src/harness/permissions.d.ts +16 -3
  36. package/dist/packages/work/src/harness/permissions.js +102 -13
  37. package/dist/packages/work/src/harness/session-store.d.ts +30 -10
  38. package/dist/packages/work/src/harness/session-store.js +317 -89
  39. package/dist/packages/work/src/hold/mcp.d.ts +25 -5
  40. package/dist/packages/work/src/hold/mcp.js +134 -12
  41. package/dist/packages/work/src/hub/client.d.ts +8 -1
  42. package/dist/packages/work/src/hub/client.js +15 -1
  43. package/dist/packages/work/src/hub/types.d.ts +137 -33
  44. package/dist/packages/work/src/hub/types.js +8 -10
  45. package/dist/packages/work/src/mcp/server.js +1 -1
  46. package/dist/packages/work/src/owenloop-bin.d.ts +1 -0
  47. package/dist/packages/work/src/owenloop-bin.js +18 -0
  48. package/dist/packages/work/src/roles/agent-run.d.ts +12 -6
  49. package/dist/packages/work/src/roles/agent-run.js +115 -34
  50. package/dist/packages/work/src/roles/exec.d.ts +8 -0
  51. package/dist/packages/work/src/roles/exec.js +27 -2
  52. package/dist/packages/work/src/roles/hold.d.ts +6 -0
  53. package/dist/packages/work/src/roles/hold.js +83 -13
  54. package/dist/packages/work/src/roles/lint.d.ts +3 -4
  55. package/dist/packages/work/src/roles/lint.js +41 -7
  56. package/dist/packages/work/src/roles/settings.js +19 -2
  57. package/dist/packages/work/src/roles/shift.d.ts +1 -0
  58. package/dist/packages/work/src/roles/shift.js +36 -2
  59. package/dist/packages/work/src/settings/settings.d.ts +70 -1
  60. package/dist/packages/work/src/settings/settings.js +90 -4
  61. package/dist/packages/work/src/shift/logretention.d.ts +253 -0
  62. package/dist/packages/work/src/shift/logretention.js +407 -0
  63. package/dist/packages/work/src/shift/logsink.d.ts +14 -0
  64. package/dist/packages/work/src/shift/logsink.js +98 -0
  65. package/dist/packages/work/src/shift/loop.d.ts +32 -4
  66. package/dist/packages/work/src/shift/loop.js +687 -166
  67. package/dist/packages/work/src/shift/protocol.d.ts +128 -1
  68. package/dist/packages/work/src/shift/protocol.js +14 -0
  69. package/dist/packages/work/src/shift/runtime.d.ts +16 -0
  70. package/dist/packages/work/src/shift/runtime.js +291 -5
  71. package/dist/packages/work/src/shift/server.d.ts +13 -0
  72. package/dist/packages/work/src/shift/server.js +61 -36
  73. package/dist/packages/work/src/shift/spawn.d.ts +115 -19
  74. package/dist/packages/work/src/shift/spawn.js +234 -30
  75. package/dist/packages/work/src/shift/state.d.ts +76 -49
  76. package/dist/packages/work/src/shift/state.js +350 -103
  77. package/dist/packages/work/src/shift/truncate.d.ts +35 -0
  78. package/dist/packages/work/src/shift/truncate.js +73 -0
  79. package/dist/packages/work/src/submit-proof.d.ts +61 -0
  80. package/dist/packages/work/src/submit-proof.js +119 -0
  81. package/dist/packages/work/src/submit-value.d.ts +48 -0
  82. package/dist/packages/work/src/submit-value.js +118 -0
  83. package/dist/packages/work/src/usage.d.ts +1 -1
  84. package/dist/packages/work/src/usage.js +11 -0
  85. package/dist/src/add.d.ts +64 -259
  86. package/dist/src/add.js +81 -615
  87. package/dist/src/archive.d.ts +97 -0
  88. package/dist/src/archive.js +591 -0
  89. package/dist/src/bundle/index.d.ts +64 -0
  90. package/dist/src/bundle/index.js +508 -0
  91. package/dist/src/bundle/manifest.d.ts +50 -0
  92. package/dist/src/bundle/manifest.js +435 -0
  93. package/dist/src/bundle/runtime.d.ts +26 -0
  94. package/dist/src/bundle/runtime.js +64 -0
  95. package/dist/src/bundle/tar.d.ts +62 -0
  96. package/dist/src/bundle/tar.js +238 -0
  97. package/dist/src/bundle/types.d.ts +105 -0
  98. package/dist/src/bundle/types.js +21 -0
  99. package/dist/src/capabilities.d.ts +93 -0
  100. package/dist/src/capabilities.js +101 -0
  101. package/dist/src/cli.d.ts +90 -20
  102. package/dist/src/cli.js +2421 -374
  103. package/dist/src/credentials.d.ts +4 -3
  104. package/dist/src/credentials.js +21 -19
  105. package/dist/src/crypto/allowed-signers.d.ts +68 -0
  106. package/dist/src/crypto/allowed-signers.js +307 -0
  107. package/dist/src/crypto/canonical.d.ts +8 -0
  108. package/dist/src/crypto/canonical.js +92 -0
  109. package/dist/src/crypto/chain.d.ts +65 -0
  110. package/dist/src/crypto/chain.js +377 -0
  111. package/dist/src/crypto/dsse.d.ts +209 -0
  112. package/dist/src/crypto/dsse.js +282 -0
  113. package/dist/src/crypto/enrollment.d.ts +78 -0
  114. package/dist/src/crypto/enrollment.js +186 -0
  115. package/dist/src/crypto/index.d.ts +48 -0
  116. package/dist/src/crypto/index.js +44 -0
  117. package/dist/src/crypto/keys.d.ts +258 -0
  118. package/dist/src/crypto/keys.js +908 -0
  119. package/dist/src/crypto/org-root.d.ts +33 -0
  120. package/dist/src/crypto/org-root.js +78 -0
  121. package/dist/src/crypto/origin-rules.d.ts +56 -0
  122. package/dist/src/crypto/origin-rules.js +143 -0
  123. package/dist/src/crypto/policy-floor.d.ts +98 -0
  124. package/dist/src/crypto/policy-floor.js +353 -0
  125. package/dist/src/crypto/records.d.ts +319 -0
  126. package/dist/src/crypto/records.js +168 -0
  127. package/dist/src/crypto/scope.d.ts +49 -0
  128. package/dist/src/crypto/scope.js +83 -0
  129. package/dist/src/crypto/ssh.d.ts +171 -0
  130. package/dist/src/crypto/ssh.js +396 -0
  131. package/dist/src/crypto/submission.d.ts +33 -0
  132. package/dist/src/crypto/submission.js +47 -0
  133. package/dist/src/crypto/trust-roots.d.ts +29 -0
  134. package/dist/src/crypto/trust-roots.js +39 -0
  135. package/dist/src/crypto/verify-consumed.d.ts +67 -0
  136. package/dist/src/crypto/verify-consumed.js +259 -0
  137. package/dist/src/crypto/verify-origin.d.ts +50 -0
  138. package/dist/src/crypto/verify-origin.js +151 -0
  139. package/dist/src/crypto/verify-publication.d.ts +60 -0
  140. package/dist/src/crypto/verify-publication.js +150 -0
  141. package/dist/src/defs.d.ts +24 -1
  142. package/dist/src/defs.js +335 -27
  143. package/dist/src/engine.d.ts +211 -6
  144. package/dist/src/engine.js +459 -59
  145. package/dist/src/factory.d.ts +16 -0
  146. package/dist/src/factory.js +16 -4
  147. package/dist/src/global-config.d.ts +59 -0
  148. package/dist/src/global-config.js +84 -0
  149. package/dist/src/hub.d.ts +102 -32
  150. package/dist/src/hub.js +147 -58
  151. package/dist/src/index.d.ts +19 -2
  152. package/dist/src/index.js +23 -2
  153. package/dist/src/install.d.ts +538 -0
  154. package/dist/src/install.js +1981 -0
  155. package/dist/src/lock.d.ts +15 -57
  156. package/dist/src/lock.js +251 -143
  157. package/dist/src/mcp/serve.d.ts +46 -22
  158. package/dist/src/mcp/serve.js +174 -44
  159. package/dist/src/mcp/server.js +12 -1
  160. package/dist/src/model.d.ts +51 -0
  161. package/dist/src/model.js +157 -1
  162. package/dist/src/order-resolver.d.ts +184 -0
  163. package/dist/src/order-resolver.js +252 -0
  164. package/dist/src/package-version.d.ts +10 -0
  165. package/dist/src/package-version.js +42 -0
  166. package/dist/src/paths.d.ts +14 -1
  167. package/dist/src/paths.js +18 -0
  168. package/dist/src/schemas/enrollment-grant.v1.schema.json +90 -0
  169. package/dist/src/schemas/index.d.ts +1433 -0
  170. package/dist/src/schemas/index.js +31 -0
  171. package/dist/src/schemas/order.v1.schema.json +120 -0
  172. package/dist/src/schemas/origin.v1.schema.json +68 -0
  173. package/dist/src/schemas/policy-floor.v1.schema.json +29 -0
  174. package/dist/src/schemas/publication.v1.schema.json +31 -0
  175. package/dist/src/schemas/revocation.v1.schema.json +39 -0
  176. package/dist/src/schemas/submission.v1.schema.json +61 -0
  177. package/dist/src/store/def-source.d.ts +52 -0
  178. package/dist/src/store/def-source.js +335 -0
  179. package/dist/src/store/index-file.d.ts +69 -0
  180. package/dist/src/store/index-file.js +151 -0
  181. package/dist/src/store/index.d.ts +27 -0
  182. package/dist/src/store/index.js +22 -0
  183. package/dist/src/store/ingestor.d.ts +43 -0
  184. package/dist/src/store/ingestor.js +211 -0
  185. package/dist/src/store/install.d.ts +228 -0
  186. package/dist/src/store/install.js +555 -0
  187. package/dist/src/store/instruction-source.d.ts +49 -0
  188. package/dist/src/store/instruction-source.js +298 -0
  189. package/dist/src/store/origin-evidence.d.ts +21 -0
  190. package/dist/src/store/origin-evidence.js +66 -0
  191. package/dist/src/store/pre-commit-verifier.d.ts +68 -0
  192. package/dist/src/store/pre-commit-verifier.js +418 -0
  193. package/dist/src/store/resolve.d.ts +138 -0
  194. package/dist/src/store/resolve.js +363 -0
  195. package/dist/src/store/types.d.ts +288 -0
  196. package/dist/src/store/types.js +356 -0
  197. package/dist/src/store.d.ts +33 -1
  198. package/dist/src/store.js +57 -4
  199. package/dist/src/types.d.ts +146 -12
  200. package/dist/src/untar.d.ts +8 -23
  201. package/dist/src/untar.js +10 -136
  202. package/docs/agent-runner.md +86 -0
  203. package/docs/authoring.md +64 -6
  204. package/docs/bundles.md +350 -0
  205. package/docs/cli.md +1298 -106
  206. package/docs/crypto.md +832 -0
  207. package/docs/design.md +342 -20
  208. package/docs/embedding.md +132 -25
  209. package/docs/shift-logs.md +642 -0
  210. package/docs/wire-contracts.md +395 -0
  211. package/package.json +6 -1
  212. package/plugins/claude-code/.claude-plugin/marketplace.json +14 -0
  213. package/plugins/claude-code/plugin/.claude-plugin/plugin.json +8 -0
  214. package/plugins/claude-code/plugin/.mcp.json +14 -0
  215. package/plugins/claude-code/plugin/hooks/hooks.json +26 -0
  216. package/plugins/claude-code/plugin/hooks/session-end.sh +40 -0
  217. package/plugins/claude-code/plugin/hooks/session-start.sh +52 -0
  218. package/plugins/claude-code/plugin/skills/author/SKILL.md +181 -0
  219. package/plugins/claude-code/plugin/skills/conduct/SKILL.md +106 -0
  220. package/plugins/claude-code/plugin/skills/shift/SKILL.md +105 -0
  221. package/plugins/codex/.agents/plugins/marketplace.json +21 -0
  222. package/plugins/codex/plugins/owenloop/.codex-plugin/plugin.json +7 -0
  223. package/plugins/codex/plugins/owenloop/.mcp.json +20 -0
  224. package/plugins/codex/plugins/owenloop/hooks/hooks.json +26 -0
  225. package/plugins/codex/plugins/owenloop/hooks/session-end.sh +40 -0
  226. package/plugins/codex/plugins/owenloop/hooks/session-start.sh +52 -0
  227. package/plugins/codex/plugins/owenloop/skills/author/SKILL.md +181 -0
  228. package/plugins/codex/plugins/owenloop/skills/conduct/SKILL.md +106 -0
  229. package/plugins/codex/plugins/owenloop/skills/shift/SKILL.md +105 -0
@@ -147,16 +147,20 @@ export function readLatestBundle(cacheDir, name) {
147
147
  }
148
148
  return best;
149
149
  }
150
+ class StaleStepSpecError extends Error {
151
+ }
150
152
  /**
151
153
  * Read the normalized step spec for `(name, hash, step)`, or `null` when it is
152
154
  * absent, unreadable, or not valid JSON. Layout mirrors `writeBundle`:
153
155
  * `<cacheDir>/bundles/<name>/<hash>/steps/<step>.json`.
154
156
  *
155
- * Never throws — a missing spec is a normal case the caller reports and moves
156
- * past (a command step has no spec by design, and a hash dir prepared before
157
- * Phase 5 has none at all). There is deliberately NO fallback to the legacy
158
- * `templates/<step>.md`: silently running a stale compiled template would be
159
- * worse than an honest "re-run `owenloop work prepare`".
157
+ * A missing or corrupt spec returns `null` — a command step has no spec by
158
+ * design, and a hash dir prepared before Phase 5 has none at all. The one
159
+ * deliberate exception is a structurally valid old normalized spec that leaves
160
+ * `filesystem` or `network` buried under `permissions.extensions`: that cache
161
+ * throws with an explicit `owenloop work prepare` repair because returning it
162
+ * would silently discard an authored restriction. There is no fallback to the
163
+ * legacy `templates/<step>.md`.
160
164
  */
161
165
  export function readStepSpec(cacheDir, name, hash, step) {
162
166
  try {
@@ -172,9 +176,19 @@ export function readStepSpec(cacheDir, name, hash, step) {
172
176
  const parsed = JSON.parse(readFileSync(file, 'utf8'));
173
177
  if (typeof parsed?.step !== 'string' || typeof parsed?.brief !== 'string')
174
178
  return null;
179
+ const extensions = parsed.permissions?.extensions;
180
+ if (extensions !== null && typeof extensions === 'object' && !Array.isArray(extensions)) {
181
+ const stale = ['filesystem', 'network'].filter((key) => key in extensions);
182
+ if (stale.length > 0) {
183
+ throw new StaleStepSpecError(`cached step '${step}' has restriction(s) ${stale.join(', ')} buried in permissions.extensions; ` +
184
+ 'rerun `owenloop work prepare`');
185
+ }
186
+ }
175
187
  return parsed;
176
188
  }
177
- catch {
189
+ catch (error) {
190
+ if (error instanceof StaleStepSpecError)
191
+ throw error;
178
192
  return null;
179
193
  }
180
194
  }
@@ -50,11 +50,13 @@ export interface HarnessCarrier {
50
50
  * forward compat for the day the grammar promotes the field, and it means the
51
51
  * promotion needs no owenloop change.
52
52
  *
53
- * Validation is loose but HONEST: a non-map `x.harness`, a non-string
54
- * `x.harness.id`, a non-string top-level `harness`, or a surviving legacy
55
- * `x.claude-code` bag is a def error naming the step, never a silent drop.
56
- * Absent keys are simply absent — a step with no harness declaration is
57
- * completely normal.
53
+ * Validation is loose but HONEST: a non-map `x.harness`, a non-string or
54
+ * blank `x.harness.id`, a non-string or blank top-level `harness`, or a
55
+ * surviving legacy `x.claude-code` bag is a def error naming the step, never a
56
+ * silent drop. Absent keys are simply absent — a step with no harness
57
+ * declaration is completely normal. Blank is not absent: accepting an explicit
58
+ * empty id would let adapter resolution treat an invalid declaration as though
59
+ * the author had selected the default harness.
58
60
  *
59
61
  * `label` names the def, `stepName` the step, so an error in a pinned child is
60
62
  * attributable. Shared with `owenloop work lint`, which parses raw YAML defs.
@@ -20,6 +20,7 @@
20
20
  * a broken closure fails at fetch, never mid-run — see `validateFetchedDef`.
21
21
  */
22
22
  import { HubError } from "../hub/types.js";
23
+ import { validateHarnessOptions } from "../harness/permissions.js";
23
24
  /** A step is an agent step (spec candidate) when it is not a command/calls step. */
24
25
  function isAgentStep(s) {
25
26
  return s.executor !== 'command' && s.calls === undefined;
@@ -61,11 +62,13 @@ function isPlainMap(v) {
61
62
  * forward compat for the day the grammar promotes the field, and it means the
62
63
  * promotion needs no owenloop change.
63
64
  *
64
- * Validation is loose but HONEST: a non-map `x.harness`, a non-string
65
- * `x.harness.id`, a non-string top-level `harness`, or a surviving legacy
66
- * `x.claude-code` bag is a def error naming the step, never a silent drop.
67
- * Absent keys are simply absent — a step with no harness declaration is
68
- * completely normal.
65
+ * Validation is loose but HONEST: a non-map `x.harness`, a non-string or
66
+ * blank `x.harness.id`, a non-string or blank top-level `harness`, or a
67
+ * surviving legacy `x.claude-code` bag is a def error naming the step, never a
68
+ * silent drop. Absent keys are simply absent — a step with no harness
69
+ * declaration is completely normal. Blank is not absent: accepting an explicit
70
+ * empty id would let adapter resolution treat an invalid declaration as though
71
+ * the author had selected the default harness.
69
72
  *
70
73
  * `label` names the def, `stepName` the step, so an error in a pinned child is
71
74
  * attributable. Shared with `owenloop work lint`, which parses raw YAML defs.
@@ -73,8 +76,13 @@ function isPlainMap(v) {
73
76
  export function parseHarnessCarrier(s, label, stepName) {
74
77
  const where = `hub workflow '${label}': step '${stepName}'`;
75
78
  const topLevel = s['harness'];
76
- if (topLevel !== undefined && typeof topLevel !== 'string') {
77
- throw new Error(`${where} has a non-string harness`);
79
+ if (topLevel !== undefined) {
80
+ if (typeof topLevel !== 'string') {
81
+ throw new Error(`${where} has a non-string harness`);
82
+ }
83
+ if (topLevel.trim() === '') {
84
+ throw new Error(`${where} has an empty or whitespace-only harness`);
85
+ }
78
86
  }
79
87
  const x = s['x'];
80
88
  // A def still carrying the OLD bag key fails HARD here, before anything else
@@ -106,6 +114,9 @@ export function parseHarnessCarrier(s, label, stepName) {
106
114
  if (typeof v !== 'string') {
107
115
  throw new Error(`${where} has a non-string x.${HARNESS_BAG_KEY}.id`);
108
116
  }
117
+ if (v.trim() === '') {
118
+ throw new Error(`${where} has an empty or whitespace-only x.${HARNESS_BAG_KEY}.id`);
119
+ }
109
120
  bagId = v;
110
121
  continue;
111
122
  }
@@ -154,6 +165,15 @@ function validateDefEnvelope(data, label) {
154
165
  // still carried through verbatim — `x.owenloop` is a separate live
155
166
  // namespace that `src/shift/routing.ts` reads.
156
167
  const carrier = parseHarnessCarrier(s, label, s['name']);
168
+ if (carrier.harnessOptions !== undefined) {
169
+ const errors = validateHarnessOptions(carrier.harnessOptions, s['name'])
170
+ .filter((finding) => finding.severity === 'error');
171
+ if (errors.length > 0) {
172
+ throw new Error(errors
173
+ .map((finding) => `hub workflow '${label}': step '${finding.step}' x.harness.${finding.field ?? '<bag>'}: ${finding.message}`)
174
+ .join('; '));
175
+ }
176
+ }
157
177
  return {
158
178
  name: s['name'],
159
179
  consumes: Array.isArray(s['consumes']) ? s['consumes'] : undefined,
@@ -123,9 +123,10 @@ export interface FetchedBundle extends FetchedDef {
123
123
  children?: Record<string, FetchedDef>;
124
124
  }
125
125
  /**
126
- * The normalized per-step spec `prepare` writes to `steps/<step>.json`, one file
127
- * per dispatchable agent step (D4). It replaces the legacy `templates/<step>.md`
128
- * compiled frontmatter entirely.
126
+ * The prepare cache may persist routing metadata for `steps/<step>.json`, but
127
+ * cached instruction text is not authoritative for execution. Runtime workers
128
+ * resolve the verified step body from the local workflow store. The cache
129
+ * replaces the legacy `templates/<step>.md` compiled frontmatter entirely.
129
130
  *
130
131
  * What is DELIBERATELY not in here: the four substitution tokens
131
132
  * (`ORDER_TOKEN`, `ORIGIN_TOKEN`, `ACCOUNT_TOKEN`, `SHIFT_TOKEN`) and the
@@ -138,7 +139,10 @@ export interface FetchedBundle extends FetchedDef {
138
139
  *
139
140
  * There is no format version and no migration. A hash dir cached before Phase 5
140
141
  * simply has no `steps/*.json`; `readStepSpec` fails honestly and the fix is to
141
- * re-run `owenloop work prepare`.
142
+ * re-run `owenloop work prepare`. A cache normalized before filesystem/network
143
+ * became neutral fields may carry those restrictions under
144
+ * `permissions.extensions`; runtime preflight refuses that shape rather than
145
+ * ignoring the restriction and tells the operator to rerun prepare.
142
146
  */
143
147
  export interface NormalizedStepSpec {
144
148
  /** The step name — the same string that names the file. */
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Driver-side binding for consume-side artifact verification.
3
+ *
4
+ * This module resolves local trust material from injected environment state and
5
+ * gates a complete order before the order reaches an agent prompt or command
6
+ * execution. A failed path refuses the whole order; the gate never removes only
7
+ * the offending value and continues with a partial packet.
8
+ */
9
+ import type { DefPolicy } from '../../../src/crypto/verify-publication.ts';
10
+ import type { PolicyFloor } from '../../../src/crypto/records.ts';
11
+ import { type VerifyConsumedOptions } from '../../../src/crypto/verify-consumed.ts';
12
+ import type { OrderPacket } from './hub/types.ts';
13
+ export interface ConsumedGateResult {
14
+ ok: true;
15
+ order: OrderPacket;
16
+ warnings: string[];
17
+ }
18
+ export interface ConsumedGateRefusal {
19
+ ok: false;
20
+ reason: string;
21
+ }
22
+ export interface CreateConsumedVerifierArgs {
23
+ env: Record<string, string | undefined>;
24
+ now: () => number;
25
+ artifactPolicy?: DefPolicy;
26
+ policyFloor?: PolicyFloor;
27
+ /** Explicit producer scope demand; the default is no additional demand. */
28
+ demand?: {
29
+ pool?: string;
30
+ label?: string;
31
+ namespace?: string;
32
+ };
33
+ /** Cryptographic seam for hermetic tests and alternate signer backends. */
34
+ signerForPrincipal?: VerifyConsumedOptions['signerForPrincipal'];
35
+ namespace?: string;
36
+ maxChainDepth?: number;
37
+ /** Optional warning sink; the returned warnings remain authoritative for callers. */
38
+ warn?: (line: string) => void;
39
+ }
40
+ export type ConsumedVerifier = (order: OrderPacket, opts: {
41
+ hardRule: boolean;
42
+ }) => Promise<ConsumedGateResult | ConsumedGateRefusal>;
43
+ /** Create a verifier bound to one injected local environment and clock. */
44
+ export declare function createConsumedVerifier(args: CreateConsumedVerifierArgs): ConsumedVerifier;
45
+ export declare function resetConsumedVerifierWarningsForTests(): void;
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Driver-side binding for consume-side artifact verification.
3
+ *
4
+ * This module resolves local trust material from injected environment state and
5
+ * gates a complete order before the order reaches an agent prompt or command
6
+ * execution. A failed path refuses the whole order; the gate never removes only
7
+ * the offending value and continues with a partial packet.
8
+ */
9
+ import { mergePolicyFloorWithLocal, } from "../../../src/crypto/policy-floor.js";
10
+ import { resolveArtifactPolicy, } from "../../../src/store/pre-commit-verifier.js";
11
+ import { loadRevocations, loadRoster, resolveOrgRoot, } from "../../../src/crypto/org-root.js";
12
+ import { verifyConsumed, } from "../../../src/crypto/verify-consumed.js";
13
+ import { validateProducer } from "../../../src/crypto/chain.js";
14
+ /** Missing-org-root warnings are intentionally latched once per order process-wide. */
15
+ const warnedMissingOrgRootOrders = new Set();
16
+ function errorText(error) {
17
+ return error instanceof Error ? error.message : String(error);
18
+ }
19
+ function linkFor(verdict) {
20
+ const match = verdict.reason.match(/^(no-proof|signature|value-digest|version|chain|scope|prerequisite):/);
21
+ if (match !== null)
22
+ return match[1];
23
+ return verdict.kind === 'invalid' ? 'signature' : 'prerequisite';
24
+ }
25
+ function detailFor(verdict) {
26
+ if (verdict.kind === 'verified')
27
+ return 'verified';
28
+ if (verdict.kind === 'absent')
29
+ return 'no proof was supplied';
30
+ const colon = verdict.reason.indexOf(':');
31
+ return colon === -1 ? verdict.reason : verdict.reason.slice(colon + 1).trim();
32
+ }
33
+ function refusal(order, link, path, detail) {
34
+ return {
35
+ ok: false,
36
+ reason: `consumed artifact refusal (${link}) for ${order.workflow}/${order.run} step '${order.step}' artifact '${path}': ${detail}`,
37
+ };
38
+ }
39
+ function policyOutcome(order, hardRule, policy, path, verdict, warnings) {
40
+ if (verdict.kind === 'verified')
41
+ return undefined;
42
+ const link = verdict.kind === 'absent' ? 'no-proof' : linkFor(verdict);
43
+ const detail = detailFor(verdict);
44
+ const mustRefuse = verdict.kind === 'invalid' || hardRule || policy === 'enforce';
45
+ if (mustRefuse)
46
+ return refusal(order, link, path, detail);
47
+ if (policy === 'warn') {
48
+ const missingOrgRoot = verdict.kind === 'unverifiable'
49
+ && verdict.reason === 'prerequisite: no org-root anchor is configured';
50
+ const orderKey = `${order.workflow}/${order.run}`;
51
+ if (!missingOrgRoot || !warnedMissingOrgRootOrders.has(orderKey)) {
52
+ warnings.push(`consumed artifact warning (${link}) for ${order.workflow}/${order.run} step '${order.step}' artifact '${path}': ${detail}`);
53
+ if (missingOrgRoot)
54
+ warnedMissingOrgRootOrders.add(orderKey);
55
+ }
56
+ }
57
+ return undefined;
58
+ }
59
+ function parseProofMap(order) {
60
+ if (order.consumesProof === undefined)
61
+ return { kind: 'ok', proofs: {} };
62
+ if (typeof order.consumesProof !== 'string') {
63
+ return { kind: 'unverifiable', reason: 'prerequisite: consumesProof is not a JSON string' };
64
+ }
65
+ let parsed;
66
+ try {
67
+ parsed = JSON.parse(order.consumesProof);
68
+ }
69
+ catch {
70
+ return { kind: 'unverifiable', reason: 'prerequisite: consumesProof is not valid JSON' };
71
+ }
72
+ if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
73
+ return { kind: 'unverifiable', reason: 'prerequisite: consumesProof is not a JSON object' };
74
+ }
75
+ const proofs = {};
76
+ for (const [path, value] of Object.entries(parsed)) {
77
+ if (typeof value !== 'string' || value === '') {
78
+ return { kind: 'unverifiable', reason: `prerequisite: consumesProof entry for artifact '${path}' is not a non-empty string` };
79
+ }
80
+ proofs[path] = value;
81
+ }
82
+ return { kind: 'ok', proofs };
83
+ }
84
+ /** Create a verifier bound to one injected local environment and clock. */
85
+ export function createConsumedVerifier(args) {
86
+ const policy = mergePolicyFloorWithLocal('off', args.policyFloor, 'off', resolveArtifactPolicy(args.env, args.artifactPolicy)).artifactPolicy;
87
+ return async (order, opts) => {
88
+ if (Object.keys(order.consumes).length === 0 && order.owes.every((owed) => owed.reasons.length === 0 && owed.proof === undefined)) {
89
+ return { ok: true, order, warnings: [] };
90
+ }
91
+ // One consumer-owned clock sample governs every path in this gate call.
92
+ // Revocation is evaluated at consume time; a later per-path sample would
93
+ // make one order internally inconsistent and defeat the cache key below.
94
+ const at = args.now();
95
+ let root;
96
+ let grants;
97
+ let revocations;
98
+ try {
99
+ root = resolveOrgRoot(args.env);
100
+ grants = loadRoster(args.env);
101
+ revocations = loadRevocations(args.env);
102
+ }
103
+ catch (error) {
104
+ const reason = `prerequisite: local producer trust material could not be loaded: ${errorText(error)}`;
105
+ const warnings = [];
106
+ for (const path of Object.keys(order.consumes)) {
107
+ const result = policyOutcome(order, opts.hardRule, policy, path, { kind: 'unverifiable', reason }, warnings);
108
+ if (result !== undefined)
109
+ return result;
110
+ }
111
+ for (const owed of order.owes) {
112
+ if (owed.reasons.length === 0 && owed.proof === undefined)
113
+ continue;
114
+ const result = policyOutcome(order, opts.hardRule, policy, owed.path, { kind: 'unverifiable', reason }, warnings);
115
+ if (result !== undefined)
116
+ return result;
117
+ }
118
+ return { ok: true, order, warnings };
119
+ }
120
+ const prerequisite = root.kind === 'absent'
121
+ ? 'prerequisite: no org-root anchor is configured'
122
+ : undefined;
123
+ const rootPublicKey = root.kind === 'present' ? root.publicKey : '';
124
+ const proofs = parseProofMap(order);
125
+ const warnings = [];
126
+ const chainCache = new Map();
127
+ const chainValidator = (input, options) => {
128
+ const cacheKey = JSON.stringify([input.targetKeyId, input.at]);
129
+ const cached = chainCache.get(cacheKey);
130
+ if (cached !== undefined)
131
+ return cached;
132
+ const pending = validateProducer(input, options);
133
+ chainCache.set(cacheKey, pending);
134
+ return pending;
135
+ };
136
+ const verifierOptions = {
137
+ ...(args.signerForPrincipal === undefined ? {} : { signerForPrincipal: args.signerForPrincipal }),
138
+ ...(args.namespace === undefined ? {} : { namespace: args.namespace }),
139
+ ...(args.maxChainDepth === undefined ? {} : { maxChainDepth: args.maxChainDepth }),
140
+ chainValidator,
141
+ warn: (line) => {
142
+ warnings.push(line);
143
+ },
144
+ };
145
+ for (const [path, value] of Object.entries(order.consumes)) {
146
+ let verdict;
147
+ if (prerequisite !== undefined) {
148
+ verdict = { kind: 'unverifiable', reason: prerequisite };
149
+ }
150
+ else if (proofs.kind === 'unverifiable') {
151
+ verdict = { kind: 'unverifiable', reason: proofs.reason };
152
+ }
153
+ else {
154
+ verdict = await verifyConsumed({
155
+ path,
156
+ value,
157
+ ...(proofs.proofs[path] === undefined ? {} : { proof: proofs.proofs[path] }),
158
+ ...(order.consumedFingerprint?.[path] === undefined ? {} : { expectedVersion: order.consumedFingerprint[path] }),
159
+ orgRootPublicKey: rootPublicKey,
160
+ grants,
161
+ revocations,
162
+ at,
163
+ demand: args.demand ?? {},
164
+ }, verifierOptions);
165
+ }
166
+ const result = policyOutcome(order, opts.hardRule, policy, path, verdict, warnings);
167
+ if (result !== undefined)
168
+ return result;
169
+ }
170
+ for (const owed of order.owes) {
171
+ if (owed.reasons.length === 0 && owed.proof === undefined)
172
+ continue;
173
+ let verdict;
174
+ if (prerequisite !== undefined) {
175
+ verdict = { kind: 'unverifiable', reason: prerequisite };
176
+ }
177
+ else if (proofs.kind === 'unverifiable') {
178
+ verdict = { kind: 'unverifiable', reason: proofs.reason };
179
+ }
180
+ else {
181
+ verdict = await verifyConsumed({
182
+ path: owed.path,
183
+ value: owed.reasons,
184
+ ...(owed.proof === undefined ? {} : { proof: owed.proof }),
185
+ ...(owed.version === undefined ? {} : { expectedVersion: owed.version }),
186
+ orgRootPublicKey: rootPublicKey,
187
+ grants,
188
+ revocations,
189
+ at,
190
+ demand: args.demand ?? {},
191
+ }, verifierOptions);
192
+ }
193
+ const result = policyOutcome(order, opts.hardRule, policy, owed.path, verdict, warnings);
194
+ if (result !== undefined)
195
+ return result;
196
+ }
197
+ const deduped = [...new Set(warnings)];
198
+ for (const warning of deduped)
199
+ args.warn?.(warning);
200
+ return { ok: true, order, warnings: deduped };
201
+ };
202
+ }
203
+ export function resetConsumedVerifierWarningsForTests() {
204
+ warnedMissingOrgRootOrders.clear();
205
+ }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Driver-side instruction resolution.
3
+ *
4
+ * A worker packet is a reference packet. The packet's `defDigest` selects
5
+ * instruction bytes from a verified local workflow-store object; command text,
6
+ * prompt text, and step metadata supplied by a remote coordinator are never
7
+ * authoritative. The command resolver returns the exact authored command and
8
+ * does not perform placeholder substitution.
9
+ */
10
+ import type { BundleIngestor, StoreInstructionSource } from '../../../../src/store/index.ts';
11
+ import type { StepDef, WorkflowDef } from '../../../../src/types.ts';
12
+ import type { DefPolicy, DefVerdict } from '../../../../src/crypto/verify-publication.ts';
13
+ import type { OriginRules } from '../../../../src/crypto/origin-rules.ts';
14
+ import type { OriginVerdict } from '../../../../src/crypto/verify-origin.ts';
15
+ import type { PolicyFloor } from '../../../../src/crypto/records.ts';
16
+ import type { OrderPacket } from '../hub/types.ts';
17
+ import type { ConsumedVerifier } from '../consumed-verifier.ts';
18
+ export type InstructionRefusalKind = 'unknown-digest' | 'unknown-step' | 'ambiguous-step' | 'integrity' | 'no-digest' | 'missing-command' | 'unverified-def' | 'origin-policy' | 'unverified-consumed';
19
+ export interface InstructionRefusal {
20
+ ok: false;
21
+ reason: string;
22
+ kind: InstructionRefusalKind;
23
+ }
24
+ export interface ResolvedCommand {
25
+ ok: true;
26
+ command: string;
27
+ /** Verified installed bundle root, when resolution has bundle provenance. */
28
+ bundleDir?: string;
29
+ }
30
+ export interface ResolvedStep {
31
+ ok: true;
32
+ step: StepDef;
33
+ /** Verified installed bundle root, when resolution has bundle provenance. */
34
+ bundleDir?: string;
35
+ }
36
+ export interface InstructionResolver {
37
+ resolveCommand(order: OrderPacket): Promise<ResolvedCommand | InstructionRefusal>;
38
+ resolveStep(order: OrderPacket): Promise<ResolvedStep | InstructionRefusal>;
39
+ }
40
+ export interface DefinitionVerifierInput {
41
+ defDigest: string;
42
+ definition: WorkflowDef;
43
+ step: StepDef;
44
+ /** Bundle digest and installed object path used to locate publication evidence. */
45
+ bundleDigest: string;
46
+ objectPath: string;
47
+ }
48
+ /**
49
+ * Execution-time trust check. The verifier is called after the store has
50
+ * re-verified the installed bytes and before instruction text is returned.
51
+ * Returning a verdict keeps unsigned/unverifiable distinct from an invalid
52
+ * present signature; throwing is treated as an integrity-style refusal.
53
+ */
54
+ export type DefinitionVerifier = (input: DefinitionVerifierInput) => Promise<DefVerdict> | DefVerdict;
55
+ /** Execution-time origin verifier bound to installed bundle evidence. */
56
+ export interface OriginVerifierInput {
57
+ bundleDigest: string;
58
+ objectPath: string;
59
+ }
60
+ export type OriginVerifier = (input: OriginVerifierInput) => Promise<OriginVerdict> | OriginVerdict;
61
+ export interface StoreInstructionResolverOptions {
62
+ projectRoot?: string;
63
+ globalRoot: string;
64
+ verifier: BundleIngestor;
65
+ source?: StoreInstructionSource;
66
+ /** Optional execution-time publication verifier. Omitted means publication trust is unverifiable and command orders refuse. */
67
+ definitionVerifier?: DefinitionVerifier;
68
+ /** Optional execution-time origin verifier. */
69
+ originVerifier?: OriginVerifier;
70
+ /** Gate dynamic consumed values before a command can reach the shell. */
71
+ consumedVerifier?: ConsumedVerifier;
72
+ /** Explicit publication policy override; otherwise env > settings file > warn. */
73
+ defPolicy?: DefPolicy;
74
+ /** Explicit origin policy override; otherwise env > settings file > warn. */
75
+ originPolicy?: DefPolicy;
76
+ /** Explicit namespace-scoped origin rules. */
77
+ originRules?: OriginRules;
78
+ /** Already-verified organization floor; absent means local policy only. */
79
+ policyFloor?: PolicyFloor;
80
+ /** Diagnostic sink for `defPolicy=warn` and `originPolicy=warn`. Defaults to stderr. */
81
+ warn?: (line: string) => void;
82
+ /** Injected environment used for policy resolution. */
83
+ env?: Record<string, string | undefined>;
84
+ }
85
+ export declare function createStoreInstructionResolver(options: StoreInstructionResolverOptions): InstructionResolver;
86
+ /** Build the production resolver from injected process environment and cwd. */
87
+ export declare function createDefaultStoreInstructionResolver(args: {
88
+ cwd: string;
89
+ env: Record<string, string | undefined>;
90
+ verifier?: BundleIngestor;
91
+ definitionVerifier?: DefinitionVerifier;
92
+ originVerifier?: OriginVerifier;
93
+ consumedVerifier?: ConsumedVerifier;
94
+ defPolicy?: DefPolicy;
95
+ originPolicy?: DefPolicy;
96
+ originRules?: OriginRules;
97
+ policyFloor?: PolicyFloor;
98
+ warn?: (line: string) => void;
99
+ }): InstructionResolver;