@codyswann/lisa 4.106.1 → 4.106.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/dist/core/nightly-e2e-guard-behavior-certificate.js +2 -2
  2. package/dist/core/upstream-evidence-manifest.d.ts.map +1 -1
  3. package/dist/core/upstream-evidence-manifest.js +30 -11
  4. package/dist/core/upstream-evidence-manifest.js.map +1 -1
  5. package/package.json +4 -4
  6. package/plugins/lisa/.claude-plugin/plugin.json +1 -1
  7. package/plugins/lisa/.codex-plugin/plugin.json +1 -1
  8. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/SKILL.md +9 -3
  9. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/cli-cycle.mjs +5 -0
  10. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/cli-usage.mjs +18 -5
  11. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/cli-workers.mjs +67 -3
  12. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/first-prompt.mjs +221 -0
  13. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/ledger.mjs +25 -3
  14. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/session-manager.mjs +4 -1
  15. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/worker-launch.mjs +91 -0
  16. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/workers.mjs +10 -5
  17. package/plugins/lisa/skills/lisa-session-manager/SKILL.md +9 -3
  18. package/plugins/lisa/skills/lisa-session-manager/scripts/cli-cycle.mjs +5 -0
  19. package/plugins/lisa/skills/lisa-session-manager/scripts/cli-usage.mjs +18 -5
  20. package/plugins/lisa/skills/lisa-session-manager/scripts/cli-workers.mjs +67 -3
  21. package/plugins/lisa/skills/lisa-session-manager/scripts/first-prompt.mjs +221 -0
  22. package/plugins/lisa/skills/lisa-session-manager/scripts/ledger.mjs +25 -3
  23. package/plugins/lisa/skills/lisa-session-manager/scripts/session-manager.mjs +4 -1
  24. package/plugins/lisa/skills/lisa-session-manager/scripts/worker-launch.mjs +91 -0
  25. package/plugins/lisa/skills/lisa-session-manager/scripts/workers.mjs +10 -5
  26. package/plugins/lisa-agy/plugin.json +1 -1
  27. package/plugins/lisa-agy/skills/lisa-session-manager/SKILL.md +9 -3
  28. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/cli-cycle.mjs +5 -0
  29. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/cli-usage.mjs +18 -5
  30. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/cli-workers.mjs +67 -3
  31. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/first-prompt.mjs +221 -0
  32. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/ledger.mjs +25 -3
  33. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/session-manager.mjs +4 -1
  34. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/worker-launch.mjs +91 -0
  35. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/workers.mjs +10 -5
  36. package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
  37. package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
  38. package/plugins/lisa-cdk-agy/plugin.json +1 -1
  39. package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
  40. package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
  41. package/plugins/lisa-cdk-devin/.claude-plugin/plugin.json +1 -1
  42. package/plugins/lisa-cdk-devin/.devin-plugin/plugin.json +1 -1
  43. package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
  44. package/plugins/lisa-copilot/skills/lisa-session-manager/SKILL.md +9 -3
  45. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/cli-cycle.mjs +5 -0
  46. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/cli-usage.mjs +18 -5
  47. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/cli-workers.mjs +67 -3
  48. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/first-prompt.mjs +221 -0
  49. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/ledger.mjs +25 -3
  50. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/session-manager.mjs +4 -1
  51. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/worker-launch.mjs +91 -0
  52. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/workers.mjs +10 -5
  53. package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
  54. package/plugins/lisa-cursor/skills/lisa-session-manager/SKILL.md +9 -3
  55. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/cli-cycle.mjs +5 -0
  56. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/cli-usage.mjs +18 -5
  57. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/cli-workers.mjs +67 -3
  58. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/first-prompt.mjs +221 -0
  59. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/ledger.mjs +25 -3
  60. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/session-manager.mjs +4 -1
  61. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/worker-launch.mjs +91 -0
  62. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/workers.mjs +10 -5
  63. package/plugins/lisa-devin/.claude-plugin/plugin.json +1 -1
  64. package/plugins/lisa-devin/.devin-plugin/plugin.json +1 -1
  65. package/plugins/lisa-devin/skills/lisa-session-manager/SKILL.md +9 -3
  66. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/cli-cycle.mjs +5 -0
  67. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/cli-usage.mjs +18 -5
  68. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/cli-workers.mjs +67 -3
  69. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/first-prompt.mjs +221 -0
  70. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/ledger.mjs +25 -3
  71. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/session-manager.mjs +4 -1
  72. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/worker-launch.mjs +91 -0
  73. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/workers.mjs +10 -5
  74. package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
  75. package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
  76. package/plugins/lisa-expo-agy/plugin.json +1 -1
  77. package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
  78. package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
  79. package/plugins/lisa-expo-devin/.claude-plugin/plugin.json +1 -1
  80. package/plugins/lisa-expo-devin/.devin-plugin/plugin.json +1 -1
  81. package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
  82. package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
  83. package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
  84. package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
  85. package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
  86. package/plugins/lisa-harper-fabric-devin/.claude-plugin/plugin.json +1 -1
  87. package/plugins/lisa-harper-fabric-devin/.devin-plugin/plugin.json +1 -1
  88. package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
  89. package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
  90. package/plugins/lisa-nestjs-agy/plugin.json +1 -1
  91. package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
  92. package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
  93. package/plugins/lisa-nestjs-devin/.claude-plugin/plugin.json +1 -1
  94. package/plugins/lisa-nestjs-devin/.devin-plugin/plugin.json +1 -1
  95. package/plugins/lisa-openclaw/.claude-plugin/plugin.json +1 -1
  96. package/plugins/lisa-openclaw/.codex-plugin/plugin.json +1 -1
  97. package/plugins/lisa-openclaw-agy/plugin.json +1 -1
  98. package/plugins/lisa-openclaw-copilot/.claude-plugin/plugin.json +1 -1
  99. package/plugins/lisa-openclaw-cursor/.claude-plugin/plugin.json +1 -1
  100. package/plugins/lisa-openclaw-devin/.claude-plugin/plugin.json +1 -1
  101. package/plugins/lisa-openclaw-devin/.devin-plugin/plugin.json +1 -1
  102. package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
  103. package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
  104. package/plugins/lisa-phaser-agy/plugin.json +1 -1
  105. package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
  106. package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
  107. package/plugins/lisa-phaser-devin/.claude-plugin/plugin.json +1 -1
  108. package/plugins/lisa-phaser-devin/.devin-plugin/plugin.json +1 -1
  109. package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
  110. package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
  111. package/plugins/lisa-rails-agy/plugin.json +1 -1
  112. package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
  113. package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
  114. package/plugins/lisa-rails-devin/.claude-plugin/plugin.json +1 -1
  115. package/plugins/lisa-rails-devin/.devin-plugin/plugin.json +1 -1
  116. package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
  117. package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
  118. package/plugins/lisa-typescript-agy/plugin.json +1 -1
  119. package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
  120. package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
  121. package/plugins/lisa-typescript-devin/.claude-plugin/plugin.json +1 -1
  122. package/plugins/lisa-typescript-devin/.devin-plugin/plugin.json +1 -1
  123. package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
  124. package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
  125. package/plugins/lisa-wiki-agy/plugin.json +1 -1
  126. package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
  127. package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
  128. package/plugins/lisa-wiki-devin/.claude-plugin/plugin.json +1 -1
  129. package/plugins/lisa-wiki-devin/.devin-plugin/plugin.json +1 -1
  130. package/plugins/src/base/skills/lisa-session-manager/SKILL.md +9 -3
  131. package/plugins/src/base/skills/lisa-session-manager/scripts/cli-cycle.mjs +5 -0
  132. package/plugins/src/base/skills/lisa-session-manager/scripts/cli-usage.mjs +18 -5
  133. package/plugins/src/base/skills/lisa-session-manager/scripts/cli-workers.mjs +67 -3
  134. package/plugins/src/base/skills/lisa-session-manager/scripts/first-prompt.mjs +221 -0
  135. package/plugins/src/base/skills/lisa-session-manager/scripts/ledger.mjs +25 -3
  136. package/plugins/src/base/skills/lisa-session-manager/scripts/session-manager.mjs +4 -1
  137. package/plugins/src/base/skills/lisa-session-manager/scripts/worker-launch.mjs +91 -0
  138. package/plugins/src/base/skills/lisa-session-manager/scripts/workers.mjs +10 -5
  139. package/scripts/check-shell-guard-refusal-coverage.mjs +9 -3
  140. package/scripts/lib/shell-guard-shard-runner.mjs +40 -35
  141. package/scripts/lib/shell-guard-shards.mjs +176 -11
@@ -355,11 +355,14 @@ function toMillis(value) {
355
355
  *
356
356
  * Order matters: a recorded DONE/FAILED wins; a pending question means the
357
357
  * worker ended its turn on purpose, so it is `waiting` even when its row
358
- * reads `done`; only then is liveness judged. `stalled` and `dead` become
359
- * `exhausted` at `maxAttempts`, which is the manager's cue to move the item to
360
- * Blocked instead of retrying it again.
361
- * @param {{agent: ReturnType<typeof normalizeAgents>[number] | null, entry: {status?: string, startedAt?: string|number, lastProgressAt?: string|number|null, logsHash?: string|null, pendingQuestion?: {askedAt?: string|number, lastPingAt?: string|number|null} | null, attempts?: number}, logsHash?: string | null | undefined, now: Date | number, stall?: {noOutputMinutes: number, requestionHours: number, maxAttempts: number}}} input - Classification inputs
362
- * @returns {"finished"|"waiting"|"requestion"|"working"|"stalled"|"dead"|"exhausted"} Classification
358
+ * reads `done`; then a first prompt that never ran (`firstPromptFailure`,
359
+ * read from the worker's transcript) is a `setup-gap` — a machine problem no
360
+ * retry can fix, so it is never `working` until the stall timer and never
361
+ * `exhausted` (lisa#4503); only then is liveness judged. `stalled` and `dead`
362
+ * become `exhausted` at `maxAttempts`, which is the manager's cue to move the
363
+ * item to Blocked instead of retrying it again.
364
+ * @param {{agent: ReturnType<typeof normalizeAgents>[number] | null, entry: {status?: string, startedAt?: string|number, lastProgressAt?: string|number|null, logsHash?: string|null, pendingQuestion?: {askedAt?: string|number, lastPingAt?: string|number|null} | null, attempts?: number}, logsHash?: string | null | undefined, now: Date | number, stall?: {noOutputMinutes: number, requestionHours: number, maxAttempts: number}, firstPromptFailure?: {kind: string, detail: string} | null}} input - Classification inputs
365
+ * @returns {"finished"|"waiting"|"requestion"|"setup-gap"|"working"|"stalled"|"dead"|"exhausted"} Classification
363
366
  */
364
367
  export function classifyWorker({
365
368
  agent,
@@ -367,6 +370,7 @@ export function classifyWorker({
367
370
  logsHash,
368
371
  now,
369
372
  stall = DEFAULT_STALL,
373
+ firstPromptFailure = null,
370
374
  }) {
371
375
  if (FINISHED_STATUSES.has(entry.status ?? "")) return "finished";
372
376
  const nowMs = toMillis(now) ?? 0;
@@ -378,6 +382,7 @@ export function classifyWorker({
378
382
  ? "requestion"
379
383
  : "waiting";
380
384
  }
385
+ if (firstPromptFailure) return "setup-gap";
381
386
  const outcome = agent?.active
382
387
  ? judgeActive(entry, logsHash, nowMs, stall)
383
388
  : "dead";
@@ -39,9 +39,15 @@
39
39
  * collect one complete native Actions shard without judging
40
40
  * its individual trace. All canonical candidates are supplied.
41
41
  * --merge-traces DIRECTORY --trace-out FILE
42
- * validate all three current run/attempt shard receipts, then
43
- * apply the original judge once to their combined trace.
44
- * A new Actions attempt must reproduce all three shards.
42
+ * validate one complete receipt per shard from the current
43
+ * run, then apply the original judge once to their combined
44
+ * trace. DIRECTORY holds every attempt of the run; for each
45
+ * shard the newest attempt wins, so "Re-run failed jobs"
46
+ * recovers a failed shard (#4634). That newest bundle must be
47
+ * complete — an older attempt is never used in its place —
48
+ * and must match the current run, source, PR head, workflow,
49
+ * runtime and roster. Each refusal names the shard, the
50
+ * attempt and the re-run that recovers it.
45
51
  *
46
52
  * Exit codes (mirroring the sibling check-* scripts):
47
53
  * 0 — the judgment passed, or --collect-only retained a complete eligible
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
3
3
  import { execFileSync, spawnSync } from "node:child_process";
4
4
  import {
5
5
  closeSync,
6
+ existsSync,
6
7
  lstatSync,
7
8
  mkdirSync,
8
9
  openSync,
@@ -19,9 +20,10 @@ import {
19
20
  } from "./shell-guard-refusal-coverage.mjs";
20
21
  import {
21
22
  identityHash,
22
- mergeBundles,
23
+ mergeShardDirectories,
23
24
  nativeArgv,
24
25
  parseShard,
26
+ RERUN_ALL,
25
27
  sha256,
26
28
  validateBundle,
27
29
  validateSnapshot,
@@ -300,6 +302,28 @@ export function collectShard(root, discover, args) {
300
302
  );
301
303
  }
302
304
 
305
+ /**
306
+ * Read one downloaded shard directory. Null when it holds no manifest.json:
307
+ * a failed shard still uploads its logs and execution.json for diagnosis.
308
+ */
309
+ function readBundle(base) {
310
+ assert(realpathSync(base) === base, "symlinked shard bundle directory");
311
+ const manifestPath = path.join(base, "manifest.json");
312
+ if (!existsSync(manifestPath)) return null;
313
+ const manifestRaw = bytes(manifestPath).toString("utf8");
314
+ return {
315
+ manifest: JSON.parse(manifestRaw),
316
+ manifestRaw,
317
+ traceRaw: bytes(path.join(base, "trace.jsonl")).toString("utf8"),
318
+ reportRaw: bytes(path.join(base, "results.json")).toString("utf8"),
319
+ terminalRaw: bytes(path.join(base, "terminal.json")).toString("utf8"),
320
+ indexRaw: bytes(path.join(base, INDEX_FILE)).toString("utf8"),
321
+ executionRaw: bytes(path.join(base, EXECUTION_FILE)).toString("utf8"),
322
+ stdoutRaw: bytes(path.join(base, STDOUT_FILE)),
323
+ stderrRaw: bytes(path.join(base, STDERR_FILE)),
324
+ };
325
+ }
326
+
303
327
  export function mergeCollected(root, discover, args) {
304
328
  assert(
305
329
  !args.includes("--collect-only") &&
@@ -313,42 +337,21 @@ export function mergeCollected(root, discover, args) {
313
337
  lstatSync(directory).isDirectory(),
314
338
  "explicit owned download directory required"
315
339
  );
316
- const entries = readdirSync(directory, { withFileTypes: true });
317
- const current = snapshot(root, discover);
318
- const prefix = `lisa-shell-guard-${current.provider.runId}-${current.provider.attempt}-shard-`;
319
- assert(
320
- entries.length === 3 &&
321
- entries.every(
322
- entry =>
323
- entry.isDirectory() &&
324
- [1, 2, 3].some(index => entry.name === prefix + index)
325
- ),
326
- "missing/extra/nonowned/stale-attempt shard bundle"
327
- );
328
- const bundles = entries.map(entry => {
329
- const base = path.join(directory, entry.name);
330
- assert(realpathSync(base) === base);
331
- const manifestRaw = bytes(path.join(base, "manifest.json")).toString(
332
- "utf8"
333
- );
334
- const manifest = JSON.parse(manifestRaw);
340
+ // #4634: the download holds every attempt of this run. A failed-jobs re-run
341
+ // rebuilds only the failed shard, so the newest complete bundle per shard is
342
+ // merged; selection and every refusal live in mergeShardDirectories.
343
+ const entries = readdirSync(directory, { withFileTypes: true }).map(entry => {
335
344
  assert(
336
- entry.name === prefix + manifest.shard.index,
337
- "bundle name/shard mismatch"
345
+ entry.isDirectory(),
346
+ `download entry ${JSON.stringify(entry.name)} is not a shard bundle directory. ${RERUN_ALL}`
338
347
  );
339
348
  return {
340
- manifest,
341
- manifestRaw,
342
- traceRaw: bytes(path.join(base, "trace.jsonl")).toString("utf8"),
343
- reportRaw: bytes(path.join(base, "results.json")).toString("utf8"),
344
- terminalRaw: bytes(path.join(base, "terminal.json")).toString("utf8"),
345
- indexRaw: bytes(path.join(base, INDEX_FILE)).toString("utf8"),
346
- executionRaw: bytes(path.join(base, EXECUTION_FILE)).toString("utf8"),
347
- stdoutRaw: bytes(path.join(base, STDOUT_FILE)),
348
- stderrRaw: bytes(path.join(base, STDERR_FILE)),
349
+ name: entry.name,
350
+ read: () => readBundle(path.join(directory, entry.name)),
349
351
  };
350
352
  });
351
- const trace = mergeBundles(current, bundles);
353
+ const current = snapshot(root, discover);
354
+ const { trace, selected } = mergeShardDirectories(current, entries);
352
355
  const target = path.resolve(root, value(args, "--trace-out"));
353
356
  mkdirSync(path.dirname(target), { recursive: true });
354
357
  // Aggregate provenance says complete shards were validated, not that guards passed.
@@ -357,14 +360,16 @@ export function mergeCollected(root, discover, args) {
357
360
  snapshot: current,
358
361
  traceSha256: sha256(trace),
359
362
  reportedFileCount: current.candidates.length,
360
- shards: bundles.map(bundle => ({
361
- index: bundle.manifest.shard.index,
363
+ shards: selected.map(({ index, attempt, name, bundle }) => ({
364
+ index,
365
+ attempt,
366
+ artifact: name,
362
367
  manifestSha256: sha256(bundle.manifestRaw),
363
368
  producerJob: bundle.manifest.producerJob,
364
369
  child: bundle.manifest.child,
365
370
  })),
366
371
  scope:
367
- "complete current native shards validated; ONE existing guard judgment remains required",
372
+ "newest complete native shard per index of the current run validated; ONE existing guard judgment remains required",
368
373
  providerArtifactDigestVerified: false,
369
374
  };
370
375
  writeFileSync(target, trace);
@@ -229,6 +229,56 @@ export function nativeArgv(root, artifactRoot, shard, candidates) {
229
229
  ];
230
230
  }
231
231
 
232
+ /*
233
+ * #4634: a failed-jobs re-run is a new Actions attempt that rebuilds only the
234
+ * failed shard, so a valid merge may combine bundles from different attempts
235
+ * of ONE run. Every refusal names the shard and attempt it judged and the
236
+ * re-run that recovers it, because the operator reading a red check has no
237
+ * other way to know a failed-jobs re-run would be wasted.
238
+ */
239
+ /** Every recovery hint starts with this; recoverable() keys on it alone. */
240
+ export const RECOVERY_PREFIX = "Recovery: ";
241
+ export const RERUN_FAILED = `${RECOVERY_PREFIX}"Re-run failed jobs" rebuilds only that shard and is enough.`;
242
+ export const RERUN_ALL = `${RECOVERY_PREFIX}"Re-run all jobs" is needed; re-running only failed jobs cannot fix this.`;
243
+ const BUNDLE_NAME = /^lisa-shell-guard-(\d+)-([1-9]\d*)-shard-([1-9]\d*)$/u;
244
+ const RUN_IDENTITY = ["runId", "sourceSha", "prHead", "workflowRef"];
245
+ const shardName = index => `shard ${index}`;
246
+ const label = (index, attempt) => `${shardName(index)} attempt ${attempt}`;
247
+ const bundleLabel = manifest =>
248
+ label(manifest?.shard?.index, manifest?.snapshot?.provider?.attempt);
249
+ /** The snapshot minus the one field a failed-jobs re-run legitimately changes. */
250
+ const withoutAttempt = value => {
251
+ const { attempt: _attempt, ...provider } = value?.provider ?? {};
252
+ return { ...value, provider };
253
+ };
254
+
255
+ /**
256
+ * Bind a bundle to the current run. The attempt may be older than the merge
257
+ * job's own (a re-run keeps earlier attempts' complete shards), never newer;
258
+ * everything else — run, source, PR head, workflow, runtime, roster — must be
259
+ * identical, because a re-run of the same run cannot change any of it.
260
+ */
261
+ function assertCurrentRun(snapshot, manifest) {
262
+ const where = bundleLabel(manifest);
263
+ const recorded = manifest?.snapshot?.provider ?? {};
264
+ for (const field of RUN_IDENTITY) {
265
+ assert(
266
+ recorded[field] === snapshot.provider[field],
267
+ `${where} records ${field} ${JSON.stringify(recorded[field])}, but the current run has ${JSON.stringify(snapshot.provider[field])}; a foreign or stale bundle cannot be merged. ${RERUN_ALL}`
268
+ );
269
+ }
270
+ assert(
271
+ /^\d+$/u.test(recorded.attempt ?? "") &&
272
+ Number(recorded.attempt) <= Number(snapshot.provider.attempt),
273
+ `${where} is not an attempt at or before the merge job's attempt ${snapshot.provider.attempt}. ${RERUN_ALL}`
274
+ );
275
+ assert.deepEqual(
276
+ withoutAttempt(manifest.snapshot),
277
+ withoutAttempt(snapshot),
278
+ `${where} was built from a different source/runtime/roster identity than the current merge job. ${RERUN_ALL}`
279
+ );
280
+ }
281
+
232
282
  export function validateBundle({
233
283
  snapshot,
234
284
  manifest,
@@ -241,18 +291,17 @@ export function validateBundle({
241
291
  stderrRaw,
242
292
  }) {
243
293
  validateSnapshot(snapshot);
244
- assert(manifest && manifest.schema === 1 && manifest.complete === true);
245
- assert.deepEqual(
246
- manifest.snapshot,
247
- snapshot,
248
- "stale source/run/attempt/runtime/roster identity"
294
+ assert(
295
+ manifest && manifest.schema === 1 && manifest.complete === true,
296
+ `${bundleLabel(manifest)} has an incomplete manifest. ${RERUN_FAILED}`
249
297
  );
298
+ assertCurrentRun(snapshot, manifest);
250
299
  parseShard(`${manifest.shard.index}/${manifest.shard.total}`);
251
300
  assert(
252
301
  manifest.child.status === 0 &&
253
302
  manifest.child.signal === null &&
254
303
  manifest.child.error === null,
255
- "child failed/killed/not started"
304
+ `${bundleLabel(manifest)} child failed/killed/not started. ${RERUN_FAILED}`
256
305
  );
257
306
  assert(manifest.producerJob === "guard-trace", "wrong native producer job");
258
307
  assert(
@@ -353,17 +402,26 @@ export function mergeBundles(snapshot, bundles) {
353
402
  validateSnapshot(snapshot);
354
403
  assert(
355
404
  Array.isArray(bundles) && bundles.length === 3,
356
- "all three complete shards required"
405
+ `all three complete shards required. ${RERUN_FAILED}`
357
406
  );
358
407
  const seen = new Set(),
359
408
  files = new Set(),
360
409
  validated = [];
361
410
  for (const bundle of bundles) {
362
- const value = validateBundle({ ...bundle, snapshot });
363
- assert(!seen.has(value.shard.index), "duplicate shard identity");
411
+ const value = recoverable(bundleLabel(bundle.manifest), () =>
412
+ validateBundle({ ...bundle, snapshot })
413
+ );
414
+ const where = bundleLabel(bundle.manifest);
415
+ assert(
416
+ !seen.has(value.shard.index),
417
+ `duplicate shard identity: ${where}. ${RERUN_ALL}`
418
+ );
364
419
  seen.add(value.shard.index);
365
420
  for (const file of value.files) {
366
- assert(!files.has(file), "duplicate reported file across shards");
421
+ assert(
422
+ !files.has(file),
423
+ `duplicate reported file across shards: ${where} reports ${file} again. ${RERUN_ALL}`
424
+ );
367
425
  files.add(file);
368
426
  }
369
427
  validated.push(value);
@@ -372,7 +430,7 @@ export function mergeBundles(snapshot, bundles) {
372
430
  assert.deepEqual(
373
431
  [...files].sort((a, b) => a.localeCompare(b)),
374
432
  snapshot.candidates.map(row => row.path),
375
- "missing/extra candidate files"
433
+ `missing/extra candidate files across ${bundles.map(bundle => bundleLabel(bundle.manifest)).join(", ")}. ${RERUN_ALL}`
376
434
  );
377
435
  // ONE existing judge receives this trace. Empty individual traces are valid.
378
436
  return `${validated
@@ -381,3 +439,110 @@ export function mergeBundles(snapshot, bundles) {
381
439
  .filter(Boolean)
382
440
  .join("\n")}\n`;
383
441
  }
442
+
443
+ /**
444
+ * A receipt-integrity refusal deep inside validateBundle (digest, argv,
445
+ * roster) or a missing artifact file does not know which bundle it judged.
446
+ * Name it, and say a full re-run is needed: the bundle's own job succeeded,
447
+ * so a failed-jobs re-run would not rebuild it.
448
+ */
449
+ function recoverable(where, validate) {
450
+ try {
451
+ return validate();
452
+ } catch (error) {
453
+ if (String(error?.message ?? "").includes(RECOVERY_PREFIX)) throw error;
454
+ throw new Error(
455
+ `${where}: ${error?.message ?? String(error)}. ${RERUN_ALL}`,
456
+ { cause: error }
457
+ );
458
+ }
459
+ }
460
+
461
+ /** Parse `lisa-shell-guard-<run>-<attempt>-shard-<index>` against this run. */
462
+ function parseBundleName(name, provider) {
463
+ const match = BUNDLE_NAME.exec(name);
464
+ assert(
465
+ match,
466
+ `download directory ${JSON.stringify(name)} is not a shell-guard shard bundle of run ${provider.runId}. ${RERUN_ALL}`
467
+ );
468
+ const [, runId, attemptText, indexText] = match;
469
+ const attempt = Number(attemptText),
470
+ index = Number(indexText);
471
+ const where = label(index, attempt);
472
+ assert(
473
+ runId === provider.runId,
474
+ `${where} (${name}) belongs to run ${runId}, not the current run ${provider.runId}. ${RERUN_ALL}`
475
+ );
476
+ assert(
477
+ index <= 3,
478
+ `${where} (${name}) is not one of shards 1-3. ${RERUN_ALL}`
479
+ );
480
+ assert(
481
+ attempt <= Number(provider.attempt),
482
+ `${where} (${name}) is newer than the merge job's attempt ${provider.attempt}. ${RERUN_ALL}`
483
+ );
484
+ return { index, attempt, where };
485
+ }
486
+
487
+ /** Keep the highest attempt per shard; two directories may not claim one slot. */
488
+ function newestPerShard(entries, provider) {
489
+ const newest = new Map(),
490
+ seen = new Set();
491
+ for (const entry of entries) {
492
+ const { index, attempt, where } = parseBundleName(entry.name, provider);
493
+ assert(
494
+ !seen.has(where),
495
+ `duplicate download directories claim ${where}. ${RERUN_ALL}`
496
+ );
497
+ seen.add(where);
498
+ if ((newest.get(index)?.attempt ?? 0) < attempt)
499
+ newest.set(index, { index, attempt, where, entry });
500
+ }
501
+ const missing = [1, 2, 3].filter(index => !newest.has(index));
502
+ assert(
503
+ missing.length === 0,
504
+ `missing shard bundle for ${missing.map(shardName).join(", ")}: no attempt 1-${provider.attempt} of run ${provider.runId} uploaded one. ${RECOVERY_PREFIX}"Re-run failed jobs" if that shard's job failed or was cancelled; "Re-run all jobs" if it succeeded without uploading.`
505
+ );
506
+ return [...newest.values()].sort((a, b) => a.index - b.index);
507
+ }
508
+
509
+ /**
510
+ * Merge downloaded shard directories from EVERY attempt of the current run
511
+ * (#4634). For each shard the newest attempt wins; its bundle must be
512
+ * complete, because falling back to an older attempt would silently judge a
513
+ * shard the operator just re-ran. `entries` are `{ name, read }`, where
514
+ * `read()` returns the bundle or null when the directory holds no
515
+ * manifest.json (a failed shard uploads diagnostics only). Only the chosen
516
+ * directories are read.
517
+ */
518
+ export function mergeShardDirectories(snapshot, entries) {
519
+ validateSnapshot(snapshot);
520
+ assert(Array.isArray(entries), "shard directory entries required");
521
+ const selected = newestPerShard(entries, snapshot.provider).map(choice => {
522
+ const bundle = recoverable(choice.where, () => choice.entry.read());
523
+ assert(
524
+ bundle,
525
+ `${choice.where} did not complete: its bundle has no manifest.json, and an older attempt is never used in its place. ${RERUN_FAILED}`
526
+ );
527
+ const recordedShard = bundle.manifest?.shard?.index,
528
+ recordedAttempt = bundle.manifest?.snapshot?.provider?.attempt;
529
+ assert(
530
+ recordedShard === choice.index &&
531
+ recordedAttempt === String(choice.attempt),
532
+ `${choice.where} (${choice.entry.name}) records shard ${recordedShard}, records attempt ${recordedAttempt} in its manifest. ${RERUN_ALL}`
533
+ );
534
+ return {
535
+ index: choice.index,
536
+ attempt: choice.attempt,
537
+ name: choice.entry.name,
538
+ bundle,
539
+ };
540
+ });
541
+ return {
542
+ trace: mergeBundles(
543
+ snapshot,
544
+ selected.map(choice => choice.bundle)
545
+ ),
546
+ selected,
547
+ };
548
+ }