wicked-crew 0.15.1 → 0.15.3

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 (66) hide show
  1. package/dist/api/gate-cache.d.ts +98 -17
  2. package/dist/api/gate-cache.d.ts.map +1 -1
  3. package/dist/api/gate-cache.js +136 -25
  4. package/dist/api/gate-cache.js.map +1 -1
  5. package/dist/api/mcp.d.ts.map +1 -1
  6. package/dist/api/mcp.js +32 -19
  7. package/dist/api/mcp.js.map +1 -1
  8. package/dist/api/recording.d.ts +3 -2
  9. package/dist/api/recording.d.ts.map +1 -1
  10. package/dist/api/recording.js +13 -1
  11. package/dist/api/recording.js.map +1 -1
  12. package/dist/api/roster-standing.js.map +1 -1
  13. package/dist/api/routes.d.ts +8 -6
  14. package/dist/api/routes.d.ts.map +1 -1
  15. package/dist/api/routes.js +557 -353
  16. package/dist/api/routes.js.map +1 -1
  17. package/dist/api/seat-agents.d.ts.map +1 -1
  18. package/dist/api/seat-agents.js +19 -5
  19. package/dist/api/seat-agents.js.map +1 -1
  20. package/dist/api/seat-standing.d.ts +13 -0
  21. package/dist/api/seat-standing.d.ts.map +1 -1
  22. package/dist/api/seat-standing.js +50 -42
  23. package/dist/api/seat-standing.js.map +1 -1
  24. package/dist/api/seat-version.d.ts +47 -5
  25. package/dist/api/seat-version.d.ts.map +1 -1
  26. package/dist/api/seat-version.js +54 -7
  27. package/dist/api/seat-version.js.map +1 -1
  28. package/dist/api/server.js +1 -1
  29. package/dist/api/server.js.map +1 -1
  30. package/dist/cli/index.js +14 -10
  31. package/dist/cli/index.js.map +1 -1
  32. package/dist/core/adapter.d.ts +11 -0
  33. package/dist/core/adapter.d.ts.map +1 -1
  34. package/dist/core/adapter.js +15 -0
  35. package/dist/core/adapter.js.map +1 -1
  36. package/dist/core/engine-roster.d.ts +12 -6
  37. package/dist/core/engine-roster.d.ts.map +1 -1
  38. package/dist/core/engine-roster.js +30 -13
  39. package/dist/core/engine-roster.js.map +1 -1
  40. package/dist/core/vault-bin.d.ts +69 -0
  41. package/dist/core/vault-bin.d.ts.map +1 -0
  42. package/dist/core/vault-bin.js +101 -0
  43. package/dist/core/vault-bin.js.map +1 -0
  44. package/dist/mcp/policies.d.ts +22 -1
  45. package/dist/mcp/policies.d.ts.map +1 -1
  46. package/dist/mcp/policies.js +56 -20
  47. package/dist/mcp/policies.js.map +1 -1
  48. package/dist/studio/assets/index-70ozkvqx.css +32 -0
  49. package/dist/studio/assets/{index-BYPgSjPo.js → index-CvH1l1tp.js} +217 -216
  50. package/dist/studio/editors/wicked-deck/editor.json +1 -1
  51. package/dist/studio/editors/wicked-deck/index.html +1 -1
  52. package/dist/studio/editors/wicked-doc/editor.json +1 -1
  53. package/dist/studio/editors/wicked-doc/index.html +1 -1
  54. package/dist/studio/editors/wicked-page/editor.json +1 -1
  55. package/dist/studio/editors/wicked-page/index.html +1 -1
  56. package/dist/studio/editors/wicked-video/editor.json +1 -1
  57. package/dist/studio/editors/wicked-video/index.html +1 -1
  58. package/dist/studio/index.html +2 -2
  59. package/dist/studio/testid-inventory.json +81 -27
  60. package/dist/team/routes.d.ts +11 -1
  61. package/dist/team/routes.d.ts.map +1 -1
  62. package/dist/team/routes.js +58 -48
  63. package/dist/team/routes.js.map +1 -1
  64. package/endpoint-manifest.json +1 -1
  65. package/package.json +4 -3
  66. package/dist/studio/assets/index-BEFOUR6c.css +0 -32
@@ -1,4 +1,5 @@
1
1
  import { busSeamsOffWarning, busUnavailableWarning } from '../core/bus-notice.js';
2
+ import { vaultBinWarning, vaultDiagnostics } from '../core/vault-bin.js';
2
3
  import { PlanSchema, toLaunchPlan } from './plan-schema.js';
3
4
  import { z } from 'zod';
4
5
  import { listRequirements, getRequirement, patchRequirement } from './requirements.js';
@@ -12,7 +13,7 @@ import { videoPalette } from '../appearance/video-palette.js';
12
13
  import { CampaignsUnsupportedError, ChatUnsupportedError, CoreAdapter, ElicitationUnsupportedError, GovernanceReplayUnsupportedError, PlanLaunchUnsupportedError, SteeringUnsupportedError, addonSupportsQeOverride, addonSupportsReducedAssurance, humanGatePhaseIds, settingsFilePath } from '../core/adapter.js';
13
14
  import { codeGraphDb, codeGraphErrorStatus, requirementsGraph } from '../core/repoPaths.js';
14
15
  import { resolveCursorUnit } from '../core/cursor.js';
15
- import { detectRefusal } from './gate-cache.js';
16
+ import { detectRefusal, isOpenGate, offeredChoices, replayOpenGate } from './gate-cache.js';
16
17
  import { acceptanceRequirementOf, buildAcceptanceView } from '../qe/acceptance.js';
17
18
  import { runIdentityOf, wireIdentity } from '../core/run-identity.js';
18
19
  import { buildEvidenceBundle, coreUnitId, evidenceFilename } from './evidence.js';
@@ -60,7 +61,7 @@ import { withPhaseSkillGaps } from '../skills/phase-skill-gaps.js';
60
61
  import { STATE_HOME_BLOCKER_CODE, STATE_HOME_REMEDY, isEngineStateHomeRefusal, stateHomeBlockerBody, } from '../projects/state-home-preflight.js';
61
62
  import { BASE_SKILL_POLICIES, BASE_SKILL_REF_SHAPE, baseSkillRemedy } from '../skills/base-skill.js';
62
63
  import { REQUIRED_GARDEN_VERSION } from '../skills/plugin-source.js';
63
- import { engineRosterJson, noEligibleSeatBody, parseNoEligibleSeat } from '../core/engine-roster.js';
64
+ import { engineRosterJson, noEligibleSeatBody, parseNoEligibleSeat, toEngineSeat } from '../core/engine-roster.js';
64
65
  import { launchAssuranceNotice } from '../core/assurance.js';
65
66
  import { launchRequiresQe, mayRequireQe, qeAcceptanceMode, qeSettingSkipReason } from '../core/qe-acceptance-setting.js';
66
67
  import { aggregateReceipt, postHocDeliveryAssurance, qeAcceptanceFromView, qeAcceptanceRefusal, QE_ACCEPTANCE_REQUIRED_CODE, receiptOf, qeAcceptanceNotRequired, qeDecisionBypasses, qeDecisionOf, unreadableQeAcceptance, } from './delivery-assurance.js';
@@ -349,12 +350,6 @@ const ExcludeLinkedIssuesSchema = z
349
350
  .array(z.string().regex(/^\s*(?:\d+|(?:(?:[\w.-]+\/)?[\w.-]+)?#\d+)\s*$/, 'an issue ref: 541, #541, repo#541 or owner/repo#541'))
350
351
  .max(50)
351
352
  .optional();
352
- /**
353
- * Why `primary` cannot lead a launch on `clisJson`'s roster, or `null` when it can (DES-TEAMING-002
354
- * §8.1). The roster is a JSON array of seats keyed by `key`; a seat crew's standing marks
355
- * `council_eligible: false` is signed out or disabled, so it cannot lead. A roster that does not
356
- * parse is left to the engine's own parse error (the `engineRosterJson` rule).
357
- */
358
353
  /**
359
354
  * The default launch roster ({@link defaultLaunchRoster}: no opt-in seats) — plus the opt-in seat a
360
355
  * launch NAMES as its lead (`primary`): naming it is the opt-in.
@@ -366,6 +361,14 @@ export function launchRosterNaming(roster, primary) {
366
361
  const named = roster.find((s) => s.key === primary);
367
362
  return named === undefined ? base : [...base, named];
368
363
  }
364
+ /**
365
+ * Why `primary` cannot lead a launch on `clisJson`'s roster, or `null` when it can (DES-TEAMING-002
366
+ * §8.1). The roster is a JSON array of seats keyed by `key`. Leading is WORK (wicked-core#572: the
367
+ * lead pick follows `seat_eligible_for_work`, not the ballot flag — crew#916), so a seat crew's
368
+ * standing marks `work_eligible: false` (signed out, benched, or reviewer-only) cannot lead, nor can a
369
+ * seat the launch would bench (its translated engine `health`). A roster that does not parse is
370
+ * left to the engine's own parse error (the `engineRosterJson` rule).
371
+ */
369
372
  export function primaryRefusal(primary, clisJson) {
370
373
  let roster;
371
374
  try {
@@ -382,8 +385,17 @@ export function primaryRefusal(primary, clisJson) {
382
385
  if (seat === undefined) {
383
386
  return `primary names a seat that is not in this launch's roster: ${primary} (roster: ${keys.join(', ') || 'none'})`;
384
387
  }
385
- if (seat.council_eligible === false) {
386
- return `primary names a seat that is signed out or disabled: ${primary} — sign it in from the Agents page, or pick another seat`;
388
+ if (seat.work_eligible === false) {
389
+ const why = typeof seat.work_ineligible_reason === 'string' && seat.work_ineligible_reason !== '' ? ` (${seat.work_ineligible_reason})` : '';
390
+ return `primary names a seat that cannot take work: ${primary}${why} — pick another seat`;
391
+ }
392
+ // The engine takes an explicit lead without a health check, so a seat the launch benches (the
393
+ // translated `health.usable: false` — a reading from a client before `work_eligible` included)
394
+ // cannot lead either.
395
+ const health = toEngineSeat(seat).health;
396
+ if (health?.usable === false) {
397
+ const why = typeof health.reason === 'string' && health.reason !== '' ? ` (${health.reason})` : '';
398
+ return `primary names a seat that cannot take work: ${primary}${why} — pick another seat`;
387
399
  }
388
400
  return null;
389
401
  }
@@ -1211,6 +1223,8 @@ runtime = {}) {
1211
1223
  // crew#742: a seat that runs ungoverned, or floor seats on a host that cannot arm their
1212
1224
  // boundary, are named before any run starts.
1213
1225
  ...seatGovernanceWarnings(seatGovernanceNow(), hostSandboxLauncher()),
1226
+ // crew#1068: the engine has no wicked-vault CLI to record held boundary findings with.
1227
+ ...[adapter.vaultBin != null ? vaultBinWarning(adapter.vaultBin) : null].filter((w) => w !== null),
1214
1228
  ];
1215
1229
  return {
1216
1230
  status: 'ok',
@@ -1286,6 +1300,9 @@ runtime = {}) {
1286
1300
  },
1287
1301
  // crew#740: which memory store this daemon reads and writes, and the isolation notice.
1288
1302
  memoryStore: memoryStoreInfo(),
1303
+ // crew#1068: the wicked-vault CLI handed to the engine, with a remedy when it has none.
1304
+ // `null` on a route set driven without the real adapter.
1305
+ vault: adapter.vaultBin != null ? vaultDiagnostics(adapter.vaultBin) : null,
1289
1306
  };
1290
1307
  });
1291
1308
  // Who am I talking to the daemon as? (task #88). In local mode this is
@@ -3325,8 +3342,33 @@ runtime = {}) {
3325
3342
  // single-shot carrier (codex on the wrapped carrier) remembers nothing of the first turn, so a
3326
3343
  // follow-up told only the question answered "the supplied repository contains only tmp".
3327
3344
  const instructions = scope !== undefined ? `${chatScopeStatement(id, scope, rules)}\n\n${withPreface(pending.preface)}` : withPreface(pending.preface);
3328
- await adapter.proposePlan(path.runId, { steps: [answerStep(stepId, instructions)] }, turnId);
3329
- await adapter.confirmGate(path.runId, true);
3345
+ // The continuation answers the path run's gate: under the per-run decision lock it re-reads the
3346
+ // run and is judged by the record before it proposes or confirms anything (crew#1083 round 7).
3347
+ // The status read above is a pre-check that changes nothing; this read is the one that counts.
3348
+ const pathRunId = path.runId;
3349
+ const refused = await decideExclusively(pathRunId, async () => {
3350
+ const view = (await adapter.sessionsDetail()).find((v) => v.session.id === pathRunId);
3351
+ if (view === undefined || view.session.status !== 'awaiting_human') {
3352
+ return { code: 409, body: {
3353
+ code: 'turn_in_flight',
3354
+ error: `chat '${id}': its path ${pathRunId} is ${view?.session.status ?? 'gone'}, not waiting for your turn — nothing was sent`,
3355
+ chatId: id,
3356
+ runId: pathRunId,
3357
+ } };
3358
+ }
3359
+ const decided = await gateForDecision(view, undefined);
3360
+ if ('refusal' in decided)
3361
+ return decided.refusal;
3362
+ await adapter.proposePlan(pathRunId, { steps: [answerStep(stepId, instructions)] }, turnId);
3363
+ await adapter.confirmGate(pathRunId, true);
3364
+ return null;
3365
+ });
3366
+ if (refused !== null) {
3367
+ askPaths.release(id, lease);
3368
+ if (turn !== null)
3369
+ chatTurns.abort(id, turn.turnId);
3370
+ return reply.code(refused.code).send(refused.body);
3371
+ }
3330
3372
  askPaths.release(id, lease);
3331
3373
  if (!askPaths.continued(id, lease, stepId)) {
3332
3374
  // Closed between the proposal and here: the DELETE cancelled the run this was accepted on.
@@ -3792,54 +3834,124 @@ runtime = {}) {
3792
3834
  }
3793
3835
  }
3794
3836
  /**
3795
- * The run's OPEN gate, resolved the way `GET /runs/:id/gate` serves it: the cache, then the
3796
- * engine's durable `interaction_requests` row (adopted into the cache), then the event-log replay.
3797
- * The caller has already checked `awaiting_human`. `null` = the log records no open gate;
3798
- * `'no-log'` = this build cannot read the log, so nothing can be said.
3837
+ * The run's OPEN gate, resolved the way `GET /runs/:id/gate` serves it: the cache, else a read of
3838
+ * the record (`readOpenGate`) adopted into the cache only once it completes with no live gate
3839
+ * event of the run's having landed meanwhile — `GateCache.resolve` re-runs a read one did
3840
+ * overlap, because neither side of such an overlap is known to be the newer (crew#1083 review).
3841
+ * The caller has already checked `awaiting_human`. `null` = the record holds no open gate;
3842
+ * `'no-log'` = this build cannot read the log, so nothing can be said; `'unstable'` = live gate
3843
+ * events kept landing while the record was read, so nothing was verified (retry).
3799
3844
  */
3800
3845
  async function resolveOpenGate(id) {
3801
- const cached = gateCache.get(id);
3802
- if (cached)
3803
- return cached;
3804
- const durableRows = typeof adapter.interactionRequests === 'function'
3805
- ? await adapter.interactionRequests(id, 'open')
3806
- : null;
3807
- const durableGate = durableRows?.find((r) => r.kind === 'gate');
3808
- if (durableGate !== undefined) {
3846
+ return gateCache.resolve(id, () => readOpenGate(id));
3847
+ }
3848
+ /**
3849
+ * One read of the run's open gate from the record: the engine's durable `interaction_requests`
3850
+ * row (DES-PROJECT-001 §5.3), enriched from the event log, else the event-log replay for engines
3851
+ * predating the table. Never consults the cache — `GateCache.resolve` owns that, and runs this
3852
+ * again when a live gate event overlaps it.
3853
+ */
3854
+ async function readOpenGate(id) {
3855
+ const recorded = await recordedOpenGate(id);
3856
+ if (recorded?.state === 'ambiguous')
3857
+ return 'ambiguous';
3858
+ if (recorded?.state === 'open') {
3859
+ const durableGate = recorded.row;
3809
3860
  // This path builds the entry inline (not through `fold`), so it must run the SAME refusal
3810
3861
  // detection (issue #419) — otherwise a gate served from the durable row after a restart would
3811
3862
  // silently drop the warning that the live/replay paths carry.
3812
3863
  const refusal = detectRefusal(durableGate.prompt);
3813
3864
  const gateKind = typeof durableGate['gate_kind'] === 'string' && durableGate['gate_kind'] !== '' ? durableGate['gate_kind'] : undefined;
3814
- const entry = {
3815
- ord: durableGate.ord ?? 0,
3865
+ const ord = recorded.ord;
3866
+ // Issue #969: the row has no `choices`/`recommended`, so they come from the newest
3867
+ // `awaitingHuman` for this ord in the event log. A log this build cannot read (null, or a read
3868
+ // that throws) leaves both out — the gate is still served, just without the offered answers.
3869
+ const events = await adapter.runEvents(id).catch(() => null);
3870
+ const frame = events?.filter((e) => e.type === 'awaitingHuman' && e.session === id && e.ord === ord).at(-1);
3871
+ return {
3872
+ ord,
3816
3873
  prompt: durableGate.prompt,
3817
3874
  lifecycle: 'open',
3818
3875
  receivedAt: new Date(durableGate.created_at).toISOString(),
3819
3876
  ...(refusal !== undefined ? { refusal } : {}),
3820
3877
  ...(gateKind !== undefined ? { gateKind } : {}),
3878
+ ...(frame !== undefined ? offeredChoices(frame) : {}),
3821
3879
  };
3822
- gateCache.adopt(id, entry);
3823
- return entry;
3824
3880
  }
3825
3881
  const events = await adapter.runEvents(id);
3826
3882
  if (events === null)
3827
3883
  return 'no-log';
3828
- return gateCache.rebuild(id, events) ?? null;
3884
+ return replayOpenGate(id, events) ?? null;
3885
+ }
3886
+ /**
3887
+ * The engine's durable record of the run's open gate: ONE reading of its open `interaction_requests`
3888
+ * gate rows, which `GET /runs/:id/gate` serves and every decision path is judged by (crew#1083
3889
+ * round 5 — GET once took the first row while POST took the newest). `null` = this engine keeps
3890
+ * no rows. Rows that disagree on the ord are `ambiguous`: the record cannot say which gate is
3891
+ * open, and says so. Agreeing rows — one, or several: the engine can hold a keyed plan_approval
3892
+ * gate whose publisher was lost plus the unkeyed team_transport row boot recovery writes for the
3893
+ * same ord (wicked-core team_gate.rs) — are `open` at that ord, and the row read is the newest
3894
+ * by `created_at`.
3895
+ */
3896
+ async function recordedOpenGate(id) {
3897
+ if (typeof adapter.interactionRequests !== 'function')
3898
+ return null;
3899
+ const rows = await adapter.interactionRequests(id, 'open');
3900
+ if (rows === null)
3901
+ return null;
3902
+ const gates = rows.filter((r) => r.kind === 'gate');
3903
+ if (gates.length === 0)
3904
+ return { state: 'none' };
3905
+ const ords = [...new Set(gates.map((r) => r.ord ?? 0))].sort((a, b) => a - b);
3906
+ if (ords.length > 1)
3907
+ return { state: 'ambiguous', ords };
3908
+ const at = (r) => new Date(r.created_at).getTime() || 0;
3909
+ const row = gates.reduce((a, b) => (at(b) >= at(a) ? b : a));
3910
+ return { state: 'open', ord: ords[0], row };
3829
3911
  }
3830
- /** The unit an open gate holds: the named `ord`, else the open gate's, else the cursor's. */
3831
- async function gatedUnitOf(run, ord) {
3832
- let gateOrd = ord;
3833
- if (gateOrd === undefined) {
3834
- // A route set whose adapter cannot read the gate (a partial stub without `runEvents`) falls
3835
- // back to the cursor unit, exactly as a log that records no open gate does.
3836
- const open = await resolveOpenGate(run.session.id).catch(() => null);
3837
- gateOrd = open !== null && open !== 'no-log' ? open.ord : undefined;
3838
- }
3839
- const units = run.units ?? [];
3840
- return gateOrd !== undefined
3841
- ? units.find((u) => u.ord === gateOrd)
3842
- : [...units].sort((a, b) => a.ord - b.ord)[run.session.unit_ix];
3912
+ /**
3913
+ * The gate a decision answers, judged by the record (crew#1083 rounds 3–5) — the one check every
3914
+ * gate-confirming path makes, inside {@link decideExclusively}, before its delivery checks and its
3915
+ * confirm: `POST /runs/:id/gate` and standing orders (through `decideGate`), a gated
3916
+ * `POST /runs/:id/resume`, a gated `POST /runs/:id/reassign`. The record's open gate, never the
3917
+ * cache's: after a confirm the cache can still hold the answered gate until its `resumed` frame is
3918
+ * pumped, and a decision judged by it would answer the next gate with the last one's checks.
3919
+ *
3920
+ * - The record is ambiguous (open rows disagree on the ord) → 409 `gate_unknown`.
3921
+ * - It shows a gate open: a decision naming another ord → 409 `gate_changed` with `openOrd`;
3922
+ * otherwise the gated unit is that gate's, and the prompt and kind are its row's.
3923
+ * - It shows none (or the engine keeps no rows): a decision naming its ord → 409 `gate_unknown`.
3924
+ * A decision without one keeps the pre-table resolution (the cache, then the log, then the
3925
+ * cursor unit) — an engine, or a run, older than the table.
3926
+ */
3927
+ async function gateForDecision(run, namedOrd) {
3928
+ const id = run.session.id;
3929
+ const unitAt = (ord) => (run.units ?? []).find((u) => u.ord === ord);
3930
+ const unknown = (why) => ({ refusal: { code: 409, body: { error: `Gate unknown: ${why} — refresh and decide again.`, code: 'gate_unknown' } } });
3931
+ const recorded = await recordedOpenGate(id);
3932
+ if (recorded?.state === 'ambiguous') {
3933
+ return unknown(`the engine's record shows gates open before units ${recorded.ords.join(' and ')} on this run, so the daemon cannot tell which one a decision answers`);
3934
+ }
3935
+ if (recorded?.state === 'open') {
3936
+ if (namedOrd !== undefined && namedOrd !== recorded.ord) {
3937
+ return { refusal: { code: 409, body: {
3938
+ error: `Gate changed: this decision was made on the gate before unit ${namedOrd}, but the open gate `
3939
+ + `is before unit ${recorded.ord} — it was answered or replaced. Read the open gate before deciding.`,
3940
+ code: 'gate_changed',
3941
+ openOrd: recorded.ord,
3942
+ } } };
3943
+ }
3944
+ const kind = recorded.row['gate_kind'];
3945
+ return { gated: unitAt(recorded.ord), prompt: recorded.row.prompt, gateKind: typeof kind === 'string' && kind !== '' ? kind : undefined };
3946
+ }
3947
+ if (namedOrd !== undefined) {
3948
+ return unknown(`this decision names the gate before unit ${namedOrd}, but the engine's record `
3949
+ + `${recorded === null ? 'keeps no durable gate row' : 'shows no open gate'} for this run, so the daemon cannot confirm the named one`);
3950
+ }
3951
+ const open = await resolveOpenGate(id).catch(() => null);
3952
+ if (isOpenGate(open))
3953
+ return { gated: unitAt(open.ord), prompt: open.prompt, gateKind: open.gateKind };
3954
+ return { gated: [...(run.units ?? [])].sort((a, b) => a.ord - b.ord)[run.session.unit_ix], prompt: undefined, gateKind: undefined };
3843
3955
  }
3844
3956
  /**
3845
3957
  * The checks an APPROVE that runs the deliver unit must pass — the ONE place for both gate paths
@@ -3856,130 +3968,149 @@ runtime = {}) {
3856
3968
  }
3857
3969
  return { qeAcceptance };
3858
3970
  }
3971
+ /** Per run: the tail of the decisions queued by {@link decideExclusively}. Never rejects. */
3972
+ const gateDecisionTails = new Map();
3973
+ /**
3974
+ * Run `fn` alone for the run (crew#1083 round 4; the `McpPolicies.exclusive` pattern of #1082):
3975
+ * after every earlier decision for the same run has settled, and before any later one starts.
3976
+ * The row read and the confirm are separate engine commands and the confirm carries no ord, so
3977
+ * two concurrent decisions could both read the sole open ord-3 row, the first confirm advance
3978
+ * the run to its ord-4 (deliver) gate, and the second confirm answer THAT gate with the delivery
3979
+ * checks evaluated for ord 3 — a studio double-click on Approve is enough. Serialized, the second
3980
+ * re-reads inside the lock and meets ord 4: 409 `gate_changed`. Other runs are not blocked.
3981
+ *
3982
+ * Every crew API path that confirms a gate takes it and is judged by the record
3983
+ * ({@link gateForDecision}) under it: `decideGate` (`POST /runs/:id/gate`, standing orders),
3984
+ * `POST /runs/:id/resume`, `POST /runs/:id/reassign` (the re-seat included), `POST /runs/:id/plan`
3985
+ * (team routes) and the chat ask path's continuation (which re-reads its run inside the lock).
3986
+ * It serializes these paths; it does not bind a confirm to the gate the operator saw. A
3987
+ * decision without an `ord` (`/resume`, `/reassign`, `/plan`) queued behind one that advanced the
3988
+ * run answers the gate the record shows then, and the stall watchdog's `reassignUnit` runs
3989
+ * outside this lock. Both need every confirm bound to the gate it answers: an ord on the engine's
3990
+ * confirm (wicked-core#1038) and the skin sending it.
3991
+ */
3992
+ function decideExclusively(runId, fn) {
3993
+ const result = (gateDecisionTails.get(runId) ?? Promise.resolve()).then(fn);
3994
+ const tail = result.catch(() => undefined);
3995
+ gateDecisionTails.set(runId, tail);
3996
+ void tail.then(() => {
3997
+ if (gateDecisionTails.get(runId) === tail)
3998
+ gateDecisionTails.delete(runId);
3999
+ });
4000
+ return result;
4001
+ }
3859
4002
  /**
3860
4003
  * THE gate decision path. `POST /runs/:id/gate` answers with it, and so does a standing order
3861
4004
  * (behaviour 10) — the same awaiting check, the same ord check and 409 `gate_changed` /
3862
4005
  * `gate_unknown`, the same `confirmGate`, the same `gate.decided` audit. There is no side path:
3863
- * an order is just another actor, named on the audit line (`extraDetail.standingOrder`).
4006
+ * an order is just another actor, named on the audit line (`extraDetail.standingOrder`). One
4007
+ * decision at a time per run ({@link decideExclusively}).
3864
4008
  */
3865
4009
  async function decideGate(id, decision, actor, extraDetail = {}) {
3866
4010
  const parsed = GateSchema.safeParse(decision);
3867
4011
  if (!parsed.success) {
3868
4012
  return { code: 400, body: invalidBody(parsed.error, 'Invalid request body') };
3869
4013
  }
3870
- const views = await adapter.sessionsDetail();
3871
- const run = views.find((v) => v.session.id === id);
3872
- if (!run)
3873
- return { code: 404, body: { error: 'Run not found' } };
3874
- if (run.session.status !== 'awaiting_human') {
3875
- return { code: 409, body: { error: `Run is not awaiting a human gate (status: ${run.session.status})` } };
3876
- }
3877
- // api-types 0.44.0: a decision that names its gate answers THAT gate or nothing. When the
3878
- // daemon cannot tell which gate is open (no cache, no durable row, no log binding, or a log
3879
- // recording no open gate) it cannot confirm the named one either, so it refuses — the skin
3880
- // re-reads the gate and the person decides again. A request without `ord` is unchanged.
3881
- if (parsed.data.ord !== undefined) {
3882
- const open = await resolveOpenGate(id);
3883
- if (open === null || open === 'no-log') {
3884
- return { code: 409, body: {
3885
- error: `Gate unknown: this decision names the gate before unit ${parsed.data.ord}, but the daemon cannot `
3886
- + 'tell which gate is open on this run — refresh and decide again.',
3887
- code: 'gate_unknown',
3888
- } };
3889
- }
3890
- if (open.ord !== parsed.data.ord) {
3891
- return { code: 409, body: {
3892
- error: `Gate changed: this decision was made on the gate before unit ${parsed.data.ord}, but the open gate `
3893
- + `is before unit ${open.ord} — it was answered or replaced. Read the open gate before deciding.`,
3894
- code: 'gate_changed',
3895
- openOrd: open.ord,
3896
- } };
3897
- }
3898
- }
3899
- // The gated unit — the named gate, else the open one, else the unit the run's cursor points at —
3900
- // and its receipt as it stands when the person decides (wicked-core#850: every gate answer
3901
- // carries it). An APPROVE that would run the deliver unit passes the delivery checks first.
3902
- const gated = await gatedUnitOf(run, parsed.data.ord);
3903
- const assurance = receiptOf(gated);
3904
- let deliveryCheck;
3905
- if (parsed.data.approve && gated !== undefined && isDeliverUnit(gated)) {
3906
- const refused = await deliverApprovalRefusal(run, gated);
3907
- if ('refusal' in refused)
3908
- return refused.refusal;
3909
- deliveryCheck = { qeAcceptance: refused.qeAcceptance };
3910
- }
3911
- // The steering-author landing (crew#388): decided — and the gate prompt captured — BEFORE
3912
- // the confirm, because a terminal-phase approve prunes the gate cache and moves the run out
3913
- // of `awaiting_human`. The landing itself runs AFTER a successful approve only. The run is
3914
- // classified by its recorded name (seam X2), so a partial-stub adapter with no registry cannot
3915
- // 500 the legacy gate path here.
3916
- const steeringPropose = parsed.data.approve && isSteeringAuthorRun(run);
3917
- const gatePrompt = steeringPropose ? gateCache.get(id)?.prompt : undefined;
3918
- try {
3919
- // All five positionals, explicit `undefined` for the absent ones (DES-L1 PR-2; the
3920
- // gate-arms tripwire pins the arity). An engine refusal (no creator to send back to,
3921
- // an engine older than the arm) surfaces as the existing 409 below.
3922
- // (operator ruling 2026-10-11) WHO answered: the adapter hands it to the engine with this
3923
- // confirm, which records it as the judge when this approve answers a `judge_unavailable`
3924
- // gate ("judged by operator").
3925
- adapter.recordGateActor?.(id, `${actor.id} (${actor.kind})`);
3926
- const status = await adapter.confirmGate(id, parsed.data.approve, parsed.data.amend, parsed.data.action, parsed.data.amendScope, parsed.data.plan !== undefined ? toLaunchPlan(parsed.data.plan) : undefined);
3927
- // WHO approved/rejected — the gate-decision audit (task #88). The engine
3928
- // records THAT the gate resolved (interaction_requests / gateDecided);
3929
- // only this HTTP layer knows the authenticated principal behind it.
3930
- audit.record('gate.decided', actor, {
3931
- runId: id,
3932
- detail: {
3933
- approve: parsed.data.approve,
3934
- ...(parsed.data.amend !== undefined ? { amend: parsed.data.amend } : {}),
3935
- ...(parsed.data.action !== undefined ? { action: parsed.data.action } : {}),
3936
- ...(parsed.data.amendScope !== undefined ? { amendScope: parsed.data.amendScope } : {}),
3937
- ...(parsed.data.plan !== undefined ? { planSteps: parsed.data.plan.steps.length } : {}),
3938
- ...(parsed.data.ord !== undefined ? { ord: parsed.data.ord } : {}),
3939
- ...extraDetail,
3940
- status,
3941
- },
3942
- });
3943
- // DC-S4a host: the gate note (amend / request_changes text) — a bare approve or reject is a
3944
- // `choice`. Post-commit, off the request path; a non-human actor (a standing order) records nothing.
3945
- if (decisions !== null) {
3946
- decisions.track(ingestDecision(decisions, {
3947
- host: 'gate',
3948
- actor,
3949
- words: parsed.data.amend ?? '',
3950
- choice: parsed.data.action ?? (parsed.data.approve ? 'approve' : 'reject'),
3951
- projectId: projects.index.projectOf(id) ?? null,
4014
+ return decideExclusively(id, async () => {
4015
+ const views = await adapter.sessionsDetail();
4016
+ const run = views.find((v) => v.session.id === id);
4017
+ if (!run)
4018
+ return { code: 404, body: { error: 'Run not found' } };
4019
+ if (run.session.status !== 'awaiting_human') {
4020
+ return { code: 409, body: { error: `Run is not awaiting a human gate (status: ${run.session.status})` } };
4021
+ }
4022
+ // Decision integrity (api-types 0.44.0; crew#1083 rounds 3–5): the gate this decision answers
4023
+ // is the RECORD's — read first thing inside the lock, so the gated unit and its delivery
4024
+ // checks below are the ones the record confirms ({@link gateForDecision}).
4025
+ const decided = await gateForDecision(run, parsed.data.ord);
4026
+ if ('refusal' in decided)
4027
+ return decided.refusal;
4028
+ // The gated unit and its receipt as it stands when the person decides (wicked-core#850: every
4029
+ // gate answer carries it). An APPROVE that would run the deliver unit passes the delivery
4030
+ // checks first.
4031
+ const gated = decided.gated;
4032
+ const assurance = receiptOf(gated);
4033
+ let deliveryCheck;
4034
+ if (parsed.data.approve && gated !== undefined && isDeliverUnit(gated)) {
4035
+ const refused = await deliverApprovalRefusal(run, gated);
4036
+ if ('refusal' in refused)
4037
+ return refused.refusal;
4038
+ deliveryCheck = { qeAcceptance: refused.qeAcceptance };
4039
+ }
4040
+ // The steering-author landing (crew#388): decided — and the gate prompt captured — BEFORE
4041
+ // the confirm, because a terminal-phase approve prunes the gate cache and moves the run out
4042
+ // of `awaiting_human`. The landing itself runs AFTER a successful approve only. The run is
4043
+ // classified by its recorded name (seam X2), so a partial-stub adapter with no registry cannot
4044
+ // 500 the legacy gate path here. The prompt is the record's row (agy, round 4): the cache
4045
+ // holds nothing after a restart and another gate's words after a late frame.
4046
+ const steeringPropose = parsed.data.approve && isSteeringAuthorRun(run);
4047
+ const gatePrompt = steeringPropose ? decided.prompt : undefined;
4048
+ try {
4049
+ // All five positionals, explicit `undefined` for the absent ones (DES-L1 PR-2; the
4050
+ // gate-arms tripwire pins the arity). An engine refusal (no creator to send back to,
4051
+ // an engine older than the arm) surfaces as the existing 409 below.
4052
+ // (operator ruling 2026-10-11) WHO answered: the adapter hands it to the engine with this
4053
+ // confirm, which records it as the judge when this approve answers a `judge_unavailable`
4054
+ // gate ("judged by operator").
4055
+ adapter.recordGateActor?.(id, `${actor.id} (${actor.kind})`);
4056
+ const status = await adapter.confirmGate(id, parsed.data.approve, parsed.data.amend, parsed.data.action, parsed.data.amendScope, parsed.data.plan !== undefined ? toLaunchPlan(parsed.data.plan) : undefined);
4057
+ // WHO approved/rejected — the gate-decision audit (task #88). The engine
4058
+ // records THAT the gate resolved (interaction_requests / gateDecided);
4059
+ // only this HTTP layer knows the authenticated principal behind it.
4060
+ audit.record('gate.decided', actor, {
3952
4061
  runId: id,
3953
- ...(parsed.data.ord !== undefined ? { ord: parsed.data.ord } : {}),
3954
- }));
4062
+ detail: {
4063
+ approve: parsed.data.approve,
4064
+ ...(parsed.data.amend !== undefined ? { amend: parsed.data.amend } : {}),
4065
+ ...(parsed.data.action !== undefined ? { action: parsed.data.action } : {}),
4066
+ ...(parsed.data.amendScope !== undefined ? { amendScope: parsed.data.amendScope } : {}),
4067
+ ...(parsed.data.plan !== undefined ? { planSteps: parsed.data.plan.steps.length } : {}),
4068
+ ...(parsed.data.ord !== undefined ? { ord: parsed.data.ord } : {}),
4069
+ ...extraDetail,
4070
+ status,
4071
+ },
4072
+ });
4073
+ // DC-S4a host: the gate note (amend / request_changes text) — a bare approve or reject is a
4074
+ // `choice`. Post-commit, off the request path; a non-human actor (a standing order) records nothing.
4075
+ if (decisions !== null) {
4076
+ decisions.track(ingestDecision(decisions, {
4077
+ host: 'gate',
4078
+ actor,
4079
+ words: parsed.data.amend ?? '',
4080
+ choice: parsed.data.action ?? (parsed.data.approve ? 'approve' : 'reject'),
4081
+ projectId: projects.index.projectOf(id) ?? null,
4082
+ runId: id,
4083
+ ...(parsed.data.ord !== undefined ? { ord: parsed.data.ord } : {}),
4084
+ }));
4085
+ }
4086
+ // APPROVE of the steering-author propose gate = the doctrine's landing moment: the
4087
+ // approved proposal is written to the governance store with `provenance.source: "chat"`,
4088
+ // audited per rule, idempotent on replay, and LOUD on failure — the response carries the
4089
+ // outcome either way, so the studio can show "landed PAT-101" or the explicit error
4090
+ // instead of the silent no-op crew#388 recorded. An approve WITH an amend note still
4091
+ // lands the proposal UNCHANGED: the amend steers the RUN's continuation, not the rule
4092
+ // text (an operator who wants different rules rejects and re-authors). A reject lands
4093
+ // nothing — the run cancels and the proposal stays an artifact.
4094
+ const landing = steeringPropose
4095
+ ? await landSteeringProposal({ adapter, audit, actor }, run, gatePrompt)
4096
+ : undefined;
4097
+ return {
4098
+ code: 200,
4099
+ body: {
4100
+ status,
4101
+ ...(landing !== undefined ? { landing } : {}),
4102
+ assurance,
4103
+ ...(deliveryCheck !== undefined ? { delivery: deliveryCheck } : {}),
4104
+ },
4105
+ };
3955
4106
  }
3956
- // APPROVE of the steering-author propose gate = the doctrine's landing moment: the
3957
- // approved proposal is written to the governance store with `provenance.source: "chat"`,
3958
- // audited per rule, idempotent on replay, and LOUD on failure — the response carries the
3959
- // outcome either way, so the studio can show "landed PAT-101" or the explicit error
3960
- // instead of the silent no-op crew#388 recorded. An approve WITH an amend note still
3961
- // lands the proposal UNCHANGED: the amend steers the RUN's continuation, not the rule
3962
- // text (an operator who wants different rules rejects and re-authors). A reject lands
3963
- // nothing — the run cancels and the proposal stays an artifact.
3964
- const landing = steeringPropose
3965
- ? await landSteeringProposal({ adapter, audit, actor }, run, gatePrompt)
3966
- : undefined;
3967
- return {
3968
- code: 200,
3969
- body: {
3970
- status,
3971
- ...(landing !== undefined ? { landing } : {}),
3972
- assurance,
3973
- ...(deliveryCheck !== undefined ? { delivery: deliveryCheck } : {}),
3974
- },
3975
- };
3976
- }
3977
- catch (err) {
3978
- if (err instanceof PlanLaunchUnsupportedError) {
3979
- return { code: 501, body: { error: message(err) } };
4107
+ catch (err) {
4108
+ if (err instanceof PlanLaunchUnsupportedError) {
4109
+ return { code: 501, body: { error: message(err) } };
4110
+ }
4111
+ return { code: 409, body: { error: message(err) } };
3980
4112
  }
3981
- return { code: 409, body: { error: message(err) } };
3982
- }
4113
+ });
3983
4114
  }
3984
4115
  // The steering gate (§11.1). approve+amend = approve-with-steer; approve:false = reject (cancels).
3985
4116
  app.post(`${V}/runs/:id/gate`, {
@@ -4017,79 +4148,86 @@ runtime = {}) {
4017
4148
  // resumeRun re-enters the cursor. Never resumeRun a gated run (it would re-pause).
4018
4149
  app.post(`${V}/runs/:id/resume`, async (req, reply) => {
4019
4150
  const { id } = req.params;
4020
- const views = await adapter.sessionsDetail();
4021
- const run = views.find((v) => v.session.id === id);
4022
- if (!run)
4023
- return reply.code(404).send({ error: 'Run not found' });
4024
- // crew#418 A: a run stranded by a deliver-phase lift collision reads `failed` from the engine
4025
- // but its work is complete and liftable — normalize to `completed` so the terminal branch
4026
- // below refuses resume with the DELIVER recovery (POST /runs/:id/deliver), not a re-entry.
4027
- const conflictStrand = normalizeStranded(run);
4028
- // A TERMINAL run is refused LOUDLY, with the actual recovery named (crew#311 defect 2).
4029
- // The engine's `resume_run` no-ops on completed/cancelled and answers the status token, so
4030
- // this route used to reply 200 {"status":"cancelled"} — on the exact runs an operator was
4031
- // trying to rescue, the recovery affordance read as "resume destroyed my run". A cancelled
4032
- // run has NO in-run recovery (the path is a retry launch, `POST /runs {retryOf}`); a
4033
- // completed run has nothing to resume — but a stranded one's work IS liftable post-hoc.
4034
- // The 409 body carries the machine-readable pointer (`ResumeRefusal` in api-types).
4035
- const terminal = run.session.status;
4036
- if (terminal === 'completed' || terminal === 'cancelled') {
4037
- const state = resolveDelivery(run, conflictStrand);
4038
- const recovery = terminal === 'completed' && state.delivery === 'stranded'
4039
- ? 'deliver'
4040
- : 'retry';
4041
- const why = terminal === 'cancelled'
4042
- ? `run ${id} is cancelled — a terminal run cannot be resumed; relaunch the work as a new run with POST /runs {"retryOf":"${id}"}`
4043
- : state.delivery === 'stranded'
4044
- ? `run ${id} is already completed — nothing to resume; its unlifted work is on the wicked/${id} branch: deliver it with POST /runs/${id}/deliver`
4045
- : state.delivery === 'pushed'
4046
- ? `run ${id} is already completed and delivered — its branch ${state.deliverBranch ?? `wicked/${id}`} is on ${state.deliverRemote ?? 'origin'} (no pull request: that origin is not a GitHub host gh can resolve); nothing to resume`
4047
- : state.delivery === 'vacuous'
4048
- ? `run ${id} is already completed, but VACUOUSLY — its units produced no work to resume or deliver; relaunch with POST /runs {"retryOf":"${id}"}`
4049
- : `run ${id} is already completed — nothing to resume; relaunch the work as a new run with POST /runs {"retryOf":"${id}"}`;
4050
- return reply.code(409).send({ error: why, recovery });
4051
- }
4052
- try {
4053
- const gated = run.session.status === 'awaiting_human';
4054
- // A gated resume IS a gate approval, so it lands a steering-author proposal exactly like
4055
- // POST /runs/:id/gate would — the "no side door" rule (task #88) holds for the landing
4056
- // write too (crew#388). Prompt captured pre-confirm; see the gate route.
4057
- const steeringPropose = gated && isSteeringAuthorRun(run);
4058
- const gatePrompt = steeringPropose ? gateCache.get(id)?.prompt : undefined;
4059
- // A gated resume IS an approve, so it passes the same delivery checks (freeze, EX-03) — no side
4060
- // door around the deliver gate — and answers with the gated unit's receipt.
4061
- const gatedUnit = gated ? await gatedUnitOf(run, undefined) : undefined;
4062
- let deliveryCheck;
4063
- if (gatedUnit !== undefined && isDeliverUnit(gatedUnit)) {
4064
- const refused = await deliverApprovalRefusal(run, gatedUnit);
4065
- if ('refusal' in refused)
4066
- return reply.code(refused.refusal.code).send(refused.refusal.body);
4067
- deliveryCheck = { qeAcceptance: refused.qeAcceptance };
4151
+ // A gated resume confirms the gate: one decision at a time per run (crew#1083 round 5).
4152
+ return decideExclusively(id, async () => {
4153
+ const views = await adapter.sessionsDetail();
4154
+ const run = views.find((v) => v.session.id === id);
4155
+ if (!run)
4156
+ return reply.code(404).send({ error: 'Run not found' });
4157
+ // crew#418 A: a run stranded by a deliver-phase lift collision reads `failed` from the engine
4158
+ // but its work is complete and liftable — normalize to `completed` so the terminal branch
4159
+ // below refuses resume with the DELIVER recovery (POST /runs/:id/deliver), not a re-entry.
4160
+ const conflictStrand = normalizeStranded(run);
4161
+ // A TERMINAL run is refused LOUDLY, with the actual recovery named (crew#311 defect 2).
4162
+ // The engine's `resume_run` no-ops on completed/cancelled and answers the status token, so
4163
+ // this route used to reply 200 {"status":"cancelled"} — on the exact runs an operator was
4164
+ // trying to rescue, the recovery affordance read as "resume destroyed my run". A cancelled
4165
+ // run has NO in-run recovery (the path is a retry launch, `POST /runs {retryOf}`); a
4166
+ // completed run has nothing to resume — but a stranded one's work IS liftable post-hoc.
4167
+ // The 409 body carries the machine-readable pointer (`ResumeRefusal` in api-types).
4168
+ const terminal = run.session.status;
4169
+ if (terminal === 'completed' || terminal === 'cancelled') {
4170
+ const state = resolveDelivery(run, conflictStrand);
4171
+ const recovery = terminal === 'completed' && state.delivery === 'stranded'
4172
+ ? 'deliver'
4173
+ : 'retry';
4174
+ const why = terminal === 'cancelled'
4175
+ ? `run ${id} is cancelled — a terminal run cannot be resumed; relaunch the work as a new run with POST /runs {"retryOf":"${id}"}`
4176
+ : state.delivery === 'stranded'
4177
+ ? `run ${id} is already completed — nothing to resume; its unlifted work is on the wicked/${id} branch: deliver it with POST /runs/${id}/deliver`
4178
+ : state.delivery === 'pushed'
4179
+ ? `run ${id} is already completed and delivered — its branch ${state.deliverBranch ?? `wicked/${id}`} is on ${state.deliverRemote ?? 'origin'} (no pull request: that origin is not a GitHub host gh can resolve); nothing to resume`
4180
+ : state.delivery === 'vacuous'
4181
+ ? `run ${id} is already completed, but VACUOUSLY — its units produced no work to resume or deliver; relaunch with POST /runs {"retryOf":"${id}"}`
4182
+ : `run ${id} is already completed — nothing to resume; relaunch the work as a new run with POST /runs {"retryOf":"${id}"}`;
4183
+ return reply.code(409).send({ error: why, recovery });
4068
4184
  }
4069
- if (gated) {
4070
- const who = actorOf(req);
4071
- adapter.recordGateActor?.(id, `${who.id} (${who.kind})`);
4185
+ try {
4186
+ const gated = run.session.status === 'awaiting_human';
4187
+ // A gated resume IS a gate approval, so it lands a steering-author proposal exactly like
4188
+ // POST /runs/:id/gate would — the "no side door" rule (task #88) holds for the landing
4189
+ // write too (crew#388). Prompt captured pre-confirm; see the gate route.
4190
+ // A gated resume IS an approve, so it is judged by the record like every decision, passes the
4191
+ // same delivery checks (freeze, EX-03) — no side door around the deliver gate — and answers
4192
+ // with the gated unit's receipt.
4193
+ const decided = gated ? await gateForDecision(run, undefined) : undefined;
4194
+ if (decided !== undefined && 'refusal' in decided)
4195
+ return reply.code(decided.refusal.code).send(decided.refusal.body);
4196
+ const steeringPropose = gated && isSteeringAuthorRun(run);
4197
+ const gatePrompt = steeringPropose ? decided?.prompt : undefined;
4198
+ const gatedUnit = decided?.gated;
4199
+ let deliveryCheck;
4200
+ if (gatedUnit !== undefined && isDeliverUnit(gatedUnit)) {
4201
+ const refused = await deliverApprovalRefusal(run, gatedUnit);
4202
+ if ('refusal' in refused)
4203
+ return reply.code(refused.refusal.code).send(refused.refusal.body);
4204
+ deliveryCheck = { qeAcceptance: refused.qeAcceptance };
4205
+ }
4206
+ if (gated) {
4207
+ const who = actorOf(req);
4208
+ adapter.recordGateActor?.(id, `${who.id} (${who.kind})`);
4209
+ }
4210
+ const status = gated ? await adapter.confirmGate(id, true) : await adapter.resumeRun(id);
4211
+ // A resume of a gated run IS a gate approval — audit it as one, so the
4212
+ // "who approved" trail has no side door (task #88).
4213
+ audit.record(gated ? 'gate.decided' : 'run.resumed', actorOf(req), {
4214
+ runId: id,
4215
+ detail: gated ? { approve: true, via: 'resume', status } : { status },
4216
+ });
4217
+ const landing = steeringPropose
4218
+ ? await landSteeringProposal({ adapter, audit, actor: actorOf(req) }, run, gatePrompt)
4219
+ : undefined;
4220
+ return reply.send({
4221
+ status,
4222
+ ...(landing !== undefined ? { landing } : {}),
4223
+ ...(gated ? { assurance: receiptOf(gatedUnit) } : {}),
4224
+ ...(deliveryCheck !== undefined ? { delivery: deliveryCheck } : {}),
4225
+ });
4072
4226
  }
4073
- const status = gated ? await adapter.confirmGate(id, true) : await adapter.resumeRun(id);
4074
- // A resume of a gated run IS a gate approval — audit it as one, so the
4075
- // "who approved" trail has no side door (task #88).
4076
- audit.record(gated ? 'gate.decided' : 'run.resumed', actorOf(req), {
4077
- runId: id,
4078
- detail: gated ? { approve: true, via: 'resume', status } : { status },
4079
- });
4080
- const landing = steeringPropose
4081
- ? await landSteeringProposal({ adapter, audit, actor: actorOf(req) }, run, gatePrompt)
4082
- : undefined;
4083
- return reply.send({
4084
- status,
4085
- ...(landing !== undefined ? { landing } : {}),
4086
- ...(gated ? { assurance: receiptOf(gatedUnit) } : {}),
4087
- ...(deliveryCheck !== undefined ? { delivery: deliveryCheck } : {}),
4088
- });
4089
- }
4090
- catch (err) {
4091
- return reply.code(409).send({ error: message(err) });
4092
- }
4227
+ catch (err) {
4228
+ return reply.code(409).send({ error: message(err) });
4229
+ }
4230
+ });
4093
4231
  });
4094
4232
  // Inject an operator message into a run's active worker(s) (§11.7).
4095
4233
  // target="all" broadcasts; any other value targets a specific CLI key.
@@ -4122,22 +4260,25 @@ runtime = {}) {
4122
4260
  return reply.code(409).send({ error: message(err) });
4123
4261
  }
4124
4262
  });
4125
- /** crew#1058: the runs with a gated reassign (re-seat, then approve) in flight — one at a time. */
4126
- const gatedReassigns = new Set();
4263
+ /**
4264
+ * crew#1058 / #1083 round 7: the runs with a reassign in flight. A second reassign of the same run
4265
+ * is refused before it queues on the decision lock — queued, it would act on the state the first
4266
+ * left (re-seat a worker the first just started, or approve twice).
4267
+ */
4268
+ const reassignsInFlight = new Set();
4127
4269
  /**
4128
4270
  * codex r1 on #1060: whether approving the run's open gate ACCEPTS the parked unit's work rather
4129
4271
  * than re-running it (wicked-core `confirm_gate`: a `judge_unavailable` / `qe_reviewer_unavailable`
4130
4272
  * denial on the cursor unit is judged by the operator; an open `review_adjudication` gate lands
4131
4273
  * with carried items). Says what the approve does, or `null` when it re-runs the unit.
4132
4274
  */
4133
- async function approveAcceptsWork(run, ord) {
4275
+ async function approveAcceptsWork(run, ord, gateKind) {
4134
4276
  const unit = run.units.find((u) => u.ord === ord);
4135
4277
  const denial = unit?.denial;
4136
4278
  if (unit?.status === 'rejected' && (denial?.source === 'judge_unavailable' || denial?.source === 'qe_reviewer_unavailable')) {
4137
4279
  return 'is the operator\'s judgment of the work as it stands (no judge could rule)';
4138
4280
  }
4139
- const open = await resolveOpenGate(run.session.id).catch(() => null);
4140
- if (open !== null && open !== 'no-log' && open.gateKind === 'review_adjudication') {
4281
+ if (gateKind === 'review_adjudication') {
4141
4282
  return 'lands the reviewed work with its carried items';
4142
4283
  }
4143
4284
  return null;
@@ -4153,135 +4294,144 @@ runtime = {}) {
4153
4294
  if (!parsed.success) {
4154
4295
  return reply.code(400).send(invalidBody(parsed.error, 'Invalid request body'));
4155
4296
  }
4156
- const views = await adapter.sessionsDetail();
4157
- const run = views.find((v) => v.session.id === id);
4158
- if (!run)
4159
- return reply.code(404).send({ error: 'Run not found' });
4160
- // Wave 6 (F-7R2-007 root fix): a run PARKED at a gate on a dead seat — the stall watchdog's
4161
- // escalation gate, or a verdict escalation — is reassigned and approved in ONE call. crew#1058:
4162
- // with a named seat it is REASSIGN-THEN-APPROVE. The engine re-seats a parked run's cursor unit
4163
- // without dispatching it (wicked-core#773, `reseat_parked_unit`, core-ts >= 0.13.0) and refuses
4164
- // a seat it cannot honour (an author's seat for a review under a distinct-evaluator contract,
4165
- // a ballot-only seat, a held plan) with the run untouched, so a refusal is a 409 with the gate
4166
- // still open; the approve then dispatches the unit once, on the new seat. The old order
4167
- // (approve, then reassign) approved first, so a refused reassign left the failed seat running
4168
- // again. Without a named seat (the council's pick) the engine needs a running unit to re-route,
4169
- // so that path stays approve-then-reassign. Audited as BOTH a gate decision
4170
- // (`via: 'reassign'`) and a reassign, so the "who approved" trail has no side door.
4171
- // A steering-author propose gate is NOT approvable this way: its approve LANDS the proposal
4172
- // through `POST /runs/:id/gate` (crew#388), and a reassign is not a review of the rules.
4173
- const gated = run.session.status === 'awaiting_human';
4174
- if (run.session.status !== 'executing' && !gated) {
4175
- return reply.code(409).send({
4176
- error: `run ${id} is ${run.session.status}, not executing — only an executing run has a cursor unit to reassign`,
4177
- });
4178
- }
4179
- if (gated && typeof adapter.confirmGate !== 'function') {
4180
- // A partial adapter (a directly-driven route set) cannot approve the gate on the caller's
4181
- // behalf — say so by name, never a TypeError dressed as a refusal.
4182
- return reply.code(409).send({
4183
- error: `run ${id} is awaiting_human, not executing — the engine reassigns only an executing run, and ` +
4184
- `this build cannot approve the gate on your behalf (no confirmGate); approve via POST /runs/${id}/gate, then reassign`,
4185
- });
4186
- }
4187
- if (gated && isSteeringAuthorRun(run)) {
4188
- return reply.code(409).send({
4189
- error: `run ${id} is awaiting a steering-author propose gate — approve or reject it via POST /runs/${id}/gate (the approve lands the proposal); reassign is not a review of the rules`,
4190
- });
4191
- }
4192
- const requestedCli = parsed.data.cli;
4193
- if (requestedCli !== undefined) {
4194
- const pool = Array.isArray(run.session.clis) ? run.session.clis : [];
4195
- if (!pool.includes(requestedCli)) {
4196
- return reply.code(400).send({
4197
- error: `cli "${requestedCli}" is not in this run's seat pool (${pool.join(', ') || 'none known'})`,
4198
- });
4199
- }
4297
+ // One reassign per run at a time, refused rather than queued (crew#1083 round 7), then the gate
4298
+ // decision lock every confirming path shares, held from this read of the run through the re-seat
4299
+ // and the confirm (crew#1083 round 5).
4300
+ if (reassignsInFlight.has(id)) {
4301
+ return reply.code(409).send({ error: `a reassign of run ${id} is already in flight. Nothing was changed.` });
4200
4302
  }
4201
- // A gated reassign APPROVES the gate first, so it passes the same delivery checks (freeze,
4202
- // wicked-core#850 EX-03) when the gate holds the deliver unit (codex r1 on the EX-03 PR).
4203
- if (gated) {
4204
- const gatedUnit = await gatedUnitOf(run, undefined);
4205
- if (gatedUnit !== undefined && isDeliverUnit(gatedUnit)) {
4206
- const refused = await deliverApprovalRefusal(run, gatedUnit);
4207
- if ('refusal' in refused)
4208
- return reply.code(refused.refusal.code).send(refused.refusal.body);
4209
- }
4210
- }
4211
- const cursor = resolveCursorUnit(run);
4212
- const ord = cursor?.ord ?? run.session.unit_ix;
4213
- const approve = async () => {
4214
- if (typeof adapter.confirmGate !== 'function')
4215
- return; // refused above for a gated run
4216
- const who = actorOf(req);
4217
- adapter.recordGateActor?.(id, `${who.id} (${who.kind})`);
4218
- const status = await adapter.confirmGate(id, true);
4219
- audit.record('gate.decided', actorOf(req), {
4220
- runId: id,
4221
- detail: { approve: true, via: 'reassign', status },
4222
- });
4223
- };
4224
- const reassigned = (approved) => {
4225
- audit.record('run.reassigned', actorOf(req), {
4226
- runId: id,
4227
- detail: { ord, ...(requestedCli !== undefined ? { cli: requestedCli } : {}), ...(gated ? { approved } : {}) },
4228
- });
4229
- };
4230
- if (gated && requestedCli !== undefined) {
4231
- // codex r1 on #1060: an approve that ACCEPTS the parked work instead of re-running the unit —
4232
- // a `judge_unavailable` / `qe_reviewer_unavailable` denial (the operator judges, the unit
4233
- // counts) or a `review_adjudication` gate (lands with carried items) — must not follow a
4234
- // re-seat: the new seat would be recorded on work it never did, and never run.
4235
- const accepts = await approveAcceptsWork(run, ord);
4236
- if (accepts !== null) {
4237
- return reply.code(409).send({
4238
- error: `run ${id} is parked at a gate whose approve ${accepts}, so a reassign would not run unit ${ord} on '${requestedCli}' — approve, send back or stop it via POST /runs/${id}/gate. Nothing was changed.`,
4239
- });
4240
- }
4241
- // codex r1 on #1060: the re-seat and the approve are two engine commands; a second reassign of
4242
- // the same run between them would dispatch a seat neither response names.
4243
- if (gatedReassigns.has(id)) {
4244
- return reply.code(409).send({ error: `a reassign of run ${id} is already in flight. Nothing was changed.` });
4245
- }
4246
- gatedReassigns.add(id);
4247
- try {
4248
- try {
4249
- await adapter.reassignUnit(id, ord, requestedCli);
4303
+ reassignsInFlight.add(id);
4304
+ try {
4305
+ return await decideExclusively(id, async () => {
4306
+ const views = await adapter.sessionsDetail();
4307
+ const run = views.find((v) => v.session.id === id);
4308
+ if (!run)
4309
+ return reply.code(404).send({ error: 'Run not found' });
4310
+ // Wave 6 (F-7R2-007 root fix): a run PARKED at a gate on a dead seat — the stall watchdog's
4311
+ // escalation gate, or a verdict escalation — is reassigned and approved in ONE call. crew#1058:
4312
+ // with a named seat it is REASSIGN-THEN-APPROVE. The engine re-seats a parked run's cursor unit
4313
+ // without dispatching it (wicked-core#773, `reseat_parked_unit`, core-ts >= 0.13.0) and refuses
4314
+ // a seat it cannot honour (an author's seat for a review under a distinct-evaluator contract,
4315
+ // a ballot-only seat, a held plan) with the run untouched, so a refusal is a 409 with the gate
4316
+ // still open; the approve then dispatches the unit once, on the new seat. The old order
4317
+ // (approve, then reassign) approved first, so a refused reassign left the failed seat running
4318
+ // again. Without a named seat (the council's pick) the engine needs a running unit to re-route,
4319
+ // so that path stays approve-then-reassign. Audited as BOTH a gate decision
4320
+ // (`via: 'reassign'`) and a reassign, so the "who approved" trail has no side door.
4321
+ // A steering-author propose gate is NOT approvable this way: its approve LANDS the proposal
4322
+ // through `POST /runs/:id/gate` (crew#388), and a reassign is not a review of the rules.
4323
+ const gated = run.session.status === 'awaiting_human';
4324
+ if (run.session.status !== 'executing' && !gated) {
4325
+ return reply.code(409).send({
4326
+ error: `run ${id} is ${run.session.status}, not executing — only an executing run has a cursor unit to reassign`,
4327
+ });
4250
4328
  }
4251
- catch (err) {
4252
- // The engine refused the seat with the run untouched: nothing was approved.
4253
- return reply.code(409).send({ error: `${message(err)}. Nothing was approved; the gate is still open.` });
4329
+ if (gated && typeof adapter.confirmGate !== 'function') {
4330
+ // A partial adapter (a directly-driven route set) cannot approve the gate on the caller's
4331
+ // behalf — say so by name, never a TypeError dressed as a refusal.
4332
+ return reply.code(409).send({
4333
+ error: `run ${id} is awaiting_human, not executing — the engine reassigns only an executing run, and ` +
4334
+ `this build cannot approve the gate on your behalf (no confirmGate); approve via POST /runs/${id}/gate, then reassign`,
4335
+ });
4336
+ }
4337
+ if (gated && isSteeringAuthorRun(run)) {
4338
+ return reply.code(409).send({
4339
+ error: `run ${id} is awaiting a steering-author propose gate — approve or reject it via POST /runs/${id}/gate (the approve lands the proposal); reassign is not a review of the rules`,
4340
+ });
4341
+ }
4342
+ const requestedCli = parsed.data.cli;
4343
+ if (requestedCli !== undefined) {
4344
+ const pool = Array.isArray(run.session.clis) ? run.session.clis : [];
4345
+ if (!pool.includes(requestedCli)) {
4346
+ return reply.code(400).send({
4347
+ error: `cli "${requestedCli}" is not in this run's seat pool (${pool.join(', ') || 'none known'})`,
4348
+ });
4349
+ }
4350
+ }
4351
+ // A gated reassign APPROVES the gate, so it is judged by the record like every decision and passes
4352
+ // the same delivery checks (freeze, wicked-core#850 EX-03) when the gate holds the deliver unit
4353
+ // (codex r1 on the EX-03 PR).
4354
+ const decided = gated ? await gateForDecision(run, undefined) : undefined;
4355
+ if (decided !== undefined && 'refusal' in decided)
4356
+ return reply.code(decided.refusal.code).send(decided.refusal.body);
4357
+ if (decided !== undefined) {
4358
+ const gatedUnit = decided.gated;
4359
+ if (gatedUnit !== undefined && isDeliverUnit(gatedUnit)) {
4360
+ const refused = await deliverApprovalRefusal(run, gatedUnit);
4361
+ if ('refusal' in refused)
4362
+ return reply.code(refused.refusal.code).send(refused.refusal.body);
4363
+ }
4364
+ }
4365
+ const cursor = resolveCursorUnit(run);
4366
+ const ord = cursor?.ord ?? run.session.unit_ix;
4367
+ const approve = async () => {
4368
+ if (typeof adapter.confirmGate !== 'function')
4369
+ return; // refused above for a gated run
4370
+ const who = actorOf(req);
4371
+ adapter.recordGateActor?.(id, `${who.id} (${who.kind})`);
4372
+ const status = await adapter.confirmGate(id, true);
4373
+ audit.record('gate.decided', actorOf(req), {
4374
+ runId: id,
4375
+ detail: { approve: true, via: 'reassign', status },
4376
+ });
4377
+ };
4378
+ const reassigned = (approved) => {
4379
+ audit.record('run.reassigned', actorOf(req), {
4380
+ runId: id,
4381
+ detail: { ord, ...(requestedCli !== undefined ? { cli: requestedCli } : {}), ...(gated ? { approved } : {}) },
4382
+ });
4383
+ };
4384
+ if (gated && requestedCli !== undefined) {
4385
+ // codex r1 on #1060: an approve that ACCEPTS the parked work instead of re-running the unit —
4386
+ // a `judge_unavailable` / `qe_reviewer_unavailable` denial (the operator judges, the unit
4387
+ // counts) or a `review_adjudication` gate (lands with carried items) — must not follow a
4388
+ // re-seat: the new seat would be recorded on work it never did, and never run.
4389
+ const accepts = await approveAcceptsWork(run, ord, decided?.gateKind);
4390
+ if (accepts !== null) {
4391
+ return reply.code(409).send({
4392
+ error: `run ${id} is parked at a gate whose approve ${accepts}, so a reassign would not run unit ${ord} on '${requestedCli}' — approve, send back or stop it via POST /runs/${id}/gate. Nothing was changed.`,
4393
+ });
4394
+ }
4395
+ // The re-seat and the approve are two engine commands; the in-flight refusal and the lock this
4396
+ // runs under keep a second reassign (or any decision) of the run from landing between them.
4397
+ try {
4398
+ await adapter.reassignUnit(id, ord, requestedCli);
4399
+ }
4400
+ catch (err) {
4401
+ // The engine refused the seat with the run untouched: nothing was approved.
4402
+ return reply.code(409).send({ error: `${message(err)}. Nothing was approved; the gate is still open.` });
4403
+ }
4404
+ try {
4405
+ await approve();
4406
+ }
4407
+ catch (err) {
4408
+ reassigned(false);
4409
+ return reply.code(409).send({
4410
+ error: `unit ${ord} was re-seated on ${requestedCli}, but approving the gate failed: ${message(err)} — read the run's gate before answering again`,
4411
+ });
4412
+ }
4413
+ reassigned(true);
4414
+ return reply.send({ status: 'ok', ord, cli: requestedCli, approved: true });
4254
4415
  }
4255
4416
  try {
4256
- await approve();
4417
+ if (gated)
4418
+ await approve();
4419
+ await adapter.reassignUnit(id, ord, requestedCli ?? null);
4420
+ reassigned(true);
4421
+ return reply.send({
4422
+ status: 'ok',
4423
+ ord,
4424
+ ...(requestedCli !== undefined ? { cli: requestedCli } : {}),
4425
+ ...(gated ? { approved: true } : {}),
4426
+ });
4257
4427
  }
4258
4428
  catch (err) {
4259
- reassigned(false);
4260
- return reply.code(409).send({
4261
- error: `unit ${ord} was re-seated on ${requestedCli}, but approving the gate failed: ${message(err)} — read the run's gate before answering again`,
4262
- });
4429
+ return reply.code(409).send({ error: message(err) });
4263
4430
  }
4264
- reassigned(true);
4265
- return reply.send({ status: 'ok', ord, cli: requestedCli, approved: true });
4266
- }
4267
- finally {
4268
- gatedReassigns.delete(id);
4269
- }
4270
- }
4271
- try {
4272
- if (gated)
4273
- await approve();
4274
- await adapter.reassignUnit(id, ord, requestedCli ?? null);
4275
- reassigned(true);
4276
- return reply.send({
4277
- status: 'ok',
4278
- ord,
4279
- ...(requestedCli !== undefined ? { cli: requestedCli } : {}),
4280
- ...(gated ? { approved: true } : {}),
4281
4431
  });
4282
4432
  }
4283
- catch (err) {
4284
- return reply.code(409).send({ error: message(err) });
4433
+ finally {
4434
+ reassignsInFlight.delete(id);
4285
4435
  }
4286
4436
  });
4287
4437
  // The gate prompt for a paused run, so a fresh browser can render the gate after a late join.
@@ -4297,9 +4447,31 @@ runtime = {}) {
4297
4447
  const run = (await adapter.sessionsDetail()).find((v) => v.session.id === id);
4298
4448
  return receiptOf(run?.units?.find((u) => u.ord === ord));
4299
4449
  };
4450
+ // The record judges the cache (crew#1083 round 7): rows that disagree on the ord are 409
4451
+ // `gate_unknown` whether or not something is cached, and a cached entry is served — for its
4452
+ // presentation fields (prompt, choices, recommended) — only when its ord is the record's open
4453
+ // row's. With no open row the gate predates the engine's table and its record is the log
4454
+ // (FINDING-051): the entry is served while the run is still parked, so a restarted daemon does
4455
+ // not replay the log on every poll, and dropped once it is not (a late frame for an answered
4456
+ // gate). Any entry not served is dropped and the gate read from the record. Only an engine that
4457
+ // keeps no rows serves the cache unjudged.
4458
+ const recorded = await recordedOpenGate(id);
4459
+ if (recorded?.state === 'ambiguous') {
4460
+ return reply.code(409).send({
4461
+ error: `Gate unknown: the engine's record shows gates open before units ${recorded.ords.join(' and ')} on this run, so the daemon cannot tell which one is open.`,
4462
+ code: 'gate_unknown',
4463
+ });
4464
+ }
4300
4465
  const cached = gateCache.get(id);
4301
- if (cached)
4302
- return { runId: id, ...cached, assurance: await receiptAt(cached.ord) };
4466
+ if (cached) {
4467
+ const servable = recorded === null
4468
+ || (recorded.state === 'open' && cached.ord === recorded.ord)
4469
+ || (recorded.state === 'none'
4470
+ && (await adapter.sessionsDetail()).find((v) => v.session.id === id)?.session.status === 'awaiting_human');
4471
+ if (servable)
4472
+ return { runId: id, ...cached, assurance: await receiptAt(cached.ord) };
4473
+ gateCache.forget(id);
4474
+ }
4303
4475
  // A miss is not evidence of anything on its own, so ask the run what it is doing before paying
4304
4476
  // to replay it. Only `awaiting_human` can have an open gate, and that answer is definitive:
4305
4477
  // every other status is a 404 that needs no log at all — which is also the overwhelmingly
@@ -4321,7 +4493,39 @@ runtime = {}) {
4321
4493
  // daemon restart reads it back instead of replaying the log; the replay stays the FALLBACK for
4322
4494
  // runs parked before the engine grew the table. `resolveOpenGate` is that one resolution, shared
4323
4495
  // with the POST route's `ord` check.
4324
- const open = await resolveOpenGate(id);
4496
+ let open = await resolveOpenGate(id);
4497
+ // The record judges what the resolver returns too (crew#1083 final review): a stale frame folded
4498
+ // into the cache after GET's record read — during the status read, before any hydration began,
4499
+ // so no revision guard ran — is a cache hit for the resolver. When the answer disagrees with
4500
+ // that read, the record is read once more (it may have moved on, legitimately, meanwhile): an
4501
+ // answer that matches it is served; otherwise the entry is dropped and the gate resolved once
4502
+ // more from the record, and if that still disagrees nothing verified is served.
4503
+ if (recorded?.state === 'open' && isOpenGate(open) && open.ord !== recorded.ord) {
4504
+ const now = await recordedOpenGate(id);
4505
+ const matchesNow = (o) => isOpenGate(o) && now?.state === 'open' && o.ord === now.ord;
4506
+ if (!matchesNow(open)) {
4507
+ gateCache.forget(id);
4508
+ open = await resolveOpenGate(id);
4509
+ if (isOpenGate(open) && !matchesNow(open))
4510
+ open = 'unstable';
4511
+ }
4512
+ }
4513
+ if (open === 'ambiguous') {
4514
+ // The engine's open gate rows disagree on the ord (crew#1083 round 5): say so, as a decision
4515
+ // naming either gate is told — never pick one.
4516
+ return reply.code(409).send({
4517
+ error: 'Gate unknown: the engine\'s record shows gates open before more than one unit on this run, so the daemon cannot tell which one is open.',
4518
+ code: 'gate_unknown',
4519
+ });
4520
+ }
4521
+ if (open === 'unstable') {
4522
+ // Live gate events kept landing while the record was read, so nothing was verified
4523
+ // (crew#1083 round 3). Retryable: the next read sees a quieter run.
4524
+ return reply.code(503).send({
4525
+ error: 'Gate unstable: live gate events kept landing while the daemon read this run\'s open gate — retry.',
4526
+ code: 'gate_unstable',
4527
+ });
4528
+ }
4325
4529
  if (open === 'no-log') {
4326
4530
  // Now — and only now — 503 is the honest answer: this run really is holding for a human, and
4327
4531
  // this build cannot say what it is asking. Distinct cause, distinct message; answering "no
@@ -6560,7 +6764,7 @@ runtime = {}) {
6560
6764
  registerPresetRoutes(app, adapter, security);
6561
6765
  // ── Team (DES-TEAMING-002 T8) — reads and commands over the engine; the live stream is the
6562
6766
  // teamEvent relay (team/ws-relay.ts). Crew publishes no team fact (§4.0).
6563
- registerTeamRoutes(app, adapter, security);
6767
+ registerTeamRoutes(app, adapter, security, { exclusive: decideExclusively, judge: gateForDecision });
6564
6768
  // ── Campaigns (crew#342 + TH-9) — the engine's durable Run-DAG scheduler over REST ──────────
6565
6769
  // Progress streams as campaign* CoreEvents on the existing allowlist-free /ws relay; these
6566
6770
  // routes are launch/resume/cancel + the reads a scoreboard builds from. The GET routes join