Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ All notable changes to Bobby are documented here. The format is based on
studio repo, from `git worktree list --porcelain`); sizes (`du`, slow) show only from the cache
or after `status --sizes`. `conductor reap --worktrees` (dry run unless `--apply`, M4 only)
removes a worktree with `git worktree remove --force` (branch kept) only when every ticket named
in its folder name is done, `git status --porcelain -uno` is empty, no live session's cwd is
in its folder name is done, `git status --porcelain --untracked-files=normal` is empty (no modified
or staged file and no untracked, non-ignored file such as a review.md or test-evidence/ — the dry run
names them, BOB-191; ignored files like node_modules do not block it), no live session's cwd is
inside it and a detached HEAD is already on a branch; folders that are not git worktrees are listed, never touched. `--sizes` measures the
candidates. A failing git or claude is an error.
- **`bobby conductor` (BOB-174): read-only studio status board.** `bobby conductor init`
Expand Down
33 changes: 24 additions & 9 deletions lib/conductor/worktree-reaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
// candidates and a locked or prunable one (folder already gone) is kept.
// - the ticket ID in its folder name is on a studio board and EVERY ticket named is done.
// No ticket in the name, or one that is not on a board, keeps it.
// - `git status --porcelain -uno` is empty: no modified or staged tracked file. Untracked
// and ignored files (node_modules, .next, logs) do not block it and go with the folder;
// committed work is on the branch, which stays.
// - `git status --porcelain --untracked-files=normal` is empty: no modified or staged tracked
// file and no untracked, non-ignored file (a review.md or test-evidence/ nobody committed
// is work that would go with the folder, BOB-191). Only IGNORED files (node_modules, .next,
// logs) go with the folder; committed work is on the branch, which stays. The untracked
// mode is passed explicitly so a user's status.showUntrackedFiles=no cannot hide them.
// - a detached HEAD must already be on some local or remote branch: a detached worktree has
// no branch of its own to keep, so removing it would orphan commits made on it.
// - no live session's cwd is inside it. Live means any session `claude agents` lists that
Expand Down Expand Up @@ -48,14 +50,21 @@ export function liveCwds(sessions) {
return sessions.filter(s => !ended.has(s) && s.cwd).map(s => ({ cwd: real(s.cwd), name: s.name }));
}

/** "review.md, test-evidence/ (+3)": the first two entries git status lists, then how many more. */
function untrackedSummary(entries) {
const shown = entries.slice(0, 2).join(', ');
return entries.length > 2 ? `${shown} (+${entries.length - 2})` : shown;
}

function describe(w) {
return { repo: w.repo, path: w.path, branch: w.branch, ticket: w.ticket_ids.join(', ') || null, stage: w.stage };
}

/**
* Sort classified worktrees (worktrees.js classifyWorktrees) into candidates and kept
* ones, each kept one with its reason. `dirty(path)` returns the porcelain text of
* `git status -uno` for a path; it is only called for a worktree that passed every other rule.
* `git status --untracked-files=normal` for a path (ignored files are not listed); it is only
* called for a worktree that passed every other rule.
* `onBranch(path)` says whether a detached HEAD is already on a branch; only asked for detached ones.
*/
export function evaluateWorktrees(worktrees, { live, cwd, dirty, onBranch }) {
Expand All @@ -80,9 +89,15 @@ export function evaluateWorktrees(worktrees, { live, cwd, dirty, onBranch }) {
kept.push({ ...describe(w), why: 'git status failed', error: e.message, notable: true });
continue;
}
if (changes.trim()) {
const n = changes.trim().split('\n').length;
keep(`${n} tracked file(s) modified or staged`, { notable: true, group: 'tracked files modified or staged' });
const lines = changes.split('\n').filter(l => l.trim());
const untracked = lines.filter(l => l.startsWith('??')).map(l => l.slice(3));
const tracked = lines.length - untracked.length;
if (tracked > 0) {
keep(`${tracked} tracked file(s) modified or staged`, { notable: true, group: 'tracked files modified or staged' });
continue;
}
if (untracked.length > 0) {
keep(`untracked: ${untrackedSummary(untracked)}`, { notable: true, group: 'untracked files that are not ignored' });
continue;
}
if (w.detached) {
Expand All @@ -95,13 +110,13 @@ export function evaluateWorktrees(worktrees, { live, cwd, dirty, onBranch }) {
}
if (!safe) { keep('detached HEAD is on no branch: removing it would orphan its commits', { notable: true }); continue; }
}
candidates.push({ ...describe(w), repo_path: w.repo_path, reason: `${w.ticket_ids.join(', ')} done, tracked files clean, no live session inside` });
candidates.push({ ...describe(w), repo_path: w.repo_path, reason: `${w.ticket_ids.join(', ')} done, no uncommitted or untracked files, no live session inside` });
}
return { candidates, kept };
}

function readDirty(run) {
return (p) => run('git', ['-C', p, 'status', '--porcelain', '-uno']);
return (p) => run('git', ['-C', p, 'status', '--porcelain', '--untracked-files=normal']);
}

/** True when a detached HEAD is contained in at least one local or remote branch. */
Expand Down
2 changes: 1 addition & 1 deletion test/lib/conductor/read-only.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('the worktree reaper does exactly one removal', () => {
test('`git -C <repo> worktree remove --force <path>` is the only exec that changes anything', () => {
const runs = [...code.matchAll(/\brun\(([^)]*)\)/g)].map(m => m[1].replace(/\s+/g, ' '));
expect(runs).toEqual([
"'git', ['-C', p, 'status', '--porcelain', '-uno']",
"'git', ['-C', p, 'status', '--porcelain', '--untracked-files=normal']",
"'git', ['-C', p, 'for-each-ref', '--contains', 'HEAD', 'refs/heads', 'refs/remotes']",
"'git', ['-C', still.repo_path, 'worktree', 'remove', '--force', still.path]",
]);
Expand Down
64 changes: 61 additions & 3 deletions test/lib/conductor/worktree-reaper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ describe('conductor worktree reaper', () => {
beforeEach(() => {
({ root, hookDir } = makeStudio());
fs.writeFileSync(path.join(root, 'tracked.txt'), 'a\n');
git(root, 'add', 'tracked.txt');
fs.writeFileSync(path.join(root, '.gitignore'), '*.log\nbuild-cache/\n');
git(root, 'add', 'tracked.txt', '.gitignore');
git(root, 'commit', '-q', '-m', 'init');
app = fs.mkdtempSync(path.join(os.tmpdir(), 'bobby-wt-app-'));
execFileSync('git', ['init', '-q', app]);
Expand Down Expand Up @@ -109,15 +110,60 @@ describe('conductor worktree reaper', () => {
expect(report.failed).toBe(0);
});

test('untracked and ignored files do not block removal (only tracked changes do)', () => {
test('ignored files do not block removal (they are rebuildable)', () => {
finish(1);
const p = wt(root, 'AL-1-ui');
fs.writeFileSync(path.join(p, 'scratch.log'), 'x');
fs.mkdirSync(path.join(p, 'build-cache'));
fs.writeFileSync(path.join(p, 'build-cache', 'a'), 'x');
const report = go(mockRun(), { apply: true });
expect(report.results[0].outcome).toBe('removed');
expect(exists(p)).toBe(false);
});

test('untracked, non-ignored files and folders keep the worktree and the dry run says which (BOB-191)', () => {
finish(1); finish(2); finish(3);
const one = wt(root, 'AL-1-ui');
const many = wt(root, 'AL-2-ui');
const clean = wt(root, 'AL-3-ui');
fs.writeFileSync(path.join(one, 'review.md'), 'x');
fs.mkdirSync(path.join(one, 'test-evidence'));
fs.writeFileSync(path.join(one, 'test-evidence', 'shot.png'), 'x');
for (const f of ['a.txt', 'b.txt', 'c.txt', 'd.txt']) fs.writeFileSync(path.join(many, f), 'x');
const m = mockRun();
const report = go(m, { apply: true });
expect(report.candidates.map(c => c.path)).toEqual([clean]);
const why = Object.fromEntries(report.kept.map(k => [path.basename(k.path), k.why]));
expect(why['AL-1-ui']).toBe('untracked: review.md, test-evidence/');
expect(why['AL-2-ui']).toBe('untracked: a.txt, b.txt (+2)');
expect(m.removals()).toEqual([['git', '-C', root, 'worktree', 'remove', '--force', clean]]);
expect(exists(path.join(one, 'review.md'))).toBe(true);
expect(exists(path.join(many, 'd.txt'))).toBe(true);
const text = renderWorktreeReap(report, { apply: false });
expect(text).toMatch(/2 {2}untracked files that are not ignored/);
expect(text).toMatch(/kept .*AL-1-ui.*untracked: review\.md, test-evidence\//);
});

test('a user setting status.showUntrackedFiles=no cannot hide untracked files from the check', () => {
finish(1);
const p = wt(root, 'AL-1-ui');
fs.writeFileSync(path.join(p, 'review.md'), 'x');
git(root, 'config', 'status.showUntrackedFiles', 'no');
const report = go(mockRun(), { apply: true });
expect(report.candidates).toEqual([]);
expect(report.kept[0].why).toBe('untracked: review.md');
expect(exists(path.join(p, 'review.md'))).toBe(true);
});

test('tracked changes are reported ahead of untracked ones', () => {
finish(1);
const p = wt(root, 'AL-1-ui');
fs.appendFileSync(path.join(p, 'tracked.txt'), 'edit\n');
fs.writeFileSync(path.join(p, 'review.md'), 'x');
const report = go(mockRun());
expect(report.kept[0].why).toBe('1 tracked file(s) modified or staged');
});

test('a session that has ended (done, no pid) does not keep a worktree; interactive and blocked ones do', () => {
finish(1); finish(3); finish(4);
const ended = wt(root, 'AL-1-ui');
Expand Down Expand Up @@ -145,6 +191,18 @@ describe('conductor worktree reaper', () => {
expect(exists(p)).toBe(true);
});

test('a review.md written between the plan and the removal keeps the worktree', () => {
finish(1);
const p = wt(root, 'AL-1-ui');
// claude agents is read once per snapshot; write the file at the second read, before the re-check's git status
const m = mockRun({ agents: (reads) => { if (reads === 2) fs.writeFileSync(path.join(p, 'review.md'), 'x'); return []; } });
const report = go(m, { apply: true });
expect(report.candidates).toHaveLength(1);
expect(report.results[0]).toMatchObject({ outcome: 'skipped', detail: 'untracked: review.md' });
expect(m.removals()).toEqual([]);
expect(exists(path.join(p, 'review.md'))).toBe(true);
});

test('the folder this command runs from is never removed', () => {
finish(1);
const p = wt(root, 'AL-1-ui');
Expand Down Expand Up @@ -247,7 +305,7 @@ describe('conductor worktree reaper', () => {
test('--sizes measures the candidates with du and caches them; without it no du runs', () => {
finish(1);
const p = wt(root, 'AL-1-ui');
fs.writeFileSync(path.join(p, 'big.bin'), Buffer.alloc(2 * 1024 * 1024));
fs.writeFileSync(path.join(p, 'big.log'), Buffer.alloc(2 * 1024 * 1024));
const plain = mockRun();
expect(go(plain).sizes).toBeNull();
expect(plain.calls.filter(c => c[0] === 'du')).toEqual([]);
Expand Down
Loading