@warren-dean/code-kit 0.25.0 → 0.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +24 -0
  4. package/README.md +70 -59
  5. package/bin/code-kit.mjs +113 -3
  6. package/hooks/act.mjs +96 -0
  7. package/hooks/guard-bash.mjs +27 -1
  8. package/hooks/hooks.json +42 -1
  9. package/hooks/lib/config.mjs +26 -2
  10. package/hooks/lib/events.mjs +626 -0
  11. package/hooks/lib/harness.mjs +8 -0
  12. package/hooks/lib/instructions.mjs +1 -1
  13. package/hooks/lib/next.mjs +22 -0
  14. package/hooks/lib/plan.mjs +45 -9
  15. package/hooks/lib/publish.mjs +144 -0
  16. package/hooks/lib/queue.mjs +36 -0
  17. package/hooks/lib/snapshot.mjs +33 -3
  18. package/hooks/lib/storymap.mjs +54 -17
  19. package/hooks/lib/timeline.mjs +94 -44
  20. package/hooks/lib/worktrees.mjs +19 -11
  21. package/hooks/mod/art.mjs +0 -81
  22. package/hooks/mod/board.mjs +1743 -0
  23. package/hooks/mod/characters.mjs +516 -0
  24. package/hooks/mod/loop.mjs +17 -3
  25. package/hooks/mod/mission.mjs +2 -93
  26. package/hooks/mod/panes.mjs +111 -422
  27. package/hooks/mod/register.mjs +482 -184
  28. package/hooks/mod/surface/character.mjs +79 -0
  29. package/hooks/mod/surface/clock.mjs +37 -0
  30. package/hooks/mod/surface/rail.mjs +33 -0
  31. package/hooks/mod/view.mjs +30 -602
  32. package/hooks/session.mjs +14 -11
  33. package/package.json +2 -1
  34. package/skills/decide/SKILL.md +1 -1
  35. package/skills/dispatch/SKILL.md +1 -1
  36. package/skills/init/SKILL.md +1 -1
  37. package/skills/next/SKILL.md +1 -1
  38. package/skills/review/SKILL.md +1 -1
  39. package/templates/agent.md +1 -1
  40. package/templates/claude-md.md +2 -2
  41. package/hooks/mod/register.test.ts +0 -4932
@@ -51,9 +51,23 @@ const field = (lines, name) =>
51
51
  .find(Boolean)?.[1]
52
52
  .trim() ?? '';
53
53
 
54
+ /** The first paragraph under a story's `**Field:**` lines, on one line, or null. */
55
+ function summaryOf(body) {
56
+ const start = body.findIndex((l) => l.trim() && !/^\*\*[^*]+:\*\*/.test(l.trim()));
57
+ if (start < 0) return null;
58
+ const end = body.findIndex((l, j) => j > start && !l.trim());
59
+ const text = body
60
+ .slice(start, end < 0 ? undefined : end)
61
+ .join(' ')
62
+ .replace(/\s+/g, ' ')
63
+ .trim();
64
+ return text || null;
65
+ }
66
+
54
67
  /**
55
- * Every story in the plan: [{ id, title, lane, requirements, dependsOn, status, fixes, milestone, slice, size }]. A story's
56
- * milestone is the `## ` heading above it ("Milestone 5: Your own key"), or null.
68
+ * Every story in the plan: [{ id, title, lane, requirements, dependsOn, status, fixes, milestone, slice, size, summary }].
69
+ * A story's milestone is the `## ` heading above it ("Milestone 5: Your own key"), or null; its summary the
70
+ * paragraph under its fields, on one line, or null.
57
71
  */
58
72
  export function readStories(root, planPath) {
59
73
  const stories = [];
@@ -87,6 +101,7 @@ export function readStories(root, planPath) {
87
101
  field(body, 'Size')
88
102
  .toLowerCase()
89
103
  .match(/^(small|medium|large)\b/)?.[1] ?? null,
104
+ summary: summaryOf(body),
90
105
  });
91
106
  });
92
107
  }
@@ -146,11 +161,28 @@ const git = (root, ...args) => {
146
161
  }
147
162
  };
148
163
 
164
+ /**
165
+ * Every local and origin branch in one read: { ref: { sha, ahead } }, `ahead` the commits it has that
166
+ * `base` hasn't. A project with hundreds of stories has hundreds of branches, and a git call each
167
+ * outlasts the mod's 30 seconds. Git before 2.41 can't count them in the same read: `ahead` is then
168
+ * null, and counted for each branch as it's asked for.
169
+ */
170
+ export function branchRefs(root, base) {
171
+ const read = (format) =>
172
+ git(root, 'for-each-ref', `--format=${format}`, 'refs/heads', 'refs/remotes/origin');
173
+ const counted = read(`%(refname)%09%(objectname)%09%(ahead-behind:${base})`);
174
+ const out = counted ?? read('%(refname)%09%(objectname)') ?? '';
175
+ const refs = {};
176
+ for (const line of out.split('\n').filter(Boolean)) {
177
+ const [ref, sha, aheadBehind] = line.split('\t');
178
+ refs[ref] = { sha, ahead: counted === null ? null : Number(aheadBehind.split(' ')[0]) };
179
+ }
180
+ return refs;
181
+ }
182
+
149
183
  /** The ref of a branch, local first, then on origin; null when neither exists. */
150
- function branchRef(root, branch) {
151
- for (const ref of [`refs/heads/${branch}`, `refs/remotes/origin/${branch}`])
152
- if (git(root, 'rev-parse', '--verify', '--quiet', ref) !== null) return ref;
153
- return null;
184
+ function branchRef(refs, branch) {
185
+ return [`refs/heads/${branch}`, `refs/remotes/origin/${branch}`].find((r) => refs[r]) ?? null;
154
186
  }
155
187
 
156
188
  /** Worktree path of each checked-out branch: { branch: path }. */
@@ -178,13 +210,16 @@ function worktrees(root) {
178
210
  export function storyStates(root, stories, base) {
179
211
  const trees = worktrees(root);
180
212
  const backs = sentBack(root);
213
+ const refs = branchRefs(root, base);
181
214
  const states = stories.map((story) => {
182
215
  const branch = storyBranch(story);
183
- const ref = story.lane ? branchRef(root, branch) : null;
184
- const ahead = ref ? Number(git(root, 'rev-list', '--count', `${base}..${ref}`) ?? 0) : 0;
216
+ const ref = story.lane ? branchRef(refs, branch) : null;
217
+ const ahead = !ref
218
+ ? 0
219
+ : (refs[ref].ahead ?? Number(git(root, 'rev-list', '--count', `${base}..${ref}`) ?? 0));
185
220
  let state = story.status;
186
221
  if (ref && story.status !== 'done') {
187
- const head = backs[branch] ? git(root, 'rev-parse', ref) : null;
222
+ const head = backs[branch] ? refs[ref].sha : null;
188
223
  if (head && head === backs[branch].sha) state = 'sent back';
189
224
  else if (ahead > 0) state = 'review';
190
225
  else state = story.status === 'review' ? 'done' : 'in progress';
@@ -193,6 +228,7 @@ export function storyStates(root, stories, base) {
193
228
  ...story,
194
229
  branch,
195
230
  branchExists: Boolean(ref),
231
+ ref,
196
232
  ahead,
197
233
  worktree: trees[branch] ?? null,
198
234
  state,
@@ -0,0 +1,144 @@
1
+ // What the merge queue merged into the local base, put onto the remote's (spec 09, MQ-6). code-kit
2
+ // merges each story locally, verified; where the base only moves through pull requests (GitHub's branch
3
+ // protection), pushing it is refused, so the merged work goes up as a pull request of its own instead of
4
+ // through an integration branch an agent invents. `branches.publish` says which: `push` or `pull-request`.
5
+ import { spawnSync } from 'node:child_process';
6
+
7
+ /** The branch a pull request publishes the base from: code-kit's own, short-lived. */
8
+ export const publishBranch = (base) => `code-kit/publish-${base}`;
9
+
10
+ /** The stories merged into `base` and not yet on the remote's, from the queue's merge commits. */
11
+ export function unpublished(root, base) {
12
+ const run = spawnSync(
13
+ 'git',
14
+ ['-C', root, 'log', '--first-parent', '--format=%s', `origin/${base}..${base}`],
15
+ { encoding: 'utf8' },
16
+ );
17
+ if (run.status !== 0) return [];
18
+ return run.stdout
19
+ .split('\n')
20
+ .map((s) => s.match(/^Merge (\S+) into /)?.[1])
21
+ .filter(Boolean)
22
+ .reverse();
23
+ }
24
+
25
+ /** A pull request's title for the branches it publishes. */
26
+ export function publishTitle(branches, count) {
27
+ const stories = branches.map((b) => b.split('/').at(-1).toUpperCase());
28
+ if (!stories.length) return `${count} commit${count === 1 ? '' : 's'} from code-kit`;
29
+ if (stories.length === 1) return `${stories[0]} from the code-kit queue`;
30
+ if (stories.length <= 3)
31
+ return `${stories.slice(0, -1).join(', ')} and ${stories.at(-1)} from the code-kit queue`;
32
+ return `${stories.slice(0, 2).join(', ')} and ${stories.length - 2} more from the code-kit queue`;
33
+ }
34
+
35
+ /**
36
+ * Puts the local `base`'s merges onto `origin`. `mode` is `push` or `pull-request`; `mayMove` is whether
37
+ * the caller may move the remote's base (the person, or the lead with `approvals.delegate.push`): without
38
+ * it a push isn't made and a pull request is opened but left for the person. `run(cmd, args)` runs git
39
+ * and gh (spawnSync by default, so tests can stand in for GitHub). Returns { state, message, url? }:
40
+ * `current` (nothing to publish), `pushed`, `merged`, `waiting` (for the person, or for GitHub) or
41
+ * `failed`. It never force-pushes, and never moves the local base except to fast-forward it.
42
+ */
43
+ export function publishBase(root, base, { mode, mayMove, run = defaultRun }) {
44
+ const git = (...a) => run('git', ['-C', root, ...a]);
45
+ const gh = (...a) => run('gh', a);
46
+ const first = (r) =>
47
+ String(r.stderr || r.stdout || '')
48
+ .trim()
49
+ .split('\n')[0];
50
+ if (git('remote', 'get-url', 'origin').status !== 0)
51
+ return { state: 'failed', message: `There's no remote named origin to publish ${base} to.` };
52
+ git('fetch', '--quiet', 'origin', base);
53
+ const remote = `origin/${base}`;
54
+ if (git('rev-parse', '--verify', '--quiet', remote).status !== 0)
55
+ return {
56
+ state: 'failed',
57
+ message: `origin has no ${base} yet. Push it once yourself (\`git push -u origin ${base}\`); code-kit publishes merges onto it from then on.`,
58
+ };
59
+ const ahead = Number(git('rev-list', '--count', `${remote}..${base}`).stdout.trim() || 0);
60
+ if (!ahead) return { state: 'current', message: `${base} is already on origin.` };
61
+ const branches = unpublished(root, base);
62
+ const what = branches.length
63
+ ? `${branches.length} merge${branches.length === 1 ? '' : 's'} (${branches.join(', ')})`
64
+ : `${ahead} commit${ahead === 1 ? '' : 's'}`;
65
+
66
+ if (mode === 'push') {
67
+ if (!mayMove)
68
+ return {
69
+ state: 'waiting',
70
+ message: `${what} on ${base} aren't on origin yet. Pushing ${base} is the person's, unless they delegate it ("approvals.delegate.push": true, committed on ${base}).`,
71
+ };
72
+ const pushed = git('push', '--quiet', 'origin', `${base}:refs/heads/${base}`);
73
+ if (pushed.status !== 0)
74
+ return {
75
+ state: 'failed',
76
+ message: `Pushing ${base} was refused: ${first(pushed)}${/protected|GH006|pull request/i.test(`${pushed.stderr}`) ? `\norigin requires a pull request to change ${base}: set "branches.publish": "pull-request" in the config.` : ''}`,
77
+ };
78
+ return { state: 'pushed', message: `Pushed ${base} to origin with ${what}.` };
79
+ }
80
+
81
+ // A pull request from code-kit's own branch, which holds exactly the base's head.
82
+ const head = publishBranch(base);
83
+ const pushed = git('push', '--quiet', 'origin', `${base}:refs/heads/${head}`);
84
+ if (pushed.status !== 0)
85
+ return {
86
+ state: 'failed',
87
+ message: `Pushing ${base} to ${head} for its pull request was refused: ${first(pushed)}`,
88
+ };
89
+ const open = gh(
90
+ ...['pr', 'list', '--head', head, '--base', base, '--state', 'open'],
91
+ ...['--json', 'url', '--jq', '.[0].url'],
92
+ );
93
+ let url = open.status === 0 ? open.stdout.trim() : '';
94
+ if (!url) {
95
+ const body = [
96
+ `Merged into ${base} by \`code-kit merge\`, each verified against ${base} first:`,
97
+ '',
98
+ ...(branches.length ? branches.map((b) => `- ${b}`) : [`- ${what}`]),
99
+ '',
100
+ 'Each merge commit says what was verified, and any checks not run.',
101
+ ].join('\n');
102
+ const made = gh(
103
+ ...['pr', 'create', '--base', base, '--head', head],
104
+ ...['--title', publishTitle(branches, ahead), '--body', body],
105
+ );
106
+ if (made.status !== 0)
107
+ return { state: 'failed', message: `Opening the pull request failed: ${first(made)}` };
108
+ url = made.stdout.trim().split('\n').at(-1);
109
+ }
110
+ if (!mayMove)
111
+ return {
112
+ state: 'waiting',
113
+ url,
114
+ message: `${what} wait to reach origin in ${url}. Merging it is the person's, unless they delegate pushing ${base} ("approvals.delegate.push": true, committed on ${base}).`,
115
+ };
116
+ const merged = gh('pr', 'merge', url, '--merge');
117
+ if (merged.status !== 0)
118
+ return {
119
+ state: 'waiting',
120
+ url,
121
+ message: `${what} wait in ${url}: GitHub didn't merge it (${first(merged)}). It merges once GitHub allows it; run \`code-kit publish\` again then.`,
122
+ };
123
+ git('push', '--quiet', 'origin', '--delete', head);
124
+ // The local base catches up with the merge GitHub made, so the next pull request holds only what's new.
125
+ git('fetch', '--quiet', 'origin', base);
126
+ const where = git('worktree', 'list', '--porcelain')
127
+ .stdout.split('\n\n')
128
+ .find((w) => w.includes(`\nbranch refs/heads/${base}`))
129
+ ?.match(/^worktree (.+)$/m)?.[1];
130
+ const caught = where
131
+ ? run('git', ['-C', where, 'merge', '--quiet', '--ff-only', remote])
132
+ : git('merge-base', '--is-ancestor', base, remote).status === 0
133
+ ? git('update-ref', `refs/heads/${base}`, remote)
134
+ : { status: 1 };
135
+ return {
136
+ state: 'merged',
137
+ url,
138
+ message: `Published ${what} to origin's ${base} through ${url}.${caught.status === 0 ? '' : ` The local ${base} couldn't fast-forward to origin's; pull it when it's free.`}`,
139
+ };
140
+ }
141
+
142
+ function defaultRun(cmd, args) {
143
+ return spawnSync(cmd, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
144
+ }
@@ -4,6 +4,8 @@
4
4
  import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
5
5
  import { dirname, join } from 'node:path';
6
6
  import { projectWorktrees } from './rules.mjs';
7
+ import { GIT, commandsOf } from './shell.mjs';
8
+ import { storyOfBranch } from './teamfacts.mjs';
7
9
 
8
10
  export const QUEUE_FILE = '.claude/state/merge-queue.json';
9
11
  /** Merged and sent-back entries kept for the pane, newest last. */
@@ -92,3 +94,37 @@ export function settle(root, branch, state, reason = null, now = Date.now()) {
92
94
  export function lastMerged(entries) {
93
95
  return [...entries].reverse().find((e) => e.state === 'merged')?.branch ?? null;
94
96
  }
97
+
98
+ /** A ref as a local branch's name: `origin/web/st-4` → `web/st-4`; others as they are. */
99
+ const local = (ref) => ref.replace(/^[\w.-]+\/(?=[^/]+\/[^/]+$)/, '');
100
+
101
+ /**
102
+ * MQ-5: the story branches `cmd` merges with git, each with the branch it lands on: the one a
103
+ * `git switch` or `git checkout` earlier in the command moves to, else `branch`, the one checked out.
104
+ * [{ story: 'web/st-4', into: 'lead/next' }].
105
+ */
106
+ export function storyMerges(cmd, branch) {
107
+ const found = [];
108
+ let on = branch;
109
+ const switches = new RegExp(
110
+ String.raw`^${GIT}(?:switch|checkout)\s+((?:-\S+\s+)*)([^\s-]\S*)\s*$`,
111
+ );
112
+ const merges = new RegExp(String.raw`^${GIT}merge\b(.*)$`);
113
+ for (const { text, code } of commandsOf(cmd)) {
114
+ const moved = code.match(switches);
115
+ if (moved) {
116
+ on = moved[2];
117
+ continue;
118
+ }
119
+ const merged = code.match(merges);
120
+ if (!merged) continue;
121
+ // Its words as written, found in the masked code, so quoted text (a message) never reads as a branch.
122
+ const from = code.length - merged[1].length;
123
+ for (const w of merged[1].matchAll(/\S+/g)) {
124
+ if (w[0].startsWith('-') || /["']/.test(w[0])) continue;
125
+ const ref = local(text.slice(from + w.index, from + w.index + w[0].length));
126
+ if (storyOfBranch(ref)) found.push({ story: ref, into: on });
127
+ }
128
+ }
129
+ return found;
130
+ }
@@ -26,8 +26,11 @@ import {
26
26
 
27
27
  import { slicesOf } from './schedule.mjs';
28
28
 
29
- /** 2 adds the project's standards (code-kit spec 22); 3 the plan's slices (spec 23, SCHED-2). */
30
- export const SNAPSHOT_VERSION = 3;
29
+ /**
30
+ * 2 adds the project's standards (code-kit spec 22); 3 the plan's slices (spec 23, SCHED-2); 4 each
31
+ * story's summary and each requirement's title.
32
+ */
33
+ export const SNAPSHOT_VERSION = 4;
31
34
 
32
35
  /** A story's stage, as a report names it. */
33
36
  export const STAGES = [
@@ -45,7 +48,8 @@ export const AGENT_KINDS = ['lead', 'lane', 'reviewer', 'security-reviewer', 'ba
45
48
  /**
46
49
  * The allow-list. A leaf is a kind: `id` (ST-4, API-2), `handle` (a GitHub login), `name` (a lane, a
47
50
  * layer, a milestone, an approval: one line, at most 120 characters), `title` (one line, at most 200),
48
- * `line` (a send-back's reason: one line, at most 200), `path` (a file path or glob: one line, at most
51
+ * `line` (a send-back's reason: one line, at most 200), `summary` (a story's summary: one line, at most
52
+ * 600), `path` (a file path or glob: one line, at most
49
53
  * 300), `ms` (a time in milliseconds), `date` (YYYY-MM-DD), `count` (a whole number, 0 or more),
50
54
  * `bool`, `enum:a|b`. A trailing `?` allows null. An array holds one shape; an object holds only the
51
55
  * keys it lists.
@@ -73,12 +77,16 @@ export const SNAPSHOT_SCHEMA = {
73
77
  merged: 'ms?',
74
78
  },
75
79
  claimedBy: 'handle?',
80
+ // Added in version 4.
81
+ summary: 'summary?',
76
82
  },
77
83
  ],
78
84
  sendBacks: [{ story: 'id', at: 'ms', reason: 'line?' }],
79
85
  requirements: [
80
86
  {
81
87
  id: 'id',
88
+ // Added in version 4.
89
+ title: 'title?',
82
90
  stories: ['id'],
83
91
  built: ['path'],
84
92
  tested: ['path'],
@@ -123,6 +131,25 @@ export const SNAPSHOT_SCHEMA = {
123
131
  /** Parts a report from an older code-kit doesn't have yet: valid when missing, checked when there. */
124
132
  export const LATER_PARTS = ['standards', 'slices'];
125
133
 
134
+ export const SUMMARY_MAX = 600;
135
+
136
+ /**
137
+ * A story's summary as a report holds it: on one line, and when longer than 600 characters, cut after
138
+ * the last sentence that fits (or, with no sentence ending in reach, at the last word, with an ellipsis).
139
+ */
140
+ export function reportedSummary(text) {
141
+ const s = String(text ?? '')
142
+ .replace(/\s+/g, ' ')
143
+ .trim();
144
+ if (!s) return null;
145
+ if (s.length <= SUMMARY_MAX) return s;
146
+ const head = s.slice(0, SUMMARY_MAX + 1);
147
+ const ends = [...head.matchAll(/[.!?](?=\s)/g)].map((m) => m.index + 1);
148
+ if (ends.length) return s.slice(0, ends.at(-1));
149
+ const word = s.slice(0, SUMMARY_MAX).lastIndexOf(' ');
150
+ return `${s.slice(0, word > 0 ? word : SUMMARY_MAX - 1).trimEnd()}…`;
151
+ }
152
+
126
153
  const ONE_LINE = (max) => (v) => typeof v === 'string' && v.length <= max && !/[\r\n]/.test(v);
127
154
  const KINDS = {
128
155
  id: (v) => typeof v === 'string' && /^[A-Za-z][A-Za-z0-9]*-\d+$/.test(v),
@@ -130,6 +157,7 @@ const KINDS = {
130
157
  name: ONE_LINE(120),
131
158
  title: ONE_LINE(200),
132
159
  line: ONE_LINE(200),
160
+ summary: ONE_LINE(SUMMARY_MAX),
133
161
  path: ONE_LINE(300),
134
162
  ms: (v) => Number.isInteger(v) && v > 0,
135
163
  date: (v) => typeof v === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(v),
@@ -303,6 +331,7 @@ export function buildSnapshot({
303
331
  merged: ms(t.merged),
304
332
  },
305
333
  claimedBy: claimed.get(s.id) ?? null,
334
+ summary: reportedSummary(s.summary),
306
335
  };
307
336
  });
308
337
 
@@ -329,6 +358,7 @@ export function buildSnapshot({
329
358
  if (CHECKS.includes(row.status)) checks.set(row.requirement, row.status);
330
359
  const requirements = trail(root, status, base).map((r) => ({
331
360
  id: r.id,
361
+ title: line(r.title),
332
362
  stories: r.stories.map((s) => s.id),
333
363
  built: r.files.filter((f) => KINDS.path(f)),
334
364
  tested: r.tests.filter((f) => KINDS.path(f)),
@@ -12,7 +12,7 @@ const git = (root, ...args) => {
12
12
  return execFileSync('git', ['-C', root, ...args], {
13
13
  encoding: 'utf8',
14
14
  stdio: ['ignore', 'pipe', 'ignore'],
15
- maxBuffer: 64 * 1024 * 1024,
15
+ maxBuffer: 256 * 1024 * 1024,
16
16
  });
17
17
  } catch {
18
18
  return '';
@@ -23,28 +23,64 @@ const git = (root, ...args) => {
23
23
  const KIT = /^(\.claude|\.ctx|\.github)\//;
24
24
  const isCode = (rel) => CODE.test(rel) && !KIT.test(rel);
25
25
 
26
+ /**
27
+ * Every story's files from one pass over the history: { id: Set of files }, from the commits on any
28
+ * branch whose message names it, as `storyFiles` finds them one story at a time. A project with hundreds
29
+ * of stories outlasts the mod's 30 seconds with a `git log --all` each.
30
+ */
31
+ export function namedFiles(root) {
32
+ const byStory = {};
33
+ const log = git(root, 'log', '--all', '--no-merges', '--format=%x1e%B%x1f', '--name-only');
34
+ for (const commit of log.split('\x1e')) {
35
+ const end = commit.indexOf('\x1f');
36
+ if (end < 0) continue;
37
+ // As storyFiles' grep: "ST-1" mustn't match "ST-12", nor "XST-1".
38
+ const ids = new Set(
39
+ [...commit.slice(0, end).matchAll(/(?:^|[^A-Za-z0-9-])(ST-\d+)(?!\d)/gm)].map((m) => m[1]),
40
+ );
41
+ if (!ids.size) continue;
42
+ const files = commit
43
+ .slice(end + 1)
44
+ .split('\n')
45
+ .filter(Boolean);
46
+ for (const id of ids) {
47
+ byStory[id] ??= new Set();
48
+ for (const f of files) byStory[id].add(f);
49
+ }
50
+ }
51
+ return byStory;
52
+ }
53
+
26
54
  /**
27
55
  * The code files a story changed: those in commits whose message names it (on any branch, so a merged
28
56
  * story still has its files) and, while its branch is open, its diff against the base. Only files that
29
- * still exist, in path order.
57
+ * still exist, in path order. `named` is `namedFiles`', where the caller reads every story.
30
58
  */
31
- export function storyFiles(root, story, from) {
59
+ export function storyFiles(root, story, from, named = null) {
32
60
  const found = new Set();
33
61
  const id = story.id;
34
- const log = git(
35
- root,
36
- 'log',
37
- '--all',
38
- '--no-merges',
39
- '-E',
40
- // POSIX ERE has no \b: "ST-1" mustn't match "ST-12".
41
- `--grep=(^|[^A-Za-z0-9-])${id}([^0-9]|$)`,
42
- '--format=',
43
- '--name-only',
44
- );
62
+ const log = named
63
+ ? [...(named[id] ?? [])].join('\n')
64
+ : git(
65
+ root,
66
+ 'log',
67
+ '--all',
68
+ '--no-merges',
69
+ '-E',
70
+ // POSIX ERE has no \b: "ST-1" mustn't match "ST-12".
71
+ `--grep=(^|[^A-Za-z0-9-])${id}([^0-9]|$)`,
72
+ '--format=',
73
+ '--name-only',
74
+ );
45
75
  for (const f of log.split('\n')) if (f) found.add(f);
46
- if (story.branchExists && story.branch)
47
- for (const f of git(root, 'diff', '--name-only', `${from}...${story.branch}`).split('\n'))
76
+ // A branch the base already holds has nothing of its own to diff.
77
+ if (story.branchExists && story.branch && story.ahead !== 0)
78
+ for (const f of git(
79
+ root,
80
+ 'diff',
81
+ '--name-only',
82
+ `${from}...${story.ref ?? story.branch}`,
83
+ ).split('\n'))
48
84
  if (f) found.add(f);
49
85
  return [...found].filter((f) => isCode(f) && existsSync(join(root, f))).sort();
50
86
  }
@@ -133,8 +169,9 @@ export function storyMap(root, story, config, from) {
133
169
  */
134
170
  export function trail(root, status, from) {
135
171
  const byStory = new Map();
172
+ const named = namedFiles(root);
136
173
  const filesOf = (s) => {
137
- if (!byStory.has(s.id)) byStory.set(s.id, storyFiles(root, s, from));
174
+ if (!byStory.has(s.id)) byStory.set(s.id, storyFiles(root, s, from, named));
138
175
  return byStory.get(s.id);
139
176
  };
140
177
  return status.requirements
@@ -2,11 +2,44 @@
2
2
  // records: when its branch was made, its commits, its merge into the base, its send-backs and the
3
3
  // approvals its commits logged. The mod draws these as a bar per lane on a shared time axis.
4
4
  import { execFileSync } from 'node:child_process';
5
+ import { readFileSync } from 'node:fs';
6
+ import { isAbsolute, join } from 'node:path';
5
7
  import { APPROVAL_LOG } from './rules.mjs';
6
8
  import { sendBacks } from './reviews.mjs';
7
9
 
8
10
  const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
9
11
 
12
+ /**
13
+ * Each object asked for, read by one `git cat-file --batch`: { name: text }, a missing one left out.
14
+ * One process for every branch's approval log, where a `git show` each outlasts the mod's 30 seconds.
15
+ */
16
+ export function readObjects(root, names) {
17
+ if (!names.length) return {};
18
+ let out;
19
+ try {
20
+ out = execFileSync('git', ['-C', root, 'cat-file', '--batch'], {
21
+ input: `${names.join('\n')}\n`,
22
+ stdio: ['pipe', 'pipe', 'ignore'],
23
+ maxBuffer: 256 * 1024 * 1024,
24
+ });
25
+ } catch {
26
+ return {};
27
+ }
28
+ const found = {};
29
+ let at = 0;
30
+ for (const name of names) {
31
+ const eol = out.indexOf(10, at);
32
+ if (eol < 0) break;
33
+ const header = out.subarray(at, eol).toString('utf8').split(' ');
34
+ at = eol + 1;
35
+ if (header[1] === 'missing' || header.length < 3) continue;
36
+ const size = Number(header[2]);
37
+ found[name] = out.subarray(at, at + size).toString('utf8');
38
+ at += size + 1;
39
+ }
40
+ return found;
41
+ }
42
+
10
43
  /**
11
44
  * For each story with a branch, or done: { id, lane, branch, state, started, lastCommit, merged,
12
45
  * sentBack: [ms], approvals: [{ at, names }] }, times in milliseconds or null.
@@ -29,49 +62,66 @@ export function storyTimes(root, stories, base) {
29
62
  return { at: Number(t) * 1000, subject: subject.join('\t') };
30
63
  });
31
64
  const backs = sendBacks(root);
32
- const logged = (ref) => new Set(lines(git('show', `${ref}:${APPROVAL_LOG}`)));
33
- const onBase = logged(base);
34
- return stories
35
- .filter((s) => s.branchExists || s.state === 'done')
36
- .map((s) => {
37
- const b = s.branch;
38
- // The reflog's oldest entry is the branch's making, when it's still there.
39
- const made = s.branchExists
40
- ? lines(git('reflog', 'show', '--format=%ct', `refs/heads/${b}`))
41
- : [];
42
- const commits = s.branchExists
43
- ? lines(git('log', '--format=%ct', `${base}..${b}`)).map((t) => Number(t) * 1000)
44
- : [];
45
- const named = new RegExp(`(^|[\\s'"])${escape(b)}($|[\\s'"])`);
46
- const merged = merges.find((m) => named.test(m.subject))?.at ?? null;
47
- const approvals = s.branchExists
48
- ? [...logged(b)]
49
- .filter((l) => !onBase.has(l))
50
- .flatMap((l) => {
51
- try {
52
- const e = JSON.parse(l);
53
- return e.grantedAt || e.at
54
- ? [{ at: Date.parse(e.grantedAt ?? e.at), names: [e.approval].filter(Boolean) }]
55
- : [];
56
- } catch {
57
- return [];
58
- }
59
- })
65
+ const onBase = new Set(lines(git('show', `${base}:${APPROVAL_LOG}`)));
66
+ const shown = stories.filter((s) => s.branchExists || s.state === 'done');
67
+ const refOf = (s) => s.ref ?? `refs/heads/${s.branch}`;
68
+ const logs = readObjects(
69
+ root,
70
+ shown.filter((s) => s.branchExists).map((s) => `${refOf(s)}:${APPROVAL_LOG}`),
71
+ );
72
+ const common = git('rev-parse', '--git-common-dir');
73
+ const reflogs = common ? (isAbsolute(common) ? common : join(root, common)) : null;
74
+ // When a branch was made: its reflog's oldest entry, read from the file git keeps it in (or from git,
75
+ // where the file isn't there), in milliseconds; null once the reflog is gone.
76
+ const madeAt = (b) => {
77
+ let first = null;
78
+ try {
79
+ first = readFileSync(join(reflogs, 'logs/refs/heads', b), 'utf8').split('\n')[0];
80
+ } catch {
81
+ const at = lines(git('reflog', 'show', '--date=unix', '--format=%gd', `refs/heads/${b}`))
82
+ .at(-1)
83
+ ?.match(/@\{(\d+)\}$/)?.[1];
84
+ return at ? Number(at) * 1000 : null;
85
+ }
86
+ const at = first?.split('\t')[0].match(/> (\d+) [+-]\d{4}$/)?.[1];
87
+ return at ? Number(at) * 1000 : null;
88
+ };
89
+ return shown.map((s) => {
90
+ const b = s.branch;
91
+ // A branch only on origin has no reflog here.
92
+ const local = s.branchExists && refOf(s).startsWith('refs/heads/');
93
+ const made = local && reflogs ? madeAt(b) : null;
94
+ // Only a branch ahead of the base has commits of its own.
95
+ const commits =
96
+ s.branchExists && s.ahead !== 0
97
+ ? lines(git('log', '--format=%ct', `${base}..${refOf(s)}`)).map((t) => Number(t) * 1000)
60
98
  : [];
61
- return {
62
- id: s.id,
63
- lane: s.lane,
64
- branch: b,
65
- state: s.state,
66
- started: made.length
67
- ? Number(made.at(-1)) * 1000
68
- : commits.length
69
- ? Math.min(...commits)
70
- : null,
71
- lastCommit: commits.length ? Math.max(...commits) : null,
72
- merged,
73
- sentBack: backs.filter((x) => x.branch === b).map((x) => Date.parse(x.at)),
74
- approvals,
75
- };
76
- });
99
+ const named = new RegExp(`(^|[\\s'"])${escape(b)}($|[\\s'"])`);
100
+ const merged = merges.find((m) => named.test(m.subject))?.at ?? null;
101
+ const approvals = s.branchExists
102
+ ? lines(logs[`${refOf(s)}:${APPROVAL_LOG}`] ?? '')
103
+ .filter((l) => !onBase.has(l))
104
+ .flatMap((l) => {
105
+ try {
106
+ const e = JSON.parse(l);
107
+ return e.grantedAt || e.at
108
+ ? [{ at: Date.parse(e.grantedAt ?? e.at), names: [e.approval].filter(Boolean) }]
109
+ : [];
110
+ } catch {
111
+ return [];
112
+ }
113
+ })
114
+ : [];
115
+ return {
116
+ id: s.id,
117
+ lane: s.lane,
118
+ branch: b,
119
+ state: s.state,
120
+ started: made ? made : commits.length ? Math.min(...commits) : null,
121
+ lastCommit: commits.length ? Math.max(...commits) : null,
122
+ merged,
123
+ sentBack: backs.filter((x) => x.branch === b).map((x) => Date.parse(x.at)),
124
+ approvals,
125
+ };
126
+ });
77
127
  }