owenloop 0.5.25 → 0.5.27

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.
@@ -1,14 +1,14 @@
1
1
  /**
2
- * The command receipt (C5, plan decision 8) — the artifact value exec submits
3
- * only for a successful ordinary command and for a judge that exits 0. Because
4
- * the hub `submit` verb has no failure channel, a failed ordinary command puts
5
- * this same receipt in `hub.ask`'s diagnostic context and submits nothing. A
6
- * non-zero judge exit uses `reject` instead, and signal-killed work submits
7
- * neither. Pure construction so it is trivially asserted in tests.
2
+ * Fold a raw `CommandResult` and its order context into the submitted receipt.
3
+ *
4
+ * `parsedPayload` is required rather than defaulted. A default could only parse
5
+ * the stdout marker, which stopped being the whole story when the payload file
6
+ * was added: it would silently drop a file payload, or publish the marker half
7
+ * of a conflict, and in both cases produce a receipt with no `payloadError` to
8
+ * show anything went wrong. Resolving both transports needs the file read, which
9
+ * only the caller holds, so the caller states the answer.
8
10
  */
9
- import { parsePayloadLine } from "./payload.js";
10
- /** Fold a raw `CommandResult` and its order context into the submitted receipt. */
11
- export function buildReceipt(result, ctx, parsedPayload = parsePayloadLine(result.payloadLine, result.payloadOverCap)) {
11
+ export function buildReceipt(result, ctx, parsedPayload) {
12
12
  return {
13
13
  kind: 'command-receipt',
14
14
  command: ctx.command,
@@ -4,7 +4,7 @@ import { type WorkOrder } from '../hub/types.ts';
4
4
  import type { FetchedStep } from '../bundle/types.ts';
5
5
  import { type ReconcileOptions, type Liveness } from './state.ts';
6
6
  import { sweepWorkDirs as sweepWorkDirsImpl } from '../agent/workdir.ts';
7
- import type { Spawner } from './spawn.ts';
7
+ import type { Spawner, WorkerFailure } from './spawn.ts';
8
8
  import { type ShiftEvent } from './protocol.ts';
9
9
  export interface ShiftLoopOptions {
10
10
  hub: HubClient;
@@ -172,11 +172,18 @@ export interface ShiftLoop {
172
172
  /**
173
173
  * Run-ended reap (metering condition (a)): drop the in-flight record for
174
174
  * `run` NOW, so its slot frees immediately instead of waiting for the next
175
- * reconcile to notice the child exited. The shift's MCP `submit` tool calls
176
- * an internal caller may use this when the hub reports a submit CLOSED the
177
- * run — the one in-process end-of-run signal the shift sees. A run whose closing submit went through
178
- * the child's own mount instead is not a problem: that child exits, and the
179
- * next reconcile's pid probe frees the slot anyway.
175
+ * reconcile to notice the child exited.
176
+ *
177
+ * NOTHING IN `src/` CALLS THIS. It is reachable only from tests. The intended
178
+ * caller was a submit path that told the shift the hub had closed a run, and
179
+ * that wiring was never made, so the slot is always freed the slower way
180
+ * instead: the child exits, and the next reconcile's pid probe frees it.
181
+ *
182
+ * Stated plainly because the gap is invisible from the type — this is on the
183
+ * `ShiftLoop` interface, so it reads as live. Do not reason from its
184
+ * existence about when `runBrakeKey` entries are cleared; in production the
185
+ * per-sweep prune is the only clearer. Wire it or delete it, but do not build
186
+ * a guard on top of it.
180
187
  */
181
188
  noteRunEnded(run: string): void;
182
189
  /**
@@ -203,10 +210,44 @@ export interface ShiftLoop {
203
210
  * A failure for a run this loop never dispatched (a foreign or already-reaped
204
211
  * run) is ignored rather than guessed at: the fan-out key is not on the
205
212
  * failure record, so there is no sound way to pick a brake key for it.
213
+ *
214
+ * ALSO HANDS THE CLAIM BACK, with the failure as the reason. A worker that
215
+ * dies before it submits leaves its order INFLIGHT on the hub with a frozen
216
+ * heartbeat: no alert, no re-offer, and no other shift able to take it,
217
+ * because this shift still holds the claim. Measured once at 34 minutes,
218
+ * indistinguishable from work in progress, until an operator released it by
219
+ * hand. The failure is known here and already named precisely, so the name
220
+ * travels with the release instead of dying in a local log line.
221
+ *
222
+ * The brake-key guard scopes the release to runs THIS shift dispatched and
223
+ * has not yet pruned. It does NOT mean "the run is still open": the only
224
+ * production clearer is the per-sweep prune below, and a child's exit event
225
+ * always beats the next sweep. So this fires for ordinary worker failures
226
+ * too, on top of the specific release the child's own final breath sends.
227
+ *
228
+ * That redundancy is deliberate and it is cheap, because the hub answers a
229
+ * release on an unheld claim by returning BEFORE it normalizes the reason or
230
+ * publishes anything (`verbs/release.ts`: "Only a currently held targeted
231
+ * release gets a diagnostic side effect"). A worker whose own final breath
232
+ * reached the hub therefore costs exactly one no-op request here — it cannot
233
+ * overwrite the reason that worker already recorded.
234
+ *
235
+ * Covering the ordinary exit path is the point, not an accident. The
236
+ * incident this exists for exited 1 with no signal, killed by ENOSPC inside
237
+ * its own error logging, so a predicate keyed on "could not have released
238
+ * for itself" — a signal death, or a spawn that never started — would have
239
+ * missed it entirely. A supervisor cannot tell those apart from outside, so
240
+ * it reports what it saw and lets the hub deduplicate.
241
+ *
242
+ * One narrow race remains, and it is worth taking. `finalBreath` gives up
243
+ * after `RELEASE_CAP_MS` while its request is still in flight; if that
244
+ * request is slow enough for this one to overtake it, the supervisor's
245
+ * account is recorded and the worker's more specific one is dropped as
246
+ * not-held. The reason string names its own provenance so that outcome still
247
+ * reads honestly, and a slightly vaguer alert beats the alternative this
248
+ * replaces, which was no alert at all for 34 minutes.
206
249
  */
207
- noteWorkerFailure(failure: {
208
- run: string;
209
- }): void;
250
+ noteWorkerFailure(failure: WorkerFailure): void;
210
251
  }
211
252
  /** The hub reaps a never-contacted claim after this long. Every client-side
212
253
  * re-offer latency must stay strictly under it. */
@@ -238,9 +279,12 @@ export declare const MAX_PENDING_CANDIDATE_AGE_MS: number;
238
279
  * each other.
239
280
  *
240
281
  * WHAT IS COUNTED, AND WHY IT IS NOT DISPATCHES. The event counted against that
241
- * key is a WORKER FAILURE — a child that exited non-zero, reported into the
242
- * loop through `noteWorkerFailure` — never a dispatch. Counting dispatches
243
- * conflates a storm with legitimate concurrency: the hub can offer several
282
+ * key is a FAILED ATTEMPT TO RUN THE STEP, in either of the two shapes that
283
+ * exist: a child that started and exited non-zero, reported into the loop
284
+ * through `noteWorkerFailure`; or a dispatch that never produced a running
285
+ * child at all, which throws out of `dispatchCandidate` and is charged there.
286
+ * What is NOT counted is a dispatch that succeeds. Counting those would
287
+ * conflate a storm with legitimate concurrency: the hub can offer several
244
288
  * DISTINCT runs of one step in a single sweep, and metering the second through
245
289
  * fifth of those would throttle healthy work. A failure is direct evidence that
246
290
  * running this step again right now is unlikely to help; a dispatch is evidence
@@ -250,4 +294,24 @@ export declare const MAX_PENDING_CANDIDATE_AGE_MS: number;
250
294
  * with zero delay, however many orders arrive for it at once.
251
295
  */
252
296
  export declare const STEP_BRAKE_DELAYS_MS: readonly [2000, 8000, 30000, 60000, 90000];
297
+ /**
298
+ * Treat a failure this long after the previous one as the start of a NEW
299
+ * streak, resetting the count to zero first. Comfortably longer than the
300
+ * longest delay in the table (90s), so a step failing at maximum backoff never
301
+ * decays out of it, while an isolated transient long after an old streak starts
302
+ * again at the shortest delay instead of inheriting a stale penalty.
303
+ */
304
+ export declare const STEP_BRAKE_DECAY_MS = 600000;
305
+ /**
306
+ * Longest release reason this shift will put on the wire, in code points.
307
+ *
308
+ * Mirrors the hub's own `MAX_RELEASE_REASON_CODE_POINTS`, which trims, treats
309
+ * blank as absent, and truncates without splitting a surrogate pair. Clamping
310
+ * on this side too means the reason we send is the reason an operator reads,
311
+ * with no silent server-side shortening in between.
312
+ *
313
+ * Exported so a test can assert the clamp against the constant rather than
314
+ * against a copy of the number, which would drift the day the hub's limit moves.
315
+ */
316
+ export declare const MAX_RELEASE_REASON_POINTS = 1024;
253
317
  export declare function createShiftLoop(opts: ShiftLoopOptions): ShiftLoop;
@@ -144,9 +144,12 @@ const CAPACITY_RELEASE_COOLDOWN_MS = 30_000;
144
144
  * each other.
145
145
  *
146
146
  * WHAT IS COUNTED, AND WHY IT IS NOT DISPATCHES. The event counted against that
147
- * key is a WORKER FAILURE — a child that exited non-zero, reported into the
148
- * loop through `noteWorkerFailure` — never a dispatch. Counting dispatches
149
- * conflates a storm with legitimate concurrency: the hub can offer several
147
+ * key is a FAILED ATTEMPT TO RUN THE STEP, in either of the two shapes that
148
+ * exist: a child that started and exited non-zero, reported into the loop
149
+ * through `noteWorkerFailure`; or a dispatch that never produced a running
150
+ * child at all, which throws out of `dispatchCandidate` and is charged there.
151
+ * What is NOT counted is a dispatch that succeeds. Counting those would
152
+ * conflate a storm with legitimate concurrency: the hub can offer several
150
153
  * DISTINCT runs of one step in a single sweep, and metering the second through
151
154
  * fifth of those would throttle healthy work. A failure is direct evidence that
152
155
  * running this step again right now is unlikely to help; a dispatch is evidence
@@ -163,13 +166,25 @@ export const STEP_BRAKE_DELAYS_MS = [2_000, 8_000, 30_000, 60_000, 90_000];
163
166
  * decays out of it, while an isolated transient long after an old streak starts
164
167
  * again at the shortest delay instead of inheriting a stale penalty.
165
168
  */
166
- const STEP_BRAKE_DECAY_MS = 600_000;
169
+ export const STEP_BRAKE_DECAY_MS = 600_000;
167
170
  /**
168
171
  * Forget a step's brake entry once this long has passed since its window
169
172
  * closed, so the map cannot grow for the life of the shift. Pure housekeeping —
170
173
  * `STEP_BRAKE_DECAY_MS` is what governs behaviour.
171
174
  */
172
175
  const STEP_BRAKE_FORGET_MS = 1_800_000;
176
+ /**
177
+ * Longest release reason this shift will put on the wire, in code points.
178
+ *
179
+ * Mirrors the hub's own `MAX_RELEASE_REASON_CODE_POINTS`, which trims, treats
180
+ * blank as absent, and truncates without splitting a surrogate pair. Clamping
181
+ * on this side too means the reason we send is the reason an operator reads,
182
+ * with no silent server-side shortening in between.
183
+ *
184
+ * Exported so a test can assert the clamp against the constant rather than
185
+ * against a copy of the number, which would drift the day the hub's limit moves.
186
+ */
187
+ export const MAX_RELEASE_REASON_POINTS = 1024;
173
188
  /**
174
189
  * How long to refuse the next dispatch of a step after `count` consecutive
175
190
  * worker failures. `count` is at least 1 at every call site (this is reached
@@ -386,6 +401,60 @@ export function createShiftLoop(opts) {
386
401
  opts.err(`shift event sink failed: ${errMsg(e)} (continuing)`);
387
402
  }
388
403
  }
404
+ /**
405
+ * Bound a release reason to what the hub will actually store.
406
+ *
407
+ * Applied to EVERY reason this supervisor sends, not only the ones that look
408
+ * long. The two senders differ sharply in how bounded they are, which is
409
+ * exactly why the clamp belongs in one shared place rather than at whichever
410
+ * call site looked risky on the day:
411
+ *
412
+ * - `describeWorkerFailure` is provably short. Its longest constructible
413
+ * output is under 100 code points, because every part is generated here or
414
+ * is one of two literals in `spawn.ts`.
415
+ * - The dispatch catch interpolates `errMsg(e)`, which is NOT bounded. A
416
+ * `FileLockTimeoutError` from `acquireDispatchLock` embeds a filesystem
417
+ * path, and a path is bounded only by PATH_MAX.
418
+ *
419
+ * Slices by code point, never by UTF-16 unit, so a clamp cannot halve a
420
+ * surrogate pair and put a lone surrogate on the wire. The hub truncates at
421
+ * `MAX_RELEASE_REASON_CODE_POINTS` too; clamping here as well means the
422
+ * reason we send is the reason an operator reads, with no silent server-side
423
+ * shortening in between.
424
+ */
425
+ const clampReason = (reason) => {
426
+ const points = Array.from(reason);
427
+ return points.length > MAX_RELEASE_REASON_POINTS
428
+ ? points.slice(0, MAX_RELEASE_REASON_POINTS).join('')
429
+ : reason;
430
+ };
431
+ /**
432
+ * The operator-facing account of a dead worker, for the release reason.
433
+ *
434
+ * It names the supervisor explicitly. A worker that fails through its own
435
+ * error path already sends a precise reason of its own, and in a narrow race
436
+ * this account can land first and take the slot the hub keeps for the first
437
+ * observation. Saying who is speaking keeps that outcome readable instead of
438
+ * looking like a worker's own vague self-report.
439
+ *
440
+ * `message` is the SPAWNER'S OWN bounded lifecycle string, never worker
441
+ * output: agent-run stderr is untrusted and is deliberately never quoted back
442
+ * (see `spawn.ts`), and the spawn error object is dropped there rather than
443
+ * interpolated, so no local path reaches the wire.
444
+ *
445
+ * The exit status or signal is what tells an operator which failure this was:
446
+ * a crash, an external kill, or a child that never launched at all. It is the
447
+ * difference between "this step is broken" and "this host is broken", so it
448
+ * travels with the message rather than staying in a local log line.
449
+ */
450
+ const describeWorkerFailure = (failure) => {
451
+ const cause = failure.signal !== null
452
+ ? `signal ${failure.signal}`
453
+ : failure.exitStatus !== null
454
+ ? `exitStatus ${failure.exitStatus}`
455
+ : 'no exit status';
456
+ return clampReason(`shift supervisor: ${failure.kind} ${failure.message} (${cause})`);
457
+ };
389
458
  /**
390
459
  * Hand a claim back so another shift can be offered it immediately. This
391
460
  * deliberately stays off the dispatch critical path; a failed release falls
@@ -445,6 +514,29 @@ export function createShiftLoop(opts) {
445
514
  * monotonic instant the next spawn of that step becomes permitted.
446
515
  */
447
516
  const stepBrake = new Map();
517
+ /**
518
+ * Charge one failure against a step's brake, extending the current streak or
519
+ * starting a new one.
520
+ *
521
+ * Shared by both places a dispatch can die: the worker failing after it
522
+ * started (`noteWorkerFailure`) and the spawn itself failing before it ever
523
+ * did (`dispatchCandidate`'s catch). Both now hand the claim back, and a
524
+ * released claim is re-offered immediately instead of lapsing after the
525
+ * 120s reap — so the brake, not the reap, is what throttles a step that
526
+ * keeps dying. Charging from only one of the two paths would leave the other
527
+ * free-running against the hub.
528
+ */
529
+ const chargeStepBrake = (brakeKey) => {
530
+ const at = monotonicNow();
531
+ const prior = stepBrake.get(brakeKey);
532
+ // A failure long after the previous one starts a NEW streak rather than
533
+ // extending a stale one — see `STEP_BRAKE_DECAY_MS`.
534
+ const priorCount = prior !== undefined && at - prior.failedAt < STEP_BRAKE_DECAY_MS
535
+ ? prior.count
536
+ : 0;
537
+ const count = priorCount + 1;
538
+ stepBrake.set(brakeKey, { count, failedAt: at, nextAllowedAt: at + stepBrakeDelayMs(count) });
539
+ };
448
540
  /**
449
541
  * Brake key of the step each dispatched run belongs to.
450
542
  *
@@ -456,9 +548,11 @@ export function createShiftLoop(opts) {
456
548
  * precedes the child's `exit` event (that fires on a later tick), so a
457
549
  * failure can never arrive before its own entry exists.
458
550
  *
459
- * Entries are removed when the run's failure is noted, when the run ends
460
- * (`noteRunEnded`), and for any run no longer live at sweep time — so this
461
- * map tracks live dispatches only and cannot grow without bound.
551
+ * Entries are removed when the run's failure is noted and, in production,
552
+ * for any run no longer live at sweep time — that prune is what bounds the
553
+ * map. (`noteRunEnded` also clears one, but nothing in `src/` calls it; see
554
+ * its declaration.) So an entry's presence means "this shift dispatched this
555
+ * run and has not pruned it yet", NOT "the run is still open".
462
556
  */
463
557
  const runBrakeKey = new Map();
464
558
  /**
@@ -596,7 +690,40 @@ export function createShiftLoop(opts) {
596
690
  message,
597
691
  });
598
692
  const prefix = c.kind === 'command' ? 'spawn' : 'agent-run spawn';
693
+ // Two different failures reach this one catch, and an operator reading the
694
+ // release reason needs to know which. `cancel` is assigned only after
695
+ // `opts.spawner` returns, so it is the exact witness: undefined means the
696
+ // spawn itself threw and no child ever existed; defined means a child WAS
697
+ // running and something after it threw — the reservation write, the
698
+ // dispatch lock, or `opts.out` on a full disk — and `cancel()` above has
699
+ // just killed it. Calling both "failed" told an operator to look for a
700
+ // fork problem that never happened.
701
+ const startedThenAborted = cancel !== undefined;
599
702
  opts.err(`${prefix} for ${c.workflow}/${c.order.run} failed: ${message}`);
703
+ // Hand the claim back. `whats_next` took it before this function ran, and
704
+ // reaching here means no worker exists to hand it back for itself — either
705
+ // no child was ever started, or `cancel()` has just killed the one that
706
+ // was. Without this the order sits INFLIGHT with a heartbeat that will
707
+ // never tick again, which is the same invisible stall a dead worker
708
+ // leaves.
709
+ //
710
+ // No `WorkerFailure` is ever reported for this path — the spawn never
711
+ // returned, or the child was killed before it could report — so
712
+ // `noteWorkerFailure` cannot cover it.
713
+ //
714
+ // Charge the brake from here too. A released claim is re-offered at once
715
+ // rather than lapsing after the 120s reap, so on a host that cannot fork
716
+ // at all the brake is now the only thing standing between this shift and
717
+ // a claim-fail-release loop against the hub.
718
+ //
719
+ // Drop the run's brake-key mapping first. On the aborted-child arm
720
+ // `runBrakeKey` may already be set, and leaving it would let a later
721
+ // `noteWorkerFailure` for the same run release a second time and charge
722
+ // the brake twice for one logical failure. Nothing depends on the entry
723
+ // once the claim is released, so deleting it is unconditional.
724
+ releaseClaim(c.workflow, c.order.run, clampReason(`shift supervisor: ${prefix} ${startedThenAborted ? 'started then aborted' : 'failed'} — ${message}`));
725
+ runBrakeKey.delete(c.order.run);
726
+ chargeStepBrake(brakeKey);
600
727
  return 'failed';
601
728
  }
602
729
  }
@@ -688,8 +815,18 @@ export function createShiftLoop(opts) {
688
815
  // exited CLEANLY reports no failure, so nothing else would ever remove its
689
816
  // entry. A non-zero exit is charged by `noteWorkerFailure`, which the
690
817
  // child's own `exit` event drives on the next event-loop tick — always well
691
- // before the next sweep, which waits a whole poll interval. If that order
692
- // ever did invert, the cost is one uncharged failure, never a wrong charge.
818
+ // before the next sweep, which waits a whole poll interval.
819
+ //
820
+ // This prune is the only clearer for a run that exits CLEANLY, and is
821
+ // therefore what bounds the map — a failing run is cleared by
822
+ // `noteWorkerFailure`, and an aborted dispatch clears its own entry. That
823
+ // makes the ordering matter more than it used to. `noteWorkerFailure` now also
824
+ // releases the claim, so an inversion here no longer costs one uncharged
825
+ // failure: it costs a claim nobody hands back, which is the stall this
826
+ // release exists to end. `iteration()` closes the window — `reconcile()`
827
+ // snapshots `live` and then awaits `hub.wake`, and a queued `exit` event is
828
+ // delivered during that await, so a child that died before the snapshot has
829
+ // already been charged and one that died after is still inside it.
693
830
  for (const [key, entry] of stepBrake) {
694
831
  if (monotonicNow() - entry.nextAllowedAt > STEP_BRAKE_FORGET_MS)
695
832
  stepBrake.delete(key);
@@ -1012,7 +1149,14 @@ export function createShiftLoop(opts) {
1012
1149
  // 'braked' and 'failed' are deliberately NOT queued into pendingCandidates:
1013
1150
  // both mean this shift is not going to run that order now, and re-queuing a
1014
1151
  // braked candidate would just re-dispatch it on the next drain, defeating
1015
- // the brake. The claim lapses through the hub's pickup window instead.
1152
+ // the brake.
1153
+ //
1154
+ // What happens to the claim differs between the two, and the difference is
1155
+ // the point of this change. A BRAKED candidate was never dispatched, so its
1156
+ // claim lapses through the hub's pickup window — the delay is deliberate,
1157
+ // and shedding it is what the brake is for. A FAILED one hands its claim
1158
+ // back explicitly in `dispatchCandidate`, so it is re-offered at once
1159
+ // instead of sitting INFLIGHT behind a heartbeat that will never tick.
1016
1160
  for (const candidate of candidates) {
1017
1161
  if (discardExpiredCandidate(candidate, false))
1018
1162
  continue;
@@ -1342,21 +1486,10 @@ export function createShiftLoop(opts) {
1342
1486
  noteWorkerFailure: (failure) => {
1343
1487
  const brakeKey = runBrakeKey.get(failure.run);
1344
1488
  if (brakeKey === undefined)
1345
- return; // not ours, or already accounted for
1489
+ return; // not a run this shift still tracks
1346
1490
  runBrakeKey.delete(failure.run);
1347
- const at = monotonicNow();
1348
- const prior = stepBrake.get(brakeKey);
1349
- // A failure long after the previous one starts a NEW streak rather than
1350
- // extending a stale one — see `STEP_BRAKE_DECAY_MS`.
1351
- const priorCount = prior !== undefined && at - prior.failedAt < STEP_BRAKE_DECAY_MS
1352
- ? prior.count
1353
- : 0;
1354
- const count = priorCount + 1;
1355
- stepBrake.set(brakeKey, {
1356
- count,
1357
- failedAt: at,
1358
- nextAllowedAt: at + stepBrakeDelayMs(count),
1359
- });
1491
+ releaseClaim(failure.workflow, failure.run, describeWorkerFailure(failure));
1492
+ chargeStepBrake(brakeKey);
1360
1493
  },
1361
1494
  };
1362
1495
  }
@@ -1,6 +1,6 @@
1
1
  import type { BundleRuntimeRequirements } from './types.ts';
2
2
  /** Runtime features implemented by this Owenloop release. */
3
- export declare const SUPPORTED_RUNTIME_FEATURES: readonly ["harness-policy-enforcement.v1", "native-judge-policy-inheritance.v1", "neutral-approval-modes.v1"];
3
+ export declare const SUPPORTED_RUNTIME_FEATURES: readonly ["harness-policy-enforcement.v1", "native-judge-policy-inheritance.v1", "neutral-approval-modes.v1", "command-payload-file.v1"];
4
4
  export interface RuntimeCompatibilityEnvironment {
5
5
  /** The running Owenloop package version. `0.0.0` is the unavailable-version sentinel. */
6
6
  version: string;
@@ -15,6 +15,19 @@ export const SUPPORTED_RUNTIME_FEATURES = Object.freeze([
15
15
  // than about an old runtime. Requiring the id moves the diagnosis to the
16
16
  // bundle check, before any order is offered.
17
17
  'neutral-approval-modes.v1',
18
+ // A command step may return its payload by writing JSON to the path in
19
+ // `$OWENLOOP_PAYLOAD_FILE` instead of printing an `##owenloop:payload##` line
20
+ // on stdout, which lifts the result ceiling from the 64 KiB stdout marker to
21
+ // the hub's artifact cap. A def whose command writes that file MUST require
22
+ // this id, because a CLI that predates it fails in the worst available way:
23
+ // it leaves the variable unset, and the natural defensive spelling
24
+ // `> "${OWENLOOP_PAYLOAD_FILE:-/dev/null}"` then EXITS ZERO having written the
25
+ // payload to /dev/null. There is no error anywhere — the step submits a
26
+ // receipt with no payload, and the first sign of trouble is a downstream
27
+ // `from: payload.value` bind failing at the hub, one step away from the cause.
28
+ // Requiring the id moves the diagnosis to the bundle check, before any order
29
+ // is offered.
30
+ 'command-payload-file.v1',
18
31
  ]);
19
32
  /** True only for one canonical strict SemVer value, with no prefix, range, or whitespace. */
20
33
  export function isCanonicalSemver(value) {
package/dist/src/defs.js CHANGED
@@ -39,7 +39,7 @@ const RAW_PRODUCE_KEYS = ['name', 'schema', 'judges', 'maxAttempts', 'maxSchemaF
39
39
  * fields, so it gets its own allowlist rather than folding into
40
40
  * RAW_PRODUCE_KEYS (§27 unknown-key rejection). */
41
41
  const RAW_GROUP_KEYS = ['group', 'mode', 'of'];
42
- const RAW_JUDGE_KEYS = ['name', 'body', 'bodyFile', 'model', 'inputs', 'cadence', 'maxRunsPerDay', 'executor', 'command', 'spec', 'capabilities', 'modifiers'];
42
+ const RAW_JUDGE_KEYS = ['name', 'body', 'bodyFile', 'model', 'inputs', 'cadence', 'maxRunsPerDay', 'executor', 'command', 'spec', 'workdir', 'workdirFrom', 'capabilities', 'modifiers'];
43
43
  /** Keys valid on a normal (non-calls:, non-include:) step entry. */
44
44
  const RAW_STEP_KEYS = [
45
45
  'name', 'consumes', 'produces', 'generates', 'invalidates', 'cadence',
@@ -362,6 +362,14 @@ function parseJudges(v, ctx, baseDir) {
362
362
  judge.command = asString(raw.command, `judge '${name}'.command`);
363
363
  if (raw.spec !== undefined)
364
364
  judge.spec = asExtension(raw.spec, `judge '${name}'.spec`);
365
+ if (raw.workdir !== undefined)
366
+ judge.workdir = asString(raw.workdir, `judge '${name}'.workdir`);
367
+ if (raw.workdirFrom !== undefined) {
368
+ judge.workdirFrom = asString(raw.workdirFrom, `judge '${name}'.workdirFrom`);
369
+ }
370
+ if (raw.workdir !== undefined && raw.workdirFrom !== undefined) {
371
+ throw new DefError(`judge '${name}' may not declare both workdir and workdirFrom`);
372
+ }
365
373
  if (raw.capabilities !== undefined) {
366
374
  const caps = asStringArray(raw.capabilities, `judge '${name}'.capabilities`);
367
375
  if (caps.length === 0) {
@@ -997,24 +1005,63 @@ export function parseDef(raw, source, baseDir) {
997
1005
  }
998
1006
  return def;
999
1007
  }
1008
+ /**
1009
+ * The stem half of a `workdirFrom` expression, resolved against a list of
1010
+ * consumed stems. Mirrors `parseWorkdirFrom`'s boundary walk (longest stem
1011
+ * first, because a stem may itself contain dots) but takes bare names, since a
1012
+ * synthesized judge has not built its consume patterns yet. Returns null when
1013
+ * no consumed stem matches — the expression then names a definition input,
1014
+ * which validateDef resolves without a consume edge.
1015
+ */
1016
+ function workdirFromStem(raw, stems) {
1017
+ const r = raw.trim();
1018
+ for (let boundary = r.lastIndexOf('.'); boundary > 0; boundary = r.lastIndexOf('.', boundary - 1)) {
1019
+ if (r.length - boundary - 1 === 0)
1020
+ continue;
1021
+ const stem = r.slice(0, boundary);
1022
+ if (stems.includes(stem))
1023
+ return stem;
1024
+ }
1025
+ return null;
1026
+ }
1000
1027
  /**
1001
1028
  * Synthesize one full StepDef per declared `judges:` entry on a produce
1002
1029
  * pattern (§24 §3.2, §7.2). Shape mirrors the `calls:` template above, with
1003
1030
  * exactly three deltas from a hand-written step: the `judges: <stem>` marker
1004
1031
  * (eligibility trigger, replacing inputsGreen — see model.ts), `produces: []`
1005
1032
  * (a judge emits a verdict against the judged stem, not a new artifact), and
1006
- * `consumes: [stem, ...(inputs ? producerConsumeStems : [])]` so authority
1007
- * flows from the existing consume-edge check (`assertAuthority`) with no
1008
- * special-casing. Everything else (cadence, maxRunsPerDay, model, body,
1009
- * maxAttempts/maxSchemaFailures defaults) is inherited exactly like
1010
- * an ordinary step, because judge orders flow through the normal
1033
+ * `consumes: [stem, ...(inputs ? producerConsumeStems : []), ...workdirFromStem]`
1034
+ * so authority flows from the existing consume-edge check (`assertAuthority`)
1035
+ * with no special-casing. Everything else (cadence, maxRunsPerDay, model, body,
1036
+ * workdir/workdirFrom, maxAttempts/maxSchemaFailures defaults) is inherited
1037
+ * exactly like an ordinary step, because judge orders flow through the normal
1011
1038
  * eligibleFirings → applySchedule → claim → buildOrder pipeline (§7.1).
1012
1039
  */
1013
- function synthesizeJudgeSteps(producerStepName, pat, producerConsumeStems, producerX, producerCapabilities) {
1040
+ function synthesizeJudgeSteps(producerStepName, pat, producerConsumeStems, producerX, producerCapabilities, producerWorkdir, producerWorkdirFrom) {
1014
1041
  if (!pat.judges || pat.judges.length === 0)
1015
1042
  return [];
1016
1043
  return pat.judges.map((j) => {
1044
+ // A judge with no workdir of its own INHERITS the producer's, so it reads
1045
+ // the same tree the work it judges was produced in. Declaring either key
1046
+ // opts out of both halves of the inheritance: producer and judge each carry
1047
+ // at most one, and silently pairing an authored `workdir` with an inherited
1048
+ // `workdirFrom` would trip validateDef's mutual-exclusion check on a
1049
+ // definition the author never wrote that way.
1050
+ const authoredWorkdir = j.workdir !== undefined || j.workdirFrom !== undefined;
1051
+ const workdir = authoredWorkdir ? j.workdir : producerWorkdir;
1052
+ const workdirFrom = authoredWorkdir ? j.workdirFrom : producerWorkdirFrom;
1017
1053
  const consumeStems = j.inputs ? [pat.stem, ...producerConsumeStems] : [pat.stem];
1054
+ // A workdirFrom value is resolved from a CONSUMED artifact, so the stem it
1055
+ // names must be on this step's consume list or validateDef rejects the whole
1056
+ // definition. `inputs: false` is the case that needs this: the judge sees
1057
+ // only the judged stem, which is never the stem the producer resolved its
1058
+ // workdir from. Deduplicated, so `inputs: true` (where the stem is already
1059
+ // carried) stays byte-identical to the pre-inheritance shape.
1060
+ if (workdirFrom !== undefined) {
1061
+ const stem = workdirFromStem(workdirFrom, producerConsumeStems);
1062
+ if (stem !== null && !consumeStems.includes(stem))
1063
+ consumeStems.push(stem);
1064
+ }
1018
1065
  const consumes = consumeStems.map((stem) => parseConsume(stem));
1019
1066
  const cadence = j.cadence ?? DEFAULTS.cadence;
1020
1067
  const step = {
@@ -1055,6 +1102,10 @@ function synthesizeJudgeSteps(producerStepName, pat, producerConsumeStems, produ
1055
1102
  step.command = j.command;
1056
1103
  if (j.spec !== undefined)
1057
1104
  step.spec = j.spec;
1105
+ if (workdir !== undefined)
1106
+ step.workdir = workdir;
1107
+ if (workdirFrom !== undefined)
1108
+ step.workdirFrom = workdirFrom;
1058
1109
  if (j.modifiers !== undefined)
1059
1110
  step.judgeModifiers = [...j.modifiers];
1060
1111
  return step;
@@ -1243,7 +1294,7 @@ function buildStep(rl, i, baseDir) {
1243
1294
  }
1244
1295
  }
1245
1296
  const producerConsumeStems = consumes.map((c) => c.stem);
1246
- const judgeSteps = producesPatterns.flatMap((p) => synthesizeJudgeSteps(name, p, producerConsumeStems, step.x, step.capabilities));
1297
+ const judgeSteps = producesPatterns.flatMap((p) => synthesizeJudgeSteps(name, p, producerConsumeStems, step.x, step.capabilities, step.workdir, step.workdirFrom));
1247
1298
  return [step, ...judgeSteps];
1248
1299
  }
1249
1300
  // ---- validation --------------------------------------------------------------
@@ -485,6 +485,19 @@ export interface ProducePattern {
485
485
  * (or additionally alongside `command`). Shape-checked as a plain map
486
486
  * only (mirrors `x:`'s asExtension contract) — contents never read. */
487
487
  spec?: Record<string, unknown>;
488
+ /** opaque location hint passed through on the judge's synthesized order.
489
+ * Omitted = INHERIT the producing step's `workdir`/`workdirFrom`, so a
490
+ * judge reads the same tree the work it judges was produced in. Mutually
491
+ * exclusive with workdirFrom, same as StepDef. */
492
+ workdir?: string;
493
+ /**
494
+ * Resolve the judge order's workdir from `<consumedStem>.<dotted.path>` in a
495
+ * consumed artifact value. Mutually exclusive with workdir. Omitted =
496
+ * INHERIT the producer's. The named stem is added to the synthesized step's
497
+ * consumes when it is not already there, so the value passes the engine's
498
+ * consume-side verification gate.
499
+ */
500
+ workdirFrom?: string;
488
501
  /** Routing capabilities for this judge's synthesized step. Omitted =
489
502
  * INHERIT the producing step's `capabilities`, so a judge routes to the
490
503
  * same grade of crew as the work it judges instead of being claimable by
package/docs/authoring.md CHANGED
@@ -320,6 +320,15 @@ modifier through `OWENLOOP_FEEDBACK`/`OWENLOOP_FEEDBACK_FILE` and
320
320
  steps](bundles.md#consumed-inputs-for-command-steps) for the reader snippet and
321
321
  the omitted-key rule.
322
322
 
323
+ The result travels the same way. A command step's accepted artifact is its whole
324
+ `CommandReceipt`; to put a value of its own inside it — the thing a downstream
325
+ `from: payload.value` bind reads — the command either prints an
326
+ `##owenloop:payload##` marker line on stdout, capped at 64 KiB, or writes the
327
+ same JSON to the path in `OWENLOOP_PAYLOAD_FILE`, capped at 24 MB. A definition
328
+ whose command writes that file must declare the `command-payload-file.v1`
329
+ runtime feature. See [`docs/bundles.md` § Returning a payload from a command
330
+ step](bundles.md#returning-a-payload-from-a-command-step).
331
+
323
332
  ## `capabilities:` — logical capability tags
324
333
 
325
334
  `capabilities:` is an optional list of strings on a step naming what that step