š fix(kavach): a write in another live session's worktree warns, 0.7.19
Under work item #43.
kavach's worktree guard admitted a write in any .claude/worktrees worktree, another session's included. A session whose shell lands in another session's worktree could edit its files, commit on its branch and push it. This adds rule other-sessions-worktree at the guard's ownership gate. It warns on trial, and refuses once rules in .claude/kavach.json says so.
When it fires: a call the gate admits in a managed worktree, when both of these hold:
- the desktop app's record of this session (the one #366 trusts:
CLAUDE_CODE_HOST_SESSION_IDnames it, itscliSessionIdis the payload'ssession_id) names a worktree that is not the shell's; - another record, not archived, names the shell's worktree as its
cwd.
The finding names both worktrees first (the log row clips at 200 characters), then the cd back. It covers Bash, Edit, Write and NotebookEdit, and the trusted and reviewed commands, since it sits before their early returns. Reads never reach it.
When it does not:
- this session's own worktree, a worktree shared with its starter (its record names it);
- a worktree the session made itself, and a subagent's
agent-*worktree (no other record names them); - no record (CLI sessions), a record that does not match, a record naming a main checkout.
In all of these the guard judges as in 0.7.18.
What changes:
boundary.worktree_owners(root, host)returns the other live records namingroot. It reads each record's first 4 KB, where the app writescwdthird.boundary.top_level_stringreads only the top-level object'scwdfrom that head. A nestedcwd(as infolderPlace), a value that is not a string, or one cut off reads the whole record instead, so it never skips one wrongly.- The whole record is read only when that
cwdis the worktree, or when the head has none and the text holds the worktree's name. That name is matched unescaped, as the app writes non-ASCII. - It never raises; a failure is no owner, so it can only stay quiet.
worktree_guard.py:other_sessions_worktreeandjudge_other_sessions_worktree, which loadrules.pyand use itsmode_of,Fire,context,answerandrefusal.- In warn mode the guard prints the rules' PreToolUse answer, carrying
additionalContext, once the call is allowed, and logs awarnrow. In refuse mode it denies with the rules'BLOCKEDline and arefuserow. - While warning, an error in the check allows and logs; once refusing, it refuses. A failed scan is not such an error: it is silent in both modes.
- In warn mode the guard prints the rules' PreToolUse answer, carrying
config.RULE_IDSgains the rule, sorulesmay name it. README rule row and docstrings.- kavach and sarathi move to 0.7.19 (sarathi's
VERSIONincli.tstoo).
Cost: the scan runs whenever the record's worktree is not the shell's. That is not rare: it covers every write in an agent-* or self-made worktree, not only another session's. Timed on this machine (1058 records, this worktree's guard, 11 runs each, medians): the own worktree adds nothing, a worktree no one owns adds about 40 ms, and one a live record owns about 35 ms. Before the head read it added 400 to 800 ms. Slice #375 and its page were written before this was measured. They call the case rare, give 0.6 s (a Node timing), and describe a byte search; the head read replaces that.
Tests (OtherSessionsWorktreeTest in test_worktree_guard.py, the app folder faked under a test HOME):
- A Bash, Write and Edit call warns. The warning is a log row naming the rule and mode
warn.refusesFromis shown. Several owners are counted. - Under
refuseit exits 2 with arefuserow. Reviewed (niyam pr-signals) and trusted (python3 scripts/worktree.py status) commands warn and refuse too. - Silent under
refuse:- the own worktree, a self-made worktree, an
agent-*worktree, and a shared worktree; - no host variable or a malformed one, no
session_id, and another CLI session's id; - a read, and a record naming a main checkout, a subfolder, a relative path or a missing folder;
- an owner that is archived, unreadable, not an object, with a relative cwd, with a malformed id, or that is a symlink, and this session's own record as the only one.
- the own worktree, a self-made worktree, an
- A
cwdpast the head still counts, and a head naming another folder is skipped. - A nested
cwdbefore the top-level one is no skip, a non-ASCII worktree name is found, andtop_level_stringhas cases of its own. - An error in the check allows and logs on trial, and raises once it refuses.
worktree_ownersnever raises.
pnpm check passes.
Checked by hand: this worktree's built sarathi, driven with crafted PreToolUse events and real records, passes the guard's warning through as additionalContext. That was a touch from another live veda worktree with this session's session_id. The same event without session_id is silent, and so is one from this worktree. The real .claude/hooks/sarathi.sh runs the root pin, so that check is repeated on the pin bump.
Open for Alok (the š approved the slice and did not settle these):
- Does #363 close when the warning ships, or stay open until
refuse? - What does
refuseneed first? A trial whose log has no fires proves nothing by itself. Then the crafted event is the only positive fire. A failed scan is also silent and logs nothing, so a quiet log cannot tell "no fires" from "scan broken", and underrefusea failed scan lets the write through. Cubic asked for it to fail closed; I kept the slice's "never raises" to avoid #335's lockout, and it is yours to settle with the switch. I propose reading the trial's rows, plus one real subagent payload confirming it carries the parent'ssession_id.
The slice's backtest found 12 moves into a worktree another record names. It counted archived owners too, so the shipped rule, which skips them, fires on fewer.
Not here: CLI sessions and unmatched records keep today's behaviour. A worktree whose owner is archived or has no record is not protected. Switching to refuse is a later change.
š¤ Generated with Claude Code