Skip to content

Install hooks user-wide by default, and record each turn once - #7

Merged
frenchie4111 merged 3 commits into
mainfrom
feat/install-global-default
Nov 20, 2026
Merged

frenchie4111 merged 3 commits into
mainfrom
feat/install-global-default

Conversation

@frenchie4111

@frenchie4111 frenchie4111 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Builds on #5 (Codex support), now merged. Rebased onto main and retargeted.

What changes

loch install now writes user-level hooks by default, so one install covers every project:

Claude Code Codex
default (user) settings.json under $CLAUDE_CONFIG_DIR or ~/.claude $CODEX_HOME/hooks.json
--local .claude/settings.local.json .codex/hooks.json (Codex has no uncommitted file, so git sees it)
--project .claude/settings.json (the old default) .codex/hooks.json

I kept --project instead of dropping it, because it's the only way to make recording travel with a repo to teammates. The code path already existed.

Ness's install --local --cwd <worktree> --command <abs> is unchanged. It writes the same lines to the same file and still means "this worktree only".

Hooks outside a repo

User-level hooks fire in every directory. A user-wide hook now records only in a git repository. Anywhere else it would have snapshotted whatever directory the session was in (the plain-directory fallback), e.g. ~/Downloads. A repository with no commits still records. The check (hook::applies) runs before any store is opened.

Measured: 2.8ms outside a repo (process start), and nothing is written. Without the check, the same 2,000-file directory took 83ms and got snapshotted. Project-level installs still record plain directories, as before.

No double recording

Both agents run every scope's hooks for an event, at once, and each hook gets byte-identical stdin. I verified this on Claude Code 2.1.329 and Codex 0.166: two user hooks and a project-local hook for one event got the same bytes. So the hooks of a session take turns under a lock (hook::claim), and a hook handed an event that another finished handling within 10s does nothing. An event is the hook's name, its payload and its transcript's length. The length matters because Claude Code stopping twice for one prompt sends identical payloads, while the transcript grows in between.

The first version of this PR had the wider hook step aside when a narrower file held loch's hook and the agent would run it. That needed guesses about where the agent reads settings, about trust, and about settings changed mid-session, and a wrong guess lost the turn. Review showed one: a team hook trusted at the same event, with loch's project hook untrusted. That was replaced, and the trust parsing is gone. A real Codex run of that exact case now records the turn.

Install also removes a second loch line for an event from Claude Code's settings. It leaves the line in Codex's file, where trust is keyed by position, and reports it as duplicates. Only user-wide lines name a scope (--scope user), so project and Ness-local lines are unchanged.

Other hooks untouched

Only lines hook_line wrote are recognised (parse_hook_line). An existing loch line in another form, such as another path or no scope, is rewritten in place (reported as replaced), so no other hook moves. Codex keys trust by position. Trust stays manual.

JSON

New fields scope, replaced and duplicates, at the top level and per agent. No field changed meaning, so schema stays 2. --local --project is a usage error.

Verified

  • cargo fmt --check, clippy -D warnings, cargo test --release.

  • New tests run the installed lines the way the harness does: they read the settings files and run every command through sh, both in parallel and one by one. They cover:

    • a user install that keeps the user's own hooks, and CLAUDE_CONFIG_DIR;
    • in-place rewrites, and duplicate loch lines (removed for Claude, left and reported for Codex);
    • a non-repo directory writing nothing (no ~/.loch, no store);
    • repos with and without commits;
    • a user-wide hook beside a local one and beside a project one, for both agents.

    With the claim disabled, the double-record tests fail: Claude shows [true, true, false] interrupted flags, and Codex records 3 turns for 2.

  • Real Codex 0.166, in a scratch HOME/CODEX_HOME, with hooks trusted through the TUI and no bypass flag:

    • Loch's project hook untrusted and a team hook trusted: the user-wide hook recorded the turn.
    • Everything trusted: the turn was recorded once and blamed.
    • A plain directory: the hooks ran, and loch wrote nothing.
  • Claude Code: it ran UserPromptSubmit under a scratch HOME before failing on its missing login. That was enough to show identical payloads across hooks, and that a session launched in repo/sub runs the repo root's .claude/settings.local.json.

🤖 Generated with Claude Code

frenchie4111 and others added 2 commits November 20, 2026 10:51
`loch install` now writes the user's hooks file (Claude Code's settings.json
under CLAUDE_CONFIG_DIR or ~/.claude, Codex's $CODEX_HOME/hooks.json), so one
install covers every project. `--local` is this project on this machine
(.claude/settings.local.json; Codex has no uncommitted file, so
.codex/hooks.json), and the old committed default stays as `--project`.

A user-wide hook records only in a git repository: anywhere else it would
snapshot whatever directory a session started in. Every scope's hooks run
for an event, so each line names its scope and a wider one steps aside when
a narrower file in the project holds loch's hook and the agent will run it
(for Codex, a trusted project and a trusted hook). Local lines are unchanged,
so Ness's `install --local --cwd --command` writes what it always did.

Install rewrites an existing loch line in place rather than adding a second,
and touches no other hook. The payload gains `scope` and `replaced`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…p aside

The user-wide hook stepped aside when a project's file held loch's hook and
the agent would run it. Answering that took guesses (where Claude Code reads
project settings, Codex trust keys, settings changed mid-session), and a
wrong one lost the turn: with a team hook trusted at the same event and
loch's project hook not, Codex skipped loch's and nobody recorded.

Both agents hand every hook for an event the same bytes (verified on Claude
Code 2.1.329 and Codex 0.166), so the hooks of a session now take turns
under a lock and one handed an event handled moments ago does nothing. An
event is the hook's name, its payload and its transcript's length, since
Claude Code stopping twice for one prompt sends identical payloads. This
needs nothing from either agent's file or trust rules, and also covers an
old committed line beside a local one.

Install also removes a second loch line for an event from Claude Code's
settings, and leaves it (reporting `duplicates`) in Codex's, whose trust is
keyed by position. Only user-wide lines name a scope now, so project lines
are what they were.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@frenchie4111
frenchie4111 force-pushed the feat/install-global-default branch from 6f6d175 to ca000e8 Compare November 20, 2026 15:52
@frenchie4111
frenchie4111 changed the base branch from feat/codex-adapter to main November 20, 2026 15:52
Another hook's copy of an event reads the claim within milliseconds of its
being written, while a genuine repeat that hashes the same (a second Stop
before the transcript is flushed) follows a model round trip. The 10s window
could only drop such an event and lose its edits; seconds also truncated the
window by up to one. A test fires an identical stop past the window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@frenchie4111
frenchie4111 merged commit 8fc97c0 into main Nov 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant