/careful no longer asks before deleting a file inside another worktree of the same repo (#4827) (#4834)

/careful no longer asks before every plain `rm` of a file inside another
`git worktree` of the same repository, so people who run several
worktrees side by side stop clicking through a prompt on each cleanup.

It keeps the safety check quiet only where it was wrong (gstack's goal:
guards that stop real mistakes, not routine work), and every other
delete still asks.

Nothing to do.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/specialist-batched#1 reader=testing
Review-Finding: p1/adversarial-native#1 reader=adversarial-native
Review-Finding: p1/adversarial-native#2 reader=adversarial-native
Review-Finding: p2/red-team#1 reader=red-team
This commit is contained in:
Garry Tanandcapy-ai[bot] authored and GitHub committed 2026-11-25 18:47:20 +00:00
1 parent 8aafd408e2
commit a48df18f51
4 files changed
+232 -3

No files matched your search

+122
View File
@@ -1274,6 +1274,128 @@ describe('check-careful.sh PowerShell and cmd coverage (#3067)', () => {
// The freeze fix (#3623) made the shared extractor keep trailing newlines and
// map lone surrogates on PATH fields only; a command must come out exactly as
// before, on both extractor branches.
// #4827: deletes inside another worktree of the session's own repository. The
// fixture lives outside /tmp, which the check already treats as scratch, so
// every ask below is the check's real decision. POSIX symlinks and paths.
describe.skipIf(process.platform === 'win32')('check-careful.sh sibling git worktrees (#4827)', () => {
const real = (p: string) => fs.realpathSync(p);
const scratchTmp = /^\/(private\/)?(tmp|var\/tmp)(\/|$)/.test(real(os.tmpdir()));
function withWorktrees(fn: (b: string) => void) {
const b = real(fs.mkdtempSync(path.join(scratchTmp ? os.homedir() : os.tmpdir(), '.gstack-careful-wt-')));
try {
const git = (cwd: string, args: string[]) => {
const r = gitArgvIn(cwd, args, 10_000);
if (r.status !== 0) throw new Error(`git ${args.join(' ')}: ${r.stderr}`);
};
for (const d of ['app/src', 'other/src', 'outside', 'plain']) fs.mkdirSync(path.join(b, d), { recursive: true });
fs.writeFileSync(path.join(b, 'app', 'src', 'a.txt'), 'a\n');
fs.writeFileSync(path.join(b, 'other', 'src', 'a.txt'), 'a\n');
fs.writeFileSync(path.join(b, 'outside', 'secret'), 's\n');
for (const repo of ['app', 'other']) {
git(path.join(b, repo), ['init', '-q', '-b', 'main']);
git(path.join(b, repo), ['add', '.']);
git(path.join(b, repo), ['commit', '-q', '-m', 'init']);
}
git(path.join(b, 'app'), ['worktree', 'add', '-q', path.join(b, 'app-192'), '-b', 'feat']);
git(path.join(b, 'app'), ['worktree', 'add', '-q', path.join(b, 'app', '.claude', 'worktrees', 'x'), '-b', 'nested']);
git(path.join(b, 'app'), ['worktree', 'add', '-q', path.join(b, 'gone'), '-b', 'gone']);
fs.rmSync(path.join(b, 'gone'), { recursive: true, force: true });
fs.mkdirSync(path.join(b, 'gone', 'src'), { recursive: true });
git(path.join(b, 'app'), ['worktree', 'prune']);
fs.symlinkSync(path.join(b, 'outside'), path.join(b, 'app-192', 'escape'));
fs.symlinkSync(path.join(b, 'outside', 'secret'), path.join(b, 'app-192', 'link-file'));
fn(b);
} finally {
fs.rmSync(b, { recursive: true, force: true });
}
}
const decide = (b: string, cwd: string, command: string, env: Record<string, string> = {}) => {
const { exitCode, output } = runHook(CAREFUL_SCRIPT, carefulInput(command.replaceAll('B/', `${b}/`)), env, path.join(b, cwd));
expect(exitCode).toBe(0);
return output.hookSpecificOutput?.permissionDecision ?? 'allow';
};
test('a file delete inside a sibling worktree, either way round or from a nested worktree, is allowed', () => {
withWorktrees((b) => {
for (const [cwd, command] of [
['app', 'rm B/app-192/src/a.txt'],
['app', 'rm -f B/app-192/src/debug.spec.ts'],
['app', 'rm -fv --interactive=never -- B/app-192/src/a.txt'],
['app', 'unlink B/app-192/src/a.txt'],
['app', 'rm B/app-192/src/a.txt && rm -f B/app-192/src/b.txt; unlink B/app-192/src/c.txt'],
['app-192', 'rm B/app/src/a.txt'],
['app/.claude/worktrees/x', 'rm -f B/app/src/a.txt'],
['app-192', 'rm B/app/.claude/worktrees/x/src/a.txt'],
]) {
expect([command, decide(b, cwd, command)]).toEqual([command, 'allow']);
}
});
}, 30_000);
test('roots, .git, the git folder, .., symlinks, globs, recursive deletes, find, rsync, shred and commands that run anything else still ask', () => {
withWorktrees((b) => {
for (const [cwd, command] of [
['app', 'rm -f B/app-192'],
['app', 'rm -f B/app-192/'],
['app', 'rm -rf B/app-192/src'],
['app', 'rm --rec B/app-192/src'],
['app', 'rm --r -f B/app-192/src'],
['app', 'rm --recurs B/app-192/src'],
['app', 'rm -fx B/app-192/src/a.txt'],
['app', 'rm --no-preserve-root B/app-192/src/a.txt'],
['app', 'find B/app-192 -delete'],
['app', 'rsync -a --delete ./src/ B/app-192'],
['app-192', 'rm -f B/app'],
['app-192', 'rm -f B/app/.claude/worktrees/x'],
['app-192', 'find B/app/.claude -delete'],
['app/.claude/worktrees/x', 'rm -f B/app/.claude'],
['app', 'rm B/app-192/.git'],
['app', 'rm B/app-192/.GIT'],
['app-192', 'rm B/app/.git/config'],
['app-192', 'rm -f B/app/.git/worktrees/app-192/HEAD'],
['app', 'rm B/app-192/src/../../outside/secret'],
['app', 'rm B/app-192/escape/secret'],
['app', 'rm B/app-192/link-file'],
['app', 'shred -u B/app-192/escape/secret'],
['app', 'rm B/app-192/src/*.txt'],
['app', 'rm -f B/app-192/.g*'],
['app', 'rm $W/src/a.txt'],
['app', 'git worktree remove --force B/app-192'],
['app', 'find B/app-192/src -name "*.log" -delete'],
['app', 'find -L B/app-192/src -delete'],
['app', 'find B/app-192/src -exec rm {} +'],
['app', 'rsync -a --delete ./src/ B/app-192/src/'],
['app', 'shred -u B/app-192/src/a.txt'],
['app', 'sudo rm B/app-192/src/a.txt'],
['app', 'echo done && rm B/app-192/src/a.txt'],
['app', 'mv B/app-192/src B/app-192/old && ln -s ~ B/app-192/src && rm B/app-192/src/.bashrc'],
['app', 'rm B/app-192/src/a.txt; ln -s ~ B/app-192/d; rm B/app-192/d/.bashrc'],
['app', 'rm B/app-192/d/.bashrc & mv B/app-192/d B/app-192/e'],
['app', 'rm -f B/app-192/src/a.txt | cat'],
]) {
expect([command, decide(b, cwd, command)]).toEqual([command, 'ask']);
}
});
}, 30_000);
test('another repository, a pruned worktree, a non-git folder, GIT_DIR, a home-folder repository and a worktree that is a personal folder grant nothing', () => {
withWorktrees((b) => {
expect(decide(b, 'app', 'rm B/other/src/a.txt')).toBe('ask');
expect(decide(b, 'other', 'rm B/app-192/src/a.txt')).toBe('ask');
expect(decide(b, 'app', 'rm B/gone/src/a.txt')).toBe('ask');
expect(decide(b, 'plain', 'rm B/app-192/src/a.txt')).toBe('ask');
expect(decide(b, 'plain', 'rm B/app-192/src/a.txt', { GIT_DIR: path.join(b, 'app', '.git') })).toBe('ask');
expect(decide(b, 'app/src', 'rm B/app-192/src/a.txt', { HOME: path.join(b, 'app') })).toBe('ask');
for (const dir of ['Documents', '.ssh']) {
const r = gitArgvIn(path.join(b, 'app'), ['worktree', 'add', '-q', path.join(b, dir), '-b', `wt-${dir}`], 10_000);
expect(r.status).toBe(0);
expect(decide(b, 'app', `rm B/${dir}/src/a.txt`, { HOME: b })).toBe('ask');
}
expect(decide(b, 'app', 'rm B/app-192/src/a.txt', { HOME: b })).toBe('allow');
});
}, 30_000);
});
describe('shared extractor keeps /careful command values unchanged (#3623)', () => {
test.skipIf(process.platform === 'win32').each(['python3', 'node'])('with %s failing, a command still drops trailing newlines and nothing else', (broken) => {
const shim = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-extract-shim-'));