@zhuxixi/pi-agent-board 0.12.0 → 0.13.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/CHANGELOG.md +36 -0
- package/README.md +7 -7
- package/docs/superpowers/plans/2026-10-19-issue-213-reply-guard-csi-key.md +257 -0
- package/docs/superpowers/plans/2026-10-19-issue-214-attach-forward-alt-modifier.md +202 -0
- package/docs/superpowers/plans/2026-10-21-issue-221-ask-user-question.md +198 -0
- package/docs/superpowers/plans/2026-10-23-issue-225-monotonic-foreground-state.md +450 -0
- package/docs/superpowers/plans/2026-10-23-issue-229-vim-jkl.md +449 -0
- package/docs/superpowers/plans/2026-10-23-issue-232-attach-bg-only-cells.md +153 -0
- package/docs/superpowers/plans/2026-10-23-lease-heartbeat-staleness.md +537 -0
- package/docs/superpowers/plans/2026-10-23-selected-row-highlight.md +405 -0
- package/docs/superpowers/plans/2026-10-24-issue-217-windows-pid-reuse-stopping.md +367 -0
- package/docs/superpowers/plans/2026-10-24-issue-230-warm-host-background-runs.md +806 -0
- package/docs/superpowers/plans/2026-10-24-issue-238-windows-process-identity-token.md +490 -0
- package/docs/superpowers/plans/2026-10-24-issue-241-coordinator-root-exit.md +461 -0
- package/docs/superpowers/plans/2026-10-25-issue-239-coordinator-replacement-identity.md +504 -0
- package/docs/superpowers/specs/2026-10-14-issue-170-attach-reply-guard-design.md +5 -0
- package/docs/superpowers/specs/2026-10-19-issue-213-reply-guard-csi-key-design.md +165 -0
- package/docs/superpowers/specs/2026-10-19-issue-214-attach-forward-alt-modifier-design.md +131 -0
- package/docs/superpowers/specs/2026-10-20-issue-221-ask-user-question-design.md +270 -0
- package/docs/superpowers/specs/2026-10-22-issue-225-monotonic-foreground-state-design.md +225 -0
- package/docs/superpowers/specs/2026-10-23-issue-229-vim-jkl-design.md +90 -0
- package/docs/superpowers/specs/2026-10-23-issue-232-attach-bg-only-cells-design.md +146 -0
- package/docs/superpowers/specs/2026-10-23-lease-heartbeat-staleness-design.md +271 -0
- package/docs/superpowers/specs/2026-10-23-selected-row-highlight-design.md +191 -0
- package/docs/superpowers/specs/2026-10-24-issue-217-windows-pid-reuse-stopping-design.md +334 -0
- package/docs/superpowers/specs/2026-10-24-issue-230-warm-host-background-runs-design.md +283 -0
- package/docs/superpowers/specs/2026-10-24-issue-238-windows-process-identity-token-design.md +286 -0
- package/docs/superpowers/specs/2026-10-24-issue-241-coordinator-root-exit-design.md +381 -0
- package/docs/superpowers/specs/2026-10-25-issue-239-coordinator-replacement-identity-design.md +331 -0
- package/package.json +3 -3
- package/runner/pty-runner.mjs +18 -22
- package/runner/state-coordinator.mjs +213 -26
- package/scripts/release_helper.mjs +39 -7
- package/scripts/run-tests.mjs +375 -0
- package/src/core/background-runs.mjs +273 -0
- package/src/core/coordinator-client.mjs +346 -26
- package/src/core/diagnostics.mjs +24 -0
- package/src/core/events.mjs +16 -3
- package/src/core/foreground-projection-cache.mjs +99 -0
- package/src/core/host-coordination.mjs +69 -1
- package/src/core/launch.mjs +21 -1
- package/src/core/locks.mjs +176 -31
- package/src/core/pid.mjs +4 -2
- package/src/core/process-identity-probe.mjs +228 -0
- package/src/core/pty-scroll.mjs +21 -14
- package/src/core/state-commands.mjs +5 -1
- package/src/core/terminal-reply-guard.mjs +26 -4
- package/src/core/types.mjs +1 -0
- package/src/core/warm-host-sweeper.mjs +7 -2
- package/src/runtime/service.mjs +385 -73
- package/src/ui/dashboard.ts +48 -25
- package/src/ui/pty-attach.ts +18 -7
|
@@ -33,10 +33,15 @@
|
|
|
33
33
|
* replay is deterministic — no re-decision, no re-binding.
|
|
34
34
|
* - Legacy adoption: a view without materializedRevision is stamped as part of
|
|
35
35
|
* its first applied command's single materialization write (no extra write).
|
|
36
|
+
* - Root guard (issue #241): a board root that disappears — or is REPLACED by a
|
|
37
|
+
* write that recreates it in the delete window — makes the supervised
|
|
38
|
+
* heartbeat step down with exit 0 and one stderr line. Neither path writes a
|
|
39
|
+
* diagnostic or state: every append helper ensureDirs its parent and would
|
|
40
|
+
* recreate the deleted root skeleton.
|
|
36
41
|
* - AGENT_BOARD_COORDINATOR=off makes the process exit 0 immediately (tests and
|
|
37
42
|
* the client's degradation path).
|
|
38
43
|
*/
|
|
39
|
-
import { existsSync, readdirSync,
|
|
44
|
+
import { existsSync, readdirSync, statSync, unlinkSync } from "node:fs";
|
|
40
45
|
import { randomBytes } from "node:crypto";
|
|
41
46
|
import { createServer } from "node:net";
|
|
42
47
|
import { readJson } from "../src/core/atomic.mjs";
|
|
@@ -50,7 +55,10 @@ import {
|
|
|
50
55
|
writeCheckpoint,
|
|
51
56
|
} from "../src/core/coordinator-journal.mjs";
|
|
52
57
|
import { ownsEndpoint } from "../src/core/host-coordination.mjs";
|
|
53
|
-
import {
|
|
58
|
+
import { appendDiagnostic } from "../src/core/diagnostics.mjs";
|
|
59
|
+
import { acquireOwnedViewLock, describeLeaseConflict, heartbeatFailureAction, withViewLockSync } from "../src/core/locks.mjs";
|
|
60
|
+
import { captureStartToken } from "../src/core/pid.mjs";
|
|
61
|
+
import { resolveTestMs } from "../src/core/test-knobs.mjs";
|
|
54
62
|
import * as P from "../src/core/paths.mjs";
|
|
55
63
|
import { readState, readStatus, writeState, writeStatus } from "../src/core/store.mjs";
|
|
56
64
|
import { TRANSIENT_KINDS, decideStateTransition, validateCommand } from "../src/core/state-commands.mjs";
|
|
@@ -97,28 +105,150 @@ main().catch((err) => {
|
|
|
97
105
|
process.exit(1);
|
|
98
106
|
});
|
|
99
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Append a coordinator-scoped diagnostic. `appendDiagnostic` throws when the
|
|
110
|
+
* disk is failing, so callers fence the write where a crash would be worse
|
|
111
|
+
* than a lost diagnostic (issue #224 D4). The AGENT_BOARD_TEST_FAIL_DIAGNOSTIC
|
|
112
|
+
* knob forces that throw so A14b can prove the step-down survives it.
|
|
113
|
+
* @param {string} code
|
|
114
|
+
* @param {Record<string, any>} details
|
|
115
|
+
* @param {"info"|"warn"|"error"} [level]
|
|
116
|
+
* @param {string} [source]
|
|
117
|
+
*/
|
|
118
|
+
function appendCoordinatorDiagnostic(code, details, level = "info", source = "coordinator") {
|
|
119
|
+
if (process.env.AGENT_BOARD_TEST_FAIL_DIAGNOSTIC === "1") {
|
|
120
|
+
throw new Error(`forced coordinator diagnostic failure (${code})`);
|
|
121
|
+
}
|
|
122
|
+
appendDiagnostic(root, "_coordinator", { source, level, code, details });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Whether the board root is provably gone. Only `ENOENT` counts (issue #241):
|
|
127
|
+
* a transient access failure (EACCES/EBUSY, e.g. a scanner holding the
|
|
128
|
+
* directory) must never kill a healthy coordinator, so it reads as "present".
|
|
129
|
+
* @returns {boolean}
|
|
130
|
+
*/
|
|
131
|
+
function rootVanished() {
|
|
132
|
+
try {
|
|
133
|
+
statSync(root);
|
|
134
|
+
return false;
|
|
135
|
+
} catch (err) {
|
|
136
|
+
return /** @type {NodeJS.ErrnoException} */ (err)?.code === "ENOENT";
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Snapshot the board root directory's filesystem identity (issue #241).
|
|
142
|
+
* `{dev, ino}` identifies one exact directory: a delete-and-recreate at the same
|
|
143
|
+
* path gets a new inode, which is the only way to tell "the root came back" from
|
|
144
|
+
* "the root never left" once a write's ensureDir has recreated it. Returns null
|
|
145
|
+
* when the root is missing or the filesystem reports no usable identity (ino 0,
|
|
146
|
+
* or no dev/ino at all), which degrades the check to existence-only.
|
|
147
|
+
* @returns {{dev: number, ino: number}|null}
|
|
148
|
+
*/
|
|
149
|
+
function captureRootIdentity() {
|
|
150
|
+
try {
|
|
151
|
+
const st = statSync(root);
|
|
152
|
+
if (!Number.isFinite(st.dev) || !Number.isFinite(st.ino) || st.ino === 0) return null;
|
|
153
|
+
return { dev: st.dev, ino: st.ino };
|
|
154
|
+
} catch {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Whether the board root is gone or was replaced since boot (issue #241).
|
|
161
|
+
* Existence alone is not enough: `appendCommand`/`writeState`/lease acquisition
|
|
162
|
+
* ensureDir their parent, so a write landing after the delete RECREATES the path
|
|
163
|
+
* and a plain stat then reads "present" (final-review I1). Comparing against the
|
|
164
|
+
* identity captured at boot detects the replacement on the next supervised tick,
|
|
165
|
+
* including the boot-window race where lease acquisition recreates the root.
|
|
166
|
+
* Only `ENOENT` counts as missing; any other stat error (EACCES/EBUSY) reads as
|
|
167
|
+
* present so a transient access failure never kills a healthy coordinator. With
|
|
168
|
+
* no captured identity the check degrades to existence-only (never a false
|
|
169
|
+
* step-down).
|
|
170
|
+
* @param {{dev: number, ino: number}|null} identity
|
|
171
|
+
* @returns {"missing"|"replaced"|null}
|
|
172
|
+
*/
|
|
173
|
+
function rootVanishedOrReplaced(identity) {
|
|
174
|
+
try {
|
|
175
|
+
const st = statSync(root);
|
|
176
|
+
if (!identity) return null;
|
|
177
|
+
return st.dev !== identity.dev || st.ino !== identity.ino ? "replaced" : null;
|
|
178
|
+
} catch (err) {
|
|
179
|
+
return /** @type {NodeJS.ErrnoException} */ (err)?.code === "ENOENT" ? "missing" : null;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The one stderr line every "root is gone" exit emits (issue #241): the boot
|
|
185
|
+
* guard and the supervised step-down. Neither writes a diagnostic: every
|
|
186
|
+
* append helper ensures its parent directory (src/core/atomic.mjs), so a write
|
|
187
|
+
* under <root>/views/_coordinator would recreate the deleted directory chain —
|
|
188
|
+
* the throwaway-root residue this issue exists to remove. Detached callers
|
|
189
|
+
* ignore stdio; a test pipes stderr and attributes the exit to this line.
|
|
190
|
+
* @param {"missing"|"replaced"} state
|
|
191
|
+
* @param {string} outcome
|
|
192
|
+
*/
|
|
193
|
+
function writeRootGone(state, outcome) {
|
|
194
|
+
process.stderr.write(`state-coordinator: board root ${state} (${root}); ${outcome}\n`);
|
|
195
|
+
}
|
|
196
|
+
|
|
100
197
|
async function main() {
|
|
101
198
|
const instanceId = randomBytes(8).toString("hex");
|
|
102
199
|
const startedAt = Date.now();
|
|
200
|
+
const heartbeatMs = resolveTestMs(process.env, "AGENT_BOARD_TEST_COORDINATOR_HEARTBEAT_MS", HEARTBEAT_MS);
|
|
201
|
+
|
|
202
|
+
// Capture the root's identity BEFORE the boot guard (issue #241, final-review
|
|
203
|
+
// I1): a delete between the guard and lease acquisition would let that
|
|
204
|
+
// ensureDir recreate the root WITH a valid lease, and the process would then
|
|
205
|
+
// serve it forever. The tick compares against this snapshot, so the recreated
|
|
206
|
+
// directory is detected on the first heartbeat.
|
|
207
|
+
const rootIdentity = captureRootIdentity();
|
|
208
|
+
|
|
209
|
+
// 0. A root that is already gone must fail closed (issue #241 A3b). Lease
|
|
210
|
+
// acquisition runs ensureDir, which would happily REBUILD the deleted board
|
|
211
|
+
// and serve that empty root forever — the exact orphan shape this issue is
|
|
212
|
+
// about. "Nothing to serve" is an expected outcome like a lost lease, so it
|
|
213
|
+
// exits 0, writes no diagnostic, and logs the shared stderr line.
|
|
214
|
+
if (rootVanished()) {
|
|
215
|
+
writeRootGone("missing", "nothing to serve; exiting");
|
|
216
|
+
process.exit(0);
|
|
217
|
+
}
|
|
103
218
|
|
|
104
219
|
// 1. Lease first: a second coordinator is an expected, idempotent no-op.
|
|
105
220
|
// Identity carries a POSIX startToken (pty-runner pattern) so a SIGKILLed
|
|
106
|
-
// coordinator's lease can be reclaimed by the next instance after pid reuse
|
|
221
|
+
// coordinator's lease can be reclaimed by the next instance after pid reuse,
|
|
222
|
+
// plus the in-process instanceId the pong reports (issue #239 A4) so a client
|
|
223
|
+
// can tell whether the live endpoint is still the lease's owner.
|
|
224
|
+
// heartbeatMs declares the staleness contract (issue #224): a holder that
|
|
225
|
+
// stops heartbeating becomes reclaimable on its own staleness.
|
|
107
226
|
/** @type {import("../src/core/locks.mjs").Lease} */
|
|
108
227
|
let lease;
|
|
109
228
|
try {
|
|
110
229
|
lease = acquireOwnedViewLock(root, "_coordinator", "state-coordinator", {
|
|
111
230
|
waitMs: 500,
|
|
112
|
-
identity: { pid: process.pid, startToken: captureStartToken(process.pid) },
|
|
231
|
+
identity: { pid: process.pid, startToken: captureStartToken(process.pid), instanceId },
|
|
232
|
+
heartbeatMs,
|
|
113
233
|
});
|
|
114
234
|
} catch (err) {
|
|
235
|
+
// Record the conflict instead of exiting silently (issue #224 D6). The
|
|
236
|
+
// diagnostic write is best-effort: it must never turn the expected
|
|
237
|
+
// idempotent exit into a crash.
|
|
238
|
+
const summary = describeLeaseConflict(root, "_coordinator", "state-coordinator");
|
|
239
|
+
if (summary) {
|
|
240
|
+
try { appendCoordinatorDiagnostic("coordinator_lease_unavailable", summary, "error"); } catch { /* best effort */ }
|
|
241
|
+
}
|
|
115
242
|
process.stderr.write(`state-coordinator: lease unavailable (${err?.code ?? err?.message ?? "unknown"}); another instance may own it; exiting\n`);
|
|
116
243
|
process.exit(0);
|
|
117
244
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
245
|
+
// Leave a trace when this acquire reclaimed a stale lease (issue #224 D6).
|
|
246
|
+
if (lease.reclaimed) {
|
|
247
|
+
try { appendCoordinatorDiagnostic("lease_reclaimed", { ...lease.reclaimed }, "info", "lock"); } catch { /* best effort */ }
|
|
248
|
+
}
|
|
249
|
+
// Boot-edge touch (issue #224 D5): reflect acquisition progress before the
|
|
250
|
+
// supervised interval starts. Never counted toward the failure counter.
|
|
251
|
+
try { lease.touch(); } catch { /* best effort */ }
|
|
122
252
|
|
|
123
253
|
// 2. Boot state: journal summary, processed-command ring, replay, counter init.
|
|
124
254
|
// Repair a crash-torn tail FIRST: without it the first post-restart append
|
|
@@ -161,6 +291,10 @@ async function main() {
|
|
|
161
291
|
}
|
|
162
292
|
let lastCheckpointBytes = 0;
|
|
163
293
|
|
|
294
|
+
// Boot-edge touch (issue #224 D5): replay is done; the heartbeat now
|
|
295
|
+
// reflects live progress before the endpoint binds. Never counted.
|
|
296
|
+
try { lease.touch(); } catch { /* best effort */ }
|
|
297
|
+
|
|
164
298
|
// 3. Control socket (JSONL, pty-runner's line-buffered server shape).
|
|
165
299
|
const socketPath = P.coordinatorEndpointPathFor(process.platform, root);
|
|
166
300
|
if (process.platform !== "win32" && existsSync(socketPath)) {
|
|
@@ -186,6 +320,16 @@ async function main() {
|
|
|
186
320
|
shutdown(1);
|
|
187
321
|
});
|
|
188
322
|
|
|
323
|
+
// Boot-edge touch (issue #224 D5): the endpoint bind is the last boot
|
|
324
|
+
// boundary; the supervised interval takes over once listen succeeds.
|
|
325
|
+
// Never counted toward the failure counter.
|
|
326
|
+
try { lease.touch(); } catch { /* best effort */ }
|
|
327
|
+
|
|
328
|
+
// Consecutive supervised heartbeat failures. Only interval touches count;
|
|
329
|
+
// the boot-edge touches above are deliberately ignored (issue #224 D4/D5).
|
|
330
|
+
let consecutiveTouchFailures = 0;
|
|
331
|
+
let heartbeatTimer = null;
|
|
332
|
+
|
|
189
333
|
server.listen(socketPath, () => {
|
|
190
334
|
if (process.platform !== "win32") {
|
|
191
335
|
try {
|
|
@@ -193,6 +337,66 @@ async function main() {
|
|
|
193
337
|
boundSocketIdentity = { dev: st.dev, ino: st.ino };
|
|
194
338
|
} catch { /* without the identity, cleanup degrades to no-op */ }
|
|
195
339
|
}
|
|
340
|
+
// Supervision starts only after the endpoint is bound (issue #224 D4):
|
|
341
|
+
// a live-but-stuck holder must step down before its lease can be
|
|
342
|
+
// preempted as stale, and stepping down before the socket exists would
|
|
343
|
+
// make the A14 endpoint cleanup a no-op.
|
|
344
|
+
heartbeatTimer = setInterval(() => {
|
|
345
|
+
// Step down when the board root is gone or was REPLACED (issue #241):
|
|
346
|
+
// a detached coordinator that outlives its root serves nobody, and its
|
|
347
|
+
// lease can never be reclaimed while the pid is alive. Detection reuses
|
|
348
|
+
// this tick — no second timer — so the worst case is one heartbeat.
|
|
349
|
+
//
|
|
350
|
+
// Existence alone is not enough (final-review I1): a write landing after
|
|
351
|
+
// the delete (appendCommand / writeState → ensureDir) recreates the
|
|
352
|
+
// directory, so a plain stat would read "present" and the coordinator
|
|
353
|
+
// would serve a dead board. The boot-time dev/ino identity distinguishes
|
|
354
|
+
// the recreated directory from the original one, and the same guard
|
|
355
|
+
// catches a root deleted between the boot guard and lease acquisition
|
|
356
|
+
// (that ensureDir would recreate it WITH a lease).
|
|
357
|
+
//
|
|
358
|
+
// No diagnostic is written on either path: every append helper
|
|
359
|
+
// ensureDirs its parent (src/core/atomic.mjs), so appending to
|
|
360
|
+
// <root>/views/_coordinator/diagnostics.jsonl would RECREATE the
|
|
361
|
+
// deleted directory chain — reintroducing exactly the throwaway-root
|
|
362
|
+
// residue this issue measures (U1). The shared stderr line is the
|
|
363
|
+
// attribution instead; detached callers ignore stdio, while a test
|
|
364
|
+
// spawns with a stderr pipe. Residual accepted (spec §7): a write in
|
|
365
|
+
// the delete window may leave a skeleton directory (data, not a
|
|
366
|
+
// process); this tick bounds its lifetime.
|
|
367
|
+
//
|
|
368
|
+
// A missing root also makes the touch below fail, but that path needs
|
|
369
|
+
// three consecutive failures; checking first attributes the step-down
|
|
370
|
+
// to the real cause and vacates the root immediately.
|
|
371
|
+
const rootState = rootVanishedOrReplaced(rootIdentity);
|
|
372
|
+
if (rootState) {
|
|
373
|
+
writeRootGone(rootState, "stepping down");
|
|
374
|
+
shutdown(0);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
let ok;
|
|
378
|
+
try {
|
|
379
|
+
ok = process.env.AGENT_BOARD_TEST_FORCE_TOUCH_FAILURE === "1" ? false : lease.touch();
|
|
380
|
+
} catch {
|
|
381
|
+
ok = false;
|
|
382
|
+
}
|
|
383
|
+
if (!ok) {
|
|
384
|
+
consecutiveTouchFailures += 1;
|
|
385
|
+
if (heartbeatFailureAction(consecutiveTouchFailures) === "abandon") {
|
|
386
|
+
// Count → diagnostic → shutdown, in that order. The
|
|
387
|
+
// diagnostic write throws when the disk is failing (the
|
|
388
|
+
// exact step-down scenario), so it is fenced and must not
|
|
389
|
+
// block the exit.
|
|
390
|
+
try {
|
|
391
|
+
appendCoordinatorDiagnostic("coordinator_heartbeat_lost", { consecutiveFailures: consecutiveTouchFailures }, "error");
|
|
392
|
+
} catch { /* a failing diagnostic must not block the step-down */ }
|
|
393
|
+
shutdown(0);
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
consecutiveTouchFailures = 0;
|
|
397
|
+
}
|
|
398
|
+
}, heartbeatMs);
|
|
399
|
+
heartbeatTimer.unref?.();
|
|
196
400
|
});
|
|
197
401
|
|
|
198
402
|
let shuttingDown = false;
|
|
@@ -202,7 +406,7 @@ async function main() {
|
|
|
202
406
|
function shutdown(code) {
|
|
203
407
|
if (shuttingDown) return;
|
|
204
408
|
shuttingDown = true;
|
|
205
|
-
try { clearInterval(
|
|
409
|
+
try { clearInterval(heartbeatTimer); } catch { /* best effort */ }
|
|
206
410
|
try { server.close(); } catch { /* already closed */ }
|
|
207
411
|
if (process.platform !== "win32" && boundSocketIdentity) {
|
|
208
412
|
try {
|
|
@@ -410,20 +614,3 @@ async function main() {
|
|
|
410
614
|
lastCheckpointBytes = gcJournal(root);
|
|
411
615
|
}
|
|
412
616
|
}
|
|
413
|
-
|
|
414
|
-
/** POSIX process start token — /proc/<pid>/stat field 22 (starttime), stable
|
|
415
|
-
* across exec(2). Same recipe as pty-runner: lets locks.mjs reclaim this
|
|
416
|
-
* coordinator's lease after a SIGKILL even if the pid was reused. null on
|
|
417
|
-
* failure or non-Linux platforms (reclaim degrades to "blocked" there — same
|
|
418
|
-
* platform parity as PTY hosts). */
|
|
419
|
-
function captureStartToken(pid) {
|
|
420
|
-
if (process.platform !== "linux" || !pid) return null;
|
|
421
|
-
try {
|
|
422
|
-
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
423
|
-
const afterComm = stat.slice(stat.lastIndexOf(")") + 1).trimStart();
|
|
424
|
-
const fields = afterComm.split(/\s+/);
|
|
425
|
-
return fields[19] ?? null;
|
|
426
|
-
} catch {
|
|
427
|
-
return null;
|
|
428
|
-
}
|
|
429
|
-
}
|
|
@@ -36,6 +36,31 @@ const CONVENTIONAL_RE =
|
|
|
36
36
|
const VERSION_SQUASH_RE = /^\d+\.\d+\.\d+(?:[-.][\w.]+)?\s*\(#\d+\)$/;
|
|
37
37
|
/** Functional whitelist for verify (CHANGELOG-worthy types). */
|
|
38
38
|
const FUNCTIONAL_TYPES = new Set(["feat", "fix", "refactor", "docs", "perf"]);
|
|
39
|
+
/** A PR ref sitting at the very end of a subject, e.g. the `(#208)` in `... (#208)`. */
|
|
40
|
+
const TRAILING_PR_RE = /\s*\(#(\d+)\)\s*$/;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Strip trailing PR refs from a subject, keeping the rightmost as the PR.
|
|
44
|
+
*
|
|
45
|
+
* GitHub merge/squash subjects can carry the number twice (`... (#208) (#208)`),
|
|
46
|
+
* and the conventional regex above captures only the last one, leaving the
|
|
47
|
+
* earlier copy inside the message where the renderer appends a ref again
|
|
48
|
+
* (issue #218). Refs in the middle of a subject (`... (issue #189) (#196)`) are
|
|
49
|
+
* not tail refs and stay verbatim.
|
|
50
|
+
* @param {string} subject
|
|
51
|
+
* @returns {{message: string, pr: number|null}}
|
|
52
|
+
*/
|
|
53
|
+
export function stripTrailingPrRefs(subject) {
|
|
54
|
+
let text = String(subject ?? "").trim();
|
|
55
|
+
let pr = null;
|
|
56
|
+
for (let m = text.match(TRAILING_PR_RE); m; m = text.match(TRAILING_PR_RE)) {
|
|
57
|
+
// Rightmost ref wins: GitHub appends its own number after any that the
|
|
58
|
+
// author already wrote, so the first match is the authoritative one.
|
|
59
|
+
if (pr === null) pr = Number(m[1]);
|
|
60
|
+
text = text.slice(0, m.index).trimEnd();
|
|
61
|
+
}
|
|
62
|
+
return { message: text, pr };
|
|
63
|
+
}
|
|
39
64
|
|
|
40
65
|
/**
|
|
41
66
|
* Parse commit subject lines into changelog entries.
|
|
@@ -51,12 +76,14 @@ export function parseCommitLines(lines) {
|
|
|
51
76
|
if (VERSION_SQUASH_RE.test(s)) continue;
|
|
52
77
|
if (/bump\s+version/i.test(s)) continue;
|
|
53
78
|
if (s.startsWith("Merge ")) continue;
|
|
54
|
-
const
|
|
55
|
-
const
|
|
79
|
+
const { message: normalized, pr: tailPr } = stripTrailingPrRefs(s);
|
|
80
|
+
const m = normalized.match(CONVENTIONAL_RE);
|
|
56
81
|
const entry = m
|
|
57
|
-
? { type: m[1], scope: m[2] || "", message: m[3].trim(), pr: m[4] ? Number(m[4]) : null }
|
|
58
|
-
: { type: "other", scope: "", message:
|
|
59
|
-
|
|
82
|
+
? { type: m[1], scope: m[2] || "", message: m[3].trim(), pr: tailPr ?? (m[4] ? Number(m[4]) : null) }
|
|
83
|
+
: { type: "other", scope: "", message: normalized, pr: tailPr };
|
|
84
|
+
// PR-aware key: two commits with the same text merged under different
|
|
85
|
+
// PRs must both keep their number (issue #218).
|
|
86
|
+
const key = `${entry.type}|${entry.scope}|${entry.message}|${entry.pr ?? ""}`;
|
|
60
87
|
if (seen.has(key)) continue;
|
|
61
88
|
seen.add(key);
|
|
62
89
|
entries.push(entry);
|
|
@@ -83,8 +110,13 @@ export function generateChangelog({ version, date, entries, prevTag = null }) {
|
|
|
83
110
|
lines.push(`### ${title}`, "");
|
|
84
111
|
for (const e of group) {
|
|
85
112
|
const scope = e.scope ? `**${e.scope}**: ` : "";
|
|
86
|
-
|
|
87
|
-
|
|
113
|
+
// Render defensively: an entry handed in with the ref already in its
|
|
114
|
+
// message (parse-time normalization missed, hand-authored fixtures)
|
|
115
|
+
// must still emit the number exactly once (issue #218).
|
|
116
|
+
const { message, pr: embeddedPr } = stripTrailingPrRefs(e.message);
|
|
117
|
+
const prNumber = e.pr ?? embeddedPr;
|
|
118
|
+
const pr = prNumber ? ` (#${prNumber})` : "";
|
|
119
|
+
lines.push(`- ${scope}${message}${pr}`);
|
|
88
120
|
}
|
|
89
121
|
lines.push("");
|
|
90
122
|
}
|