@warren-dean/code-kit 0.36.1 → 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,
@@ -241,9 +243,12 @@ import {
241
243
  coreRequests,
242
244
  decideCore,
243
245
  hasStreams,
246
+ heldByCore,
247
+ lastTurn,
244
248
  platformOf,
245
249
  sharedLanes,
246
250
  streamOfLane,
251
+ streamOfStory,
247
252
  streamsOf,
248
253
  } from '../hooks/lib/streams.mjs';
249
254
  import {
@@ -388,6 +393,8 @@ function checkJson() {
388
393
  Object.entries(config.lanes).map(([name, l]) => [name, { agent: l.agent, paths: l.paths }]),
389
394
  );
390
395
  report.adapters = config.adapters;
396
+ // STRM-10: the streams' names, for the mod to read them.
397
+ report.streams = streamsOf(config).map((st) => st.name);
391
398
  report.docs = config.docs ?? null;
392
399
  report.harness = config.harness;
393
400
  // The harness settings the person chose, as opposed to defaults: the mod asks before acting on autonomy.
@@ -702,11 +709,15 @@ const firstMerge = (into, branch) =>
702
709
 
703
710
  /** Why a merge didn't happen: `refused` (the rules), `verify` (problems) or `conflict`. */
704
711
  class MergeStop extends Error {
705
- /** `first`: the first problem's first line, for the queue's record of why (#57). */
706
- 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) {
707
717
  super(message);
708
718
  this.kind = kind;
709
719
  this.first = first;
720
+ this.at = at;
710
721
  }
711
722
  }
712
723
 
@@ -888,9 +899,9 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
888
899
  }
889
900
  }
890
901
  };
891
- const fail = (message, kind = 'refused', first = null) => {
902
+ const fail = (message, kind = 'refused', first = null, at = null) => {
892
903
  cleanup();
893
- throw new MergeStop(kind, message, first);
904
+ throw new MergeStop(kind, message, first, at);
894
905
  };
895
906
  const worktree = (b, detach) => {
896
907
  const dir = mkdtempSync(join(tmpdir(), 'code-kit-merge-'));
@@ -938,6 +949,8 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
938
949
  fail(
939
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}`,
940
951
  'conflict',
952
+ null,
953
+ [branch],
941
954
  );
942
955
  }
943
956
  temporary.push(fresh);
@@ -961,11 +974,20 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
961
974
  );
962
975
  for (const m of members) {
963
976
  const merged = mergeInto(verifyIn, m.tip);
964
- if (merged.status !== 0)
965
- fail(
966
- `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()}`,
967
- 'conflict',
968
- );
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
+ );
969
991
  }
970
992
  // MQ-1: the checks run with installs that match the merged tree, and say whose they were.
971
993
  const installs = matchInstalls(verifyIn, installsFrom, { prepare: config.regression?.prepare });
@@ -984,7 +1006,7 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
984
1006
  'installs',
985
1007
  );
986
1008
  const lane = members.length ? null : laneOfBranch(branch, config);
987
- const { changed, found, skipped, unfinished } = verifyBranch({
1009
+ const { changed, found, skipped, unrun } = verifyBranch({
988
1010
  root: verifyIn,
989
1011
  base: into,
990
1012
  config,
@@ -992,6 +1014,9 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
992
1014
  checks: true,
993
1015
  branch,
994
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();
995
1020
  // MQ-7: each member is still held to its own lane's paths, for its own changes alone: measured against
996
1021
  // the base merged with its partners, so a lane's branch built on its lead partner's (as dispatch has
997
1022
  // it) isn't held to the partner's files. A partner built on this member already holds the member's
@@ -1022,8 +1047,10 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
1022
1047
  fixes: false,
1023
1048
  branch: b.branch,
1024
1049
  });
1025
- for (const key of Object.keys(found))
1050
+ for (const key of Object.keys(found)) {
1026
1051
  found[key].push(...held.found[key].map((p) => `${b.branch}: ${p}`));
1052
+ if (held.found[key].length) faulty.add(b.branch);
1053
+ }
1027
1054
  }
1028
1055
  const problems = [
1029
1056
  ...new Set(
@@ -1032,10 +1059,11 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
1032
1059
  ),
1033
1060
  ),
1034
1061
  ];
1035
- // #62: a check that only ran out of time says nothing against the branch: it's tried again.
1036
- 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)
1037
1065
  fail(
1038
- `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.`,
1039
1067
  'unfinished',
1040
1068
  );
1041
1069
  if (problems.length)
@@ -1043,6 +1071,7 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
1043
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.`,
1044
1072
  'verify',
1045
1073
  problems[0].split('\n')[0],
1074
+ members.length && !groupWide ? [...faulty] : null,
1046
1075
  );
1047
1076
  const mergeIn = checkedOut(into) ?? worktree(into, false);
1048
1077
  // Checked again now the verify is done: only changes to what the merge changes stop it.
@@ -1075,6 +1104,8 @@ function mergeOne(branch, config, byPerson, commit = null, partners = []) {
1075
1104
  fail(
1076
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()}`,
1077
1106
  'conflict',
1107
+ null,
1108
+ [b.branch],
1078
1109
  );
1079
1110
  }
1080
1111
  }
@@ -1266,16 +1297,24 @@ function queue() {
1266
1297
  );
1267
1298
  }
1268
1299
  const group = [head, ...partners];
1269
- // One merge at a time: a second run while the first verifies would merge the same head twice.
1270
- 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)
1304
+ die(
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)
1271
1309
  die(
1272
- `${head.branch} is being merged now (queue merge, process ${head.pid}); nothing more was started. It says when it's done.`,
1310
+ `Another queue merge (process ${holder}) has just started; nothing more was started. It says when it's done.`,
1273
1311
  );
1274
1312
  markMerging(root, head.branch, process.pid);
1275
1313
  // However this run ends without settling the head, it goes back to waiting in its place.
1276
1314
  process.on('exit', () => {
1277
1315
  if (readQueue(root).some((e) => e.branch === head.branch && e.state === 'merging'))
1278
1316
  markMerging(root, head.branch, null);
1317
+ releaseMergeLock(root);
1279
1318
  });
1280
1319
  try {
1281
1320
  for (const p of partners) {
@@ -1325,17 +1364,20 @@ function queue() {
1325
1364
  e.kind === 'conflict'
1326
1365
  ? `conflicts with ${into}${after ? ` after ${after}` : ''}`
1327
1366
  : `fails verify against ${into}${after ? ` after ${after}` : ''}${e.first ? `: ${e.first}` : ''}`;
1328
- // A group goes back together: it only lands as one.
1329
- 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) {
1330
1371
  const sha = execFileSync('git', ['rev-parse', '--verify', `${m.branch}^{commit}`], {
1331
1372
  encoding: 'utf8',
1332
1373
  }).trim();
1333
1374
  recordSentBack(root, m.branch, sha, why);
1334
1375
  settle(root, m.branch, 'sent back', why);
1335
1376
  }
1336
- 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);
1337
1379
  die(
1338
- `${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'}.` : ''}`,
1339
1381
  );
1340
1382
  }
1341
1383
  }
@@ -2035,7 +2077,11 @@ function deferCommand() {
2035
2077
  if (error) die(error);
2036
2078
  const plan = config.docs?.plan;
2037
2079
  if (plan) {
2038
- 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
+ );
2039
2085
  const unknown = stories.map((s) => s.toUpperCase()).filter((s) => !known.has(s));
2040
2086
  if (unknown.length)
2041
2087
  die(`There is no story ${unknown.join(', ')} in ${plan}; nothing was held.`);
@@ -3652,8 +3698,21 @@ function streamsCommand() {
3652
3698
  const root = resolve('.');
3653
3699
  const all = streamsOf(config);
3654
3700
  const claims = laneClaims(root);
3701
+ // STRM-10: what the mod scopes a stream lead's views to: each stream's stories and requirements, the
3702
+ // stories core holds, and what it asks of core or has to decide for it.
3703
+ const stories = config.docs?.plan ? readStories(root, config.docs.plan) : [];
3704
+ const requirements = config.docs?.specs ? readRequirements(root, config.docs.specs) : [];
3705
+ const held = heldByCore(
3706
+ root,
3707
+ stories.map((s) => ({ id: s.id, state: s.status })),
3708
+ );
3709
+ const asks = coreRequests(root).filter((r) => r.state === 'open');
3710
+ const candidates = promotions(root).filter((c) => c.state === 'open');
3711
+ const me = callerSession ? readSession(root, callerSession) : null;
3655
3712
  const rows = all.map((st) => {
3656
3713
  const lead = leadSession(root, Date.now(), st.name);
3714
+ const own = stories.filter((s) => streamOfStory(config, s) === st.name);
3715
+ const cited = new Set(own.flatMap((s) => s.requirements));
3657
3716
  return {
3658
3717
  ...st,
3659
3718
  lead: lead
@@ -3662,13 +3721,28 @@ function streamsCommand() {
3662
3721
  running: claims
3663
3722
  .filter((c) => st.lanes.includes(c.lane))
3664
3723
  .map((c) => ({ lane: c.lane, story: c.story })),
3724
+ stories: own.map((s) => s.id),
3725
+ requirements: requirements
3726
+ .filter((r) => cited.has(r.id) || (st.specs.length && matchesAny(r.file, st.specs)))
3727
+ .map((r) => r.id),
3728
+ held: own.filter((s) => held.has(s.id)).map((s) => ({ story: s.id, why: held.get(s.id) })),
3729
+ asks: asks.filter((r) => r.stream === st.name).map((r) => r.id),
3730
+ toDecide: [
3731
+ ...(st.platform ? asks.map((r) => r.id) : []),
3732
+ ...candidates.filter((c) => decidedFor(config, c) === st.name).map((c) => c.id),
3733
+ ],
3665
3734
  };
3666
3735
  });
3667
3736
  const shared = sharedLanes(config).map((lane) => ({
3668
3737
  lane,
3669
3738
  running: claims.find((c) => c.lane === lane)?.story ?? null,
3739
+ turn: lastTurn(root, lane),
3670
3740
  }));
3671
- if (flag('--json')) return out(JSON.stringify({ streams: rows, shared }, null, 2));
3741
+ // The streams this session leads, or, a working session's, the one it works in.
3742
+ const mine = rows
3743
+ .filter((r) => (callerSession && r.lead?.session === callerSession) || r.name === me?.stream)
3744
+ .map((r) => r.name);
3745
+ if (flag('--json')) return out(JSON.stringify({ streams: rows, shared, mine }, null, 2));
3672
3746
  if (!all.length)
3673
3747
  return out(
3674
3748
  "This project isn't built as streams: one lead runs every lane. docs/specs/31-streams-and-platform.md says how to add them.",
@@ -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
  /**