forgedock 1.51.0 → 1.53.0

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 (103) hide show
  1. package/bin/engine/outcome-codes.mjs +3 -0
  2. package/bin/engine/telemetry-seam.mjs +4 -2
  3. package/bin/engine-cli.mjs +61 -17
  4. package/bin/experiment.mjs +58 -6
  5. package/bin/forge-utils.mjs +6 -2
  6. package/bin/forgedock.mjs +25 -1
  7. package/bin/lean/account-watch.mjs +28 -0
  8. package/bin/lean/account-watch.test.mjs +30 -0
  9. package/bin/lean/activation.mjs +20 -0
  10. package/bin/lean/activation.test.mjs +14 -1
  11. package/bin/lean/basered.mjs +115 -0
  12. package/bin/lean/basered.test.mjs +66 -0
  13. package/bin/lean/batch-health.mjs +40 -6
  14. package/bin/lean/batch-health.test.mjs +34 -1
  15. package/bin/lean/batch.mjs +41 -2
  16. package/bin/lean/batch.test.mjs +104 -2
  17. package/bin/lean/checkpoint-reconcile.mjs +43 -0
  18. package/bin/lean/checkpoint-reconcile.test.mjs +95 -0
  19. package/bin/lean/conformance.e2e.test.mjs +1 -1
  20. package/bin/lean/conformance.mjs +205 -23
  21. package/bin/lean/conformance.test.mjs +164 -3
  22. package/bin/lean/context.mjs +27 -17
  23. package/bin/lean/context.test.mjs +15 -0
  24. package/bin/lean/effects.mjs +11 -7
  25. package/bin/lean/execution.mjs +19 -3
  26. package/bin/lean/execution.test.mjs +39 -0
  27. package/bin/lean/findinglabel.mjs +16 -0
  28. package/bin/lean/findinglabel.test.mjs +39 -0
  29. package/bin/lean/heavy.mjs +6 -3
  30. package/bin/lean/hostload.mjs +78 -0
  31. package/bin/lean/hostload.test.mjs +117 -0
  32. package/bin/lean/hotspots-remote.e2e.test.mjs +71 -0
  33. package/bin/lean/issuewatch-release.mjs +6 -2
  34. package/bin/lean/issuewatch-release.test.mjs +16 -0
  35. package/bin/lean/issuewatch.mjs +11 -1
  36. package/bin/lean/lanelock.mjs +14 -0
  37. package/bin/lean/lean.mjs +171 -36
  38. package/bin/lean/lean.test.mjs +302 -10
  39. package/bin/lean/light.mjs +48 -7
  40. package/bin/lean/light.test.mjs +37 -3
  41. package/bin/lean/limits.test.mjs +142 -9
  42. package/bin/lean/orchestrate-cancel.test.mjs +143 -0
  43. package/bin/lean/orchestrate.mjs +110 -26
  44. package/bin/lean/postdeploy-check.test.mjs +39 -0
  45. package/bin/lean/prompts.mjs +8 -7
  46. package/bin/lean/quota.mjs +39 -0
  47. package/bin/lean/recovery.test.mjs +10 -0
  48. package/bin/lean/relaxation.mjs +73 -0
  49. package/bin/lean/relaxation.test.mjs +54 -0
  50. package/bin/lean/release-postmerge.mjs +119 -0
  51. package/bin/lean/release-rehearsal.mjs +63 -0
  52. package/bin/lean/release-rehearsal.test.mjs +93 -0
  53. package/bin/lean/release-review.e2e.test.mjs +77 -0
  54. package/bin/lean/release-review.mjs +198 -0
  55. package/bin/lean/release-review.test.mjs +180 -0
  56. package/bin/lean/release.mjs +228 -0
  57. package/bin/lean/release.test.mjs +292 -0
  58. package/bin/lean/rotator.mjs +150 -31
  59. package/bin/lean/rotator.test.mjs +105 -2
  60. package/bin/lean/sandbox-remote.mjs +9 -2
  61. package/bin/lean/schedd.mjs +56 -6
  62. package/bin/lean/schedd.test.mjs +108 -6
  63. package/bin/lean/verifier.mjs +3 -2
  64. package/bin/lean/verify.mjs +2 -1
  65. package/bin/lean/verify.test.mjs +6 -0
  66. package/bin/pool/agent.mjs +18 -4
  67. package/bin/pool/router.mjs +68 -25
  68. package/bin/review-staging.mjs +23 -0
  69. package/bin/stats/conformance-outcomes.mjs +208 -0
  70. package/bin/stats/conformance-outcomes.test.mjs +188 -0
  71. package/bin/stats/depth.mjs +218 -0
  72. package/bin/stats/depth.test.mjs +133 -0
  73. package/bin/stats/experiment.mjs +7 -5
  74. package/bin/stats/github-source.mjs +2 -2
  75. package/bin/stats/index.mjs +64 -2
  76. package/bin/stats/local-source.mjs +11 -0
  77. package/bin/stats/query.mjs +69 -0
  78. package/bin/stats/selffiled.mjs +52 -0
  79. package/bin/stats/selffiled.test.mjs +71 -0
  80. package/commands/orchestrate.md +1 -1
  81. package/commands/review-pr-agents/security.md +4 -0
  82. package/commands/review-pr-staging.md +2 -0
  83. package/commands/work-on/build.md +4 -6
  84. package/docs/CONFIG.md +66 -8
  85. package/docs/ISSUE-WATCH.md +2 -0
  86. package/docs/OUTCOME-CODES.md +3 -0
  87. package/docs/STATS.md +44 -0
  88. package/docs/TELEMETRY.md +1 -1
  89. package/package.json +1 -1
  90. package/pool/README.md +1 -1
  91. package/pool/image/cs-cred +14 -1
  92. package/pool/install/install-agent.ps1 +6 -2
  93. package/scripts/backfill-finding-labels.mjs +50 -0
  94. package/scripts/backfill-finding-labels.test.mjs +36 -0
  95. package/scripts/doctor-pipeline-state.sh +21 -6
  96. package/scripts/file-deferred-followups.sh +60 -238
  97. package/scripts/file-deferred-followups.test.sh +99 -187
  98. package/scripts/followup-route.mjs +12 -0
  99. package/scripts/hotspots.sh +12 -8
  100. package/scripts/hotspots.test.sh +22 -0
  101. package/scripts/owner-route-parity.test.sh +17 -1
  102. package/scripts/prior-attempts.test.mjs +1 -1
  103. package/scripts/review-pr-side-effects.test.sh +40 -0
@@ -248,24 +248,157 @@ describe("account rotator and usage-limit holds (#5094)", () => {
248
248
  assert.deepEqual(starts, [t0]);
249
249
  });
250
250
 
251
- it("#5094 #5095 a limited pool account keeps its own hold under a rotator with headroom; the host account is never held", async () => {
251
+ it("#5094 #5095 #5203 a limited pool account rotates under a rotator with headroom (no pool hold); once every account was tried it keeps its own hold", async () => {
252
252
  const { c, now, sleep } = fakeClock();
253
253
  const t0 = c.t;
254
254
  const limits = openLimits(storeFile(), { now });
255
255
  const rotator = { accounts: [{ n: 1 }], headroom: () => true, resumeAt: () => now(), isAccount: (id) => id === "acct-1", currentIdentity: () => "acct-1" };
256
256
  const remote = { max: 2, fallbackLocal: true, isRemote: () => true, place: () => ({ where: "remote", reason: "heavy" }), identity: () => "remote:pool" };
257
- const runs = [];
257
+ const runs = [], pausedHolds = [];
258
258
  const rows = await orchestrate({ issues: [1, 2], parallel: 1, now, sleep, readBody: async () => "", limits, rotator, remote, identity: () => laneBackendId({}, rotator),
259
+ onUpdate: (u) => { if (u.outcome === "paused") pausedHolds.push(limits.holdUntil("remote:pool")); },
259
260
  runOne: async (n, _l, ctx) => {
260
261
  runs.push({ n, at: now(), where: ctx.placement?.where, poolHeld: limits.holdUntil("remote:pool"), hostHeld: limits.holdUntil("acct-1") });
261
- return n === 1 && runs.length === 1 ? limited("remote:pool", t0 + 600_000) : { terminalReason: "merged" };
262
+ return n === 1 && runs.filter((x) => x.n === 1).length <= 2 ? limited("remote:pool", t0 + 600_000) : { terminalReason: "merged" };
262
263
  } });
263
264
  assert.deepEqual(rows.map((r) => r.outcome), ["merged", "merged"]);
264
- const second = runs.find((x) => x.n === 2);
265
- assert.equal(runs[0].where, "remote");
266
- assert.equal(second.at, t0, "the host account is not held: admissions continue");
267
- assert.equal(second.poolHeld, t0 + 600_000, "the pool's own hold is recorded (#5095)");
268
- assert.ok(!(second.hostHeld > now()), "no host-wide hold while the rotator has headroom");
269
- assert.equal(second.where, "local", "hybrid falls back local while the pool is held");
265
+ const ones = runs.filter((x) => x.n === 1);
266
+ assert.equal(ones[0].where, "remote");
267
+ assert.equal(ones[1].at, t0, "the pool limit reruns at once (the router hands out another login, #5165)");
268
+ assert.equal(ones[1].poolHeld, null, "no pool hold while an account has headroom (#5203)");
269
+ assert.equal(ones[1].where, "remote");
270
+ assert.ok(runs.every((x) => !(x.hostHeld > x.at)), "no host-wide hold while the rotator has headroom");
271
+ // the second limit exhausted the one-account rotation budget: the pool's own hold applies again (#5095)
272
+ assert.deepEqual(pausedHolds, [null, t0 + 600_000], "only the second limit records the pool hold (#5095)");
273
+ });
274
+
275
+ it("#5203 a rate-limited pool lane with another usable account rotates and sets no batch-wide notBefore", async () => {
276
+ const { c, now, sleep } = fakeClock();
277
+ const t0 = c.t;
278
+ const limits = openLimits(storeFile(), { now });
279
+ const rotator = { accounts: [{ n: 1 }, { n: 3 }, { n: 5 }], headroom: () => true, resumeAt: () => now(), isAccount: (id) => /^acct-/.test(id), currentIdentity: () => "acct-1" };
280
+ const remote = { max: 2, fallbackLocal: false, isRemote: () => true, place: () => ({ where: "remote", reason: "heavy" }), identity: () => "remote:pool" };
281
+ let state = null;
282
+ const store = { load: () => state, save: (s) => { state = JSON.parse(JSON.stringify(s)); } };
283
+ const runs = [];
284
+ const rows = await orchestrate({ issues: [1, 2, 3], parallel: 1, now, sleep, readBody: async () => "", limits, rotator, remote, store, identity: () => laneBackendId({}, rotator),
285
+ runOne: async (n) => {
286
+ runs.push({ n, at: now(), poolHeld: limits.holdUntil("remote:pool"), notBefore: { ...(state?.notBefore ?? {}) }, quota: state?.quota?.[1] ?? null });
287
+ return n === 1 && runs.length === 1 ? limited("remote:pool", t0 + 2 * 3_600_000) : { terminalReason: "merged" };
288
+ } });
289
+ assert.deepEqual(rows.map((r) => r.outcome), ["merged", "merged", "merged"]);
290
+ assert.ok(runs.every((x) => x.at === t0), "every lane starts at once: nothing waits for the limited account's reset");
291
+ const rerun = runs.filter((x) => x.n === 1)[1];
292
+ assert.equal(rerun.quota.rotations, 1, "rotations >= 1");
293
+ assert.equal(rerun.poolHeld, null, "no pool hold");
294
+ assert.ok(runs.every((x) => Object.values(x.notBefore).every((v) => !(v > t0))), "no batch-wide notBefore");
295
+ });
296
+
297
+ it("#5203 when every account is limited, the pool hold and notBefore are the earliest reset across accounts", async () => {
298
+ const { c, now, sleep } = fakeClock();
299
+ const t0 = c.t;
300
+ let everyLimited = false; // the rotator has headroom until the lane reports the limit
301
+ const earliest = t0 + 1_800_000, laneReset = t0 + 3 * 3_600_000;
302
+ const limits = openLimits(storeFile(), { now });
303
+ const rotator = { accounts: [{ n: 1 }, { n: 2 }], headroom: () => !everyLimited || now() >= earliest, resumeAt: () => Math.max(earliest, now()), isAccount: (id) => /^acct-/.test(id), currentIdentity: () => "acct-1" };
304
+ const remote = { max: 2, fallbackLocal: false, isRemote: () => true, place: () => ({ where: "remote", reason: "heavy" }), identity: () => "remote:pool" };
305
+ let poolHold = null;
306
+ const runs = [], log = [];
307
+ const rows = await orchestrate({ issues: [1, 2], parallel: 1, now, sleep, readBody: async () => "", limits, rotator, remote, identity: () => laneBackendId({}, rotator),
308
+ onUpdate: (u) => { log.push(u); if (u.outcome === "paused") poolHold = limits.holdUntil("remote:pool"); },
309
+ runOne: async (n) => {
310
+ runs.push({ n, at: now() });
311
+ if (n === 1 && runs.length === 1) { everyLimited = true; return limited("remote:pool", laneReset); }
312
+ return { terminalReason: "merged" };
313
+ } });
314
+ assert.deepEqual(rows.map((r) => r.outcome), ["merged", "merged"]);
315
+ assert.equal(poolHold, earliest, "the pool hold (the notBefore of every remote-placed issue) is the earliest reset across accounts, not the lane's own");
316
+ assert.equal(runs.find((x) => x.n === 2).at, earliest, "the next lane starts at the earliest reset");
317
+ assert.match(log.find((u) => u.outcome === "paused").detail, new RegExp(`until ${new Date(earliest).toISOString()}`));
318
+ });
319
+
320
+ it("#5203 --requeue of a quota-paused pending issue clears its notBefore and the pool hold", async () => {
321
+ const { c, now, sleep } = fakeClock();
322
+ const t0 = c.t;
323
+ const limits = openLimits(storeFile(), { now });
324
+ const remote = { max: 2, fallbackLocal: false, isRemote: () => true, place: () => ({ where: "remote", reason: "heavy" }), identity: () => "remote:pool" };
325
+ let paused = false;
326
+ const runs = [], log = [];
327
+ const rows = await orchestrate({ issues: [1], parallel: 1, now, sleep, pollMs: 5000, readBody: async () => "", limits, remote, identity: () => "local", onUpdate: (u) => log.push(u),
328
+ getRequeued: () => (paused ? [{ issue: 1, reason: "accounts 1, 3, 5 usable", by: "operator", at: "2026-11-07T08:05:00Z" }] : []),
329
+ runOne: async (n) => {
330
+ runs.push({ n, at: now() });
331
+ if (runs.length === 1) { paused = true; return limited("remote:pool", t0 + 2 * 3_600_000); }
332
+ return { terminalReason: "merged" };
333
+ } });
334
+ assert.equal(rows[0].outcome, "merged");
335
+ assert.equal(runs.length, 2);
336
+ assert.ok(runs[1].at < t0 + 60_000, `the released issue runs on the next poll, not at the reset (ran at +${runs[1].at - t0}ms)`);
337
+ assert.equal(limits.holdUntil("remote:pool"), null, "the pool hold is cleared");
338
+ assert.ok(log.some((u) => u.issue === 1 && u.outcome === "requeued" && /quota pause released/.test(u.detail)));
339
+ });
340
+ });
341
+
342
+ describe("tokenbaton rotation holds (#5205)", () => {
343
+ const H = 3_600_000;
344
+ const memStore = () => { const states = []; return { states, load: () => null, save: (s) => states.push(JSON.parse(JSON.stringify(s))) }; };
345
+
346
+ it("#5205 all accounts limited: the hold lasts until tokenbaton's earliest reset, not the limited account's own; rotations summed", async () => {
347
+ const { c, now, sleep } = fakeClock();
348
+ const t0 = c.t, earliest = t0 + 2 * H, own = t0 + 5 * H;
349
+ const picks = [];
350
+ let exhausted = false; // every account limited once the first run returns
351
+ const rot = { accounts: [{ n: 1 }, { n: 2 }, { n: 3 }], headroom: () => !exhausted || now() >= earliest, resumeAt: () => t0 + 4 * H, isAccount: (id) => id === "acct-1", currentIdentity: () => "acct-1",
352
+ pick: ({ exclude }) => { picks.push([...exclude.keys()]); return now() >= earliest ? { account: 2, stale: false } : { account: null, earliestReset: earliest }; } };
353
+ const limits = openLimits(storeFile(), { now }), store = memStore(), updates = [], starts = [];
354
+ const rows = await orchestrate({ issues: [1], now, sleep, readBody: async () => "", rotator: rot, limits, identity: () => "acct-1", store, onUpdate: (u) => updates.push(u),
355
+ runOne: async () => { starts.push(now()); exhausted = true; return starts.length === 1 ? { ...limited("acct-1", own), rotations: 2, limited: [{ account: 1, until: own }, { account: 3, until: t0 + 3 * H }] } : { terminalReason: "merged" }; } });
356
+ assert.equal(rows[0].outcome, "merged");
357
+ assert.equal(limits.holdUntil("acct-1") ?? earliest, earliest, "the hold is tokenbaton's earliest reset");
358
+ const paused = updates.find((u) => u.outcome === "paused");
359
+ assert.equal(paused.quota.resumeAt, new Date(earliest).toISOString());
360
+ assert.equal(starts[1], earliest, "rerun at the earliest reset, not the account's own reset");
361
+ assert.deepEqual(picks[0]?.sort(), [1, 3], `lane-reported limited accounts are excluded: ${JSON.stringify({ picks, starts, u: updates.map((u) => u.outcome) })}`);
362
+ const st = store.states.find((s) => s.quota?.["1"]);
363
+ assert.equal(st.quota["1"].rotations, 2, "lane-reported rotations add to quota.rotations");
364
+ assert.deepEqual(Object.keys(st.limitedAccounts).sort(), ["1", "3"], "limited accounts persist with the batch");
365
+ });
366
+
367
+ it("#5236 a quota.exhausted lane result is held until the earliest reset and the batch log row names the code and reset", async () => {
368
+ const { c, now, sleep } = fakeClock();
369
+ const t0 = c.t, earliest = t0 + 3 * H;
370
+ const rot = { accounts: [{ n: 1 }, { n: 2 }], headroom: () => now() >= earliest, resumeAt: () => earliest, isAccount: (id) => id === "acct-1", currentIdentity: () => "acct-1",
371
+ pick: () => (now() >= earliest ? { account: 2, stale: false } : { account: null, earliestReset: earliest }) };
372
+ const limits = openLimits(storeFile(), { now }), updates = [], starts = [];
373
+ const rows = await orchestrate({ issues: [1], now, sleep, readBody: async () => "", rotator: rot, limits, identity: () => "acct-1", store: memStore(), onUpdate: (u) => updates.push(u),
374
+ runOne: async () => { starts.push(now()); return starts.length === 1 ? { terminalReason: "deferred", code: "quota.exhausted", backendId: "acct-1", resetAtEpochMs: earliest, detail: "quota exhausted: no account has capacity" } : { terminalReason: "merged" }; } });
375
+ assert.equal(rows[0].outcome, "merged");
376
+ assert.equal(starts[1], earliest, "rerun at the earliest reset");
377
+ const paused = updates.find((u) => u.outcome === "paused");
378
+ assert.equal(paused.quota.resumeAt, new Date(earliest).toISOString());
379
+ assert.match(paused.detail, /quota\.exhausted/);
380
+ const evt = updates.find((u) => u.event === "quota.exhausted");
381
+ assert.ok(evt, "batch-level quota.exhausted event");
382
+ assert.equal(evt.resumeAt, new Date(earliest).toISOString());
383
+ });
384
+
385
+ it("#5205 a lane-reported limited account is excluded from headroom until its reset", async () => {
386
+ const { c, now, sleep } = fakeClock();
387
+ const t0 = c.t, seen = [];
388
+ const rot = { accounts: [{ n: 1 }, { n: 2 }], headroom: (ex) => { seen.push(ex ? [...ex.keys()] : []); return !(ex?.has(1) && ex?.has(2)); }, resumeAt: () => t0 + H, isAccount: () => true, currentIdentity: () => "acct-1", pick: () => ({ account: null, earliestReset: t0 + H }) };
389
+ let runs = 0;
390
+ await orchestrate({ issues: [1, 2], parallel: 1, now, sleep, readBody: async () => "", rotator: rot, limits: openLimits(storeFile(), { now }), identity: () => "acct-1",
391
+ runOne: async (n) => { runs++; return n === 1 ? { terminalReason: "merged", rotations: 1, limited: [{ account: 1, until: t0 + H }] } : { terminalReason: "merged" }; } });
392
+ assert.equal(runs, 2);
393
+ assert.ok(seen.some((x) => x.includes(1)), `headroom saw the limited account excluded: ${JSON.stringify(seen)}`);
394
+ });
395
+
396
+ it("#5205 no rotator: the hold is now + the reset delay, as before", async () => {
397
+ const { c, now, sleep } = fakeClock();
398
+ const limits = openLimits(storeFile(), { now });
399
+ const until = c.t + H;
400
+ await orchestrate({ issues: [1], now, sleep, readBody: async () => "", limits, identity: () => "A",
401
+ runOne: async () => { if (now() < until) { const r = limited("A", until); setImmediate(() => {}); return r; } return { terminalReason: "merged" }; },
402
+ onUpdate: (u) => { if (u.outcome === "paused") assert.equal(limits.holdUntil("A"), until); } });
270
403
  });
271
404
  });
@@ -0,0 +1,143 @@
1
+ // SPDX-FileCopyrightText: Copyright (c) RapierCraft Studios
2
+ // SPDX-License-Identifier: AGPL-3.0-or-later
3
+ // #5241: --cancel --stop-remote confirms or reports each lane, releases its lease; dead-lease requeue; active-batch skip.
4
+ import { describe, it } from "node:test";
5
+ import assert from "node:assert/strict";
6
+ import { tmpDir } from "../tests/_tmp.mjs";
7
+ import { orchestrate } from "./orchestrate.mjs";
8
+ import { openBatch, activeBatchOf, cancelledOwners } from "./batch.mjs";
9
+ import { clearLease } from "./lanelock.mjs";
10
+ import { providerLanes } from "./execution.mjs";
11
+ import { orchestrateFromCli } from "../engine-cli.mjs";
12
+
13
+ process.env.FORGEDOCK_SCHED_DIR = tmpDir("cancel-sched-");
14
+ const until = async (cond, ms = 5000) => {
15
+ const end = Date.now() + ms;
16
+ while (!cond()) { if (Date.now() > end) throw new Error("timed out waiting"); await new Promise((r) => setTimeout(r, 5)); }
17
+ };
18
+ const clock = () => { const c = { t: 1_000_000 }; return { now: () => c.t, tick: (ms) => { c.t += ms; } }; };
19
+
20
+ describe("cancel --stop-remote teardown (#5241)", () => {
21
+ it("retries a stop that was not accepted, reports a lane still running after the timeout, then releases its lease when it ends", async () => {
22
+ let cancel = null, saved = null, release;
23
+ const store = { load: () => saved, save: (s) => { saved = s; }, log: () => {} };
24
+ const gate = new Promise((r) => { release = r; });
25
+ const { now, tick } = clock();
26
+ const tries = [], released = [], rows = [];
27
+ const done = orchestrate({ issues: [7], parallel: 1, pollMs: 5, now, stopRetryMs: 1000, stopTimeoutMs: 5000, readBody: async () => "", store, getCancelled: () => cancel, onUpdate: (r) => rows.push(r),
28
+ stopLane: (n, rec) => { tries.push(n); rec.stopNote = "the pool does not support stop (404/405)"; return false; },
29
+ releaseLease: async (n) => { released.push(n); },
30
+ runOne: async (n, _l, ctx) => { ctx.onStart?.({ remote: true, handle: { id: "sbx" } }); await gate; return { terminalReason: "deferred", code: "run.interrupted" }; } });
31
+ await until(() => saved?.running?.[7]);
32
+ cancel = { at: "2026-01-01T00:00:00Z", reason: "operator", stopRemote: true };
33
+ await until(() => tries.length >= 1);
34
+ tick(1500); await until(() => tries.length >= 2); // retried: the first stop was refused
35
+ tick(6000); await until(() => rows.some((r) => /#7 not confirmed stopped/.test(r.detail ?? "")));
36
+ assert.match(rows.find((r) => /not confirmed stopped/.test(r.detail ?? "")).detail, /404\/405/);
37
+ assert.deepEqual(released, [], "a lane still running keeps its lease");
38
+ release(); await done;
39
+ assert.deepEqual(released, [7], "the ended lane's lease is released");
40
+ });
41
+
42
+ it("stopLane records why a lane could not be stopped on its record", async () => {
43
+ const provider = { name: "p", idFor: (n) => `s${n}`, stop: async () => false, stopReason: () => "the pool does not support stop (404/405)" };
44
+ const lanes = providerLanes({ provider, laneArgs: () => [], ledgerFor: () => null });
45
+ const rec = { handle: { id: "s1" } };
46
+ assert.equal(await lanes.stopLane(1, rec), false);
47
+ assert.match(rec.stopNote, /404\/405/);
48
+ });
49
+ });
50
+
51
+ describe("clearLease (#5241)", () => {
52
+ const mk = (lease) => { const st = { lease, phase: "x" }; return { st, projector: { readState: async () => st, writeState: async (_n, s) => { Object.assign(st, s); } } }; };
53
+ it("clears a lease the guard accepts, keeps one it refuses, and no-ops without one", async () => {
54
+ const a = mk({ by: "core_a", until: 9e15 });
55
+ assert.equal(await clearLease({ projector: a.projector, issue: 1 }), true);
56
+ assert.equal(a.st.lease, null);
57
+ const b = mk({ by: "core_b", until: 9e15 });
58
+ assert.equal(await clearLease({ projector: b.projector, issue: 1, shouldClear: () => false }), false);
59
+ assert.equal(b.st.lease.by, "core_b");
60
+ assert.equal(await clearLease({ projector: mk(null).projector, issue: 1 }), false);
61
+ });
62
+ });
63
+
64
+ describe("dead-lease requeue and active-batch lookup (#5241)", () => {
65
+ const seed = (root, id, { repo = "o/r", order = [5], running = {}, pending = [], notBefore = {}, cancelled = false, report = false } = {}) => {
66
+ const b = openBatch(root, id);
67
+ b.writePlan({ v: 1, id, repo });
68
+ b.store.save({ order, running, pending, results: {}, notBefore });
69
+ if (cancelled) b.requestCancel({});
70
+ if (report) b.writeReport({ done: true });
71
+ return b;
72
+ };
73
+ it("requeue accepts a pending issue deferred on a lease whose holder is a cancelled batch, and still refuses an ordinary deferral", () => {
74
+ const root = tmpDir("cancel-requeue-");
75
+ seed(root, "bcancel", { cancelled: true, running: { 5: { remote: true } } });
76
+ const live = seed(root, "blive", { pending: [5, 6], order: [5, 6], notBefore: { 5: Date.now() + 1e6, 6: Date.now() + 1e6 } });
77
+ const r = live.requeue([5, 6]);
78
+ assert.deepEqual(r.requeued, [5]);
79
+ assert.deepEqual(r.refused, [{ issue: 6, reason: "not terminal" }]);
80
+ });
81
+ it("is refused while a live batch runs the issue, or when no cancelled batch owns it", () => {
82
+ const root = tmpDir("cancel-requeue2-");
83
+ seed(root, "bcancel", { cancelled: true });
84
+ seed(root, "bother", { running: { 5: {} } });
85
+ const live = seed(root, "blive", { pending: [5], notBefore: { 5: Date.now() + 1e6 } });
86
+ assert.deepEqual(cancelledOwners(root, "o/r", 5, "blive"), []);
87
+ assert.equal(live.requeue([5]).refused[0].reason, "not terminal");
88
+ });
89
+ it("activeBatchOf names an unfinished, uncancelled batch of the same repo; not a finished, cancelled, stale or other-repo one", () => {
90
+ const root = tmpDir("cancel-active-");
91
+ seed(root, "bpend", { pending: [5] });
92
+ assert.equal(activeBatchOf(root, "o/r", 5), "bpend");
93
+ assert.equal(activeBatchOf(root, "o/other", 5), null);
94
+ assert.equal(activeBatchOf(root, "o/r", 5, { now: Date.now() + 4 * 3600e3 }), null, "stale");
95
+ openBatch(root, "bpend").writeReport({ done: true });
96
+ assert.equal(activeBatchOf(root, "o/r", 5), null, "finished");
97
+ const root2 = tmpDir("cancel-active2-");
98
+ seed(root2, "bc", { pending: [5], cancelled: true });
99
+ assert.equal(activeBatchOf(root2, "o/r", 5), null, "cancelled");
100
+ });
101
+ });
102
+
103
+ describe("orchestrate start skips issues of another active batch (#5241)", () => {
104
+ const mkDeps = (root, batchId, ran) => ({ dir: tmpDir("active-run-"), batchRoot: root, batchId, allowTmpfs: true, emitTelemetry: async () => {}, io: { gh: async () => "o/r\n" }, sched: null,
105
+ leanIo: { gh: async (a) => (a.includes("milestone") ? JSON.stringify({ milestone: null }) : JSON.stringify({ body: "" })), git: async (a) => (a[0] === "ls-remote" ? "abc\trefs/heads/staging\n" : "") },
106
+ decide: () => {}, orchestration: { breaker: null, maxDeferrals: 3, globalParallel: null },
107
+ leanModule: { runCore: async (o) => { ran.push(o.issue); return { terminalReason: "merged", pr: 50 + o.issue }; } } });
108
+ const run = async (argv, deps) => { const lines = []; const l = console.log; console.log = (...a) => lines.push(a.join(" ")); try { return { res: await orchestrateFromCli(argv, deps), lines }; } finally { console.log = l; } };
109
+
110
+ it("skips the issue naming the batch, runs the rest, and --force starts it anyway", async () => {
111
+ const root = tmpDir("active-cli-");
112
+ const other = openBatch(root, "bother");
113
+ other.writePlan({ v: 1, id: "bother", repo: "o/r" });
114
+ other.store.save({ order: [1], running: { 1: { pid: 1 } }, pending: [], results: {} });
115
+ const ran = [];
116
+ const a = await run(["1", "2", "--parallel", "1", "--repo", "o/r", "--no-prefilter"], mkDeps(root, "bnew", ran));
117
+ assert.match(a.lines.join("\n"), /skipped: #1 is already in active batch bother/);
118
+ assert.deepEqual(ran, [2]);
119
+ assert.deepEqual(openBatch(root, "bnew").readPlan().argv, ["2", "--parallel", "1", "--repo", "o/r", "--no-prefilter"]);
120
+ const ran2 = [];
121
+ await run(["1", "--parallel", "1", "--repo", "o/r", "--no-prefilter", "--force", "sibling batch is dead"], mkDeps(root, "bforce", ran2));
122
+ assert.deepEqual(ran2, [1]);
123
+ await assert.rejects(run(["1", "--repo", "o/r", "--no-prefilter"], mkDeps(root, "bnone", [])), /every issue is already in an active batch \(bother\)/);
124
+ });
125
+ });
126
+
127
+ describe("--requeue of a lease-deferred pending issue (#5241)", () => {
128
+ it("releases the deferral wait so the next pass relaunches it at once", async () => {
129
+ let saved = null, entries = [];
130
+ const store = { load: () => saved, save: (s) => { saved = s; }, log: () => {} };
131
+ const runs = [];
132
+ const done = orchestrate({ issues: [9], parallel: 1, pollMs: 5, readBody: async () => "", store, getRequeued: () => entries,
133
+ runOne: async (n) => {
134
+ runs.push(n);
135
+ return runs.length === 1 ? { terminalReason: "deferred", code: "lease-held", detail: `issue 9 leased by core_x until ${new Date(Date.now() + 3600e3).toISOString()}` } : { terminalReason: "merged", pr: 1 };
136
+ } });
137
+ await until(() => Number.isFinite(saved?.notBefore?.[9]));
138
+ entries = [{ issue: 9, reason: "dead lease", by: "operator", at: "2026-01-01T00:00:00Z" }];
139
+ const rows = await done;
140
+ assert.deepEqual(runs, [9, 9]);
141
+ assert.equal(rows.find((r) => r.issue === 9).outcome, "merged");
142
+ });
143
+ });