owenloop 0.5.1 → 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 (81) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +9 -2
  3. package/dist/packages/work/src/agent/brief.d.ts +21 -0
  4. package/dist/packages/work/src/agent/brief.js +42 -1
  5. package/dist/packages/work/src/agent/capability-model.d.ts +123 -0
  6. package/dist/packages/work/src/agent/capability-model.js +148 -0
  7. package/dist/packages/work/src/agent/loop.d.ts +47 -4
  8. package/dist/packages/work/src/agent/loop.js +83 -53
  9. package/dist/packages/work/src/exec/loop.d.ts +17 -0
  10. package/dist/packages/work/src/exec/loop.js +195 -34
  11. package/dist/packages/work/src/exec/runner.js +15 -5
  12. package/dist/packages/work/src/harness/session-store.d.ts +3 -1
  13. package/dist/packages/work/src/harness/session-store.js +46 -22
  14. package/dist/packages/work/src/hold/mcp.js +9 -2
  15. package/dist/packages/work/src/hub/client.d.ts +7 -1
  16. package/dist/packages/work/src/hub/client.js +1 -0
  17. package/dist/packages/work/src/hub/types.d.ts +71 -3
  18. package/dist/packages/work/src/roles/agent-run.js +8 -9
  19. package/dist/packages/work/src/roles/hold.d.ts +2 -0
  20. package/dist/packages/work/src/roles/hold.js +39 -8
  21. package/dist/packages/work/src/roles/settings.js +18 -6
  22. package/dist/packages/work/src/roles/shift.js +13 -1
  23. package/dist/packages/work/src/settings/settings.d.ts +60 -7
  24. package/dist/packages/work/src/settings/settings.js +69 -22
  25. package/dist/packages/work/src/shift/logretention.d.ts +253 -0
  26. package/dist/packages/work/src/shift/logretention.js +407 -0
  27. package/dist/packages/work/src/shift/logsink.d.ts +14 -0
  28. package/dist/packages/work/src/shift/logsink.js +98 -0
  29. package/dist/packages/work/src/shift/loop.d.ts +18 -1
  30. package/dist/packages/work/src/shift/loop.js +250 -12
  31. package/dist/packages/work/src/shift/protocol.d.ts +124 -1
  32. package/dist/packages/work/src/shift/protocol.js +14 -0
  33. package/dist/packages/work/src/shift/runtime.d.ts +14 -0
  34. package/dist/packages/work/src/shift/runtime.js +229 -6
  35. package/dist/packages/work/src/shift/server.d.ts +13 -0
  36. package/dist/packages/work/src/shift/server.js +61 -36
  37. package/dist/packages/work/src/shift/spawn.d.ts +78 -5
  38. package/dist/packages/work/src/shift/spawn.js +158 -12
  39. package/dist/packages/work/src/shift/state.d.ts +25 -0
  40. package/dist/packages/work/src/shift/state.js +29 -5
  41. package/dist/packages/work/src/shift/truncate.d.ts +35 -0
  42. package/dist/packages/work/src/shift/truncate.js +73 -0
  43. package/dist/packages/work/src/submit-proof.d.ts +31 -6
  44. package/dist/packages/work/src/submit-proof.js +33 -7
  45. package/dist/packages/work/src/submit-value.d.ts +48 -0
  46. package/dist/packages/work/src/submit-value.js +118 -0
  47. package/dist/packages/work/src/usage.d.ts +1 -1
  48. package/dist/packages/work/src/usage.js +11 -0
  49. package/dist/src/capabilities.d.ts +93 -0
  50. package/dist/src/capabilities.js +101 -0
  51. package/dist/src/cli.js +14 -9
  52. package/dist/src/crypto/records.d.ts +3 -0
  53. package/dist/src/crypto/records.js +11 -0
  54. package/dist/src/defs.js +192 -4
  55. package/dist/src/engine.d.ts +112 -3
  56. package/dist/src/engine.js +224 -28
  57. package/dist/src/hub.d.ts +27 -11
  58. package/dist/src/hub.js +24 -18
  59. package/dist/src/index.d.ts +3 -1
  60. package/dist/src/index.js +4 -1
  61. package/dist/src/mcp/serve.js +11 -4
  62. package/dist/src/model.d.ts +36 -0
  63. package/dist/src/model.js +46 -0
  64. package/dist/src/schemas/index.d.ts +17 -0
  65. package/dist/src/schemas/order.v1.schema.json +17 -3
  66. package/dist/src/store.d.ts +33 -1
  67. package/dist/src/store.js +57 -4
  68. package/dist/src/types.d.ts +102 -2
  69. package/docs/authoring.md +7 -0
  70. package/docs/bundles.md +103 -3
  71. package/docs/cli.md +127 -9
  72. package/docs/crypto.md +23 -7
  73. package/docs/shift-logs.md +642 -0
  74. package/docs/wire-contracts.md +34 -19
  75. package/package.json +1 -1
  76. package/plugins/claude-code/plugin/.claude-plugin/plugin.json +1 -1
  77. package/plugins/claude-code/plugin/.mcp.json +1 -1
  78. package/plugins/codex/plugins/owenloop/.codex-plugin/plugin.json +1 -1
  79. package/plugins/codex/plugins/owenloop/.mcp.json +1 -1
  80. package/dist/packages/work/src/agent/model-policy.d.ts +0 -49
  81. package/dist/packages/work/src/agent/model-policy.js +0 -138
@@ -62,7 +62,7 @@ import { isResumeUnavailable } from "../harness/contract.js";
62
62
  import { preflightStepPermissions } from "../harness/permissions.js";
63
63
  import { orderId } from "../harness/session-store.js";
64
64
  import { buildOwenloopMcp, renderBrief, renderRejection, renderReplayBrief, } from "./brief.js";
65
- import { pickModel, } from "./model-policy.js";
65
+ import { resolveCapabilityModel, } from "./capability-model.js";
66
66
  /** Default confirm-phase cadence: one `get_order` per second. */
67
67
  export const DEFAULT_CONFIRM_INTERVAL_MS = 1_000;
68
68
  /** Default confirm-phase bound: give the hub 15s to show the submit's outcome. */
@@ -70,60 +70,20 @@ export const DEFAULT_SUBMIT_GRACE_MS = 15_000;
70
70
  function errMsg(e) {
71
71
  return e instanceof Error ? e.message : String(e);
72
72
  }
73
- function laneFromPacket(packet) {
74
- const plan = packet.consumes?.['plan'];
75
- if (plan === null || typeof plan !== 'object' || Array.isArray(plan))
76
- return undefined;
77
- const lane = plan['lane'];
78
- return lane === 'express' || lane === 'standard' || lane === 'deep' ? lane : undefined;
79
- }
80
- function escalationFromPacket(packet, extensionKey) {
81
- const extension = packet.x?.[extensionKey];
82
- if (extension === null || typeof extension !== 'object' || Array.isArray(extension))
83
- return undefined;
84
- if (!('escalation' in extension))
85
- return undefined;
86
- const raw = extension['escalation'];
87
- if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
88
- return undefined;
89
- const bag = raw;
90
- const model = typeof bag['model'] === 'string' && bag['model'] !== '' ? bag['model'] : undefined;
91
- const attempts = typeof bag['attempts'] === 'number' && Number.isInteger(bag['attempts']) && bag['attempts'] > 0
92
- ? bag['attempts']
93
- : undefined;
94
- return {
95
- ...(model !== undefined ? { model } : {}),
96
- ...(attempts !== undefined ? { attempts } : {}),
97
- };
98
- }
99
73
  /**
100
- * The reject depth driving retry escalation: the max across the step's owed
101
- * outputs, with consult paths excluded.
74
+ * Resolve an order's composed capabilities against this machine's map.
102
75
  *
103
- * The exclusion matters. A consult artifact's rejects are the mentor handing
104
- * advice back for another round, not a signal that this producer is stuck — and
105
- * they ride a separate per-produce attempt budget for exactly that reason.
106
- * Counting them would let one ordinary mentor round escalate the model on every
107
- * later firing of the step, permanently, on a producer that was never failing.
76
+ * ONLY `packet.capabilities` is consulted. `packet.model` — the def's authored
77
+ * tier name under the retired scheme — is deliberately ignored: a tier is an
78
+ * abstract rung with no meaning left, and reading it as a model id would put a
79
+ * literal `strong` on the wire to a vendor.
108
80
  */
109
- function judgmentRejectsFromPacket(packet) {
110
- let max = 0;
111
- for (const owed of packet.owes) {
112
- const path = String(owed.path ?? '');
113
- if (path === 'consultRequest' || path.endsWith('Consult'))
114
- continue;
115
- const rejects = owed.judgmentRejects;
116
- if (typeof rejects === 'number' && Number.isFinite(rejects) && rejects > max)
117
- max = rejects;
118
- }
119
- return max;
120
- }
121
- function modelForPacket(packet, policy) {
122
- if (packet.model === undefined)
123
- return undefined;
124
- if (policy === undefined)
125
- return { model: packet.model };
126
- return pickModel(packet.model, laneFromPacket(packet), judgmentRejectsFromPacket(packet), policy, undefined, escalationFromPacket(packet, policy.escalationExtensionKey));
81
+ export function resolveOrderRouting(packet, map) {
82
+ const capabilities = packet.capabilities ?? [];
83
+ if (capabilities.length === 0)
84
+ return { kind: 'unrouted' };
85
+ const resolution = map === undefined ? undefined : resolveCapabilityModel(map, capabilities);
86
+ return resolution === undefined ? { kind: 'refused', capabilities } : { kind: 'resolved', resolution };
127
87
  }
128
88
  /**
129
89
  * Poll `get_order` at `intervalMs` for at most `graceMs`, answering ONE
@@ -315,6 +275,40 @@ export function createAgentRunLoop(opts) {
315
275
  return;
316
276
  }
317
277
  }
278
+ /**
279
+ * Tell the hub what this shift resolved the order to (plan §6), before the
280
+ * harness launches.
281
+ *
282
+ * BEST EFFORT, ALWAYS. A hub that is unreachable, older than the verb, or
283
+ * simply erroring must never be able to stop work the shift is otherwise
284
+ * ready to do — the report is observability, not a gate, and the hub's own
285
+ * verb says so. A failure is logged and swallowed.
286
+ *
287
+ * An `unrouted` order is not reported at all: the payload's `capability` is
288
+ * required and non-empty on the hub side, and there is no capability to name.
289
+ */
290
+ async function reportResolution(routing, harness) {
291
+ if (routing.kind === 'unrouted')
292
+ return;
293
+ const payload = routing.kind === 'resolved'
294
+ ? {
295
+ capability: routing.resolution.capability,
296
+ match: routing.resolution.match,
297
+ model: routing.resolution.model,
298
+ effort: routing.resolution.effort,
299
+ harness,
300
+ }
301
+ : // The refusal names the capability the order was OFFERED under — the
302
+ // first one — because that is the row an operator has to add. Model
303
+ // and effort are absent: nothing was selected.
304
+ { capability: routing.capabilities[0] ?? '', match: 'refused', harness };
305
+ try {
306
+ await opts.hub.reportResolution({ workflow, run: runId, resolution: payload });
307
+ }
308
+ catch (e) {
309
+ opts.err(`owenloop work agent-run: reporting the resolution for ${order} failed (continuing): ${errMsg(e)}`);
310
+ }
311
+ }
318
312
  /** Release and finish. Used by every path that hands the order back. */
319
313
  async function releaseWith(reason, outcome) {
320
314
  lease.stop(reason);
@@ -404,6 +398,10 @@ export function createAgentRunLoop(opts) {
404
398
  origin: opts.origin,
405
399
  account: opts.account,
406
400
  ...(opts.shiftId !== undefined ? { shiftId: opts.shiftId } : {}),
401
+ // Straight from the packet, not from settings: the modifier is the hub's
402
+ // statement about the RUN, and a shift has no say in it.
403
+ ...(packet.modifier !== undefined ? { modifier: packet.modifier } : {}),
404
+ ...(packet.escalated === true ? { escalated: true } : {}),
407
405
  };
408
406
  // Permissions arrive PRE-NORMALIZED in the spec. `prepare` ran
409
407
  // `normalizeStepPermissions` over the step's `x.harness` options at cache
@@ -424,6 +422,36 @@ export function createAgentRunLoop(opts) {
424
422
  }
425
423
  return releaseWith('incompatible-harness-policy', 'incompatible-harness-policy');
426
424
  }
425
+ // ---- ROUTING: which model serves this order, and telling the hub --------
426
+ //
427
+ // DELIBERATELY THE LAST GATE BEFORE DISPATCH, and deliberately AFTER every
428
+ // launch gate above (misroute, consumed verification, step spec, adapter,
429
+ // harness policy). Plan §3: the launch gates block on a malformed order
430
+ // before model selection is even attempted, so a refusal never has to be
431
+ // explained in terms of a model the order was never going to reach.
432
+ const routing = resolveOrderRouting(packet, opts.capabilityModels);
433
+ // Plan §6: the record must exist BEFORE tokens are spent, which is why this
434
+ // is a dedicated verb and not a field on the next heartbeat. It is reported
435
+ // on the refusal path too — a refusal is a true observation of what this
436
+ // shift resolved, and the whole point of the report is that the hub can say
437
+ // why an order did or did not run.
438
+ await reportResolution(routing, resolution.id);
439
+ if (routing.kind === 'refused') {
440
+ opts.err(`owenloop work agent-run: no settings row for ${order} — the order's capabilities ` +
441
+ `[${routing.capabilities.join(', ')}] match neither an exact row nor a bare name row in ` +
442
+ `'capabilityModels'. Add a row for one of them (a bare row such as ` +
443
+ `'${routing.capabilities[0]?.split(':')[0] ?? ''}' covers every modifier) and restart the shift. ` +
444
+ `Releasing — this shift will not pick a model it was not told to use.`);
445
+ return releaseWith('unresolvable-capability', 'unresolvable-capability');
446
+ }
447
+ if (routing.kind === 'unrouted') {
448
+ opts.err(`owenloop work agent-run: ${order} carries no capabilities, so no settings row applies — ` +
449
+ `running on '${resolution.id}''s own default model.`);
450
+ }
451
+ else {
452
+ opts.out(`owenloop work agent-run: ${order} resolved '${routing.resolution.capability}' ` +
453
+ `(${routing.resolution.match} row) → ${routing.resolution.model} at ${routing.resolution.effort}`);
454
+ }
427
455
  // ---- PHASE 4: resume or cold replay? -----------------------------------
428
456
  //
429
457
  // The delta first, because "is there anything new to say?" is one of the
@@ -485,7 +513,9 @@ export function createAgentRunLoop(opts) {
485
513
  deliveredReasonAt = delta.deliveredReasonAt;
486
514
  }
487
515
  const owenloopMcp = buildOwenloopMcp(spec);
488
- const resolvedModel = modelForPacket(packet, opts.modelPolicy);
516
+ const resolvedModel = routing.kind === 'resolved'
517
+ ? { model: routing.resolution.model, effort: routing.resolution.effort }
518
+ : undefined;
489
519
  const deliverArgs = {
490
520
  cwd: recordCwd,
491
521
  owenloopMcp,
@@ -4,6 +4,23 @@ import type { CommandRunner } from './runner.ts';
4
4
  import type { InstructionResolver } from './instructions.ts';
5
5
  import { type SubmissionKeyManager } from '../submit-proof.ts';
6
6
  import type { SshProcessAdapter } from '../../../../src/crypto/ssh.ts';
7
+ /**
8
+ * The largest serialized `consumes` payload delivered inline in
9
+ * `OWENLOOP_CONSUMES`; anything strictly larger goes to a file named by
10
+ * `OWENLOOP_CONSUMES_FILE`. Measured with `Buffer.byteLength(json, 'utf8')` —
11
+ * never `json.length`, which counts UTF-16 code units and under-reports every
12
+ * multi-byte character.
13
+ *
14
+ * 64 KiB, chosen against the binding OS limit rather than a round number:
15
+ * Linux `execve` enforces `MAX_ARG_STRLEN`, a hard PER-ENTRY cap of 131072
16
+ * bytes (32 × 4 KiB pages) independent of the total `ARG_MAX`; macOS has no
17
+ * per-entry cap but shares ~1 MB of `ARG_MAX` across argv AND the whole
18
+ * environment block, and the child environment here starts from a full
19
+ * inherited `process.env`. 64 KiB clears the Linux per-entry cap with room for
20
+ * the `OWENLOOP_CONSUMES=` prefix and leaves the rest of the macOS budget to
21
+ * the inherited block.
22
+ */
23
+ export declare const CONSUMES_INLINE_MAX_BYTES = 65536;
7
24
  /** Discriminated result of an exec run; the role maps these to exit codes. */
8
25
  export type ExecOutcome = 'submitted' | 'completed' | 'misroute' | 'unresolved-instructions' | 'killed' | 'lease-lost' | 'ownership-error' | 'hub-unreachable' | 'submit-rejected' | 'submit-failed' | 'rejected' | 'judge-rejected' | 'judge-no-verdict' | 'reject-failed' | 'stopped';
9
26
  export interface ExecLoopOptions {
@@ -39,15 +39,117 @@
39
39
  * The role (`src/roles/exec.ts`) maps each `ExecOutcome` to an exit code.
40
40
  */
41
41
  import { createHash } from 'node:crypto';
42
+ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
43
+ import { tmpdir } from 'node:os';
44
+ import { join, resolve } from 'node:path';
42
45
  import { createLeaseLoop } from "../lease/loop.js";
43
46
  import { parsePayloadLine } from "./payload.js";
44
47
  import { buildReceipt } from "./receipt.js";
45
48
  import { buildSubmitProof } from "../submit-proof.js";
46
49
  /** sha256 of the empty byte string — the hash for a run with no captured output. */
47
50
  const EMPTY_HASH = createHash('sha256').digest('hex');
51
+ /**
52
+ * The largest serialized `consumes` payload delivered inline in
53
+ * `OWENLOOP_CONSUMES`; anything strictly larger goes to a file named by
54
+ * `OWENLOOP_CONSUMES_FILE`. Measured with `Buffer.byteLength(json, 'utf8')` —
55
+ * never `json.length`, which counts UTF-16 code units and under-reports every
56
+ * multi-byte character.
57
+ *
58
+ * 64 KiB, chosen against the binding OS limit rather than a round number:
59
+ * Linux `execve` enforces `MAX_ARG_STRLEN`, a hard PER-ENTRY cap of 131072
60
+ * bytes (32 × 4 KiB pages) independent of the total `ARG_MAX`; macOS has no
61
+ * per-entry cap but shares ~1 MB of `ARG_MAX` across argv AND the whole
62
+ * environment block, and the child environment here starts from a full
63
+ * inherited `process.env`. 64 KiB clears the Linux per-entry cap with room for
64
+ * the `OWENLOOP_CONSUMES=` prefix and leaves the rest of the macOS budget to
65
+ * the inherited block.
66
+ */
67
+ export const CONSUMES_INLINE_MAX_BYTES = 65_536;
48
68
  function errMsg(e) {
49
69
  return e instanceof Error ? e.message : String(e);
50
70
  }
71
+ /**
72
+ * Put the order's consumed inputs on `childEnv`, and return the temp DIRECTORY
73
+ * holding the overflow file — or `undefined` when the payload went inline.
74
+ *
75
+ * The contract a command script sees, and the reason it is two variables:
76
+ *
77
+ * - `OWENLOOP_CONSUMES` carries `JSON.stringify(order.consumes ?? {})`
78
+ * verbatim whenever that fits within `CONSUMES_INLINE_MAX_BYTES`. It is set
79
+ * even when there are no consumed inputs — the value is then `{}`, not an
80
+ * absent variable.
81
+ * - `OWENLOOP_CONSUMES_FILE` names a `0600` UTF-8 file holding that same JSON
82
+ * when it does not fit.
83
+ *
84
+ * Exactly ONE of the two is present on any spawn, because this function always
85
+ * assigns one and `delete`s the other. That mutual exclusion is what lets the
86
+ * presence of `OWENLOOP_CONSUMES_FILE` act as the discriminator with no third
87
+ * "mode" variable, and it is required anyway by the collision rule below.
88
+ *
89
+ * COLLISION RULE (the same reasoning as `OWENLOOP_WORKFLOW`/`OWENLOOP_RUN` at
90
+ * the spawn site): `childEnv` starts from `process.env`, and a shift can itself
91
+ * have been launched from inside another command step, so both names may
92
+ * already carry the PARENT order's values — a stale inputs object in the inline
93
+ * case, and in the file case a path whose directory has already been removed.
94
+ * Neither name is ever conditionally assigned.
95
+ *
96
+ * The JSON is the raw `order.consumes` object with no envelope, so a command
97
+ * script and an agent step see the identical shape. Key omission is preserved:
98
+ * an input that was declared but never produced is ABSENT from the object, not
99
+ * present as `null`, and a script tests for it with `'key' in consumes`.
100
+ */
101
+ function deliverConsumes(childEnv, consumes) {
102
+ // `order.consumes` arrives JSON-decoded off the wire, so there is no
103
+ // realistic cycle or BigInt to throw on. The throw is still surfaced rather
104
+ // than swallowed: leaving the variable unset would push a script back onto
105
+ // deriving its context from its cwd — the exact failure this delivery exists
106
+ // to remove — so the caller turns it into a machinery failure and no child is
107
+ // spawned.
108
+ let json;
109
+ try {
110
+ json = JSON.stringify(consumes ?? {});
111
+ }
112
+ catch (e) {
113
+ throw new Error(`cannot serialize the order's consumed inputs for OWENLOOP_CONSUMES: ${errMsg(e)}`);
114
+ }
115
+ if (Buffer.byteLength(json, 'utf8') <= CONSUMES_INLINE_MAX_BYTES) {
116
+ childEnv['OWENLOOP_CONSUMES'] = json;
117
+ delete childEnv['OWENLOOP_CONSUMES_FILE'];
118
+ return undefined;
119
+ }
120
+ // `mkdtempSync` rather than a composed run/step/random filename: several
121
+ // shifts run concurrently on one machine, and this gives a collision-free
122
+ // `0700` directory without owning a naming scheme. The file is `0600`
123
+ // because its content is agent-produced artifact data.
124
+ const dir = mkdtempSync(join(tmpdir(), 'owenloop-consumes-'));
125
+ try {
126
+ const file = join(dir, 'consumes.json');
127
+ writeFileSync(file, json, { encoding: 'utf8', mode: 0o600 });
128
+ childEnv['OWENLOOP_CONSUMES_FILE'] = file;
129
+ delete childEnv['OWENLOOP_CONSUMES'];
130
+ return dir;
131
+ }
132
+ catch (e) {
133
+ // The directory exists but the caller will never learn its path, so remove
134
+ // it here rather than leaking it; the caller's `finally` only covers a
135
+ // directory this function returned.
136
+ removeConsumesDir(dir);
137
+ throw e;
138
+ }
139
+ }
140
+ /** Best-effort removal of an overflow directory; a cleanup failure never fails a step. */
141
+ function removeConsumesDir(dir) {
142
+ if (dir === undefined)
143
+ return;
144
+ try {
145
+ rmSync(dir, { recursive: true, force: true });
146
+ }
147
+ catch {
148
+ // Swallowed on purpose: the child has already exited and its receipt is
149
+ // authoritative. A temp-directory that outlives the run is a tidiness
150
+ // problem, not a result.
151
+ }
152
+ }
51
153
  export function createExecLoop(opts) {
52
154
  const { hub, runner, workflow } = opts;
53
155
  const runId = opts.run;
@@ -282,43 +384,102 @@ export function createExecLoop(opts) {
282
384
  return 'unresolved-instructions';
283
385
  }
284
386
  // Run the command and race it against the lease going terminal.
285
- let cmd;
387
+ // `consumesDir` holds the OWENLOOP_CONSUMES_FILE overflow directory when
388
+ // the serialized inputs did not fit inline. The `finally` below removes it
389
+ // on every path out of this block, including a spawn that threw.
390
+ //
391
+ // On the RECEIPT path the removal follows the child's exit, because a
392
+ // script may read the file late in its run. On the LEASE-TERMINAL path it
393
+ // does not: `cmd.kill()` (see `exec/runner.ts`) posts SIGTERM, races `done`
394
+ // against the grace timer, then posts SIGKILL and returns WITHOUT awaiting
395
+ // `done`, so this unlink can precede full reaping of the killed process
396
+ // group. That is deliberate and harmless — the run is abandoned and submits
397
+ // no receipt — but it is not a guarantee to build on. Do not add work here
398
+ // that assumes the child is gone.
399
+ let consumesDir;
286
400
  try {
287
- // spawn's env replaces the child environment. Start from the actual exec
288
- // process environment so config-only opts.env cannot strip PATH/HOME, then
289
- // explicitly remove bundle provenance for loose definitions.
290
- const childEnv = { ...process.env };
291
- if (resolvedBundleDir === undefined)
292
- delete childEnv['OWENLOOP_BUNDLE_DIR'];
293
- else
294
- childEnv['OWENLOOP_BUNDLE_DIR'] = resolvedBundleDir;
295
- // Run identity is engine-derived from ExecLoopOptions, never a consumed
296
- // artifact value, and always present. Overwrite inherited values so a
297
- // nested exec sees its own identity rather than its parent's.
298
- childEnv['OWENLOOP_WORKFLOW'] = workflow;
299
- childEnv['OWENLOOP_RUN'] = runId;
300
- const startOptions = { cwd: order.workdir ?? opts.cwd, env: childEnv };
301
- cmd = runner.start(resolvedCommand, startOptions);
302
- }
303
- catch (e) {
304
- return submitReceipt(machineryFailure(e), order, resolvedCommand);
305
- }
306
- running = cmd;
307
- opts.out(`owenloop work exec: running ${workflow}/${runId} (step '${order.step}')`);
308
- const outcome = await Promise.race([
309
- cmd.done.then((r) => ({ t: 'done', r })).catch((e) => ({ t: 'done', r: machineryFailure(e) })),
310
- leasePromise.then((o) => ({ t: 'lease', o })),
311
- ]);
312
- if (outcome.t === 'lease') {
313
- await cmd.kill(); // no receipt — a submit now would race the re-offer
314
- if (signalled) {
315
- opts.err(`owenloop work exec: signalled mid-run — killed the command group, released ${workflow}/${runId}`);
316
- return 'killed';
401
+ let cmd;
402
+ try {
403
+ // spawn's env replaces the child environment. Start from the actual exec
404
+ // process environment so config-only opts.env cannot strip PATH/HOME, then
405
+ // explicitly remove bundle provenance for loose definitions.
406
+ const childEnv = { ...process.env };
407
+ if (resolvedBundleDir === undefined)
408
+ delete childEnv['OWENLOOP_BUNDLE_DIR'];
409
+ else
410
+ childEnv['OWENLOOP_BUNDLE_DIR'] = resolvedBundleDir;
411
+ // Run identity is engine-derived from ExecLoopOptions, never a consumed
412
+ // artifact value, and always present. The consumed inputs that follow ARE
413
+ // artifact values, and they are admitted here only because
414
+ // `resolveCommand` above already ran the consume-side verification gate
415
+ // (`hardRule: true`) and refused the order outright on absent,
416
+ // unverifiable, or invalid evidence. They travel in the environment
417
+ // block and nowhere else — never in argv and never in the command text,
418
+ // which is returned verbatim from the verified store and is not
419
+ // interpolated. All four names below overwrite inherited values so a
420
+ // nested exec sees its own order rather than its parent's.
421
+ childEnv['OWENLOOP_WORKFLOW'] = workflow;
422
+ childEnv['OWENLOOP_RUN'] = runId;
423
+ consumesDir = deliverConsumes(childEnv, order.consumes);
424
+ // The order carries no workdir when the step declared neither `workdir:`
425
+ // nor `workdirFrom:` (the hub resolves `workdirFrom` and ships the
426
+ // result, so an absent field IS that signal). Substituting the shift's
427
+ // launch directory silently is how a run ends up operating on an
428
+ // unintended tree with nothing in the output saying so, hence the
429
+ // record. It stays a WARNING: `provisioner` creates the worktree its
430
+ // successors use and `deprovisioner` deletes its own, so both
431
+ // legitimately have no workdir to resolve, and a throw here would break
432
+ // every delivery run the moment it shipped.
433
+ //
434
+ // WHERE THIS LINE ACTUALLY GOES. `opts.err` is this process's stderr.
435
+ // Run by hand, that is the operator's terminal. Dispatched by
436
+ // `owenloop shift start`, `createDefaultSpawner` (`src/shift/spawn.ts`)
437
+ // now attaches fd 1 and fd 2 to `<log-dir>/<run>.log`, so this warning
438
+ // is on disk under the run id it belongs to and outlives both the
439
+ // worker and the shift. See `docs/shift-logs.md`.
440
+ //
441
+ // It was previously discarded: every worker launched with
442
+ // `stdio: ['ignore','ignore','ignore']`, so a shift-dispatched exec
443
+ // wrote this line to `/dev/null`. That was the blocker on turning this
444
+ // warning into a throw — an unseen warning is not a migration notice.
445
+ // The delivery half is now done; the enforcement release is still a
446
+ // separate decision. `docs/bundles.md` ("Working directory for command
447
+ // steps") tracks the same state for bundle authors.
448
+ //
449
+ // The RECEIPT still does not carry it: the receipt captures the
450
+ // *command's* streams, and this line is the exec worker's own.
451
+ const cwd = order.workdir ?? opts.cwd;
452
+ if (order.workdir === undefined) {
453
+ opts.err(`owenloop work exec: step '${order.step}' (${workflow}/${runId}) declared neither workdir nor ` +
454
+ `workdirFrom — inheriting the shift launch directory '${resolve(cwd)}' as the command's cwd. ` +
455
+ 'A future release will require a step to declare this inheritance explicitly.');
456
+ }
457
+ const startOptions = { cwd, env: childEnv };
458
+ cmd = runner.start(resolvedCommand, startOptions);
459
+ }
460
+ catch (e) {
461
+ return submitReceipt(machineryFailure(e), order, resolvedCommand);
462
+ }
463
+ running = cmd;
464
+ opts.out(`owenloop work exec: running ${workflow}/${runId} (step '${order.step}')`);
465
+ const outcome = await Promise.race([
466
+ cmd.done.then((r) => ({ t: 'done', r })).catch((e) => ({ t: 'done', r: machineryFailure(e) })),
467
+ leasePromise.then((o) => ({ t: 'lease', o })),
468
+ ]);
469
+ if (outcome.t === 'lease') {
470
+ await cmd.kill(); // no receipt — a submit now would race the re-offer
471
+ if (signalled) {
472
+ opts.err(`owenloop work exec: signalled mid-run — killed the command group, released ${workflow}/${runId}`);
473
+ return 'killed';
474
+ }
475
+ opts.err(`owenloop work exec: lease ${outcome.o} while the command ran — killed the command, no receipt submitted`);
476
+ return mapLeaseDuringRun(outcome.o);
317
477
  }
318
- opts.err(`owenloop work exec: lease ${outcome.o} while the command ran — killed the command, no receipt submitted`);
319
- return mapLeaseDuringRun(outcome.o);
478
+ return submitReceipt(outcome.r, order, resolvedCommand);
479
+ }
480
+ finally {
481
+ removeConsumesDir(consumesDir);
320
482
  }
321
- return submitReceipt(outcome.r, order, resolvedCommand);
322
483
  }
323
484
  function stop(reason) {
324
485
  if (signalled)
@@ -43,11 +43,21 @@ const DEFAULT_GRACE_MS = 5_000; // SIGTERM → grace → SIGKILL on a group kill
43
43
  * `/bin/sh` keeps the default runner portable (ubuntu-latest CI, macOS dev).
44
44
  */
45
45
  export function buildCommandPlan(command, cwd, env) {
46
- // If a future package interpolates consumed values into command text or the
47
- // child environment, that path must inherit resolveCommand's consume-side
48
- // verification gate before this spawn plan is ever built. OWENLOOP_BUNDLE_DIR,
49
- // when supplied by exec/loop.ts, is resolver-derived after that gate and is
50
- // not a consumed artifact value.
46
+ // Consumed values DO reach the child environment: exec/loop.ts delivers the
47
+ // order's consumed inputs as OWENLOOP_CONSUMES (or OWENLOOP_CONSUMES_FILE).
48
+ // That path inherits resolveCommand's consume-side verification gate — the
49
+ // env block is built only after `resolveCommand` returned, and that call
50
+ // refuses the whole order under `hardRule: true` on absent, unverifiable, or
51
+ // invalid consumed evidence — so no unverified dynamic value is ever placed
52
+ // here. OWENLOOP_BUNDLE_DIR is resolver-derived after the same gate and is
53
+ // not a consumed artifact value at all.
54
+ //
55
+ // Command TEXT stays uninterpolated: `command` is the authored bytes from the
56
+ // verified store, and `/bin/sh -c` expands `$OWENLOOP_CONSUMES` only where the
57
+ // authored text itself asks for it. A consumed value delivered through the
58
+ // environment block never becomes shell syntax. Any FUTURE path that puts a
59
+ // consumed value into command text or argv must re-argue that, and must
60
+ // likewise sit behind the consume-side gate.
51
61
  return {
52
62
  command: '/bin/sh',
53
63
  args: ['-c', command],
@@ -40,7 +40,9 @@ export interface SessionRecord {
40
40
  updatedAt: number;
41
41
  }
42
42
  export interface SessionStoreOptions {
43
- /** Called once per skipped corrupt line. Default writes to stderr. */
43
+ /** Called once per skipped line — either corrupt bytes that would not parse
44
+ * ("skipping corrupt record") or a parsed object that failed the schema
45
+ * ("skipping invalid record"). Default writes to stderr. */
44
46
  warn?: (line: string) => void;
45
47
  /** Test barrier invoked under the writer lock after a retirement snapshot. */
46
48
  afterWriterSnapshot?: () => void;
@@ -18,9 +18,10 @@
18
18
  *
19
19
  * FAILURE STANCE, three different answers on purpose:
20
20
  * - READ is fail-open: a missing file reads as `[]` (mirrors `readChildRecords`
21
- * in `src/shift/state.ts`), and a corrupt line is skipped and reported
22
- * through the injectable `warn` callback, never thrown. A store that cannot
23
- * be parsed must degrade a resume into a replay, not break the worker.
21
+ * in `src/shift/state.ts`), and an unusable line — corrupt bytes that will
22
+ * not parse, or a parsed object that fails the schema — is skipped and
23
+ * reported through the injectable `warn` callback, never thrown. A store that
24
+ * cannot be parsed must degrade a resume into a replay, not break the worker.
24
25
  * - APPEND PROPAGATES. Unlike the shift's advisory metering records, a lost
25
26
  * session token silently degrades a Phase 4 resume into a cold replay — real
26
27
  * work thrown away — so the caller must see the failure.
@@ -79,28 +80,44 @@ function isNonEmptyString(v) {
79
80
  return typeof v === 'string' && v !== '';
80
81
  }
81
82
  /**
82
- * Validate one parsed line. Checks exactly the fields whose absence would make
83
- * the record unusable for a resume: the three key fields plus the harness and
84
- * token it exists to carry, and a `status` inside the four literals. Everything
85
- * else is carried verbatim.
83
+ * The non-empty-string fields `isSessionRecord` requires, in the order it checks
84
+ * them. Declared once and kept stable so a record failing several checks always
85
+ * reports the SAME field, which keeps the skip message deterministic.
86
+ * `status` is not here — its check is a membership test, not a string test — and
87
+ * is applied after these, preserving the original check order.
86
88
  */
87
- function isSessionRecord(v) {
89
+ const SESSION_RECORD_STRING_FIELDS = ['workflow', 'run', 'step', 'harness', 'token'];
90
+ /**
91
+ * Name of the first field of `v` that fails the `SessionRecord` schema, or
92
+ * `null` when the record is valid. `'<root>'` means `v` is not a plain object at
93
+ * all, so no field could be read from it.
94
+ *
95
+ * THE SINGLE SOURCE OF TRUTH for record validity: `isSessionRecord` is defined
96
+ * in terms of this function, so the accept/reject decision and the reported
97
+ * field name can never drift apart.
98
+ *
99
+ * Checks exactly the fields whose absence would make the record unusable for a
100
+ * resume: the three key fields plus the harness and token it exists to carry,
101
+ * and a `status` inside the four literals. Everything else is carried verbatim.
102
+ */
103
+ function firstInvalidSessionRecordField(v) {
88
104
  if (typeof v !== 'object' || v === null || Array.isArray(v))
89
- return false;
105
+ return '<root>';
90
106
  const r = v;
91
- if (!isNonEmptyString(r['workflow']))
92
- return false;
93
- if (!isNonEmptyString(r['run']))
94
- return false;
95
- if (!isNonEmptyString(r['step']))
96
- return false;
97
- if (!isNonEmptyString(r['harness']))
98
- return false;
99
- if (!isNonEmptyString(r['token']))
100
- return false;
107
+ for (const field of SESSION_RECORD_STRING_FIELDS) {
108
+ if (!isNonEmptyString(r[field]))
109
+ return field;
110
+ }
101
111
  if (typeof r['status'] !== 'string' || !STATUSES.has(r['status']))
102
- return false;
103
- return true;
112
+ return 'status';
113
+ return null;
114
+ }
115
+ /**
116
+ * Validate one parsed line. A type predicate, so a `true` return narrows `v` to
117
+ * `SessionRecord` at the call site.
118
+ */
119
+ function isSessionRecord(v) {
120
+ return firstInvalidSessionRecordField(v) === null;
104
121
  }
105
122
  /** Flush a directory entry update where the platform exposes directory fsync. */
106
123
  function syncDirectory(dir, sync = fsyncSync) {
@@ -262,7 +279,14 @@ export function readSessions(file, opts = {}) {
262
279
  continue;
263
280
  }
264
281
  if (!isSessionRecord(parsed)) {
265
- warn(`owenloop work sessions: skipping corrupt record at ${file}:${i + 1}`);
282
+ // DISTINCT WORDING ON PURPOSE — "invalid", not "corrupt". These bytes
283
+ // parsed fine; they just failed the schema. 36 records once whose only
284
+ // fault was an empty `token` were all reported as "corrupt", which sent
285
+ // debugging after a file-integrity problem that did not exist. The second
286
+ // pass runs only on this failure branch, so valid records pay nothing.
287
+ // The field NAME only, never its value — `token` is credential-shaped.
288
+ const badField = firstInvalidSessionRecordField(parsed) ?? '<unknown>';
289
+ warn(`owenloop work sessions: skipping invalid record at ${file}:${i + 1}: field "${badField}" failed schema check`);
266
290
  continue;
267
291
  }
268
292
  out.push(parsed);
@@ -28,6 +28,7 @@
28
28
  */
29
29
  import { textResult } from "../mcp/server.js";
30
30
  import { buildSubmitProof } from "../submit-proof.js";
31
+ import { normalizeSubmitValue } from "../submit-value.js";
31
32
  import { createHoldLoop } from "./loop.js";
32
33
  export const HOLD_MCP_TOOL_NAMES = ['get_order', 'submit', 'reject'];
33
34
  function errMsg(e) {
@@ -173,6 +174,12 @@ export function createHoldMcp(deps) {
173
174
  return textResult({ error: 'submit requires a "value"' }, true);
174
175
  }
175
176
  const done = args['done'];
177
+ // Normalize a JSON-encoded string value to the object the hub would
178
+ // store BEFORE signing it. Signing the string instead makes the hub drop
179
+ // the proof (it stores normalized bytes the signature does not cover),
180
+ // silently, on both sides — see `../submit-value.ts`. One value is used
181
+ // for both the proof and the wire so the two can never diverge.
182
+ const value = normalizeSubmitValue(args['value']);
176
183
  try {
177
184
  // Submit is also a dynamic-data boundary. Fetch and gate the bound
178
185
  // packet even when no origin was supplied for submit-proof signing;
@@ -192,7 +199,7 @@ export function createHoldMcp(deps) {
192
199
  origin: deps.origin,
193
200
  order: orderResponse.order,
194
201
  path,
195
- value: args['value'],
202
+ value,
196
203
  now: deps.now,
197
204
  warn: deps.err,
198
205
  ...(deps.principalKeys !== undefined ? { principalKeys: deps.principalKeys } : {}),
@@ -204,7 +211,7 @@ export function createHoldMcp(deps) {
204
211
  workflow,
205
212
  run,
206
213
  path,
207
- value: args['value'],
214
+ value,
208
215
  ...(typeof done === 'boolean' ? { done } : {}),
209
216
  ...(proof !== undefined ? { proof } : {}),
210
217
  ...holderReq,