@codyswann/lisa 4.101.10 → 4.102.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 (99) hide show
  1. package/all/copy-overwrite/scripts/check-third-party-review-evidence.mjs +124 -28
  2. package/all/copy-overwrite/scripts/lib/completion-proof-github.mjs +5 -2
  3. package/all/copy-overwrite/scripts/lib/evidence-prune-apply.mjs +227 -0
  4. package/all/copy-overwrite/scripts/lib/evidence-prune-config.mjs +165 -0
  5. package/all/copy-overwrite/scripts/lib/evidence-prune-gh.mjs +256 -0
  6. package/all/copy-overwrite/scripts/lib/evidence-prune-records.mjs +101 -0
  7. package/all/copy-overwrite/scripts/lib/evidence-prune.mjs +243 -0
  8. package/all/copy-overwrite/scripts/lisa-evidence-prune.mjs +226 -0
  9. package/all/copy-overwrite/scripts/npm-updater-helper-graph.json +1 -1
  10. package/dist/cli/doctor-third-party-evidence-seed.d.ts +9 -2
  11. package/dist/cli/doctor-third-party-evidence-seed.d.ts.map +1 -1
  12. package/dist/cli/doctor-third-party-evidence-seed.js +64 -4
  13. package/dist/cli/doctor-third-party-evidence-seed.js.map +1 -1
  14. package/dist/core/lisa-owned-hash-ledger.d.ts.map +1 -1
  15. package/dist/core/lisa-owned-hash-ledger.js +76 -0
  16. package/dist/core/lisa-owned-hash-ledger.js.map +1 -1
  17. package/dist/core/nightly-e2e-guard-behavior-certificate.js +2 -2
  18. package/dist/core/nightly-e2e-guard-behavior-certificate.js.map +1 -1
  19. package/dist/core/third-party-evidence-seed.d.ts +9 -1
  20. package/dist/core/third-party-evidence-seed.d.ts.map +1 -1
  21. package/dist/core/third-party-evidence-seed.js +41 -9
  22. package/dist/core/third-party-evidence-seed.js.map +1 -1
  23. package/dist/core/upstream-evidence-manifest.d.ts.map +1 -1
  24. package/dist/core/upstream-evidence-manifest.js +34 -6
  25. package/dist/core/upstream-evidence-manifest.js.map +1 -1
  26. package/package.json +4 -4
  27. package/plugins/lisa/.claude-plugin/plugin.json +1 -1
  28. package/plugins/lisa/.codex-plugin/plugin.json +1 -1
  29. package/plugins/lisa-agy/plugin.json +1 -1
  30. package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
  31. package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
  32. package/plugins/lisa-cdk-agy/plugin.json +1 -1
  33. package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
  34. package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
  35. package/plugins/lisa-cdk-devin/.claude-plugin/plugin.json +1 -1
  36. package/plugins/lisa-cdk-devin/.devin-plugin/plugin.json +1 -1
  37. package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
  38. package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
  39. package/plugins/lisa-devin/.claude-plugin/plugin.json +1 -1
  40. package/plugins/lisa-devin/.devin-plugin/plugin.json +1 -1
  41. package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
  42. package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
  43. package/plugins/lisa-expo-agy/plugin.json +1 -1
  44. package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
  45. package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
  46. package/plugins/lisa-expo-devin/.claude-plugin/plugin.json +1 -1
  47. package/plugins/lisa-expo-devin/.devin-plugin/plugin.json +1 -1
  48. package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
  49. package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
  50. package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
  51. package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
  52. package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
  53. package/plugins/lisa-harper-fabric-devin/.claude-plugin/plugin.json +1 -1
  54. package/plugins/lisa-harper-fabric-devin/.devin-plugin/plugin.json +1 -1
  55. package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
  56. package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
  57. package/plugins/lisa-nestjs-agy/plugin.json +1 -1
  58. package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
  59. package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
  60. package/plugins/lisa-nestjs-devin/.claude-plugin/plugin.json +1 -1
  61. package/plugins/lisa-nestjs-devin/.devin-plugin/plugin.json +1 -1
  62. package/plugins/lisa-openclaw/.claude-plugin/plugin.json +1 -1
  63. package/plugins/lisa-openclaw/.codex-plugin/plugin.json +1 -1
  64. package/plugins/lisa-openclaw-agy/plugin.json +1 -1
  65. package/plugins/lisa-openclaw-copilot/.claude-plugin/plugin.json +1 -1
  66. package/plugins/lisa-openclaw-cursor/.claude-plugin/plugin.json +1 -1
  67. package/plugins/lisa-openclaw-devin/.claude-plugin/plugin.json +1 -1
  68. package/plugins/lisa-openclaw-devin/.devin-plugin/plugin.json +1 -1
  69. package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
  70. package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
  71. package/plugins/lisa-phaser-agy/plugin.json +1 -1
  72. package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
  73. package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
  74. package/plugins/lisa-phaser-devin/.claude-plugin/plugin.json +1 -1
  75. package/plugins/lisa-phaser-devin/.devin-plugin/plugin.json +1 -1
  76. package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
  77. package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
  78. package/plugins/lisa-rails-agy/plugin.json +1 -1
  79. package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
  80. package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
  81. package/plugins/lisa-rails-devin/.claude-plugin/plugin.json +1 -1
  82. package/plugins/lisa-rails-devin/.devin-plugin/plugin.json +1 -1
  83. package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
  84. package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
  85. package/plugins/lisa-typescript-agy/plugin.json +1 -1
  86. package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
  87. package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
  88. package/plugins/lisa-typescript-devin/.claude-plugin/plugin.json +1 -1
  89. package/plugins/lisa-typescript-devin/.devin-plugin/plugin.json +1 -1
  90. package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
  91. package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
  92. package/plugins/lisa-wiki-agy/plugin.json +1 -1
  93. package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
  94. package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
  95. package/plugins/lisa-wiki-devin/.claude-plugin/plugin.json +1 -1
  96. package/plugins/lisa-wiki-devin/.devin-plugin/plugin.json +1 -1
  97. package/scripts/two-channel-couplings.json +1 -1
  98. package/typescript/copy-overwrite/.prettierignore +6 -0
  99. package/typescript/create-only/.github/workflows/third-party-review-evidence.yml +34 -12
@@ -165,20 +165,33 @@
165
165
  * runs from the default branch, so the job's own check run lands on the
166
166
  * default branch's latest commit — never on the pull request head it judged.
167
167
  * `--publish` reads the verdict a `--json` run saved, and the exit code it
168
- * ended with, and creates ONE check run on that head: `success` for
168
+ * ended with, and creates a check run on that head: `success` for
169
169
  * `satisfied` and `substituted`, `failure` for `unsatisfied` and for
170
170
  * `pull-requests-unreadable`. A run with no head to show it on, or that cannot
171
171
  * change the answer, publishes nothing and says why. A missing or unparseable
172
172
  * verdict, an exit code other than the verdict's own, or a refused write fails
173
173
  * the mode, so broken machinery still shows on the job.
174
174
  *
175
+ * ## The verdict is published under every declared reviewer's name (#4738)
176
+ *
177
+ * The check run is named `🕵️ Third-party review evidence (<context>)`. The
178
+ * verdict covers every declared reviewer, so one is created for `--context`
179
+ * AND for each reviewer the checkout's config or the saved readings declare,
180
+ * each name once. Otherwise a red `(A)` stays red after B's later review makes
181
+ * the head acceptable, because B's run would update only `(B)`. An empty
182
+ * `--context` — a manual run that names no reviewer — adds the bare name; a
183
+ * manual run that names one overwrites that reviewer's stale check run, which
184
+ * is how a red check is cleared after a writer's substitute, since a comment
185
+ * wakes no run. Each refused write is reported and fails the mode; the other
186
+ * names are still written.
187
+ *
175
188
  * CLI:
176
189
  * node scripts/check-third-party-review-evidence.mjs --sha <sha> [--json]
177
190
  * [--context <context>] [--root <dir>] [--repo <owner/repo>]
178
191
  * [--arm [--vacuity-report <path>]]
179
192
  * node scripts/check-third-party-review-evidence.mjs --sha <sha>
180
193
  * --publish <saved-verdict.json> --prover-exit <code>
181
- * [--context <context>] [--repo <owner/repo>]
194
+ * [--context <context>] [--root <dir>] [--repo <owner/repo>]
182
195
  *
183
196
  * `--arm` answers the merge driver's arm gate instead (#4653): whether
184
197
  * auto-merge may be turned on for this head, and if not, what to do next.
@@ -2170,6 +2183,60 @@ function writeArmDecision(verdict, { opts, io, out, repo, pulls, canWrite }) {
2170
2183
  /** The check run a verdict is published as, before a waking reviewer's name. */
2171
2184
  export const PUBLISHED_CHECK_NAME = "🕵️ Third-party review evidence";
2172
2185
 
2186
+ /**
2187
+ * The check run name a verdict is published under for one reviewer context.
2188
+ * @param {string} context - A reviewer context; empty for the bare name.
2189
+ * @returns {string} The check run name.
2190
+ */
2191
+ const publishedCheckName = context =>
2192
+ context ? `${PUBLISHED_CHECK_NAME} (${context})` : PUBLISHED_CHECK_NAME;
2193
+
2194
+ /**
2195
+ * Every reviewer context one publishing run publishes its verdict under
2196
+ * (#4738), the supplied one first and each once.
2197
+ *
2198
+ * The verdict covers every declared reviewer, so it is published under each
2199
+ * one's name: otherwise a red `(A)` stays red after B's later review makes the
2200
+ * head acceptable, because B's run updated only `(B)`. The declared reviewers
2201
+ * are the config's, joined by every context the saved readings name, so a
2202
+ * verdict saved with no readings — an unreadable pull-request list — still
2203
+ * reaches them. The supplied context is kept even when nothing declares it,
2204
+ * and an empty one (the manual path) is the bare name.
2205
+ * @param {object} options - The run.
2206
+ * @param {string} options.context - The context that woke the run; empty on the manual path.
2207
+ * @param {object} options.gates - The `gates` block of `.lisa.config.json`.
2208
+ * @param {ReadonlyArray<{context?: unknown}>} options.readings - The saved verdict's readings.
2209
+ * @returns {string[]} The contexts, deduplicated; "" stands for the bare name.
2210
+ */
2211
+ function publishedContexts({ context, gates, readings }) {
2212
+ const declared = [
2213
+ ...configuredReviewers({ gates }).map(reviewer => reviewer.context),
2214
+ ...readings
2215
+ .map(reading => reading?.context)
2216
+ .filter(named => typeof named === "string" && named !== ""),
2217
+ ];
2218
+ return [...new Set([context, ...declared])];
2219
+ }
2220
+
2221
+ /**
2222
+ * The `gates` block the publish step's checkout declares.
2223
+ *
2224
+ * It is only consulted to widen which names a verdict is published under, so
2225
+ * a config that cannot be read narrows the publication to the supplied name
2226
+ * and the saved readings' — today's behaviour — rather than failing a run
2227
+ * that already reached a verdict.
2228
+ * @param {string} root - The checkout's root.
2229
+ * @returns {object} The gates block, or none.
2230
+ */
2231
+ function publishingGates(root) {
2232
+ // probe-direction: neutral — no gate reads this; it only chooses which names an already-reached verdict is shown under.
2233
+ try {
2234
+ return readGates(root).gates;
2235
+ } catch {
2236
+ return {};
2237
+ }
2238
+ }
2239
+
2173
2240
  /**
2174
2241
  * The conclusion each published outcome carries on the head it judged.
2175
2242
  *
@@ -2263,7 +2330,7 @@ function savedVerdict(text, sha) {
2263
2330
  * @param {string} options.proverExit - The exit code the prover ended with.
2264
2331
  * @param {string} options.sha - The head the verdict judged.
2265
2332
  * @param {string} options.context - The reviewer that woke the run; empty on the manual path.
2266
- * @returns {{error: string}|{report: string, why: string}|{report: string, body: object}} The publication.
2333
+ * @returns {{error: string}|{report: string, why: string}|{report: string, body: object, readings: ReadonlyArray<ReportedReading>}} The publication: `body` is named for `context`.
2267
2334
  */
2268
2335
  export function verdictPublication({ text, proverExit, sha, context }) {
2269
2336
  if (proverExit !== "0" && proverExit !== "1") {
@@ -2298,10 +2365,9 @@ export function verdictPublication({ text, proverExit, sha, context }) {
2298
2365
  }
2299
2366
  return {
2300
2367
  report,
2368
+ readings: verdict.readings,
2301
2369
  body: {
2302
- name: context
2303
- ? `${PUBLISHED_CHECK_NAME} (${context})`
2304
- : PUBLISHED_CHECK_NAME,
2370
+ name: publishedCheckName(context),
2305
2371
  head_sha: sha,
2306
2372
  status: "completed",
2307
2373
  conclusion,
@@ -2335,11 +2401,14 @@ function refusalOf(error) {
2335
2401
  /**
2336
2402
  * Publish a saved verdict on the head it judged, and return the exit code.
2337
2403
  *
2338
- * 0 once the check run is created, or when nothing is to be published; 1
2339
- * when the verdict could not be read or trusted, or GitHub refused the write.
2340
- * The report goes to stdout and to the step summary either way.
2404
+ * The verdict is published under every name `publishedContexts` returns
2405
+ * (#4738). 0 once every check run is created, or when nothing is to be
2406
+ * published; 1 when the verdict could not be read or trusted, or GitHub
2407
+ * refused any one write — each refusal is reported, and the other names are
2408
+ * still attempted. The report goes to stdout and to the step summary either way.
2341
2409
  * @param {ReturnType<typeof parseArgs> & {sha: string}} opts - Parsed options.
2342
2410
  * @param {object} io - Injected side effects.
2411
+ * @param {object} [io.gates] - The gates block naming the declared reviewers, read from `--root` when absent.
2343
2412
  * @param {(path: string) => string} [io.readFile] - Reader for the saved verdict.
2344
2413
  * @param {(args: string[], input: string) => string} [io.gh] - Writer: `gh` with a request body on stdin.
2345
2414
  * @param {(text: string) => void} [io.appendSummary] - Appends to the job's step summary.
@@ -2381,7 +2450,31 @@ function publishVerdict(opts, io, out, err) {
2381
2450
  summarise(`Nothing published: ${plan.why}.\n`);
2382
2451
  return 0;
2383
2452
  }
2384
- const { name, conclusion } = plan.body;
2453
+ const contexts = publishedContexts({
2454
+ context: opts.context ?? "",
2455
+ gates: io.gates ?? publishingGates(opts.root),
2456
+ readings: plan.readings,
2457
+ });
2458
+ const write = checkRunWriter(opts, io, { out, err, summarise });
2459
+ // Every name is attempted, so one refusal leaves no other name stale; any
2460
+ // refusal still fails the job.
2461
+ const published = contexts.map(context =>
2462
+ write({ ...plan.body, name: publishedCheckName(context) })
2463
+ );
2464
+ return published.every(Boolean) ? 0 : 1;
2465
+ }
2466
+
2467
+ /**
2468
+ * A writer that creates one check run on the judged head and reports it.
2469
+ * @param {ReturnType<typeof parseArgs> & {sha: string}} opts - Parsed options.
2470
+ * @param {{gh?: (args: string[], input: string) => string}} io - Injected side effects.
2471
+ * @param {object} sinks - Where the outcome is reported.
2472
+ * @param {{write: (text: string) => void}} sinks.out - Stdout.
2473
+ * @param {{write: (text: string) => void}} sinks.err - Stderr.
2474
+ * @param {(text: string) => void} sinks.summarise - Step-summary writer.
2475
+ * @returns {(body: {name: string, conclusion: string}) => boolean} Creates one check run; true once GitHub accepted it.
2476
+ */
2477
+ function checkRunWriter(opts, io, { out, err, summarise }) {
2385
2478
  const repo = opts.repo ?? process.env.GITHUB_REPOSITORY ?? "";
2386
2479
  const gh =
2387
2480
  io.gh ??
@@ -2391,31 +2484,34 @@ function publishVerdict(opts, io, out, err) {
2391
2484
  input,
2392
2485
  stdio: ["pipe", "pipe", "pipe"],
2393
2486
  }));
2394
- // probe-direction: fail-closed — a write that was refused, or killed at its deadline, published nothing, so the job fails.
2395
- try {
2396
- if (!/^[^/\s]+\/[^/\s]+$/u.test(repo)) {
2397
- throw new Error(`repository must be owner/repo, not "${repo}"`);
2487
+ return body => {
2488
+ const { name, conclusion } = body;
2489
+ // probe-direction: fail-closed — a write that was refused, or killed at its deadline, published nothing, so the job fails.
2490
+ try {
2491
+ if (!/^[^/\s]+\/[^/\s]+$/u.test(repo)) {
2492
+ throw new Error(`repository must be owner/repo, not "${repo}"`);
2493
+ }
2494
+ gh(
2495
+ ["api", `repos/${repo}/check-runs`, "--method", "POST", "--input", "-"],
2496
+ JSON.stringify(body)
2497
+ );
2498
+ } catch (error) {
2499
+ err.write(
2500
+ `::error title=Review evidence verdict not published::The check run "${name}" was not published on ${opts.sha}: ${refusalOf(error)}. The verdict is not on the pull request head, so this job fails.\n`
2501
+ );
2502
+ return false;
2398
2503
  }
2399
- gh(
2400
- ["api", `repos/${repo}/check-runs`, "--method", "POST", "--input", "-"],
2401
- JSON.stringify(plan.body)
2402
- );
2403
- } catch (error) {
2404
- err.write(
2405
- `::error title=Review evidence verdict not published::The check run "${name}" was not published on ${opts.sha}: ${refusalOf(error)}. The verdict is not on the pull request head, so this job fails.\n`
2406
- );
2407
- return 1;
2408
- }
2409
- out.write(`Published "${name}" on ${opts.sha}: ${conclusion}.\n`);
2410
- summarise(`Published \`${name}\` on \`${opts.sha}\`: **${conclusion}**.\n`);
2411
- return 0;
2504
+ out.write(`Published "${name}" on ${opts.sha}: ${conclusion}.\n`);
2505
+ summarise(`Published \`${name}\` on \`${opts.sha}\`: **${conclusion}**.\n`);
2506
+ return true;
2507
+ };
2412
2508
  }
2413
2509
 
2414
2510
  /**
2415
2511
  * The CLI body.
2416
2512
  * @param {string[]} argv - Arguments after the script path.
2417
2513
  * @param {object} [io] - Injected side effects, for tests.
2418
- * @param {object} [io.gates] - The gates block, read from disk when absent.
2514
+ * @param {object} [io.gates] - The gates block, read from disk when absent; `--publish` reads it for the declared reviewers' names.
2419
2515
  * @param {(sha: string) => number[]|null} [io.pulls] - Head-pull-request reader; null when the read failed.
2420
2516
  * @param {(sha: string) => ReadonlyArray<object>|null} [io.statuses] - Status reader.
2421
2517
  * @param {(sha: string, context: string) => ReadonlyArray<RawCheckRun>|null} [io.checkRuns] -
@@ -34,9 +34,12 @@ const ACCEPTED_KEY = "verification.completion.acceptedEvidenceReleases";
34
34
  /**
35
35
  * Whether a concrete name is a tag GitHub and git both accept: the character
36
36
  * set and length, and none of git's ref-name exclusions that the character set
37
- * still lets through (`..`, a trailing `.`, a `.lock` ending).
37
+ * still lets through (`..`, a trailing `.`, a `.lock` ending). Exported so the
38
+ * evidence prune validates its archive release by the same rule (#4720).
39
+ * @param {string} name - A concrete release name.
40
+ * @returns {boolean} True when the name is a usable tag.
38
41
  */
39
- function usableTag(name) {
42
+ export function usableTag(name) {
40
43
  return (
41
44
  RELEASE_TAG.test(name) &&
42
45
  !name.includes("..") &&
@@ -0,0 +1,227 @@
1
+ // This file is managed by Lisa and IS replaced on each `lisa` run.
2
+ // Do not edit directly — durable changes belong upstream in Lisa.
3
+
4
+ /**
5
+ * The `--apply` half of the evidence prune: copy, confirm, and only then delete.
6
+ *
7
+ * Each candidate is downloaded, uploaded to the archive release under the same
8
+ * name, read back, and compared by sha256. Just before the delete, completion
9
+ * records published since the plan was made are read, so a record that came to
10
+ * depend on the file mid-run keeps it. The original is deleted only after both
11
+ * checks pass, so a failed or corrupted copy can never cost the only copy of a
12
+ * file, and the first failure stops the run (CodySwannGT/lisa#4720).
13
+ * @module scripts/lib/evidence-prune-apply
14
+ */
15
+ import {
16
+ EVIDENCE_RELEASE_ASSET_LIMIT,
17
+ EVIDENCE_RELEASE_NEAR_FULL,
18
+ } from "./completion-proof-github.mjs";
19
+ import {
20
+ collectPins,
21
+ EvidencePruneError,
22
+ isPinned,
23
+ mergePins,
24
+ reportedSha256,
25
+ } from "./evidence-prune.mjs";
26
+ import { hashBytes } from "./evidence-prune-gh.mjs";
27
+
28
+ /**
29
+ * The exact refusal an operator reads when the archive has no room left.
30
+ * @param {string} tag - Archive release tag.
31
+ * @returns {string} The message.
32
+ */
33
+ export function nearlyFullMessage(tag) {
34
+ return `Archive release ${tag} is nearly full (${EVIDENCE_RELEASE_NEAR_FULL} of GitHub's ${EVIDENCE_RELEASE_ASSET_LIMIT}-file limit); re-run next month or pass --archive-release <name>.`;
35
+ }
36
+
37
+ /** The archive release, created from the default branch when absent. */
38
+ function openArchive(gh, tag, branch) {
39
+ const existing = gh.release(tag);
40
+ if (existing) return existing;
41
+ gh.createRelease(tag, branch);
42
+ const created = gh.release(tag);
43
+ if (!created)
44
+ throw new EvidencePruneError(
45
+ `The archive release ${tag} was created but cannot be read back; nothing was archived.`
46
+ );
47
+ return created;
48
+ }
49
+
50
+ /** Refuse a copy whose bytes differ, naming the file and both hashes. */
51
+ function requireSame(name, expected, actual, where) {
52
+ if (expected !== actual)
53
+ throw new EvidencePruneError(
54
+ `${where} of ${name} does not match the original (original sha256 ${expected}, ${where.toLowerCase()} sha256 ${actual}); the original was not deleted.`
55
+ );
56
+ }
57
+
58
+ /**
59
+ * Read the archive copy back and require the original's exact bytes.
60
+ *
61
+ * The copy is downloaded and hashed rather than trusted by its reported
62
+ * `digest`: the digest is GitHub's claim about the bytes, and the only copy of
63
+ * a file is about to be deleted on the strength of this check. When a digest
64
+ * is reported it must agree as well.
65
+ */
66
+ function confirmCopy(gh, candidate, copy, sha) {
67
+ const readBack = gh.asset(copy.id);
68
+ if (readBack?.name !== candidate.name || readBack.state !== "uploaded")
69
+ throw new EvidencePruneError(
70
+ `The archive copy of ${candidate.name} cannot be read back as an uploaded file; the original was not deleted.`
71
+ );
72
+ requireSame(
73
+ candidate.name,
74
+ sha,
75
+ hashBytes(gh.download(readBack)),
76
+ "The archive copy"
77
+ );
78
+ const reported = reportedSha256(readBack);
79
+ if (reported)
80
+ requireSame(candidate.name, sha, reported, "The archive copy's digest");
81
+ }
82
+
83
+ /** The state after leaving one candidate where it is, with the reason. */
84
+ function skip(state, candidate, reason) {
85
+ return {
86
+ ...state,
87
+ skipped: [...state.skipped, { name: candidate.name, reason }],
88
+ };
89
+ }
90
+
91
+ /** Pins with every record published since the last read folded in. */
92
+ function repin(ctx, state) {
93
+ const fresh = ctx.rescan(state.known);
94
+ return {
95
+ ...state,
96
+ pins: mergePins(state.pins, collectPins(fresh.records)),
97
+ known: new Set([...state.known, ...fresh.ids]),
98
+ };
99
+ }
100
+
101
+ /** Delete the original; a failure here leaves a confirmed archive copy. */
102
+ function removeOriginal(ctx, state, candidate) {
103
+ try {
104
+ ctx.gh.remove(candidate);
105
+ return { ...state, archived: [...state.archived, candidate.name] };
106
+ } catch (error) {
107
+ return {
108
+ ...state,
109
+ archivedNotDeleted: [...state.archivedNotDeleted, candidate.name],
110
+ error: error.message,
111
+ };
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Archive one candidate, confirm it, re-check its pins, then delete it.
117
+ * @param {object} ctx - Run context: `gh`, `archive`, `held`, `tag`,
118
+ * `release` and `rescan`.
119
+ * @param {object} state - Outcome so far, archive count and current pins.
120
+ * @param {object} candidate - The file to move.
121
+ * @returns {object} The next state.
122
+ */
123
+ function archiveStep(ctx, state, candidate) {
124
+ const { gh, held, tag, release } = ctx;
125
+ const prior = held.get(candidate.name);
126
+ // An interrupted upload leaves an asset GitHub never finished; it cannot be
127
+ // hashed or replaced, and deleting it is the operator's call, not ours.
128
+ if (prior && prior.state !== "uploaded")
129
+ throw new EvidencePruneError(
130
+ `${candidate.name} in ${tag} is a half-finished upload (state ${JSON.stringify(prior.state)}); delete it from ${tag} and re-run.`
131
+ );
132
+ if (!prior && state.count >= EVIDENCE_RELEASE_NEAR_FULL)
133
+ throw new EvidencePruneError(nearlyFullMessage(tag));
134
+ const bytes = gh.download(candidate);
135
+ const sha = hashBytes(bytes);
136
+ if (candidate.sha256)
137
+ requireSame(candidate.name, candidate.sha256, sha, "The download");
138
+ // post-evidence.sh reuses names such as `pr-<N>-<file>` with --clobber, so a
139
+ // same-named archive file may be a different file, not an earlier copy.
140
+ if (prior && gh.sha256(prior) !== sha)
141
+ return skip(
142
+ state,
143
+ candidate,
144
+ `a different file named ${candidate.name} is already in ${tag}; kept in ${release}`
145
+ );
146
+ const copy = prior ?? gh.upload(ctx.archive, candidate.name, bytes);
147
+ confirmCopy(gh, candidate, copy, sha);
148
+ const next = repin(ctx, { ...state, count: state.count + (prior ? 0 : 1) });
149
+ if (isPinned(next.pins, candidate.id, sha))
150
+ return skip(
151
+ next,
152
+ candidate,
153
+ `${candidate.name}: a completion record published during this run now depends on it; kept in ${release}, and its archive copy stays in ${tag}`
154
+ );
155
+ return removeOriginal(ctx, next, candidate);
156
+ }
157
+
158
+ /** One reducer step: nothing more happens once a step has failed. */
159
+ function step(ctx, state, candidate) {
160
+ if (state.error) return state;
161
+ try {
162
+ return archiveStep(ctx, state, candidate);
163
+ } catch (error) {
164
+ return { ...state, error: error.message };
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Archive and delete every candidate in order, stopping at the first failure.
170
+ * @param {object} gh - The prune's GitHub operations.
171
+ * @param {object[]} candidates - The plan's ordered candidates.
172
+ * @param {object} target - Archive selection and pin sources.
173
+ * @param {string} target.tag - Archive release tag.
174
+ * @param {string} target.branch - Default branch the archive is created from.
175
+ * @param {string} target.release - The release being pruned.
176
+ * @param {object} target.pins - Pins from the snapshot's records.
177
+ * @param {Set<number>} target.known - Record asset ids already read.
178
+ * @param {Function} target.rescan - Reads records published since `known`.
179
+ * @returns {object} `archived`, `archivedNotDeleted`, `skipped`,
180
+ * `notArchived` and `error`.
181
+ */
182
+ export function applyPrune(gh, candidates, target) {
183
+ const start = {
184
+ archived: [],
185
+ archivedNotDeleted: [],
186
+ skipped: [],
187
+ error: null,
188
+ pins: target.pins,
189
+ known: target.known,
190
+ };
191
+ const outcome = (() => {
192
+ if (candidates.length === 0) return start;
193
+ try {
194
+ const archive = openArchive(gh, target.tag, target.branch);
195
+ const listed = gh.assets(archive);
196
+ const ctx = {
197
+ ...target,
198
+ gh,
199
+ archive,
200
+ held: new Map(listed.map(asset => [asset.name, asset])),
201
+ };
202
+ return candidates.reduce(
203
+ (state, candidate) => step(ctx, state, candidate),
204
+ {
205
+ ...start,
206
+ count: listed.length,
207
+ }
208
+ );
209
+ } catch (error) {
210
+ return { ...start, error: error.message };
211
+ }
212
+ })();
213
+ const handled = new Set([
214
+ ...outcome.archived,
215
+ ...outcome.archivedNotDeleted,
216
+ ...outcome.skipped.map(row => row.name),
217
+ ]);
218
+ return {
219
+ archived: outcome.archived,
220
+ archivedNotDeleted: outcome.archivedNotDeleted,
221
+ skipped: outcome.skipped,
222
+ notArchived: candidates
223
+ .map(row => row.name)
224
+ .filter(name => !handled.has(name)),
225
+ error: outcome.error,
226
+ };
227
+ }
@@ -0,0 +1,165 @@
1
+ // This file is managed by Lisa and IS replaced on each `lisa` run.
2
+ // Do not edit directly — durable changes belong upstream in Lisa.
3
+
4
+ /**
5
+ * Inputs for the evidence prune: its flags, the project's Lisa config, which
6
+ * release to prune, and which repository it lives in.
7
+ *
8
+ * The release is resolved exactly as `lisa-work-item.mjs evidence-release`
9
+ * resolves it — `--release`, then `LISA_EVIDENCE_RELEASE`, then the configured
10
+ * `verification.completion.evidenceRelease` — through the one shared resolver,
11
+ * so the prune always reads the release new evidence is written to
12
+ * (CodySwannGT/lisa#4692, #4720).
13
+ * @module scripts/lib/evidence-prune-config
14
+ */
15
+ import { existsSync, readFileSync } from "node:fs";
16
+ import { join } from "node:path";
17
+
18
+ import {
19
+ evidenceReleaseOverride,
20
+ resolveEvidenceRelease,
21
+ usableTag,
22
+ } from "./completion-proof-github.mjs";
23
+ import { DEFAULT_MIN_AGE_DAYS, EvidencePruneError } from "./evidence-prune.mjs";
24
+
25
+ const VALUE_FLAGS = new Map([
26
+ ["--release", "release"],
27
+ ["--archive-release", "archiveRelease"],
28
+ ["--repo", "repo"],
29
+ ["--min-age-days", "minAgeDays"],
30
+ ]);
31
+ const SWITCHES = new Map([
32
+ ["--apply", "apply"],
33
+ ["--json", "json"],
34
+ ["--help", "help"],
35
+ ]);
36
+
37
+ /**
38
+ * Parse the prune's command line; an unknown or valueless flag is refused.
39
+ * @param {string[]} args - Arguments after the script name.
40
+ * @returns {object} Parsed options.
41
+ */
42
+ export function parseFlags(args) {
43
+ const parse = (index, flags) => {
44
+ if (index >= args.length) return flags;
45
+ const arg = args[index];
46
+ if (SWITCHES.has(arg))
47
+ return parse(index + 1, { ...flags, [SWITCHES.get(arg)]: true });
48
+ const value = args[index + 1];
49
+ if (!VALUE_FLAGS.has(arg) || value === undefined || value.startsWith("--"))
50
+ throw new EvidencePruneError(
51
+ `Unrecognised argument ${JSON.stringify(arg)}. Run with --help for the options.`
52
+ );
53
+ // An empty operand must not fall through to a default: `--release ""`
54
+ // would silently prune the configured release, and Number("") is 0.
55
+ if (value.trim() === "")
56
+ throw new EvidencePruneError(
57
+ `${arg} needs a value; it was given an empty one. Run with --help for the options.`
58
+ );
59
+ return parse(index + 2, { ...flags, [VALUE_FLAGS.get(arg)]: value });
60
+ };
61
+ const flags = parse(0, {});
62
+ const days = Number(flags.minAgeDays ?? DEFAULT_MIN_AGE_DAYS);
63
+ if (!Number.isInteger(days) || days < 0)
64
+ throw new EvidencePruneError(
65
+ `--min-age-days must be a whole number of days, not ${JSON.stringify(flags.minAgeDays)}.`
66
+ );
67
+ return { ...flags, minAgeDays: days };
68
+ }
69
+
70
+ /** A JSON file, or `{}` when it is absent; unreadable config is a refusal. */
71
+ function readJson(file) {
72
+ if (!existsSync(file)) return {};
73
+ try {
74
+ return JSON.parse(readFileSync(file, "utf8"));
75
+ } catch {
76
+ throw new EvidencePruneError(
77
+ `${file} is not valid JSON; fix it and retry.`
78
+ );
79
+ }
80
+ }
81
+
82
+ /** Objects merge key by key; anything else in `override` replaces `base`. */
83
+ function merge(base, override) {
84
+ const plain = value =>
85
+ value && typeof value === "object" && !Array.isArray(value);
86
+ if (!plain(base) || !plain(override)) return override ?? base;
87
+ return Object.fromEntries(
88
+ [...new Set([...Object.keys(base), ...Object.keys(override)])].map(key => [
89
+ key,
90
+ key in override ? merge(base[key], override[key]) : base[key],
91
+ ])
92
+ );
93
+ }
94
+
95
+ /**
96
+ * The project's Lisa config, with `.lisa.config.local.json` layered on top.
97
+ * @param {string} cwd - Project root.
98
+ * @returns {object} Effective config.
99
+ */
100
+ export function readLisaConfig(cwd) {
101
+ return merge(
102
+ readJson(join(cwd, ".lisa.config.json")),
103
+ readJson(join(cwd, ".lisa.config.local.json"))
104
+ );
105
+ }
106
+
107
+ /**
108
+ * The release to prune and the archive it moves into.
109
+ * @param {object} flags - Parsed flags.
110
+ * @param {object} config - Effective Lisa config.
111
+ * @param {object} env - Process environment.
112
+ * @param {Date} now - Run time.
113
+ * @param {(release: string) => string} archiveFor - Default archive name.
114
+ * @returns {{release: string, archiveRelease: string}} Concrete tags.
115
+ */
116
+ export function resolveReleases(flags, config, env, now, archiveFor) {
117
+ const policy = config.verification?.completion ?? {};
118
+ const pinned = env.LISA_EVIDENCE_RELEASE?.trim();
119
+ const release = flags.release
120
+ ? evidenceReleaseOverride(policy, flags.release, "--release")
121
+ : pinned
122
+ ? evidenceReleaseOverride(policy, pinned)
123
+ : resolveEvidenceRelease(policy, now);
124
+ const archiveRelease = flags.archiveRelease ?? archiveFor(release);
125
+ if (usableTag(archiveRelease) && archiveRelease !== release)
126
+ return { release, archiveRelease };
127
+ if (flags.archiveRelease)
128
+ throw new EvidencePruneError(
129
+ `--archive-release ${JSON.stringify(archiveRelease)} is not usable: name a different release using letters, digits, ".", "_" or "-".`
130
+ );
131
+ throw new EvidencePruneError(
132
+ `The archive release name made from ${release} (${archiveRelease}) is not a usable GitHub release name; it is likely over 100 characters. Name a shorter archive release explicitly (see --help).`
133
+ );
134
+ }
135
+
136
+ /**
137
+ * `owner/name` from `--repo`, then the Lisa config, then the checkout's remote.
138
+ * @param {object} flags - Parsed flags.
139
+ * @param {object} config - Effective Lisa config.
140
+ * @param {Function} run - Child-process runner.
141
+ * @returns {string} The repository.
142
+ */
143
+ export function resolveRepository(flags, config, run) {
144
+ const configured =
145
+ config.github?.org && config.github?.repo
146
+ ? `${config.github.org}/${config.github.repo}`
147
+ : undefined;
148
+ const viewed = () => {
149
+ const result = run("gh", ["repo", "view", "--json", "nameWithOwner"], {});
150
+ try {
151
+ return result.status === 0
152
+ ? JSON.parse(result.stdout).nameWithOwner
153
+ : undefined;
154
+ } catch {
155
+ // probe-direction: fail-closed — an unreadable `gh repo view` leaves no repository, and the caller refuses to run rather than guessing one.
156
+ return undefined;
157
+ }
158
+ };
159
+ const repository = flags.repo ?? configured ?? viewed();
160
+ if (!/^[\w.-]+\/[\w.-]+$/.test(repository ?? ""))
161
+ throw new EvidencePruneError(
162
+ "Could not tell which GitHub repository holds the evidence. Pass --repo <owner/name>, or set github.org and github.repo in .lisa.config.json."
163
+ );
164
+ return repository;
165
+ }