@warren-dean/code-kit 0.26.1 → 0.27.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.
@@ -8,7 +8,8 @@ import { loadConfig, mergeDelegation } from './config.mjs';
8
8
  import { buildStatus, readStories, DESIGN, LEAD } from './plan.mjs';
9
9
  import { pendingSetup } from './adapters/index.mjs';
10
10
  import { CONFIG_FILE, SETUP_VERSION } from './config.mjs';
11
- import { readQueue, waiting } from './queue.mjs';
11
+ import { mergingNow, nextToMerge, readQueue, waiting } from './queue.mjs';
12
+ import { handedOver, readRegister, toolOf } from './designs.mjs';
12
13
  import { decidedBy, laneOf, openRequests } from './rules.mjs';
13
14
  import { instructionsState } from './instructions.mjs';
14
15
  import { SECURITY_AGENT_FILE } from './security.mjs';
@@ -265,8 +266,21 @@ export function nextStep(root, base) {
265
266
  `The plan's status lines are out of date for ${status.drift.map((d) => d.story).join(', ')}.`,
266
267
  );
267
268
 
269
+ // A lane's story whose agent stopped before its first commit is ready again, for a fresh agent.
270
+ const stopped = status.stories.filter((s) => s.stopped).map((s) => s.id);
271
+ if (stopped.length)
272
+ attention.push(
273
+ `${stopped.join(', ')} ${stopped.length === 1 ? 'has its branch but no commits, and no agent on it' : 'have their branches but no commits, and no agent on them'}: ${stopped.length === 1 ? 'it goes' : 'they go'} to a fresh agent, on the same branch.`,
274
+ );
275
+
268
276
  // A branch already in the merge queue has passed its review: it isn't reviewed again.
269
- const queued = waiting(readQueue(root)).map((e) => e.branch);
277
+ const entries = readQueue(root);
278
+ const inQueue = waiting(entries);
279
+ // MQ-7: what can merge next; a branch queued with partners waits until they're queued too.
280
+ const { head: mergeHead, held: heldGroups } = nextToMerge(entries);
281
+ const queued = inQueue.map((e) => e.branch);
282
+ // MQ-1: a queue merge running now (its verify takes minutes) isn't asked for again.
283
+ const merging = mergingNow(inQueue[0]) ? inQueue[0].branch : null;
270
284
  const ids = (state) =>
271
285
  status.stories
272
286
  .filter((s) => s.state === state && !(state === 'review' && queued.includes(s.branch)))
@@ -364,11 +378,19 @@ export function nextStep(root, base) {
364
378
  attention.push(
365
379
  `${queued.join(', ')} passed review and ${queued.length === 1 ? 'waits' : 'wait'} for the person to merge (code-kit queue merge --person, or Merge in the code-kit band).`,
366
380
  );
367
- if (queued.length && leadMerges)
381
+ if (merging)
382
+ attention.push(
383
+ `${merging} is being merged now (queue merge is running); it says when it's done.`,
384
+ );
385
+ for (const h of heldGroups)
386
+ attention.push(
387
+ `${h.branch} passed review and merges together with ${h.missing.join(', ')}: it waits until ${h.missing.length === 1 ? 'that is' : 'they are'} reviewed and queued.`,
388
+ );
389
+ if (mergeHead && leadMerges && !merging)
368
390
  return result(
369
391
  'merge',
370
- `${queued[0]} passed review and is next in the merge queue${queued.length > 1 ? ` (then ${queued.slice(1).join(', ')})` : ''}.`,
371
- { args: queued[0] },
392
+ `${mergeHead.branch} passed review and is next in the merge queue${queued.length > 1 ? ` (then ${queued.filter((b) => b !== mergeHead.branch).join(', ')})` : ''}.`,
393
+ { args: mergeHead.branch },
372
394
  );
373
395
  // SCHED-1, SCHED-2: one story for each free lane, across every session; the open slice's first.
374
396
  const busy = new Set([
@@ -393,6 +415,12 @@ export function nextStep(root, base) {
393
415
  attention.push(
394
416
  `${sched.waiting.join(', ')} ${sched.waiting.length === 1 ? 'waits' : 'wait'} for ${sched.waiting.length === 1 ? 'its lane' : 'their lanes'}: a lane runs one story at a time.`,
395
417
  );
418
+ // HAND-5: approved designs still only in a design tool, which no lane can read, are handed over once.
419
+ const toolOnly = readRegister(root, config).entries.filter((e) => !handedOver(e));
420
+ if (toolOnly.length)
421
+ attention.push(
422
+ `${toolOnly.length} approved screen${toolOnly.length === 1 ? ' is' : 's are'} only in ${[...new Set(toolOnly.map(toolOf))].join(', ')}, not as files a lane can build from: hand ${toolOnly.length === 1 ? 'it' : 'them'} over with /code-kit:design --hand-over.`,
423
+ );
396
424
  const then = [];
397
425
  // A sent-back story goes back to its lane with the review's findings, before new work starts.
398
426
  const toDispatch = [...fixes, ...ready];
@@ -6,14 +6,19 @@
6
6
  // `**Requirements:** none` marks a story that delivers no requirement, such as a spike.
7
7
  import { execFileSync } from 'node:child_process';
8
8
  import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
9
- import { join } from 'node:path';
9
+ import { isAbsolute, join } from 'node:path';
10
+ import { readEvents } from './events.mjs';
11
+ import { laneClaims } from './laneclaims.mjs';
10
12
  import { waitingStories } from './questions.mjs';
11
13
  import { sentBack } from './reviews.mjs';
14
+ import { WORKTREES_DIR } from './worktrees.mjs';
12
15
 
13
16
  export const REQUIREMENT = /\b(?!ST-)([A-Z][A-Z0-9]*-\d+)\b/g;
14
17
  const STORY_HEADING = /^###\s+(ST-\d+)\b\s*(.*)$/;
15
18
  const REQUIREMENT_HEADING = /^###\s+((?!ST-)[A-Z][A-Z0-9]*-\d+)\b\s*(.*)$/;
16
19
  export const STATES = ['todo', 'in progress', 'review', 'done'];
20
+ /** A story taken out of the plan (folded into another, or no longer wanted): never built, never waited on. */
21
+ export const DROPPED = 'dropped';
17
22
  export const LEAD = 'lead';
18
23
  /** ONB-2: the pseudo-lane for design stories, which the lead designs with the person before any screen is built. */
19
24
  export const DESIGN = 'design';
@@ -92,7 +97,7 @@ export function readStories(root, planPath) {
92
97
  // REG-1: what a fix story fixes, or null.
93
98
  fixes: field(body, 'Fixes') || null,
94
99
  dependsOn: [...field(body, 'Depends on').matchAll(/\bST-\d+\b/g)].map((r) => r[0]),
95
- status: STATES.includes(status) ? status : 'todo',
100
+ status: STATES.includes(status) || status === DROPPED ? status : 'todo',
96
101
  milestone,
97
102
  // SCHED-2: the slice it belongs to, a feature built across lanes, or null.
98
103
  slice: field(body, 'Slice') || null,
@@ -155,6 +160,8 @@ const git = (root, ...args) => {
155
160
  return execFileSync('git', ['-C', root, ...args], {
156
161
  encoding: 'utf8',
157
162
  stdio: ['ignore', 'pipe', 'ignore'],
163
+ // A long-lived project's log runs to megabytes.
164
+ maxBuffer: 256 * 1024 * 1024,
158
165
  }).trim();
159
166
  } catch {
160
167
  return null;
@@ -180,6 +187,35 @@ export function branchRefs(root, base) {
180
187
  return refs;
181
188
  }
182
189
 
190
+ /**
191
+ * How each local branch was made, from its reflog's oldest entry: (branch) => { sha, at } (the commit
192
+ * it was made at, and when, in milliseconds), or null once the reflog is gone. Read from the file git
193
+ * keeps it in, or from git where there's no file (a reftable repository).
194
+ */
195
+ export function branchMaking(root) {
196
+ const common = git(root, 'rev-parse', '--git-common-dir');
197
+ const dir = common ? (isAbsolute(common) ? common : join(root, common)) : null;
198
+ return (branch) => {
199
+ if (!dir) return null;
200
+ try {
201
+ const first = readFileSync(join(dir, 'logs/refs/heads', branch), 'utf8').split('\n')[0];
202
+ const [head] = first.split('\t');
203
+ const sha = head.split(' ')[1];
204
+ const at = head.match(/> (\d+) [+-]\d{4}$/)?.[1];
205
+ return sha && at ? { sha, at: Number(at) * 1000 } : null;
206
+ } catch {
207
+ const oldest = (
208
+ git(root, 'reflog', 'show', '--date=unix', '--format=%H %gd', `refs/heads/${branch}`) ?? ''
209
+ )
210
+ .split('\n')
211
+ .filter(Boolean)
212
+ .at(-1)
213
+ ?.match(/^([0-9a-f]+) .*@\{(\d+)\}$/);
214
+ return oldest ? { sha: oldest[1], at: Number(oldest[2]) * 1000 } : null;
215
+ }
216
+ };
217
+ }
218
+
183
219
  /** The ref of a branch, local first, then on origin; null when neither exists. */
184
220
  function branchRef(refs, branch) {
185
221
  return [`refs/heads/${branch}`, `refs/remotes/origin/${branch}`].find((r) => refs[r]) ?? null;
@@ -201,16 +237,51 @@ function worktrees(root) {
201
237
  /**
202
238
  * Where each story is. The plan's `done` stands (the lead marks it after merging, and merged
203
239
  * branches are often deleted or squashed). Otherwise git decides: a branch with commits not in
204
- * `base` is in review; one level with it is in progress, or done once the plan had it in review.
205
- * A branch still at the commit a review sent back is `sent back`, waiting for its lane's fix.
206
- * Without a branch, the plan's status stands. A story is `blocked` while a story it depends on
207
- * isn't done, whatever its branch holds; a todo story whose dependencies are done is `ready`. A story
208
- * with an open question for the person that holds it (`code-kit ask --blocks`) is `waiting on you`.
240
+ * `base` is in review. A branch still at the commit a review sent back is `sent back`, waiting for
241
+ * its lane's fix. Without a branch, the plan's status stands. A story is `blocked` while a story it
242
+ * depends on isn't done, whatever its branch holds; a todo story whose dependencies are done is `ready`.
243
+ * A story with an open question for the person that holds it (`code-kit ask --blocks`) is `waiting on
244
+ * you`.
245
+ *
246
+ * A branch level with the base is in progress while something builds it: a live claim of its lane (an
247
+ * agent at work on it, SCHED-1), or the branch checked out where a person works (the main checkout, or a
248
+ * worktree of theirs). With nothing on it, it's done when its commits merged (the plan had it in review,
249
+ * the branch moved since it was made, or with no reflog, the base names the story or merged the branch),
250
+ * whatever the plan says yet. A lane's branch that never moved is `stopped`: its agent stopped before its
251
+ * first commit, and the story is ready again (or blocked) for a fresh agent, so its lane isn't held by
252
+ * nothing. A branch only on origin may be a teammate's, and stays in progress unless the base holds it.
209
253
  */
210
254
  export function storyStates(root, stories, base) {
211
255
  const trees = worktrees(root);
212
256
  const backs = sentBack(root);
213
257
  const refs = branchRefs(root, base);
258
+ const claims = laneClaims(root).filter((c) => !c.stale);
259
+ const live = new Set(claims.map((c) => `${c.lane}|${c.story ?? ''}`));
260
+ // A lane agent dispatched on the story and not handed back since: its commits are work in progress,
261
+ // even where the branch moved only by taking in the base (a re-dispatch after a send-back).
262
+ const building = (story) => {
263
+ const claim = claims.find((c) => c.lane === story.lane && c.story === story.id);
264
+ if (!claim) return false;
265
+ const since = Date.parse(claim.since);
266
+ return !readEvents(root, story.id).some(
267
+ (e) => e.kind === 'handback' && Date.parse(e.at) >= since,
268
+ );
269
+ };
270
+ // What the base already holds: the stories its commits name, and the story branches merged into it.
271
+ const onBase = { stories: new Set(), branches: new Set() };
272
+ for (const m of (git(root, 'log', base, '--format=%B%x1e') ?? '').matchAll(
273
+ /(?:^|[^A-Za-z0-9-])(ST-\d+)(?!\d)|([A-Za-z0-9_.-]+\/st-\d+)(?![\w-])/gim,
274
+ ))
275
+ if (m[1]) onBase.stories.add(m[1]);
276
+ else onBase.branches.add(m[2].toLowerCase());
277
+ const making = branchMaking(root);
278
+ const merged = (story, branch) =>
279
+ onBase.stories.has(story.id) || onBase.branches.has(branch.toLowerCase());
280
+ const agentTree = (path) => path?.split('\\').join('/').includes(`/${WORKTREES_DIR}/`);
281
+ const builtBy = (story, branch) =>
282
+ live.has(`${story.lane}|${story.id}`) ||
283
+ live.has(`${story.lane}|`) ||
284
+ (trees[branch] !== undefined && !agentTree(trees[branch]));
214
285
  const states = stories.map((story) => {
215
286
  const branch = storyBranch(story);
216
287
  const ref = story.lane ? branchRef(refs, branch) : null;
@@ -218,11 +289,24 @@ export function storyStates(root, stories, base) {
218
289
  ? 0
219
290
  : (refs[ref].ahead ?? Number(git(root, 'rev-list', '--count', `${base}..${ref}`) ?? 0));
220
291
  let state = story.status;
221
- if (ref && story.status !== 'done') {
292
+ let stopped = false;
293
+ if (ref && !['done', DROPPED].includes(story.status)) {
222
294
  const head = backs[branch] ? refs[ref].sha : null;
223
295
  if (head && head === backs[branch].sha) state = 'sent back';
224
- else if (ahead > 0) state = 'review';
225
- else state = story.status === 'review' ? 'done' : 'in progress';
296
+ else if (ahead > 0) state = building(story) ? 'in progress' : 'review';
297
+ else if (story.status === 'review') state = 'done';
298
+ else if (builtBy(story, branch)) state = 'in progress';
299
+ else {
300
+ // Level with the base, and nothing on it: its commits merged (the plan may not say so yet), or
301
+ // none was made. A local branch's reflog says which: it moved from the commit it was made at only
302
+ // by being committed to. Without one, the base's own log says whether it holds the story's work.
303
+ const made = ref.startsWith('refs/heads/') ? making(branch) : null;
304
+ if (made ? made.sha !== refs[ref].sha : merged(story, branch)) state = 'done';
305
+ else if (made && ![LEAD, DESIGN].includes(story.lane)) {
306
+ state = 'todo';
307
+ stopped = true;
308
+ } else state = 'in progress';
309
+ }
226
310
  }
227
311
  return {
228
312
  ...story,
@@ -232,14 +316,16 @@ export function storyStates(root, stories, base) {
232
316
  ahead,
233
317
  worktree: trees[branch] ?? null,
234
318
  state,
319
+ stopped,
235
320
  waitingOn: [],
236
321
  };
237
322
  });
238
- const done = new Set(states.filter((s) => s.state === 'done').map((s) => s.id));
323
+ // A dropped story is as good as done for the stories that depend on it: it won't be built.
324
+ const done = new Set(states.filter((s) => ['done', DROPPED].includes(s.state)).map((s) => s.id));
239
325
  // ASK-3: a story whose question for the person is open waits on them, whatever its branch holds.
240
326
  const asked = waitingStories(root);
241
327
  for (const s of states) {
242
- if (s.state === 'done') continue;
328
+ if (['done', DROPPED].includes(s.state)) continue;
243
329
  s.waitingOn = s.dependsOn.filter((d) => !done.has(d));
244
330
  s.waitingFor = asked.has(s.id)
245
331
  ? { id: asked.get(s.id).id, question: asked.get(s.id).question }
@@ -267,7 +353,7 @@ export function traceProblems(requirements, stories, lanes) {
267
353
  if (!s.lane) problems.push(`${s.id} has no **Lane:**`);
268
354
  else if (s.lane !== LEAD && s.lane !== DESIGN && !lanes.includes(s.lane))
269
355
  problems.push(`${s.id} names an unknown lane "${s.lane}"`);
270
- if (!s.requirements.length && !s.noRequirements)
356
+ if (!s.requirements.length && !s.noRequirements && s.status !== DROPPED)
271
357
  problems.push(`${s.id} cites no requirements (write "none" if it delivers none)`);
272
358
  for (const r of s.requirements)
273
359
  if (!ids.has(r)) problems.push(`${s.id} cites ${r}, which no spec defines`);
@@ -275,7 +361,9 @@ export function traceProblems(requirements, stories, lanes) {
275
361
  if (!stories.some((o) => o.id === d))
276
362
  problems.push(`${s.id} depends on ${d}, which isn't in the plan`);
277
363
  }
278
- const covered = new Set(stories.flatMap((s) => s.requirements));
364
+ const covered = new Set(
365
+ stories.filter((s) => s.status !== DROPPED).flatMap((s) => s.requirements),
366
+ );
279
367
  for (const r of requirements)
280
368
  if (!r.removed && !covered.has(r.id)) problems.push(`${r.id} is in no story (${r.file})`);
281
369
  return problems;
@@ -289,7 +377,7 @@ export function buildStatus(root, config, base, files) {
289
377
  const tested = testedRequirements(root, files, specs);
290
378
  const problems = traceProblems(requirements, stories, Object.keys(config.lanes));
291
379
  const byRequirement = requirements.map((r) => {
292
- const covering = stories.filter((s) => s.requirements.includes(r.id));
380
+ const covering = stories.filter((s) => s.state !== DROPPED && s.requirements.includes(r.id));
293
381
  let state = 'no story';
294
382
  if (r.removed) state = 'removed';
295
383
  else if (covering.length && covering.every((s) => s.state === 'done')) state = 'done';
@@ -90,6 +90,91 @@ export function settle(root, branch, state, reason = null, now = Date.now()) {
90
90
  writeQueue(root, [...entries.filter((e) => !settled.includes(e)), ...settled]);
91
91
  }
92
92
 
93
+ /**
94
+ * MQ-1: the head being merged by a running `queue merge` (process `pid`), in its place; `null` puts it
95
+ * back to waiting, in its place too.
96
+ */
97
+ export function markMerging(root, branch, pid, now = Date.now()) {
98
+ writeQueue(
99
+ root,
100
+ readQueue(root).map((e) => {
101
+ if (e.branch !== branch || !['waiting', 'merging'].includes(e.state)) return e;
102
+ const { pid: _pid, ...rest } = e;
103
+ return pid
104
+ ? { ...rest, state: 'merging', pid, at: new Date(now).toISOString() }
105
+ : { ...rest, state: 'waiting' };
106
+ }),
107
+ );
108
+ }
109
+
110
+ /** Whether `entry` is being merged now: marked merging by a process still running. */
111
+ export function mergingNow(entry) {
112
+ if (entry?.state !== 'merging' || !entry.pid) return false;
113
+ try {
114
+ process.kill(entry.pid, 0);
115
+ return true;
116
+ } catch (e) {
117
+ return e.code === 'EPERM';
118
+ }
119
+ }
120
+
121
+ /**
122
+ * MQ-7: what the queue merges next. A branch queued `with` others merges only together with them, once
123
+ * each is queued too: verified as one tree, merged one after another. Until then it waits, and the
124
+ * queue goes on with the next. `{ head, partners, held }`: `head` null when nothing can merge yet;
125
+ * `held` lists each waiting branch with the partners it still needs queued.
126
+ */
127
+ export function nextToMerge(entries) {
128
+ const queue = waiting(entries);
129
+ const queued = new Set(queue.map((e) => e.branch));
130
+ // A partner already merged on its own is no longer waited for.
131
+ const merged = new Set(entries.filter((e) => e.state === 'merged').map((e) => e.branch));
132
+ const groupOf = (e) => {
133
+ const group = new Set([e.branch]);
134
+ for (let grew = true; grew;) {
135
+ grew = false;
136
+ for (const x of queue)
137
+ if (
138
+ !group.has(x.branch) &&
139
+ ((x.with ?? []).some((b) => group.has(b)) ||
140
+ [...group].some((b) =>
141
+ (queue.find((y) => y.branch === b)?.with ?? []).includes(x.branch),
142
+ ))
143
+ ) {
144
+ group.add(x.branch);
145
+ grew = true;
146
+ }
147
+ }
148
+ return group;
149
+ };
150
+ const held = [];
151
+ for (const e of queue) {
152
+ const group = groupOf(e);
153
+ const missing = [...group]
154
+ .flatMap((b) => queue.find((y) => y.branch === b)?.with ?? [])
155
+ .filter((b) => !queued.has(b) && !merged.has(b));
156
+ if (missing.length) {
157
+ held.push({ branch: e.branch, missing: [...new Set(missing)] });
158
+ continue;
159
+ }
160
+ const partners = queue.filter((x) => x.branch !== e.branch && group.has(x.branch));
161
+ return { head: e, partners, held };
162
+ }
163
+ return { head: null, partners: [], held };
164
+ }
165
+
166
+ /** MQ-7: a queued branch's partners, which it merges together with. */
167
+ export function setPartners(root, branch, partners) {
168
+ writeQueue(
169
+ root,
170
+ readQueue(root).map((e) =>
171
+ e.branch === branch && ['waiting', 'merging'].includes(e.state)
172
+ ? { ...e, with: [...new Set([...(e.with ?? []), ...partners])] }
173
+ : e,
174
+ ),
175
+ );
176
+ }
177
+
93
178
  /** The branch merged last, for a send-back's reason ("conflicts with main after web/st-4"). */
94
179
  export function lastMerged(entries) {
95
180
  return [...entries].reverse().find((e) => e.state === 'merged')?.branch ?? null;
@@ -109,7 +194,7 @@ export function storyMerges(cmd, branch) {
109
194
  const switches = new RegExp(
110
195
  String.raw`^${GIT}(?:switch|checkout)\s+((?:-\S+\s+)*)([^\s-]\S*)\s*$`,
111
196
  );
112
- const merges = new RegExp(String.raw`^${GIT}merge\b(.*)$`);
197
+ const merges = new RegExp(String.raw`^${GIT}merge(?![\w-])(.*)$`);
113
198
  for (const { text, code } of commandsOf(cmd)) {
114
199
  const moved = code.match(switches);
115
200
  if (moved) {
@@ -4,9 +4,9 @@
4
4
  // WHO-2), the person's otherwise and always on the lead's own. The commit hook
5
5
  // refuses a weakening commit; verify finds what a branch weakened, from git.
6
6
  import { execFileSync, spawnSync } from 'node:child_process';
7
- import { existsSync, mkdtempSync, readFileSync, rmSync, symlinkSync } from 'node:fs';
8
- import { tmpdir } from 'node:os';
9
- import { basename, dirname, join } from 'node:path';
7
+ import { existsSync, readFileSync, rmSync } from 'node:fs';
8
+ import { join } from 'node:path';
9
+ import { freshCheckout } from './checkout.mjs';
10
10
  import { runChecks } from './checks.mjs';
11
11
  import { matchesAny } from './glob.mjs';
12
12
  import { readStories } from './plan.mjs';
@@ -39,8 +39,9 @@ const SKIPS = [
39
39
  /\bt\.Skip(?:f|Now)?\s*\(/,
40
40
  /@(?:Disabled|Ignore)\b/,
41
41
  ];
42
+ // `expect(` and its retrying and soft forms (`expect.poll(`, `expect.soft(`), each one assertion.
42
43
  const ASSERTION =
43
- /\bexpect\s*\(|\bassert\w*\s*[.(]|\bt\.(?:Error|Errorf|Fatal|Fatalf|Fail)\b|\bshould\.\w/g;
44
+ /\bexpect(?:\.(?:poll|soft))?\s*\(|\bassert\w*\s*[.(]|\bt\.(?:Error|Errorf|Fatal|Fatalf|Fail)\b|\bshould\.\w/g;
44
45
 
45
46
  const skipLines = (text) =>
46
47
  String(text ?? '')
@@ -178,10 +179,22 @@ export function branchWeakenings(root, from, config) {
178
179
 
179
180
  /**
180
181
  * REG-2 for a commit about to be made: `files` as the commit hook lists them, HEAD against the
181
- * working tree.
182
+ * working tree. `gone`: files the commit's own command takes away first (`git rm`, `git mv`), which
183
+ * are still on disk as the hook runs. `base`: the branch the work merges into; a test the branch
184
+ * added itself since it left it isn't the base's to lose, so moving or deleting it weakens nothing.
182
185
  */
183
- export function commitWeakenings(root, files, config) {
186
+ export function commitWeakenings(root, files, config, gone = new Set(), base = null) {
187
+ let from = null;
188
+ try {
189
+ from = base ? git(root, 'merge-base', base, 'HEAD').trim() : null;
190
+ } catch {
191
+ // no common history with the base: every test counts
192
+ }
193
+ // What this commit adds stays, to pair a moved test with where it went.
194
+ if (from)
195
+ files = files.filter((f) => showAt(root, 'HEAD', f) === null || showAt(root, from, f) !== null);
184
196
  const read = (rel) => {
197
+ if (gone.has(rel)) return null;
185
198
  try {
186
199
  return readFileSync(join(root, rel), 'utf8');
187
200
  } catch {
@@ -190,7 +203,7 @@ export function commitWeakenings(root, files, config) {
190
203
  };
191
204
  const changes = files.map((file) => {
192
205
  const was = showAt(root, 'HEAD', file) !== null;
193
- const is = existsSync(join(root, file));
206
+ const is = !gone.has(file) && existsSync(join(root, file));
194
207
  return { status: !was ? 'A' : is ? 'M' : 'D', file };
195
208
  });
196
209
  return weakenedTests(changes, (rel) => showAt(root, 'HEAD', rel), read, config);
@@ -219,38 +232,24 @@ const UNREACHABLE =
219
232
  const ASSERTED =
220
233
  /AssertionError|AssertionFailedError|Expected:|Received:|expected .+ to |toBe|toEqual|assert\w* failed|^\s*--- FAIL|Error: expect\(/m;
221
234
 
235
+ /** The names a failure says can't be reached: quoted ones on its unreachable lines. */
236
+ function missingNames(output) {
237
+ const names = new Set();
238
+ for (const line of output.split('\n').filter((l) => UNREACHABLE.test(l)))
239
+ for (const m of line.matchAll(
240
+ /['"`‘’]([A-Za-z_$][\w$]*)['"`‘’]|ReferenceError: ([A-Za-z_$][\w$]*)|undefined: (\w+)|import name '?(\w+)/g,
241
+ ))
242
+ names.add(m[1] ?? m[2] ?? m[3] ?? m[4]);
243
+ return names;
244
+ }
245
+
222
246
  /** Whether a failure's output only shows the tests couldn't reach what the fix adds. */
223
247
  export const onlyUnreachable = (output) =>
224
248
  UNREACHABLE.test(output) && !ASSERTED.test(output.replace(UNREACHABLE, ''));
225
249
 
226
- // The folders an install puts beside the code, which a fresh checkout lacks.
227
- const INSTALLS = new Set(['node_modules', '.venv', 'venv']);
228
-
229
- /** Links the checkout's installs (node_modules and the like, wherever git ignores them) into `dir`. */
230
- function linkInstalls(root, dir) {
231
- let ignored = [];
232
- try {
233
- ignored = git(root, 'ls-files', '--others', '--ignored', '--exclude-standard', '--directory')
234
- .split('\n')
235
- .map((l) => l.replace(/\/$/, ''))
236
- .filter((l) => l && INSTALLS.has(basename(l)));
237
- } catch {
238
- return;
239
- }
240
- for (const rel of ignored) {
241
- const to = join(dir, rel);
242
- if (!existsSync(dirname(to)) || existsSync(to)) continue;
243
- try {
244
- symlinkSync(join(root, rel), to, 'dir');
245
- } catch {
246
- // the check then says what it couldn't run
247
- }
248
- }
249
- }
250
-
251
250
  /**
252
- * REG-1: whether a fix's tests prove its bug. In a temporary worktree of the branch (with the
253
- * checkout's installs linked in, after `regression.prepare`), the checks covering the tests it added
251
+ * REG-1: whether a fix's tests prove its bug. In a fresh checkout of the branch (with its own copy of
252
+ * the checkout's installs, after `regression.prepare`), the checks covering the tests it added
254
253
  * or changed must pass, and fail, as the bug, once every change outside the tests goes back to `from`.
255
254
  * Problems in words; empty when the tests prove it.
256
255
  */
@@ -263,6 +262,9 @@ export function proveFix({ root, from, base, changed, config, fix }) {
263
262
  return [
264
263
  `${what}, but it adds or changes no test. Add a test that fails without the fix: it's what stops the bug coming back.`,
265
264
  ];
265
+ // A fix made wholly in the tests (a flaky test, a broken helper) has no code to take back: its tests
266
+ // are the fix. REG-2 still holds them, so it can't be made by weakening one.
267
+ if (changed.every((f) => isTestFile(f, config) || SNAPSHOT.test(f))) return [];
266
268
  const covering = config.checks.filter((c) =>
267
269
  tests.some((f) => matchesAny(f, c.files) && !matchesAny(f, c.exclude)),
268
270
  );
@@ -272,11 +274,8 @@ export function proveFix({ root, from, base, changed, config, fix }) {
272
274
  ];
273
275
  const strict = { ...config, checks: covering.map((c) => ({ ...c, ifMissing: 'fail' })) };
274
276
  const actor = { kind: 'lead', label: 'lead' };
275
- const dir = mkdtempSync(join(tmpdir(), 'code-kit-fix-'));
276
- rmSync(dir, { recursive: true });
277
+ const { dir, remove } = freshCheckout(root, 'HEAD', { prefix: 'code-kit-fix-' });
277
278
  try {
278
- git(root, 'worktree', 'add', '--quiet', '--detach', dir, 'HEAD');
279
- linkInstalls(root, dir);
280
279
  const prepare = config.regression?.prepare;
281
280
  if (prepare) {
282
281
  const res = spawnSync(prepare, { cwd: dir, shell: true, encoding: 'utf8', timeout: 900000 });
@@ -300,7 +299,22 @@ export function proveFix({ root, from, base, changed, config, fix }) {
300
299
  return [
301
300
  `${what}, but its tests (${tests.join(', ')}) also pass without the fix, so they don't prove the bug and it can come back. Write a test that fails on ${base} and passes here.`,
302
301
  ];
303
- const output = `${without.title}\n${without.output ?? ''}`;
302
+ let output = `${without.title}\n${without.output ?? ''}`;
303
+ // Tests that call what the fix adds can stop a whole run (a typecheck) before a test through the
304
+ // existing code fails on its assertion: set them aside, and run the rest without the fix.
305
+ if (onlyUnreachable(output)) {
306
+ const missing = missingNames(output);
307
+ const aside = tests.filter((f) => {
308
+ const text = showAt(root, 'HEAD', f) ?? '';
309
+ return [...missing].some((n) => new RegExp(String.raw`\b${n}\b`).test(text));
310
+ });
311
+ const rest = tests.filter((f) => !aside.includes(f));
312
+ if (aside.length && rest.length) {
313
+ for (const f of aside) rmSync(join(dir, f), { force: true });
314
+ const { failure: again } = runChecks(strict, rest, dir, actor);
315
+ if (again) output = `${again.title}\n${again.output ?? ''}`;
316
+ }
317
+ }
304
318
  if (onlyUnreachable(output)) {
305
319
  const line =
306
320
  output
@@ -313,10 +327,6 @@ export function proveFix({ root, from, base, changed, config, fix }) {
313
327
  }
314
328
  return [];
315
329
  } finally {
316
- try {
317
- git(root, 'worktree', 'remove', '--force', dir);
318
- } catch {
319
- rmSync(dir, { recursive: true, force: true });
320
- }
330
+ remove();
321
331
  }
322
332
  }