Skip to content

Rust rewrite, and attribution by content instead of git notes - #3

Merged
frenchie4111 merged 18 commits into
mainfrom
rust-rewrite
Nov 19, 2026
Merged

frenchie4111 merged 18 commits into
mainfrom
rust-rewrite

Conversation

@frenchie4111

Copy link
Copy Markdown
Contributor

Summary

loch is now one static Rust binary, and it attributes lines to the turns that wrote them by content instead of with git notes.

  • Rust rewrite. Every command was ported and held byte for byte to the Python implementation: 51 synthetic scenarios plus all 472 project directories on one machine came out identical. Then the Python was deleted. Its 205 tests survive as Rust integration tests, 224 in all (10 more are opt-in: live S3 and the real-transcript corpus).
  • Attribution without git. Notes hung each link on a commit id, so loch format v1, and blame/log/ask: from a line of code back to its conversation #2's squash merge orphaned every one of them. The hooks now snapshot the workspace at each turn's start and end (UserPromptSubmit, Stop, SessionEnd) and record hashes of the lines the turn added. That covers however the turn added them: edit tools, sed, generators, formatters. Blame matches today's text against those hashes, so squashes, rebases and a teammate's fresh clone all see the same answers. Snapshots are taken on demand with a git-style stat cache; there's no background process. SPEC.md §10 specifies the format.
  • Cancelled and crashed turns are closed by the next prompt or the session's end, so their edits aren't lost. Snapshots are collected git-gc style once nothing needs them.
  • loch backfill rebuilds attribution for history from before the hooks. It uses the transcripts' edit calls, plus the agent's commits where the match is exact. Blame follows renames via git log --follow.
  • Faster hooks. They run twice per turn instead of twice per shell command. Each costs about 45ms on a 2k-file repo; the old design paid about 73ms per call around every Bash call.

Breaking changes

  • The Python package and the uv tool install wheel are gone. Install a release binary or the npm package (@ness-dev/loch, not published yet), and uv tool uninstall loch first.
  • The JSON envelope schema is now 2. blame, log, ask and install payloads changed, ask --at-commit became --at-turn, and backfill was rewritten.
  • Error codes not-a-repository, git-failed, call-not-found and s3-extra-missing are gone. file-unreadable and turn-not-found are new.
  • Re-run loch install in each repo to swap the old per-Bash-call hooks for the new ones. Old git notes are imported by backfill, and nothing else reads them.

Not yet verified

  • CI: this PR is the first run of test.yml, and the first test of the Linux build. The six-target release workflow has never run.
  • SessionEnd hasn't been seen firing in a real session. The prompt and stop hooks have been verified live.
  • The S3 backend has only run against moto, never a real bucket. Windows has never been built.

🤖 Generated with Claude Code

frenchie4111 and others added 18 commits November 17, 2026 15:16
Claude Code reads $CLAUDE_CONFIG_DIR/projects when the variable is set;
loch always read ~/.claude, so on a machine running a second account it
listed, restored and forked into a directory Claude never looks at.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rust/ holds a single crate whose binary implements `loch hook` (pre-tool,
post-tool, stop with a file store) plus --version and help; every other
command answers not-ported. HEAD and the git dir are read natively on
layouts the fast path fully understands and from git otherwise, which takes
a hook call from ~75ms to ~3ms (rust/bench/hook.py).

test/test_equivalence.py runs both implementations on identical worlds
with pinned git dates and compares stdout, stderr, status, the notes ref,
pending state and store bytes, including a pre hook under one and the post
hook under the other. LOCH_CORPUS=1 adds call recovery on real transcripts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SigV4 by hand over ureq/rustls rather than the AWS SDK: one signed PUT
doesn't justify an async runtime. Credentials come from AWS_* and the
shared credentials/config files; SSO, credential_process and instance
metadata are not read. The signer is pinned to AWS's published example,
and the harness compares both builds against a moto server that refuses a
wrong signature.

The toolchain is pinned to 1.91.0 in rust/rust-toolchain.toml: 1.83 can't
build current url/idna, and the pin is scoped to rust/ so nobody's default
changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ness-dev/loch with per-platform optionalDependencies, as Claude Code
ships: a target map, an assembler, a postinstall that puts the native
binary itself on PATH, and binaryPath() for embedders. Dry-run locally
for darwin-arm64; the Node shim costs ~24ms a call, which is why
embedders must spawn the binary directly. rust-binaries.yml builds the
six targets and assembles the packages, manually and without publishing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From 3.11 gzip.compress hands the header to zlib, which writes its own OS
byte (19 on macOS, 3 on Linux) where 3.10 wrote 255, so the same sessions
packed to different bytes and therefore different refs. The header is now
written by hand, byte-identical to 3.10's on every platform. Bundles
already saved keep their refs; a re-save of the same sessions from 3.11+
gets a new one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
read_session_meta caught OSError only, so one transcript with a bad byte
raised out of list and hid every session in the directory. Metadata now
decodes with replacement; save refuses that session with a new code,
transcript-unreadable, instead of a traceback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ported module for module: argparse (3.12.10's parse loop, nargs patterns
and help formatter), json (Python's parser and writers, lone surrogates,
exact integers, RFC 8785), claude, redact (fancy-regex, Python's \s set
spelled out), rewrite (the single-pass alternation run by hand), bundle
(zlib's own deflate via flate2), index, store and S3 (signed get, list,
delete), format, ingest, trace, blame_html (template extracted verbatim)
and cli. Only the hook is held to the reference by the harness so far.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The harness now compares every file a run leaves behind, not just the
hook's, and renames generated v4 UUIDs so --new-id and ask's fork compare
exactly. test_equivalence_commands.py covers every command in both
renderings: usage errors and -h at several widths, list over moved and
undecodable sessions and CLAUDE_CONFIG_DIR, save/list --remote/restore/
delete/reindex including unreadable bundles, ingest's graph cases and
author keys, install, blame/--html/log/ask (also from the store) and
backfill. Planted bugs in redact, rewrite, ingest, argparse, json, trace,
index and cli each fail it.

SOURCE_DATE_EPOCH now fixes save's createdAt in both builds, the
reproducible-builds convention, which is what lets two builds' bundle
refs be compared at all. base64 key errors follow binascii's strict mode.

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

Redaction was most of save's cost in both builds — 150s in Python and
264s in Rust over 15 project directories — because a backtracking engine
tries every rule at every position and env-assignment can start at any
capital. The Rust build now searches the nine prefixed rules with the
regex crate (boundary checks by hand) and tries env-assignment only at
the start of each capital run holding a keyword, which is the only place
it can match; 15s. The full Python alternation stays as a test oracle,
fuzzed against the fast path over 40k generated strings that exercise
every rule.

test_equivalence_corpus.py lists, saves, restores onto a temporary home
and ingests every project directory on the machine with both builds,
read-only towards ~/.claude, reporting only offsets and hashes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Listings now send encoding-type=url and decode the keys, as boto3 does,
so a key with any character survives and a connection error names the
same URL. The corpus check runs directories in parallel, since the
reference's redaction makes a serial run take hours. CLAUDE.md's Rust
section now covers the whole port: what equivalence required, the three
reference bugs fixed on the way, the divergences kept, the harness and the
numbers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Python is going, so Poetry no longer owns the root. A library target
lets integration tests in tests/ reach the modules directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test.yml runs fmt, clippy and the tests on Linux and macOS. A tag now
builds all six targets, packages them, assembles the npm packages and
attaches everything to the GitHub release with SHA256SUMS; run by hand
it rehearses all of that without publishing. release.sh bumps Cargo.toml
instead of pyproject.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Rust build is the only implementation now. Every test in test/ was
ported to tests/*.rs against the library or the binary (211 passing, 10
opt-in: live S3 endpoint and the real-transcript corpus); none needed
ignoring for a behaviour difference. Three were dropped as Python-only
machinery: an injectable broken redaction rule, a monkeypatched redact
failure, and an uninstalled-checkout version. tests/git_fast_path.rs is
new: the native HEAD answer must equal git's on every layout, and is shown
to need no git process on the ones it handles.

Gone with it: the equivalence harness, Poetry, the boto3 extra and
s3-extra-missing, and bench/hook.py (now examples/bench_hook.rs). CLAUDE.md
and the README describe a Rust-only repository; module comments no longer
point at deleted files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Git notes hung each link on a commit id, which squash, rebase, amend and
cherry-pick all rewrite: #2's squash left every note on commits main
doesn't contain. Attribution now snapshots the workspace at each turn's
start and end (UserPromptSubmit and Stop) and records hashes of the lines
the turn added, however it added them: an edit tool, sed, a generator, a
formatter. Records go to the store, one per session, with a file index;
blame matches the current text against them in runs, most recent turn
winning, so a squash, a rebase or a teammate's fresh clone sees the same
answers. Snapshots are on demand with a stat cache, as git's index is, and
nothing runs in the background.

Gone: git notes, backfill, the per-Bash-call hooks (install removes them)
and the native HEAD fast path they needed. ask forks with --at-turn at the
end of the turn that wrote the line. reindex rebuilds the file index. New
codes file-unreadable and turn-not-found; not-a-repository, git-failed and
call-not-found are gone, and blame/log/ask/install payloads changed, so
the envelope schema is now 2. SPEC.md section 10 specifies the record, the
line hash and the matching rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude Code runs no Stop for a cancelled turn and nothing for a crash, so
an interrupted turn's edits became the next turn's baseline and belonged
to nobody, and its start snapshot was kept forever. A session's state now
says whether its turn is open; the next prompt, or SessionEnd (a third
hook install adds), closes it first as an interrupted turn. State and
record files are written aside and renamed, so a killed hook leaves no
half-written file.

Snapshots are only needed until a turn is diffed, so a mark-and-sweep
keeps each workspace's latest and each live session's turn start and
deletes the rest past an hour's grace, from the hooks every 50 snapshots
or as loch gc. A session untouched for 7 days is abandoned and lets go.
Pins are dropped: ask resumes the conversation against today's files.
The edit record loses its local snapshot ids and gains interrupted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
loch backfill rebuilds turns from before the hooks, marked backfilled:
the transcripts' successful Edit/Write/MultiEdit calls give the exact
text written, and the commits the agent made give everything else,
shell edits included. A commit is credited only on an exact match, an old
git note naming the call or exactly one Bash call across all sessions
holding its subject and bracketing its time; a contested commit stays
unlinked. Lines a session's tool edits already explain stay theirs. Hook
turns are never replaced and re-runs are idempotent.

Blame now asks git for a file's earlier names and looks turns up under
all of them, so a rename no longer severs the link. On this repository
that took files written before the crate left rust/ from 1-16%
attributed to 96-99%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@frenchie4111
frenchie4111 merged commit 97cb8bc into main Nov 19, 2026
2 checks passed
frenchie4111 added a commit that referenced this pull request Nov 20, 2026
…view findings (#8)

* fix(restore): refuse a bundle whose session id isn't a plain file name

A session id from a bundle was joined into the restored transcript's path,
so a crafted bundle could write anywhere (`../../x`, `/x`) and inject into
resumeCommand. Bundles come from shared stores, so the id is now checked
on unpack (bundle-unreadable) and in both find_transcripts.

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

* test: pin review findings on Codex support (#2 passes, #3 and #4 ignored)

- SessionEnd killed at Codex's 3s while the store hangs: the turn is
  already closed locally; the store copy and the state cleanup are lost.
- ask --at-turn on a turn before a move: the fork keeps naming that
  turn's cwd, which Codex's resume then offers as the session directory.
- One directory recorded two ways (resolved, and -C through a link):
  reported as otherCwds, and only one spelling is rewritten on restore.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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