✨ feat(kavach): a session writes its own scratchpad and notes folder

Merged
AT
19d ago
+774-18

kavach's worktree guard lets a session, from its own worktree, write the two folders Claude Code gives it outside every checkout. It releases as kavach 0.6.11 with sarathi 0.6.11.

Slice: #68

What a session can now write

  • Its scratchpad: <CLAUDE_CODE_TMPDIR or /tmp>/claude-<uid>/<project>/<session_id>/scratchpad. The session_id comes from the PreToolUse payload.

  • The side-session notes folders the desktop app granted it. The guard reads the app's record of the session (claude-code-sessions/…/<host>.json), which counts only when:

    • the hook's environment names the host session (CLAUDE_CODE_HOST_SESSION_ID);
    • exactly one record is found;
    • its cliSessionId is the payload's session_id.

    Its addDirectories grants and the session's own folder count.

What stays refused

  • Another session's folders.
  • Everything else under the temp folder, $TMPDIR and the app's support folder.
  • A folder that is a symlink, has one on the way, or sits inside a git work tree.
  • Targets compare resolved, so a .., or a symlink that already exists in a folder and leads out of it, reaches nothing.
  • A refusal near these folders says which ones are this session's.

Not covered: a symlink that the same command creates and then writes through (ln -s <main> x && touch x/f). That gap was already in the guard, inside the worktree too, and is filed as #110.

Tests

  • SessionFoldersTest: 12 unit tests over Write, Edit and Bash calls.
  • The corpus fixture now holds this session's folders and a stranger's, and the payload names the session. The oracle counts a write to a stranger's folder as an outside write.
  • 10 corpus rows: own folders allowed; another session's, .. and symlink escapes refused.
  • I ran the built guard against this machine's real desktop record. The scratchpad and the granted notes folder were allowed; a stranger's notes folder, another scratchpad and the temp root were refused.
  • pnpm check passes at the root.

🤖 Generated with Claude Code


Summary by cubic

kavach's worktree guard now lets a session, from its owned checkout, write its own scratchpad and the side-session notes folders the desktop app granted it.

  • The scratchpad is <CLAUDE_CODE_TMPDIR or /tmp>/claude-<uid>/<project>/<session_id>/scratchpad, named by the PreToolUse payload; the notes folders come from the app's session record, which only counts when the environment's host session id and the record's cliSessionId match the payload's session_id.
  • Another session's folders, symlink escapes, and everything else under the temp and app support folders stay refused. A near-miss denial names this session's own folders.

Releases as kavach 0.6.11 with sarathi 0.6.11. Tests: SessionFoldersTest adds 12 unit tests, and the corpus fixture gains 10 rows covering the new allowances and refusals.