-
Notifications
You must be signed in to change notification settings - Fork 0
feat(hooks): wire Claude Code PreCompact snapshot #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gradata
wants to merge
1
commit into
main
Choose a base branch
from
gra-1210-precompact
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| from gradata.hooks import pre_compact | ||
| from gradata.hooks._base import run_hook | ||
|
|
||
|
|
||
| def test_pre_compact_writes_snapshot(tmp_path: Path, monkeypatch) -> None: | ||
| brain = tmp_path / "brain" | ||
| brain.mkdir() | ||
| (brain / "brain_prompt.md").write_text("remember this rule", encoding="utf-8") | ||
| monkeypatch.setenv("BRAIN_DIR", str(brain)) | ||
|
|
||
| result = pre_compact.main( | ||
| { | ||
| "hook_event_name": "PreCompact", | ||
| "session_id": "abc123", | ||
| "trigger": "manual", | ||
| "cwd": "/repo", | ||
| "transcript_path": "/tmp/transcript.jsonl", | ||
| "custom_instructions": "be concise", | ||
| } | ||
| ) | ||
|
|
||
| assert result is None | ||
| snapshot_path = brain / ".precompact-snapshots" / "abc123.json" | ||
| assert snapshot_path.exists() | ||
| snapshot = json.loads(snapshot_path.read_text(encoding="utf-8")) | ||
| assert snapshot["event"] == "PreCompact" | ||
| assert snapshot["session_id"] == "abc123" | ||
| assert snapshot["trigger"] == "manual" | ||
| assert snapshot["cwd"] == "/repo" | ||
| assert snapshot["transcript_path"] == "/tmp/transcript.jsonl" | ||
| assert snapshot["custom_instructions"] == "be concise" | ||
| assert snapshot["relevant_context"]["brain_prompt_md"] == "remember this rule" | ||
| assert snapshot["limits"]["transcript_content_captured"] is False | ||
|
|
||
|
|
||
| def test_pre_compact_sanitizes_session_id(tmp_path: Path, monkeypatch) -> None: | ||
| brain = tmp_path / "brain" | ||
| brain.mkdir() | ||
| monkeypatch.setenv("BRAIN_DIR", str(brain)) | ||
|
|
||
| pre_compact.main({"session_id": "../../escape/session"}) | ||
|
|
||
| snapshots = list((brain / ".precompact-snapshots").glob("*.json")) | ||
| assert len(snapshots) == 1 | ||
| assert snapshots[0].parent == brain / ".precompact-snapshots" | ||
| assert ".." not in snapshots[0].name | ||
| assert "/" not in snapshots[0].name | ||
|
|
||
|
|
||
| def test_pre_compact_missing_brain_noops(tmp_path: Path, monkeypatch) -> None: | ||
| monkeypatch.setenv("BRAIN_DIR", str(tmp_path / "missing")) | ||
|
|
||
| assert pre_compact.main({"session_id": "abc123"}) is None | ||
|
|
||
|
|
||
| def test_pre_compact_callable_via_run_hook(tmp_path: Path, monkeypatch) -> None: | ||
| brain = tmp_path / "brain" | ||
| brain.mkdir() | ||
| monkeypatch.setenv("BRAIN_DIR", str(brain)) | ||
|
|
||
| run_hook( | ||
| pre_compact.main, | ||
| pre_compact.HOOK_META, | ||
| raw_input=json.dumps({"session_id": "via-run-hook", "hook_event_name": "PreCompact"}), | ||
| ) | ||
|
|
||
| assert (brain / ".precompact-snapshots" / "via-run-hook.json").exists() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use truly bounded reads in
_read_bounded.Line 49 uses
path.read_bytes()[:limit], which loads the full file before truncating. That defeats bounded-read behavior and can spike memory on large files.Suggested fix
def _read_bounded(path: Path, *, limit: int = _MAX_TEXT_BYTES) -> str | None: try: if not path.is_file(): return None - data = path.read_bytes()[:limit] + with path.open("rb") as fh: + data = fh.read(limit) return data.decode("utf-8", errors="replace") except OSError: return None🤖 Prompt for AI Agents