@event4u/agent-config 9.21.0 → 9.22.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.
@@ -16,11 +16,18 @@
16
16
  * scope-overlap Scan every worktree's .worktree-scope.md and
17
17
  * report pairwise `owns:` overlaps (the hazard
18
18
  * /worktree status + verify must surface).
19
+ * inventory Classify EVERY worktree safe / review / live and
20
+ * report the counts, the review reasons, and a
21
+ * prepared removal plan for the safe set. Reporting
22
+ * only — it never removes anything, because bulk
23
+ * worktree + branch deletion is a Hard-Floor action
24
+ * (`non-destructive-by-default`) that needs the
25
+ * user's explicit this-turn approval.
19
26
  *
20
27
  * Reachability counts ALL other refs — local branches, remotes, AND tags
21
28
  * (a branch whose only other ref is a tag is still reachable elsewhere).
22
- * Exit codes: 0 = allowed / no overlap, 1 = refused / overlap found,
23
- * 2 = usage error, 3 = internal error.
29
+ * Exit codes: 0 = allowed / no overlap / inventory reported, 1 = refused /
30
+ * overlap found, 2 = usage error, 3 = internal error.
24
31
  */
25
32
 
26
33
  import { execFileSync } from 'node:child_process';
@@ -53,6 +60,17 @@ function tryGit(cwd: string, args: string[]): string | null {
53
60
  }
54
61
  }
55
62
 
63
+ /**
64
+ * `git status` with the index left alone. Plain `git status` refreshes the
65
+ * on-disk index, which bumps its mtime — and the inventory mode reads that
66
+ * mtime as the "another session is working here" signal. Without
67
+ * `--no-optional-locks` the check corrupts the very signal it is measured
68
+ * against: two consecutive inventory runs moved 10 worktrees from safe to live.
69
+ */
70
+ function statusPorcelain(cwd: string): string {
71
+ return git(cwd, ['--no-optional-locks', 'status', '--porcelain']);
72
+ }
73
+
56
74
  /** Refs (heads + remotes + tags) EXCLUDING the given branch's own head ref. */
57
75
  function otherRefs(cwd: string, branch: string): string[] {
58
76
  const out = git(cwd, ['for-each-ref', '--format=%(refname)']);
@@ -77,7 +95,7 @@ export function checkWorktree(worktreePath: string): CheckResult {
77
95
  }
78
96
  const branch = headRef.trim().replace(/^refs\/heads\//, '');
79
97
 
80
- const status = git(worktreePath, ['status', '--porcelain']);
98
+ const status = statusPorcelain(worktreePath);
81
99
  if (status.trim().length > 0) {
82
100
  const files = status
83
101
  .trimEnd()
@@ -194,9 +212,260 @@ export function findScopeOverlaps(repoPath: string): ScopeOverlap[] {
194
212
  return overlaps;
195
213
  }
196
214
 
215
+ /**
216
+ * A worktree is "live" when another session may be working in it. Git touches
217
+ * the per-worktree index on almost every command, so its mtime is the cheapest
218
+ * available activity signal.
219
+ */
220
+ export const LIVE_WINDOW_HOURS = 48;
221
+
222
+ /**
223
+ * The two conventional worktree roots, relative to the repo. Anything else is
224
+ * a non-standard location: it is not wrong, but it is excluded from the safe
225
+ * set on purpose — worktrees that sit beside the repo can be mistaken for
226
+ * sibling packages, so their removal is a judgement the maintainer makes.
227
+ */
228
+ export const STANDARD_WORKTREE_DIRS = ['.claude/worktrees', '.worktrees'] as const;
229
+
230
+ /**
231
+ * Symlink-resolved absolute path, falling back to a plain resolve when the
232
+ * path is gone. Git reports worktree paths as realpaths, so a repo reached
233
+ * through a symlinked parent (`/tmp` → `/private/tmp` on macOS, or any
234
+ * symlinked checkout root) would otherwise fail every path comparison and
235
+ * mis-report conventional worktrees as non-standard.
236
+ */
237
+ function canonical(p: string): string {
238
+ const abs = path.resolve(p);
239
+ const tail: string[] = [];
240
+ let head = abs;
241
+ for (;;) {
242
+ try {
243
+ // Resolve the longest existing ancestor, then re-append the rest —
244
+ // a registration pointing at an already-deleted directory still has
245
+ // to classify its location correctly.
246
+ const real = fs.realpathSync(head);
247
+ return tail.length === 0 ? real : path.join(real, ...tail);
248
+ } catch {
249
+ const parent = path.dirname(head);
250
+ if (parent === head) return abs;
251
+ tail.unshift(path.basename(head));
252
+ head = parent;
253
+ }
254
+ }
255
+ }
256
+
257
+ export type Classification = 'safe' | 'review' | 'live';
258
+
259
+ export interface InventoryRow {
260
+ path: string;
261
+ branch: string | null;
262
+ classification: Classification;
263
+ /** Why it is not safe. Empty exactly when `classification === 'safe'`. */
264
+ reasons: string[];
265
+ /** The branch is an ancestor of the trunk, so `git branch -d` is safe. */
266
+ mergedIntoTrunk: boolean;
267
+ standardLocation: boolean;
268
+ /** Main worktree — never removable, and never counted as residue. */
269
+ isMain: boolean;
270
+ }
271
+
272
+ export interface Inventory {
273
+ trunk: string;
274
+ rows: InventoryRow[];
275
+ counts: {
276
+ total: number;
277
+ safe: number;
278
+ review: number;
279
+ live: number;
280
+ /** Review rows grouped by reason, highest first. */
281
+ reviewReasons: Record<string, number>;
282
+ };
283
+ }
284
+
285
+ /** First existing trunk ref, preferring the remote (the merge authority). */
286
+ export function resolveTrunk(repoPath: string): string {
287
+ for (const ref of [
288
+ 'refs/remotes/origin/main',
289
+ 'refs/remotes/origin/master',
290
+ 'refs/heads/main',
291
+ 'refs/heads/master',
292
+ ]) {
293
+ if (tryGit(repoPath, ['rev-parse', '--verify', '--quiet', ref]) !== null) return ref;
294
+ }
295
+ return 'HEAD';
296
+ }
297
+
298
+ function isAncestor(repoPath: string, branch: string, trunk: string): boolean {
299
+ try {
300
+ git(repoPath, ['merge-base', '--is-ancestor', branch, trunk]);
301
+ return true;
302
+ } catch {
303
+ return false;
304
+ }
305
+ }
306
+
307
+ export function isStandardLocation(repoPath: string, worktreePath: string): boolean {
308
+ const rel = path.relative(canonical(repoPath), canonical(worktreePath));
309
+ if (rel.startsWith('..') || path.isAbsolute(rel)) return false;
310
+ return STANDARD_WORKTREE_DIRS.some(
311
+ (dir) => rel === dir || rel.startsWith(`${dir}${path.sep}`),
312
+ );
313
+ }
314
+
315
+ /**
316
+ * Per-worktree git-dir mtime, or null when it cannot be read. A linked
317
+ * worktree's `.git` is a file holding `gitdir: <path>`; the main worktree's is
318
+ * a directory.
319
+ */
320
+ function lastGitActivity(worktreePath: string): Date | null {
321
+ const dotGit = path.join(worktreePath, '.git');
322
+ let gitDir: string;
323
+ try {
324
+ const st = fs.statSync(dotGit);
325
+ if (st.isDirectory()) {
326
+ gitDir = dotGit;
327
+ } else {
328
+ const m = fs.readFileSync(dotGit, 'utf-8').match(/^gitdir:\s*(.+)$/m);
329
+ if (!m) return null;
330
+ gitDir = path.resolve(worktreePath, m[1]!.trim());
331
+ }
332
+ } catch {
333
+ return null;
334
+ }
335
+ let newest: number | null = null;
336
+ for (const name of ['index', 'HEAD']) {
337
+ try {
338
+ const t = fs.statSync(path.join(gitDir, name)).mtimeMs;
339
+ if (newest === null || t > newest) newest = t;
340
+ } catch {
341
+ /* absent is not an error — the other candidate may still exist */
342
+ }
343
+ }
344
+ return newest === null ? null : new Date(newest);
345
+ }
346
+
347
+ /**
348
+ * Classify every worktree. `now` is injectable so the live-window boundary is
349
+ * testable rather than wall-clock dependent.
350
+ */
351
+ export function buildInventory(repoPath: string, now: Date = new Date()): Inventory {
352
+ const trunk = resolveTrunk(repoPath);
353
+ const entries = listWorktrees(repoPath);
354
+ const mainPath = canonical(entries.length > 0 ? entries[0]!.path : repoPath);
355
+ const liveCutoff = now.getTime() - LIVE_WINDOW_HOURS * 3600 * 1000;
356
+
357
+ const rows: InventoryRow[] = entries.map((e) => {
358
+ const resolved = canonical(e.path);
359
+ const isMain = resolved === mainPath;
360
+ const standardLocation = isMain ? true : isStandardLocation(repoPath, resolved);
361
+ const reasons: string[] = [];
362
+
363
+ if (!fs.existsSync(resolved)) {
364
+ return {
365
+ path: e.path,
366
+ branch: e.branch,
367
+ classification: 'review',
368
+ reasons: ['registration points at a missing directory — `git worktree prune` clears it'],
369
+ mergedIntoTrunk: false,
370
+ standardLocation,
371
+ isMain,
372
+ };
373
+ }
374
+
375
+ const activity = lastGitActivity(resolved);
376
+ if (activity !== null && activity.getTime() >= liveCutoff) {
377
+ return {
378
+ path: e.path,
379
+ branch: e.branch,
380
+ classification: 'live',
381
+ reasons: [`git activity within the last ${LIVE_WINDOW_HOURS}h — another session may hold it`],
382
+ mergedIntoTrunk: e.branch !== null && isAncestor(resolved, e.branch, trunk),
383
+ standardLocation,
384
+ isMain,
385
+ };
386
+ }
387
+
388
+ if (isMain) reasons.push('main worktree — cannot be removed');
389
+ if (e.branch === null) {
390
+ reasons.push('detached HEAD — no branch to judge reachability for');
391
+ }
392
+ if (!standardLocation) {
393
+ reasons.push('non-standard location — outside the conventional worktree roots');
394
+ }
395
+
396
+ const mergedIntoTrunk = e.branch !== null && isAncestor(resolved, e.branch, trunk);
397
+
398
+ // A branch that is an ancestor of the trunk has every commit reachable
399
+ // from the trunk ref, so the expensive rev-list gate cannot find a
400
+ // unique commit. Only run it for the branches that are NOT merged —
401
+ // that is where it can actually refuse.
402
+ if (e.branch !== null && !mergedIntoTrunk) {
403
+ reasons.push(`branch is not an ancestor of ${trunk} — unmerged work`);
404
+ const gate = checkWorktree(resolved);
405
+ if (!gate.allowed) reasons.push(...gate.reasons);
406
+ } else if (e.branch !== null) {
407
+ const status = statusPorcelain(resolved);
408
+ if (status.trim().length > 0) {
409
+ const n = status.trimEnd().split('\n').length;
410
+ reasons.push(`unsaved work — ${n} dirty or untracked path(s)`);
411
+ }
412
+ }
413
+
414
+ return {
415
+ path: e.path,
416
+ branch: e.branch,
417
+ classification: reasons.length === 0 ? 'safe' : 'review',
418
+ reasons,
419
+ mergedIntoTrunk,
420
+ standardLocation,
421
+ isMain,
422
+ };
423
+ });
424
+
425
+ const reviewReasons: Record<string, number> = {};
426
+ for (const r of rows) {
427
+ if (r.classification !== 'review') continue;
428
+ // Group by the first reason: it is the primary disqualifier, and the
429
+ // per-row reasons stay available in --json for the full picture.
430
+ const key = (r.reasons[0] ?? 'unknown').split('\n')[0]!;
431
+ reviewReasons[key] = (reviewReasons[key] ?? 0) + 1;
432
+ }
433
+
434
+ return {
435
+ trunk,
436
+ rows,
437
+ counts: {
438
+ total: rows.length,
439
+ safe: rows.filter((r) => r.classification === 'safe').length,
440
+ review: rows.filter((r) => r.classification === 'review').length,
441
+ live: rows.filter((r) => r.classification === 'live').length,
442
+ reviewReasons: Object.fromEntries(
443
+ Object.entries(reviewReasons).sort((a, b) => b[1] - a[1]),
444
+ ),
445
+ },
446
+ };
447
+ }
448
+
449
+ /**
450
+ * The removal commands for the safe set — printed for a human to review and
451
+ * run, never executed here. `git branch -d` (never `-D`) so git itself
452
+ * re-checks the merge before the branch goes.
453
+ */
454
+ export function removalPlan(inv: Inventory): string[] {
455
+ return inv.rows
456
+ .filter((r) => r.classification === 'safe')
457
+ .map((r) => {
458
+ const remove = `git worktree remove ${JSON.stringify(r.path)}`;
459
+ return r.branch !== null && r.mergedIntoTrunk
460
+ ? `${remove} && git branch -d ${JSON.stringify(r.branch)}`
461
+ : remove;
462
+ });
463
+ }
464
+
197
465
  function usage(): never {
198
466
  process.stderr.write(
199
- 'usage: worktree_cleanup_check check <worktree-path> | scope-overlap [repo-path]\n',
467
+ 'usage: worktree_cleanup_check check <worktree-path> | scope-overlap [repo-path] | ' +
468
+ 'inventory [repo-path] [--json|--plan]\n',
200
469
  );
201
470
  process.exit(2);
202
471
  }
@@ -236,6 +505,52 @@ export function main(argv: readonly string[] = process.argv.slice(2)): number {
236
505
  }
237
506
  return 1;
238
507
  }
508
+ if (mode === 'inventory') {
509
+ const rest = argv.slice(1);
510
+ const flags = rest.filter((a) => a.startsWith('--'));
511
+ const positional = rest.filter((a) => !a.startsWith('--'));
512
+ if (positional.length > 1 || flags.some((f) => f !== '--json' && f !== '--plan')) usage();
513
+ const repo = path.resolve(positional[0] ?? '.');
514
+ const inv = buildInventory(repo);
515
+
516
+ if (flags.includes('--json')) {
517
+ process.stdout.write(`${JSON.stringify(inv, null, 2)}\n`);
518
+ return 0;
519
+ }
520
+ if (flags.includes('--plan')) {
521
+ const plan = removalPlan(inv);
522
+ if (plan.length === 0) {
523
+ process.stdout.write('# no safe worktrees — nothing to propose.\n');
524
+ return 0;
525
+ }
526
+ process.stdout.write(
527
+ `# Prepared removal plan for ${plan.length} safe worktree(s).\n` +
528
+ '# NOT executed: bulk worktree + branch deletion is a Hard-Floor action that\n' +
529
+ '# needs the maintainer\'s explicit approval. Review, then run deliberately.\n',
530
+ );
531
+ process.stdout.write(`${plan.join('\n')}\n`);
532
+ return 0;
533
+ }
534
+
535
+ const c = inv.counts;
536
+ process.stdout.write(
537
+ `worktree inventory · ${c.total} registered · trunk ${inv.trunk}\n` +
538
+ ` safe ${c.safe}\n` +
539
+ ` review ${c.review}\n` +
540
+ ` live ${c.live}\n`,
541
+ );
542
+ if (c.review > 0) {
543
+ process.stdout.write('\nreview reasons (primary disqualifier):\n');
544
+ for (const [reason, n] of Object.entries(c.reviewReasons)) {
545
+ process.stdout.write(` ${String(n).padStart(4)} ${reason}\n`);
546
+ }
547
+ }
548
+ process.stdout.write(
549
+ '\nThis mode reports only. `--plan` prints the removal commands for the safe set;\n' +
550
+ 'running them is a Hard-Floor action needing the maintainer\'s explicit approval.\n',
551
+ );
552
+ return 0;
553
+ }
239
554
  usage();
240
555
  }
241
556