Skip to content

feat: add post-file-edit hook for real-time file tracking#583

Draft
khaong wants to merge 7 commits intomainfrom
alex/add-edit-tool-hooks
Draft

feat: add post-file-edit hook for real-time file tracking#583
khaong wants to merge 7 commits intomainfrom
alex/add-edit-tool-hooks

Conversation

@khaong
Copy link
Contributor

@khaong khaong commented Mar 2, 2026

Summary

  • Adds a post-file-edit hook that fires on every Write/Edit tool call in Claude Code, updating FilesTouched in session state in real-time
  • Fixes an orphaned checkpoint bug where FilesTouched was only updated at turn boundaries — if the agent committed mid-turn before SaveStep, PostCommit's overlap check would fail
  • Appends detailed edit records (file path, action, lines added/removed) to an append-only JSONL log included in checkpoint metadata during condensation

Test plan

  • Unit tests for CountLines, FileEdit types, input parsing, JSONL operations
  • Integration tests for hook dispatch, FilesTouched updates, edit log creation, idempotency, multi-file edits, no-session graceful handling, out-of-repo path handling
  • mise run fmt && mise run lint && mise run test:ci all pass

🤖 Generated with Claude Code

Adds a post-file-edit hook that fires on every Write/Edit tool call in
Claude Code. This fixes an orphaned checkpoint bug where FilesTouched was
only updated at turn boundaries — if the agent committed mid-turn before
SaveStep, PostCommit's overlap check would fail because FilesTouched was
empty.

The hook updates FilesTouched in session state in real-time and appends
detailed edit records to an append-only JSONL log that gets included in
checkpoint metadata during condensation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Entire-Checkpoint: c5f9a06afa32
Copilot AI review requested due to automatic review settings March 2, 2026 22:04
@cursor
Copy link

cursor bot commented Mar 2, 2026

PR Summary

Medium Risk
Adds a new Claude Code hook that writes session state and introduces a new file_edits.jsonl artifact in committed checkpoints, which affects hook dispatch and checkpoint serialization paths. Risk is moderate due to new I/O and state mutations during active turns, though failures are handled as non-fatal warnings and paths/session IDs are validated/normalized.

Overview
Adds a new Claude Code hook, post-file-edit, installed as PostToolUse matchers for Write and Edit, and registers it in the hook registry as a tool hook.

Implements post-file-edit handling that parses tool payloads, counts lines added/removed, appends agent.FileEdit records to an append-only per-session JSONL log, and merges edited paths into FilesTouched during the turn (skipping paths outside the repo and treating all errors as non-blocking warnings).

Extends committed checkpoint writing to optionally include a file_edits.jsonl session artifact, loads/clears recorded edits during manual-commit condensation, and adds unit/integration coverage plus updated hook installation expectations.

Written by Cursor Bugbot for commit 1a1e674. Configure here.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Claude Code post-file-edit hook to track per-tool file edits in real time, persist them as an append-only JSONL log, and include that log in condensed checkpoint artifacts to improve mid-turn commit attribution.

Changes:

  • Added post-file-edit hook handling for Claude Code Write/Edit tool invocations and wired it into the hook registry + hook installation.
  • Introduced append-only session-scoped JSONL storage for FileEdit records and plumbed those records into checkpoint condensation output.
  • Added unit + integration test coverage for parsing, persistence, and hook behavior.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
cmd/entire/cli/strategy/manual_commit_condensation.go Loads session file-edit records during condensation, writes them into committed checkpoint data, and clears the session edit log afterward.
cmd/entire/cli/session/file_edits.go Implements append/read/clear operations for a per-session JSONL file edit log.
cmd/entire/cli/session/file_edits_test.go Unit tests for JSONL append/read/clear behaviors and input validation.
cmd/entire/cli/paths/paths.go Adds file_edits.jsonl constant for checkpoint artifact naming.
cmd/entire/cli/hooks_file_edit.go Parses PostToolUse hook payloads for Write/Edit into a normalized file-edit input (path + line deltas).
cmd/entire/cli/hooks_file_edit_test.go Unit tests for parsing Write/Edit inputs and error cases.
cmd/entire/cli/hooks_claudecode_postfileedit.go New Claude Code hook handler that appends edit records and updates FilesTouched in real time.
cmd/entire/cli/hook_registry.go Routes post-file-edit hook invocations to the new handler.
cmd/entire/cli/checkpoint/checkpoint.go Extends committed checkpoint write options to include file edit records.
cmd/entire/cli/checkpoint/committed.go Persists FileEdits into the checkpoint tree as file_edits.jsonl.
cmd/entire/cli/agent/types.go Adds FileEdit/FileEditAction types and a CountLines helper.
cmd/entire/cli/agent/types_test.go Tests for CountLines and the FileEdit struct fields.
cmd/entire/cli/agent/claudecode/hooks.go Installs PostToolUse matchers for Write and Edit to invoke post-file-edit.
cmd/entire/cli/agent/claudecode/hooks_test.go Ensures hook installation preserves user hooks and adds the new Entire hooks.
cmd/entire/cli/agent/claudecode/lifecycle.go Declares post-file-edit as Claude-specific (handled outside lifecycle dispatcher).
cmd/entire/cli/integration_test/file_edit_hooks_test.go Integration tests for hook dispatch, FilesTouched updates, edit-log creation, idempotency, and out-of-repo handling.
cmd/entire/cli/integration_test/setup_claude_hooks_test.go Updates hook setup test expectations to include Write/Edit.
cmd/entire/cli/integration_test/agent_test.go Updates expected installed hook count to include the new matchers.

khaong and others added 6 commits March 3, 2026 09:11
- Fix orphan edits: check session state exists before appending JSONL
- Switch from fmt.Fprintf(os.Stderr) to logging.Warn for consistency
- Add filepath.ToSlash for cross-platform path normalization
- Fix doc comment on ReadFileEdits (returns nil, not empty slice)
- Rename TestNewFileEdit to TestFileEditFields
- Add orphan edits assertion to NoSession integration test

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Entire-Checkpoint: 3553fce5455f
Error handling:
- Log malformed JSONL lines in ReadFileEdits instead of silent skip
- Log marshal failures in committed.go instead of silent continue
- Add Debug logs for nil-state and outside-repo early returns
- Document atomic write assumption in AppendFileEdit
- Consolidate dual NewStateStore calls in condensation into single
  load-with-cleanup pattern

Input validation:
- Validate session_id non-empty in parseFileEditHookInput
- Validate file_path non-empty in Write/Edit tool_input parsing

Code quality:
- Use cmd.InOrStdin() instead of hardcoded os.Stdin for testability
- Use slog.String("error", err.Error()) consistently (not slog.Any)
- Update hook_registry.go doc comments to mention PostFileEdit
- Improve type doc comments (FileEditAction, FileEdit fields)
- Add SubagentCheckpointHookInput reuse explanation comment

Tests:
- Replace struct-assignment test with JSON round-trip test
- Add tests for missing session_id and missing file_path
- Add integration test for relative path normalization
- Use exact line count assertion (== 7) instead of >= 1

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Entire-Checkpoint: bdd07189c1f1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants