fix(tracking): record stats for hook-rewritten commands (#1082)#1223
Open
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
Open
fix(tracking): record stats for hook-rewritten commands (#1082)#1223ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
ousamabenyounes wants to merge 1 commit intortk-ai:developfrom
Conversation
When Claude Code rewrites `git status` → `rtk git status` via its PreToolUse hook, the hook first runs `rtk rewrite` which spawns a background telemetry thread holding a brief DB connection. If that process exits before the thread finishes, SQLite's WAL shared-memory file (.db-shm) may be inconsistent for a few milliseconds. Because `execute_batch()` stops on first error, bundling `PRAGMA journal_mode=WAL` and `PRAGMA busy_timeout=5000` in one call meant that a transient WAL-mode error silently left `busy_timeout=0`. With no wait budget, any lock-contention attempt in `Tracker::new()` failed immediately, silently dropping the tracking record. Fix two things: 1. Run `PRAGMA busy_timeout=5000` in its own `execute_batch()` call so it is always applied, regardless of whether the WAL pragma succeeds. 2. Add a single retry (10 ms sleep) in `TimedExecution::track()` and `track_passthrough()` to outlast the transient .db-shm window. Add regression tests for concurrent-open correctness and for the full hook-rewrite tracking path. Fixes rtk-ai#1082 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1082 —
rtk gainshowed no stats for commands rewritten by the Claude Code PreToolUse hook (e.g.git status→rtk git status).Root cause (two interacting bugs in
src/core/tracking.rs):execute_batch()stops on first error.Tracker::new()bundledPRAGMA journal_mode=WALandPRAGMA busy_timeout=5000in a singleexecute_batch()call. When the Claude Code hook runs, it first invokesrtk rewrite, which spawns a background telemetry thread that briefly holds a DB connection. If that process exits before the thread finishes, SQLite's WAL shared-memory file (.db-shm) can be briefly inconsistent, causing the WAL pragma to return a transient error. Becauseexecute_batch()halts on first error,busy_timeoutsilently stayed at its default of 0 ms — meaning every subsequent lock-contention attempt inTracker::new()failed immediately.Silent failure.
TimedExecution::track()/track_passthrough()usedif let Ok(tracker) = Tracker::new() { let _ = tracker.record(...) }— both error paths were silently swallowed, so the stat record was dropped with no indication.Fix:
PRAGMA busy_timeout=5000in its ownexecute_batch()call so it is always applied, independently of the WAL pragma.track()andtrack_passthrough()to outlast the transient.db-shmwindow before giving up.New regression tests:
Trackerconnections can both write successfully (busy_timeout is in effect).TimedExecution::track()records the stat even when another reader holds the DB open simultaneously.Test plan
cargo test --all— 1381 passed, 0 failedcargo fmt --all && cargo clippy --all-targets— clean🤖 Generated with Claude Code