@warren-dean/code-kit 0.37.0 → 0.38.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.
package/bin/code-kit.mjs CHANGED
@@ -143,7 +143,9 @@ import {
143
143
  markMerging,
144
144
  mergingNow,
145
145
  nextToMerge,
146
+ releaseMergeLock,
146
147
  setPartners,
148
+ takeMergeLock,
147
149
  pauseQueue,
148
150
  queuePause,
149
151
  readQueue,
@@ -707,11 +709,15 @@ const firstMerge = (into, branch) =>
707
709
 
708
710
  /** Why a merge didn't happen: `refused` (the rules), `verify` (problems) or `conflict`. */
709
711
  class MergeStop extends Error {
710
- /** `first`: the first problem's first line, for the queue's record of why (#57). */
711
- constructor(kind, message, first = null) {
712
+ /**
713
+ * `first`: the first problem's first line, for the queue's record of why (#57). `at`: the group's
714
+ * branches the stop is down to, when it's known (MQ-7); null when it's the group's as a whole.
715
+ */
716
+ constructor(kind, message, first = null, at = null) {
712
717
  super(message);
713
718
  this.kind = kind;
714
719
  this.first = first;
720
+ this.at = at;
715
721
  }
716
722
  }
717
723
 
@@ -893,9 +899,9 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
893
899
  }
894
900
  }
895
901
  };
896
- const fail = (message, kind = 'refused', first = null) => {
902
+ const fail = (message, kind = 'refused', first = null, at = null) => {
897
903
  cleanup();
898
- throw new MergeStop(kind, message, first);
904
+ throw new MergeStop(kind, message, first, at);
899
905
  };
900
906
  const worktree = (b, detach) => {
901
907
  const dir = mkdtempSync(join(tmpdir(), 'code-kit-merge-'));
@@ -943,6 +949,8 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
943
949
  fail(
944
950
  `Nothing was merged: ${branch} doesn't merge cleanly into ${into}. Merge ${into} into ${branch} and resolve it there, then merge again.\n${e.message}`,
945
951
  'conflict',
952
+ null,
953
+ [branch],
946
954
  );
947
955
  }
948
956
  temporary.push(fresh);
@@ -966,11 +974,20 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
966
974
  );
967
975
  for (const m of members) {
968
976
  const merged = mergeInto(verifyIn, m.tip);
969
- if (merged.status !== 0)
970
- fail(
971
- `Nothing was merged: ${m.branch} doesn't merge cleanly with ${branch} on ${into}. Merge one into the other and resolve it there, then merge again.\n${`${merged.stdout ?? ''}${merged.stderr ?? ''}`.trim()}`,
972
- 'conflict',
973
- );
977
+ if (merged.status === 0) continue;
978
+ // MQ-7: a member that doesn't merge onto the base alone is the one at fault; one that does
979
+ // conflicts with its partners, which the group resolves together.
980
+ const alone = freshCheckout(root, into, { prefix: 'code-kit-merge-', installsFrom: null });
981
+ temporary.push(alone);
982
+ const onBase = mergeInto(alone.dir, m.tip).status === 0;
983
+ fail(
984
+ onBase
985
+ ? `Nothing was merged: ${m.branch} doesn't merge cleanly with ${branch} on ${into}. Merge one into the other and resolve it there, then merge again.\n${`${merged.stdout ?? ''}${merged.stderr ?? ''}`.trim()}`
986
+ : `Nothing was merged: ${m.branch} doesn't merge cleanly into ${into}. Merge ${into} into ${m.branch} and resolve it there, then merge again.\n${`${merged.stdout ?? ''}${merged.stderr ?? ''}`.trim()}`,
987
+ 'conflict',
988
+ null,
989
+ onBase ? null : [m.branch],
990
+ );
974
991
  }
975
992
  // MQ-1: the checks run with installs that match the merged tree, and say whose they were.
976
993
  const installs = matchInstalls(verifyIn, installsFrom, { prepare: config.regression?.prepare });
@@ -989,7 +1006,7 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
989
1006
  'installs',
990
1007
  );
991
1008
  const lane = members.length ? null : laneOfBranch(branch, config);
992
- const { changed, found, skipped, unfinished } = verifyBranch({
1009
+ const { changed, found, skipped, unrun } = verifyBranch({
993
1010
  root: verifyIn,
994
1011
  base: into,
995
1012
  config,
@@ -997,6 +1014,9 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
997
1014
  checks: true,
998
1015
  branch,
999
1016
  });
1017
+ // MQ-7: what the merged tree's checks found is the group's; what a member's own changes break is its.
1018
+ const groupWide = Object.values(found).some((list) => list.length);
1019
+ const faulty = new Set();
1000
1020
  // MQ-7: each member is still held to its own lane's paths, for its own changes alone: measured against
1001
1021
  // the base merged with its partners, so a lane's branch built on its lead partner's (as dispatch has
1002
1022
  // it) isn't held to the partner's files. A partner built on this member already holds the member's
@@ -1027,8 +1047,10 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
1027
1047
  fixes: false,
1028
1048
  branch: b.branch,
1029
1049
  });
1030
- for (const key of Object.keys(found))
1050
+ for (const key of Object.keys(found)) {
1031
1051
  found[key].push(...held.found[key].map((p) => `${b.branch}: ${p}`));
1052
+ if (held.found[key].length) faulty.add(b.branch);
1053
+ }
1032
1054
  }
1033
1055
  const problems = [
1034
1056
  ...new Set(
@@ -1037,10 +1059,11 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
1037
1059
  ),
1038
1060
  ),
1039
1061
  ];
1040
- // #62: a check that only ran out of time says nothing against the branch: it's tried again.
1041
- if (problems.length === 1 && unfinished)
1062
+ // #62: a check that only ran out of time, or couldn't run for want of an install, says nothing against
1063
+ // the branch: it's tried again.
1064
+ if (unrun && Object.values(found).flat().length === unrun)
1042
1065
  fail(
1043
- `Nothing was merged: a check on ${branch} didn't finish, which says nothing against the branch (checked in ${verifyIn}${installedWith ? `, ${installedWith}` : ''}):\n ${problems[0]}\nIt stays at the head of the queue; merge again once fewer suites are running.`,
1066
+ `Nothing was merged: ${unrun === 1 ? 'a check' : 'checks'} on ${branch} couldn't run or didn't finish, which says nothing against the branch (checked in ${verifyIn}${installedWith ? `, ${installedWith}` : ''}):\n ${problems.join('\n ')}\nIt stays at the head of the queue; make the install work, or merge again once fewer suites are running.`,
1044
1067
  'unfinished',
1045
1068
  );
1046
1069
  if (problems.length)
@@ -1048,6 +1071,7 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
1048
1071
  `Nothing was merged. code-kit verify found ${problems.length} problem(s) on ${branch} (checked in ${verifyIn}${installedWith ? `, ${installedWith}` : ''}):\n ${problems.join('\n ')}\nSend it back to its lane, or fix it, then merge again.`,
1049
1072
  'verify',
1050
1073
  problems[0].split('\n')[0],
1074
+ members.length && !groupWide ? [...faulty] : null,
1051
1075
  );
1052
1076
  const mergeIn = checkedOut(into) ?? worktree(into, false);
1053
1077
  // Checked again now the verify is done: only changes to what the merge changes stop it.
@@ -1080,6 +1104,8 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
1080
1104
  fail(
1081
1105
  `Nothing was merged: ${b.branch} doesn't merge cleanly into ${into}. Merge ${into} into ${b.branch} and resolve it there, then merge again.\n${String(e.stderr ?? e.message).trim()}`,
1082
1106
  'conflict',
1107
+ null,
1108
+ [b.branch],
1083
1109
  );
1084
1110
  }
1085
1111
  }
@@ -1271,16 +1297,24 @@ function queue() {
1271
1297
  );
1272
1298
  }
1273
1299
  const group = [head, ...partners];
1274
- // One merge at a time: a second run while the first verifies would merge the same head twice.
1275
- if (mergingNow(head))
1300
+ // One merge at a time, whichever lead runs it: a second run while the first verifies would merge
1301
+ // onto a base the first is about to change, or merge the same head twice.
1302
+ const merging = waiting(entries).find(mergingNow);
1303
+ if (merging)
1276
1304
  die(
1277
- `${head.branch} is being merged now (queue merge, process ${head.pid}); nothing more was started. It says when it's done.`,
1305
+ `${merging.branch} is being merged now (queue merge, process ${merging.pid}); nothing more was started. It says when it's done.`,
1306
+ );
1307
+ const holder = takeMergeLock(root);
1308
+ if (holder)
1309
+ die(
1310
+ `Another queue merge (process ${holder}) has just started; nothing more was started. It says when it's done.`,
1278
1311
  );
1279
1312
  markMerging(root, head.branch, process.pid);
1280
1313
  // However this run ends without settling the head, it goes back to waiting in its place.
1281
1314
  process.on('exit', () => {
1282
1315
  if (readQueue(root).some((e) => e.branch === head.branch && e.state === 'merging'))
1283
1316
  markMerging(root, head.branch, null);
1317
+ releaseMergeLock(root);
1284
1318
  });
1285
1319
  try {
1286
1320
  for (const p of partners) {
@@ -1330,17 +1364,20 @@ function queue() {
1330
1364
  e.kind === 'conflict'
1331
1365
  ? `conflicts with ${into}${after ? ` after ${after}` : ''}`
1332
1366
  : `fails verify against ${into}${after ? ` after ${after}` : ''}${e.first ? `: ${e.first}` : ''}`;
1333
- // A group goes back together: it only lands as one.
1334
- for (const m of group) {
1367
+ // MQ-7: what's down to one member goes back alone, and its partners keep their places and their
1368
+ // reviews, waiting for its fix; what's the merged tree's as a whole sends the group back together.
1369
+ const back = e.at?.length ? group.filter((m) => e.at.includes(m.branch)) : group;
1370
+ for (const m of back) {
1335
1371
  const sha = execFileSync('git', ['rev-parse', '--verify', `${m.branch}^{commit}`], {
1336
1372
  encoding: 'utf8',
1337
1373
  }).trim();
1338
1374
  recordSentBack(root, m.branch, sha, why);
1339
1375
  settle(root, m.branch, 'sent back', why);
1340
1376
  }
1341
- const names = group.map((m) => m.branch).join(', ');
1377
+ const names = back.map((m) => m.branch).join(', ');
1378
+ const kept = group.filter((m) => !back.includes(m)).map((m) => m.branch);
1342
1379
  die(
1343
- `${e.message}\n${names} ${group.length === 1 ? 'is sent back to its lane' : 'are sent back to their lanes'} (${why}) and ${group.length === 1 ? 'leaves' : 'leave'} the queue; ${group.length === 1 ? 'its fix rejoins' : 'their fixes rejoin'} after the next review.`,
1380
+ `${e.message}\n${names} ${back.length === 1 ? 'is sent back to its lane' : 'are sent back to their lanes'} (${why}) and ${back.length === 1 ? 'leaves' : 'leave'} the queue; ${back.length === 1 ? 'its fix rejoins' : 'their fixes rejoin'} after the next review.${kept.length ? ` ${kept.join(', ')} ${kept.length === 1 ? 'keeps its' : 'keep their'} place${kept.length === 1 ? '' : 's'} and wait${kept.length === 1 ? 's' : ''} for ${back.length === 1 ? 'it' : 'them'}.` : ''}`,
1344
1381
  );
1345
1382
  }
1346
1383
  }
@@ -2040,7 +2077,11 @@ function deferCommand() {
2040
2077
  if (error) die(error);
2041
2078
  const plan = config.docs?.plan;
2042
2079
  if (plan) {
2043
- const known = new Set(readStories(root, plan).map((s) => s.id));
2080
+ // STRM-2: a stream's lead adds its stories in its own worktree's plan, which reaches the main
2081
+ // checkout only when its lead branch merges: both plans count.
2082
+ const known = new Set(
2083
+ [...new Set([resolve('.'), root])].flatMap((at) => readStories(at, plan).map((s) => s.id)),
2084
+ );
2044
2085
  const unknown = stories.map((s) => s.toUpperCase()).filter((s) => !known.has(s));
2045
2086
  if (unknown.length)
2046
2087
  die(`There is no story ${unknown.join(', ')} in ${plan}; nothing was held.`);
@@ -18,6 +18,7 @@ import {
18
18
  projectWorktrees,
19
19
  relativeTo,
20
20
  worktreeRoot,
21
+ sharedListProblem,
21
22
  subagentOf,
22
23
  writeProblem,
23
24
  } from './lib/rules.mjs';
@@ -100,6 +101,9 @@ function textAfter(before) {
100
101
  const file = join(root, rel);
101
102
  const before = existsSync(file) ? readFileSync(file, 'utf8') : null;
102
103
  const after = textAfter(before);
104
+ // STRM-6: a shared list takes additions from any lane; what's already there is its owner's.
105
+ const listed = sharedListProblem(actorFor(input, root, config), rel, before, after, config);
106
+ if (listed) block(`Blocked: ${listed}`);
103
107
  if (after !== null) {
104
108
  const existing = new Set(before === null ? [] : layerProblems(rel, before, config));
105
109
  const added = newProblems(rel, layerProblems(rel, after, config), loadBaseline(project)).filter(
@@ -272,7 +272,12 @@ export function freshCheckout(
272
272
  const emptied = installsFrom && dir === slot && installsOf(dir).length > 0 && hollow(dir);
273
273
  if (emptied)
274
274
  for (const rel of installsOf(dir)) rmSync(join(dir, rel), { recursive: true, force: true });
275
- if (installsFrom && !installsOf(dir).length) {
275
+ // Each install the source has and the checkout lacks is copied, so a checkout holding one (a service's
276
+ // virtualenv) isn't left without another (the web app's node_modules).
277
+ const lacking = installsFrom
278
+ ? installsOf(installsFrom).filter((rel) => !existsSync(join(dir, rel)))
279
+ : [];
280
+ if (lacking.length) {
276
281
  copyInstalls(installsFrom, dir);
277
282
  // The copied installs were made for installsFrom's manifests and lockfiles.
278
283
  if (dir === slot) {
@@ -73,6 +73,7 @@ function runCheck(check, files, root, actor, skipped) {
73
73
  if (check.ifMissing === 'fail')
74
74
  return {
75
75
  check: check.name,
76
+ missing: true,
76
77
  title: `${check.name}: \`${what}\` is not installed${where}. Install it so the check can run.`,
77
78
  };
78
79
  skipped.push({
@@ -70,7 +70,10 @@ export function mergeDelegation(root, config, file = CONFIG_FILE) {
70
70
  /** The rules the hooks enforce: the project's config, the kit's defaults, and any active adapters. */
71
71
  export const effectiveConfig = (raw, root) => applyAdapters(withDefaults(raw), raw, root);
72
72
 
73
- /** An anyActor entry's glob: entries are globs, or { glob, reviewer } naming who reviews changes to it. */
73
+ /**
74
+ * An anyActor entry's glob: entries are globs, or { glob, reviewer, adds } naming who reviews changes to
75
+ * it, and with `adds`, a shared list: any lane adds to it, and only its owner changes what's there.
76
+ */
74
77
  export const anyActorGlob = (e) => (typeof e === 'string' ? e : e?.glob);
75
78
 
76
79
  /**
@@ -94,6 +97,8 @@ export function withDefaults(c) {
94
97
  anyActorReviewers: (c.anyActor ?? [])
95
98
  .filter((e) => typeof e === 'object' && e?.reviewer)
96
99
  .map((e) => ({ glob: e.glob, reviewer: e.reviewer })),
100
+ // STRM-6: shared lists (an allow-list, a catalogue), which every stream's branches add entries to.
101
+ sharedLists: (c.anyActor ?? []).filter((e) => e?.adds === true).map((e) => e.glob),
97
102
  protected: [KIT, ...(c.protected ?? []).filter((p) => p.glob !== KIT.glob)],
98
103
  layers: c.layers ?? [],
99
104
  // Spec 31: the project's streams; none is one stream holding every lane.
@@ -397,9 +402,10 @@ export function validate(c) {
397
402
  isStr(e) ||
398
403
  (isObj(e) &&
399
404
  isStr(e.glob) &&
400
- (e.reviewer === undefined || e.reviewer === 'lead' || lanes.includes(e.reviewer))),
405
+ (e.reviewer === undefined || e.reviewer === 'lead' || lanes.includes(e.reviewer)) &&
406
+ (e.adds === undefined || typeof e.adds === 'boolean')),
401
407
  )),
402
- '"anyActor" must be a list of globs, or { "glob", "reviewer" } with the lead or a lane as reviewer',
408
+ '"anyActor" must be a list of globs, or { "glob", "reviewer", "adds" } with the lead or a lane as reviewer and "adds" true or false',
403
409
  );
404
410
  each('protected', (e, at) => {
405
411
  need(isStr(e.glob) && isStr(e.why), `${at} needs "glob" and "why"`);
@@ -107,7 +107,7 @@ export const SETTINGS = {
107
107
  "How many slices are in focus at once: a free lane builds an open slice's stories first, and may start the next slice only while fewer are open",
108
108
  },
109
109
  'promote.tokens': {
110
- default: 60,
110
+ default: 100,
111
111
  problem: (v) =>
112
112
  Number.isInteger(v) && v >= 20 && v <= 1000 ? null : 'must be a whole number from 20 to 1000',
113
113
  about:
@@ -161,19 +161,28 @@ export function nextStep(root, base, { stream = null, projectWide = true } = {})
161
161
  // LOOP-15, MQ-8: what the lead holds on purpose: stories, and the whole merge queue.
162
162
  let deferred = [];
163
163
  let queuePaused = null;
164
- const result = (step, why, extra = {}) => ({
165
- step,
166
- why,
167
- args: '',
168
- then: [],
169
- attention,
170
- brief: briefQuestions,
171
- worktrees: holdingTrees,
172
- models,
173
- deferred,
174
- queuePaused,
175
- ...extra,
176
- });
164
+ // STRM-3: the merge queue's head, for another stream's lead to merge when it has nothing of its own.
165
+ let idleMerge = null;
166
+ const result = (step, why, extra = {}) =>
167
+ idleMerge && ['wait', 'done'].includes(step)
168
+ ? result(
169
+ 'merge',
170
+ `${idleMerge.why} This stream has nothing of its own to do (${why.replace(/\.$/, '')}), so this lead merges it: the queue is the project's, and any lead merges its head.`,
171
+ { args: idleMerge.args },
172
+ )
173
+ : {
174
+ step,
175
+ why,
176
+ args: '',
177
+ then: [],
178
+ attention,
179
+ brief: briefQuestions,
180
+ worktrees: holdingTrees,
181
+ models,
182
+ deferred,
183
+ queuePaused,
184
+ ...extra,
185
+ };
177
186
  const isRepo = git(root, 'rev-parse', '--is-inside-work-tree') === 'true';
178
187
  if (!isRepo)
179
188
  attention.push('This folder is not a git repository yet; run `git init` before init.');
@@ -390,8 +399,8 @@ export function nextStep(root, base, { stream = null, projectWide = true } = {})
390
399
  );
391
400
  const driftNote = teamState ? driftUnchecked(teamState) : null;
392
401
  const heldByDrift = new Set(drifting.map((d) => d.branch));
393
- // MQ-1: a queue merge running now (its verify takes minutes) isn't asked for again.
394
- const merging = mergingNow(inQueue[0]) ? inQueue[0].branch : null;
402
+ // MQ-1: a queue merge running now (its verify takes minutes) isn't asked for again, by any lead.
403
+ const merging = inQueue.find(mergingNow)?.branch ?? null;
395
404
  const ids = (state) =>
396
405
  status.stories
397
406
  .filter((s) => s.state === state && !(state === 'review' && queued.includes(s.branch)))
@@ -548,12 +557,15 @@ export function nextStep(root, base, { stream = null, projectWide = true } = {})
548
557
  attention.push(
549
558
  `${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.`,
550
559
  );
560
+ const mergeWhy =
561
+ mergeHead &&
562
+ `${mergeHead.branch} passed review and is next in the merge queue${queued.length > 1 ? ` (then ${queued.filter((b) => b !== mergeHead.branch).join(', ')})` : ''}.`;
551
563
  if (mergeHead && leadMerges && !merging && !queuePaused && projectWide)
552
- return result(
553
- 'merge',
554
- `${mergeHead.branch} passed review and is next in the merge queue${queued.length > 1 ? ` (then ${queued.filter((b) => b !== mergeHead.branch).join(', ')})` : ''}.`,
555
- { args: mergeHead.branch },
556
- );
564
+ return result('merge', mergeWhy, { args: mergeHead.branch });
565
+ // STRM-3: the main checkout's lead merges first, but one queue for every stream can't wait on one
566
+ // session's own work: another stream's lead merges the head whenever its own steps are only waiting.
567
+ if (mergeHead && leadMerges && !merging && !queuePaused && !projectWide)
568
+ idleMerge = { why: mergeWhy, args: mergeHead.branch };
557
569
  // HAND-6: a screen story whose screens' approved designs are only in a design tool can't be built:
558
570
  // the gate refuses each screen until the lead hands its design over. It waits, and the hand-over is
559
571
  // the step, rather than a lane dispatched to be refused.
@@ -17,6 +17,7 @@ import {
17
17
  import { dirname, isAbsolute, join, relative } from 'node:path';
18
18
  import { CORE, hasStreams, ownerStreamOf, platformOf, streamsOf } from './streams.mjs';
19
19
  import { projectWorktrees } from './rules.mjs';
20
+ import { isTestFile } from './testfiles.mjs';
20
21
  import { EVENTS_DIR } from './events.mjs';
21
22
  import { commandsOf, wordsOf } from './shell.mjs';
22
23
 
@@ -213,7 +214,8 @@ function append(root, record) {
213
214
 
214
215
  /**
215
216
  * Every candidate: [{ id, key, kind, what, where, streams, evidence, raised, state, decided }]. `state`
216
- * is open, promoted (with the platform story that moves it) or rejected (with why).
217
+ * is open, promoted (with the platform story that moves it), rejected (with why) or withdrawn (a scan of
218
+ * the whole project no longer found it).
217
219
  */
218
220
  export function promotions(root) {
219
221
  const byId = new Map();
@@ -227,7 +229,9 @@ export function promotions(root) {
227
229
  where: [...new Set([...had.where, ...r.where])],
228
230
  streams: [...new Set([...had.streams, ...r.streams])],
229
231
  evidence: [...had.evidence, r.evidence],
230
- ...(had.state === 'rejected' && r.reopens ? { state: 'open', decided: null } : {}),
232
+ ...(['rejected', 'withdrawn'].includes(had.state) && r.reopens
233
+ ? { state: 'open', decided: null }
234
+ : {}),
231
235
  });
232
236
  if (r.kind === 'decide')
233
237
  byId.set(r.id, {
@@ -264,7 +268,9 @@ export function raise(root, { key, signal, what, where, streams, evidence }, now
264
268
  }
265
269
  const newWhere = where.filter((w) => !had.where.includes(w));
266
270
  const newStreams = streams.filter((s) => !had.streams.includes(s));
267
- if (!newWhere.length && !newStreams.length) return null;
271
+ // A withdrawn clone found again is open again, as it was.
272
+ const back = had.state === 'withdrawn';
273
+ if (!newWhere.length && !newStreams.length && !back) return null;
268
274
  if (had.state === 'promoted') return null;
269
275
  append(root, {
270
276
  kind: 'evidence',
@@ -273,7 +279,7 @@ export function raise(root, { key, signal, what, where, streams, evidence }, now
273
279
  where: newWhere,
274
280
  streams: newStreams,
275
281
  evidence,
276
- reopens: had.state === 'rejected',
282
+ reopens: had.state === 'rejected' || back,
277
283
  });
278
284
  return promotions(root).find((p) => p.id === had.id);
279
285
  }
@@ -301,13 +307,16 @@ export function decidePromotion(
301
307
 
302
308
  const sideWords = (s) => `${s.file}:${s.from}-${s.to}${s.symbol ? ` (${s.symbol})` : ''}`;
303
309
 
304
- /** PROM-1: a clone between two owners as a candidate's fields. */
310
+ /**
311
+ * PROM-1: a clone between two owners as a candidate's fields. One candidate per pair of files: every run
312
+ * the two share is the same decision, and a symbol or line that moves doesn't make a new one.
313
+ */
305
314
  export function cloneCandidate(clone) {
306
315
  const sides = [clone.a, clone.b].sort((x, y) => x.file.localeCompare(y.file));
307
316
  const core = sides.find((s) => s.owner === CORE);
308
317
  const streams = [...new Set(sides.map((s) => s.owner).filter((o) => o !== CORE))];
309
318
  return {
310
- key: `clone:${sides.map((s) => `${s.file}#${s.symbol ?? s.from}`).join('|')}`,
319
+ key: `clone:${sides.map((s) => s.file).join('|')}`,
311
320
  signal: core ? 'core-clone' : 'clone',
312
321
  what: core
313
322
  ? `${sideWords(sides.find((s) => s !== core))} repeats core's ${sideWords(core)}`
@@ -446,8 +455,9 @@ export function scanForPromotions(
446
455
  { files = null, ref = null, tokens = null, record = true, now = Date.now() } = {},
447
456
  ) {
448
457
  if (!hasStreams(config)) return { raised: [], clones: [] };
449
- const size = tokens ?? config.harness?.promote?.tokens ?? 60;
450
- const owner = (f) => ownerStreamOf(config, f);
458
+ const size = tokens ?? config.harness?.promote?.tokens ?? 100;
459
+ // Tests repeat their setup by nature: a test alike in two streams says nothing about where code belongs.
460
+ const owner = (f) => (isTestFile(f, config) ? null : ownerStreamOf(config, f));
451
461
  const sources = trackedSources(root, ref).filter((f) => owner(f));
452
462
  const texts = ref ? textsAt(root, ref, sources) : {};
453
463
  if (!ref)
@@ -462,10 +472,31 @@ export function scanForPromotions(
462
472
  let clones = findClones(texts, owner, { tokens: size });
463
473
  if (files) clones = clones.filter((c) => files.includes(c.a.file) || files.includes(c.b.file));
464
474
  const raised = [];
465
- for (const c of record ? clones : []) {
466
- const r = raise(root, cloneCandidate(c), now);
475
+ // One candidate per pair of files, from its longest run (findClones lists those first).
476
+ const byPair = new Map();
477
+ for (const c of clones) {
478
+ const candidate = cloneCandidate(c);
479
+ if (!byPair.has(candidate.key)) byPair.set(candidate.key, candidate);
480
+ }
481
+ for (const candidate of record ? byPair.values() : []) {
482
+ const r = raise(root, candidate, now);
467
483
  if (r) raised.push(r);
468
484
  }
485
+ // A whole-project scan withdraws the open clones it no longer finds: the code changed, or the rules
486
+ // did (a bigger promote.tokens, tests left out), so nobody decides what isn't there.
487
+ if (!files && record) {
488
+ const found = new Set(byPair.keys());
489
+ for (const p of promotions(root))
490
+ if (p.state === 'open' && ['clone', 'core-clone'].includes(p.kind) && !found.has(p.key))
491
+ append(root, {
492
+ kind: 'decide',
493
+ id: p.id,
494
+ at: new Date(now).toISOString(),
495
+ how: 'withdrawn',
496
+ reason: 'no longer found by a scan of the whole project',
497
+ by: null,
498
+ });
499
+ }
469
500
  if (!files && record)
470
501
  for (const copy of copiesIn(root, config)) {
471
502
  const r = raise(
@@ -548,7 +579,11 @@ export function repeatedRequirements(config, requirements, story, { at = 0.6 } =
548
579
  return out.sort((x, y) => y.score - x.score);
549
580
  }
550
581
 
551
- /** PROM-2: whose a candidate is to decide: the platform's lead, or, without one, a stream it touches. */
582
+ /**
583
+ * PROM-2: whose a candidate is to decide: the platform's lead, or, without one, a stream it touches. A
584
+ * module's code that repeats core is its own stream's to switch to core's: core has it already.
585
+ */
552
586
  export function decidedFor(config, candidate) {
587
+ if (candidate.kind === 'core-clone' && candidate.streams[0]) return candidate.streams[0];
553
588
  return platformOf(config)?.name ?? candidate.streams[0] ?? null;
554
589
  }
@@ -108,15 +108,53 @@ export function markMerging(root, branch, pid, now = Date.now()) {
108
108
  );
109
109
  }
110
110
 
111
- /** Whether `entry` is being merged now: marked merging by a process still running. */
112
- export function mergingNow(entry) {
113
- if (entry?.state !== 'merging' || !entry.pid) return false;
111
+ const LOCK_FILE = '.claude/state/merge-queue.lock';
112
+ const lockFile = (root) => join(projectWorktrees(root)[0] ?? root, LOCK_FILE);
113
+ const running = (pid) => {
114
114
  try {
115
- process.kill(entry.pid, 0);
115
+ process.kill(pid, 0);
116
116
  return true;
117
117
  } catch (e) {
118
118
  return e.code === 'EPERM';
119
119
  }
120
+ };
121
+
122
+ /**
123
+ * MQ-1: one merge at a time, whichever lead runs it: takes the queue's lock for process `pid`, created
124
+ * only if no other holds it, so two leads that read the same head can't both merge it. A lock whose
125
+ * process has ended is taken over. Returns null when taken, else the pid that holds it.
126
+ */
127
+ export function takeMergeLock(root, pid = process.pid) {
128
+ const file = lockFile(root);
129
+ mkdirSync(dirname(file), { recursive: true });
130
+ for (let tries = 0; tries < 2; tries++) {
131
+ try {
132
+ writeFileSync(file, String(pid), { flag: 'wx' });
133
+ return null;
134
+ } catch (e) {
135
+ if (e.code !== 'EEXIST') throw e;
136
+ const holder = Number(readFileSync(file, 'utf8')) || null;
137
+ if (holder && holder !== pid && running(holder)) return holder;
138
+ rmSync(file, { force: true });
139
+ }
140
+ }
141
+ return Number(readFileSync(file, 'utf8')) || null;
142
+ }
143
+
144
+ /** Gives the queue's lock back, if `pid` holds it. */
145
+ export function releaseMergeLock(root, pid = process.pid) {
146
+ const file = lockFile(root);
147
+ try {
148
+ if (Number(readFileSync(file, 'utf8')) === pid) rmSync(file, { force: true });
149
+ } catch {
150
+ // no lock
151
+ }
152
+ }
153
+
154
+ /** Whether `entry` is being merged now: marked merging by a process still running. */
155
+ export function mergingNow(entry) {
156
+ if (entry?.state !== 'merging' || !entry.pid) return false;
157
+ return running(entry.pid);
120
158
  }
121
159
 
122
160
  /**
@@ -6,7 +6,7 @@
6
6
  import { execFileSync, spawnSync } from 'node:child_process';
7
7
  import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
8
8
  import { join } from 'node:path';
9
- import { freshCheckout } from './checkout.mjs';
9
+ import { freshCheckout, matchInstalls } from './checkout.mjs';
10
10
  import { runChecks } from './checks.mjs';
11
11
  import { isDependencyFile } from './dependencies.mjs';
12
12
  import { literalPath, matchesAny } from './glob.mjs';
@@ -250,7 +250,9 @@ export const onlyUnreachable = (output) =>
250
250
  * REG-1: whether a fix's tests prove its bug. In a fresh checkout of the branch (with its own copy of
251
251
  * the checkout's installs, after `regression.prepare`), the checks covering the tests it added
252
252
  * or changed must pass, and fail, as the bug, once every change outside the tests goes back to `from`.
253
- * Problems in words; empty when the tests prove it.
253
+ * Problems in words; empty when the tests prove it. Where the tests couldn't run at all (an install that
254
+ * failed, a tool that isn't installed, a run out of time), the list is marked `unrun`: that says nothing
255
+ * against the branch, so the queue keeps it in its place.
254
256
  */
255
257
  export function proveFix({ root, from, base, changed, config, fix }) {
256
258
  const what = `This branch fixes ${fix.what}${fix.story ? ` (${fix.story})` : ''}`;
@@ -292,17 +294,30 @@ export function proveFix({ root, from, base, changed, config, fix }) {
292
294
  ];
293
295
  const strict = { ...config, checks: covering.map((c) => ({ ...c, ifMissing: 'fail' })) };
294
296
  const actor = { kind: 'lead', label: 'lead' };
297
+ const unrun = (text) => Object.assign([text], { unrun: true });
295
298
  const { dir, remove } = freshCheckout(root, 'HEAD', { prefix: 'code-kit-fix-' });
296
299
  try {
297
300
  const prepare = config.regression?.prepare;
298
301
  if (prepare) {
299
302
  const res = spawnSync(prepare, { cwd: dir, shell: true, encoding: 'utf8', timeout: 900000 });
300
303
  if (res.status !== 0)
301
- return [
304
+ return unrun(
302
305
  `regression.prepare (\`${prepare}\`) failed in a fresh checkout, so the fix's tests couldn't run:\n${`${res.stdout ?? ''}${res.stderr ?? ''}`.trim().split('\n').slice(-20).join('\n')}`,
303
- ];
306
+ );
307
+ } else {
308
+ // As the queue's merged tree does (MQ-1): the installs copied in match this tree, or the
309
+ // project's install runs here first.
310
+ const installs = matchInstalls(dir, root);
311
+ if (installs.failed)
312
+ return unrun(
313
+ `The fix's tests couldn't run: the fresh checkout's ${installs.differ.join(', ')} differ from the installs copied in, and \`${installs.command}\` ${installs.failed.missing ? "isn't installed here" : 'failed'} there${installs.failed.output ? `:\n${installs.failed.output}` : '.'}`,
314
+ );
304
315
  }
305
316
  const { failure: withFix } = runChecks(strict, tests, dir, actor);
317
+ if (withFix?.missing || withFix?.timedOut)
318
+ return unrun(
319
+ `${what}, but its tests couldn't run in a fresh checkout of the branch (${withFix.title.replace(/ Fix it:$/, '')}). That says nothing against the fix; if they need an install or a build first, set regression.prepare.`,
320
+ );
306
321
  if (withFix)
307
322
  return [
308
323
  `${what}, but its tests don't pass in a fresh checkout of the branch (${withFix.title.replace(/ Fix it:$/, '')}), so code-kit can't tell whether they fail without it. If they need an install or a build first, set regression.prepare.${withFix.output ? `\n${String(withFix.output).trim().split('\n').slice(-20).join('\n')}` : ''}`,
@@ -929,6 +929,38 @@ export function laneOf(rel, config) {
929
929
  );
930
930
  }
931
931
 
932
+ /** The first line of `before` that `after` doesn't keep, in order, or null when it keeps them all. */
933
+ export function droppedLine(before, after) {
934
+ const kept = (after ?? '').split('\n');
935
+ let at = 0;
936
+ for (const line of (before ?? '').split('\n')) {
937
+ while (at < kept.length && kept[at] !== line) at++;
938
+ if (at === kept.length) return line;
939
+ at++;
940
+ }
941
+ return null;
942
+ }
943
+
944
+ /**
945
+ * STRM-6: why a change to a shared list (an anyActor entry with `adds`) is refused, or null. Any lane
946
+ * adds entries to one in its own branch, where its story needs them, with no change request to core;
947
+ * changing or removing an entry already there is its owner's, as with any file it owns.
948
+ */
949
+ export function sharedListProblem(actor, rel, before, after, config) {
950
+ if (!matchesAny(rel, config.sharedLists ?? []) || before === null || after === null) return null;
951
+ if (
952
+ actor.kind === 'lane' &&
953
+ matchesAny(rel, config.lanes[actor.lane]?.paths ?? []) &&
954
+ !matchesAny(rel, config.lanes[actor.lane]?.exclude ?? [])
955
+ )
956
+ return null;
957
+ if (actor.kind === 'lead' && matchesAny(rel, config.lead.paths)) return null;
958
+ const dropped = droppedLine(before, after);
959
+ if (dropped === null) return null;
960
+ const owner = ownerOf(rel, config) ?? 'its owner';
961
+ return `${rel} is a shared list: any lane adds entries to it, and only ${owner} changes or removes what's already there. This change ${dropped.trim() ? `changes or removes the line \`${dropped.trim().slice(0, 120)}\`` : 'takes out a blank line'}. Keep every existing line as it is and add yours beside them; if an existing entry must change, ask for it as a change to core.`;
962
+ }
963
+
932
964
  /** Who reviews a change to a file any agent may write: the lane an anyActor entry names, else the lead. Null for other files. */
933
965
  export function reviewerOf(rel, config) {
934
966
  if (!matchesAny(rel, config.anyActor)) return null;