wicked-crew 0.7.11 → 0.7.13

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 (48) hide show
  1. package/dist/api/delivery-index.d.ts +21 -0
  2. package/dist/api/delivery-index.d.ts.map +1 -1
  3. package/dist/api/delivery-index.js +35 -0
  4. package/dist/api/delivery-index.js.map +1 -1
  5. package/dist/api/endpoint-manifest.d.ts +26 -6
  6. package/dist/api/endpoint-manifest.d.ts.map +1 -1
  7. package/dist/api/endpoint-manifest.js +46 -10
  8. package/dist/api/endpoint-manifest.js.map +1 -1
  9. package/dist/api/post-hoc-deliver.d.ts +27 -0
  10. package/dist/api/post-hoc-deliver.d.ts.map +1 -1
  11. package/dist/api/post-hoc-deliver.js +54 -0
  12. package/dist/api/post-hoc-deliver.js.map +1 -1
  13. package/dist/api/routes.d.ts +5 -1
  14. package/dist/api/routes.d.ts.map +1 -1
  15. package/dist/api/routes.js +124 -12
  16. package/dist/api/routes.js.map +1 -1
  17. package/dist/api/seat-health.d.ts +11 -0
  18. package/dist/api/seat-health.d.ts.map +1 -1
  19. package/dist/api/seat-health.js +23 -0
  20. package/dist/api/seat-health.js.map +1 -1
  21. package/dist/api/server.d.ts.map +1 -1
  22. package/dist/api/server.js +5 -1
  23. package/dist/api/server.js.map +1 -1
  24. package/dist/api/testing.d.ts.map +1 -1
  25. package/dist/api/testing.js +11 -27
  26. package/dist/api/testing.js.map +1 -1
  27. package/dist/campaigns/rollup.d.ts.map +1 -1
  28. package/dist/campaigns/rollup.js +17 -3
  29. package/dist/campaigns/rollup.js.map +1 -1
  30. package/dist/cli/index.js +13 -2
  31. package/dist/cli/index.js.map +1 -1
  32. package/dist/core/adapter.d.ts +0 -12
  33. package/dist/core/adapter.d.ts.map +1 -1
  34. package/dist/core/adapter.js +0 -14
  35. package/dist/core/adapter.js.map +1 -1
  36. package/dist/core/daemon-signal-log.d.ts +44 -0
  37. package/dist/core/daemon-signal-log.d.ts.map +1 -0
  38. package/dist/core/daemon-signal-log.js +57 -0
  39. package/dist/core/daemon-signal-log.js.map +1 -0
  40. package/dist/core/deliver.d.ts +20 -3
  41. package/dist/core/deliver.d.ts.map +1 -1
  42. package/dist/core/deliver.js +113 -9
  43. package/dist/core/deliver.js.map +1 -1
  44. package/package.json +2 -2
  45. package/dist/campaigns/worktrees.d.ts +0 -39
  46. package/dist/campaigns/worktrees.d.ts.map +0 -1
  47. package/dist/campaigns/worktrees.js +0 -95
  48. package/dist/campaigns/worktrees.js.map +0 -1
@@ -34,8 +34,8 @@ import { AcpFoldCache, EngineVersionCache, eventsDirOf, installedPackageVersion,
34
34
  import { RetryIndex } from './retry-index.js';
35
35
  import { GroupIndex } from './group-index.js';
36
36
  import { GuidanceIndex } from './guidance-index.js';
37
- import { DeliveryIndex, deliveryStateWithVacuity, gitRunBranchIsEmpty, gitWorktreeIsClean, prUrlFrom, } from './delivery-index.js';
38
- import { runDeliverScript } from './post-hoc-deliver.js';
37
+ import { DeliveryIndex, deliveryStateWithVacuity, gitRunBranchIsEmpty, gitWorktreeIsClean, isDeliverConflictStranded, prUrlFrom, } from './delivery-index.js';
38
+ import { gitReprovisionWorktree, runDeliverScript, } from './post-hoc-deliver.js';
39
39
  import { LOCAL_ACTOR } from './auth.js';
40
40
  // Re-exported so existing `import { API_PREFIX } from './routes.js'` callers keep working; the
41
41
  // value lives in the leaf module api-prefix.ts to keep unit-output.ts out of this file's cycle.
@@ -263,6 +263,9 @@ runtime = {}) {
263
263
  }),
264
264
  };
265
265
  const deliverExec = runtime.deliverExec ?? runDeliverScript;
266
+ const reprovisionWorktree = runtime.reprovisionWorktree ?? gitReprovisionWorktree;
267
+ /** Repo root for a repo ref, from the registry — shared by the reprovision path below. */
268
+ const repoRootOf = async (repoRef) => (await adapter.listRepos()).find((r) => r.id === repoRef)?.root_path;
266
269
  // The run-DTO joins (DES-UX-001 §8.2/§8.3, DES-UX-002 §7.2): `project_id` from the membership
267
270
  // record — `null` = genuinely unfiled, so the field is ALWAYS present on served runs —
268
271
  // `retry_of` from the lineage index and `guidance` from the guidance index, each set only
@@ -275,7 +278,42 @@ runtime = {}) {
275
278
  // "done" with nothing produced must be LOUD on the wire, never silently green.
276
279
  // Applied at DTO assembly on exactly the two endpoints that serve the run DTO
277
280
  // (GET /runs + GET /runs/:id); the internal sessionsDetail() consumers are untouched.
281
+ // crew#418 A: a `failed` run whose ONLY failure was the deliver phase's LIFT collision (a
282
+ // rebase conflict the changelog union merge could not clear, or a non-fast-forward push) is
283
+ // reinterpreted on the wire as `completed` + `delivery: 'stranded'` — recoverable, not a hard
284
+ // failure. The engine's durable record stays `failed` (audit trail); this is a wire derivation,
285
+ // exactly like `delivery` itself, applied at the SAME three terminal-status decision points so
286
+ // the run reads consistently across GET /runs(/:id), resume, and POST /runs/:id/deliver. The
287
+ // deliver unit stays `rejected` with its marker-bearing `denial_reason`, so WHY it stranded is
288
+ // still on the wire.
289
+ //
290
+ // Returns whether this run IS such a strand — the caller uses it for a BRANCH-based `'stranded'`
291
+ // read: the engine reaps a failed-deliver run's worktree once the deliver phase has committed
292
+ // the work (a clean tree), so the work lives on the `wicked/<id>` branch, not the worktree — the
293
+ // worktree-stat derivation would read `'none'` (and flicker as the async reap lands). The
294
+ // lift-conflict marker is proof the branch carries the work (the deliver script verified it was
295
+ // ahead of the default branch before it ever attempted the rebase), so the read is a STABLE
296
+ // `'stranded'` regardless of whether the worktree survived. Idempotent: a second call sees the
297
+ // already-flipped `completed` and returns false, so the flip never re-fires.
298
+ const normalizeStranded = (view) => {
299
+ const conflictStrand = isDeliverConflictStranded(view); // true only while status === 'failed'
300
+ if (conflictStrand)
301
+ view.session.status = 'completed';
302
+ return conflictStrand;
303
+ };
304
+ /** The run's delivery state, honest by construction (crew#393/#311/#418): a recorded PR wins
305
+ * (`'delivered'`); else a lift-conflict strand reads `'stranded'` from the branch; else the
306
+ * worktree-stat derivation (stranded / vacuous / none). */
307
+ const resolveDelivery = async (view, conflictStrand) => {
308
+ const url = deliveryIndex.urlFor(view.session.id);
309
+ if (url !== undefined)
310
+ return { delivery: 'delivered', deliverUrl: url };
311
+ if (conflictStrand)
312
+ return { delivery: 'stranded' };
313
+ return deliveryStateWithVacuity(view.session, undefined, vacuityProbes);
314
+ };
278
315
  const decorateRun = async (view) => {
316
+ const conflictStrand = normalizeStranded(view);
279
317
  view.session.project_id = projects.index.projectOf(view.session.id) ?? null;
280
318
  const retryOf = retryIndex.retryOfFor(view.session.id);
281
319
  if (retryOf !== undefined)
@@ -292,7 +330,7 @@ runtime = {}) {
292
330
  const guidance = guidanceIndex.guidanceFor(view.session.id);
293
331
  if (guidance !== undefined)
294
332
  view.session.guidance = guidance;
295
- const state = await deliveryStateWithVacuity(view.session, deliveryIndex.urlFor(view.session.id), vacuityProbes);
333
+ const state = await resolveDelivery(view, conflictStrand);
296
334
  view.session.delivery = state.delivery;
297
335
  if (state.deliverUrl !== undefined)
298
336
  view.session.deliverUrl = state.deliverUrl;
@@ -906,6 +944,30 @@ runtime = {}) {
906
944
  // existing worktree. Idempotent: a delivered run answers its recorded URL, never a second PR.
907
945
  // Failure is a loud 4xx/5xx carrying the script's own words — never a silent 200.
908
946
  const deliverInFlight = new Set();
947
+ // Per-REPO serialization for the worktree-admin-sensitive region (reprovision → deliver →
948
+ // cleanup). deliverInFlight is per-RUN and stops the same run double-delivering, but two
949
+ // DIFFERENT stranded runs in the same repo would both run `git worktree prune/add/remove`
950
+ // against one `.git/worktrees` admin state and race. Chaining a promise per repo ref serializes
951
+ // them; different repos still run in parallel. The tail entry is pruned so the map cannot grow.
952
+ const repoDeliverChain = new Map();
953
+ const withRepoDeliverLock = async (repoRef, fn) => {
954
+ const prior = repoDeliverChain.get(repoRef) ?? Promise.resolve();
955
+ let release;
956
+ const mine = new Promise((r) => {
957
+ release = r;
958
+ });
959
+ repoDeliverChain.set(repoRef, mine); // WE are now the tail
960
+ await prior.catch(() => undefined); // wait our turn; a prior failure never blocks ours
961
+ try {
962
+ return await fn();
963
+ }
964
+ finally {
965
+ release(); // unblock whoever chained after us
966
+ // If nothing chained after us, drop the entry so idle repos leave no trace.
967
+ if (repoDeliverChain.get(repoRef) === mine)
968
+ repoDeliverChain.delete(repoRef);
969
+ }
970
+ };
909
971
  /** The last words of a failed deliver script — enough to name the refusal, bounded so a full
910
972
  * git transcript never becomes an error body. */
911
973
  const deliverErrorTail = (output) => {
@@ -934,6 +996,10 @@ runtime = {}) {
934
996
  const run = views.find((v) => v.session.id === id);
935
997
  if (!run)
936
998
  return reply.code(404).send({ error: 'Run not found' });
999
+ // crew#418 A: a run stranded by a deliver-phase lift collision reads `failed` from the
1000
+ // engine but IS liftable post-hoc — normalize its status to `completed` so this route
1001
+ // accepts it, exactly as the run wire and the resume route see it.
1002
+ const conflictStrand = normalizeStranded(run);
937
1003
  const s = run.session;
938
1004
  if (s.status !== 'completed') {
939
1005
  return reply.code(409).send({
@@ -945,22 +1011,55 @@ runtime = {}) {
945
1011
  error: `run ${id} is not repo-scoped — there is no worktree to lift into a PR`,
946
1012
  });
947
1013
  }
948
- if (!worktreeExists(s.workdir)) {
949
- return reply.code(409).send({
950
- error: `run ${id}'s worktree is gone (${s.workdir}) — nothing left to deliver`,
951
- });
952
- }
1014
+ // Capture the ref + workdir while the null-check narrowing is fresh — an intervening await
1015
+ // below invalidates property narrowing, so re-reading `s.repo_ref`/`s.workdir` later widens
1016
+ // back to `| null`.
1017
+ const repoRef = s.repo_ref;
1018
+ const initialWorkdir = s.workdir;
953
1019
  // One delivery per run at a time: the script pushes and opens a PR, so two concurrent
954
- // spawns could race gh into two PRs — the exact double-open idempotency forbids.
1020
+ // spawns could race gh into two PRs — the exact double-open idempotency forbids. The guard
1021
+ // wraps the reprovision too, so a reaped run's `wicked/<id>` branch is only ever checked out
1022
+ // by one throwaway worktree at a time.
955
1023
  if (deliverInFlight.has(id)) {
956
1024
  return reply.code(409).send({
957
1025
  error: `a delivery for run ${id} is already in progress — wait for it to finish`,
958
1026
  });
959
1027
  }
960
1028
  deliverInFlight.add(id);
1029
+ // The worktree the script runs in. Usually the run's own; but the engine REAPS a
1030
+ // failed-deliver run's worktree once its work is committed (crew#418) — the work then lives
1031
+ // on the `wicked/<id>` branch. For such a strand, stand a throwaway worktree back up from
1032
+ // that branch and lift THAT; the branch (the record) is untouched. `cleanupWorktree` tears
1033
+ // the throwaway down after the lift.
961
1034
  let result;
1035
+ let worktreeGone = false;
962
1036
  try {
963
- result = await deliverExec(s.workdir, s.problem);
1037
+ // The worktree admin (reprovision), the deliver spawn, AND the throwaway teardown all run
1038
+ // under the per-repo lock, so a second stranded run in this repo cannot touch
1039
+ // `.git/worktrees` until this one has stood its worktree up, lifted, and torn it back
1040
+ // down. `cw` is a LOCAL torn down in the lock body's own finally — nothing captured-mutated
1041
+ // crosses the closure boundary.
1042
+ await withRepoDeliverLock(repoRef, async () => {
1043
+ let workdir = initialWorkdir;
1044
+ let cw = null;
1045
+ try {
1046
+ if (!worktreeExists(workdir)) {
1047
+ const root = conflictStrand ? await repoRootOf(repoRef) : undefined;
1048
+ const reprov = root !== undefined ? await reprovisionWorktree(root, id) : null;
1049
+ if (reprov === null) {
1050
+ worktreeGone = true;
1051
+ return;
1052
+ }
1053
+ workdir = reprov.workdir;
1054
+ cw = reprov.cleanup;
1055
+ }
1056
+ result = await deliverExec(workdir, s.problem ?? undefined);
1057
+ }
1058
+ finally {
1059
+ if (cw !== null)
1060
+ await cw(); // tear the throwaway down whether the lift succeeded or threw
1061
+ }
1062
+ });
964
1063
  }
965
1064
  catch (err) {
966
1065
  return reply.code(500).send({ error: `deliver script could not run: ${message(err)}` });
@@ -968,6 +1067,15 @@ runtime = {}) {
968
1067
  finally {
969
1068
  deliverInFlight.delete(id);
970
1069
  }
1070
+ if (worktreeGone) {
1071
+ return reply.code(409).send({
1072
+ error: `run ${id}'s worktree is gone (${s.workdir}) — nothing left to deliver`,
1073
+ });
1074
+ }
1075
+ if (result === undefined) {
1076
+ // Unreachable: the lock body assigns `result` on every path that is not `worktreeGone`.
1077
+ return reply.code(500).send({ error: `deliver for run ${id} produced no result` });
1078
+ }
971
1079
  if (result.spawnFailure === true) {
972
1080
  // The script never reached its own verdict (spawn failure, timeout kill) — an infra
973
1081
  // fault, not a refusal: 500 so the caller knows a retry is reasonable.
@@ -1342,6 +1450,10 @@ runtime = {}) {
1342
1450
  const run = views.find((v) => v.session.id === id);
1343
1451
  if (!run)
1344
1452
  return reply.code(404).send({ error: 'Run not found' });
1453
+ // crew#418 A: a run stranded by a deliver-phase lift collision reads `failed` from the engine
1454
+ // but its work is complete and liftable — normalize to `completed` so the terminal branch
1455
+ // below refuses resume with the DELIVER recovery (POST /runs/:id/deliver), not a re-entry.
1456
+ const conflictStrand = normalizeStranded(run);
1345
1457
  // A TERMINAL run is refused LOUDLY, with the actual recovery named (crew#311 defect 2).
1346
1458
  // The engine's `resume_run` no-ops on completed/cancelled and answers the status token, so
1347
1459
  // this route used to reply 200 {"status":"cancelled"} — on the exact runs an operator was
@@ -1351,14 +1463,14 @@ runtime = {}) {
1351
1463
  // The 409 body carries the machine-readable pointer (`ResumeRefusal` in api-types).
1352
1464
  const terminal = run.session.status;
1353
1465
  if (terminal === 'completed' || terminal === 'cancelled') {
1354
- const state = await deliveryStateWithVacuity(run.session, deliveryIndex.urlFor(id), vacuityProbes);
1466
+ const state = await resolveDelivery(run, conflictStrand);
1355
1467
  const recovery = terminal === 'completed' && state.delivery === 'stranded'
1356
1468
  ? 'deliver'
1357
1469
  : 'retry';
1358
1470
  const why = terminal === 'cancelled'
1359
1471
  ? `run ${id} is cancelled — a terminal run cannot be resumed; relaunch the work as a new run with POST /runs {"retryOf":"${id}"}`
1360
1472
  : state.delivery === 'stranded'
1361
- ? `run ${id} is already completed — nothing to resume; its unlifted work is in the worktree: deliver it with POST /runs/${id}/deliver`
1473
+ ? `run ${id} is already completed — nothing to resume; its unlifted work is on the wicked/${id} branch: deliver it with POST /runs/${id}/deliver`
1362
1474
  : state.delivery === 'vacuous'
1363
1475
  ? `run ${id} is already completed, but VACUOUSLY — its units produced no work to resume or deliver; relaunch with POST /runs {"retryOf":"${id}"}`
1364
1476
  : `run ${id} is already completed — nothing to resume; relaunch the work as a new run with POST /runs {"retryOf":"${id}"}`;