-
Notifications
You must be signed in to change notification settings - Fork 0
feat(hooks): add Claude Code pre-compact snapshot hook #226
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||||
| from __future__ import annotations | ||||||||||||||
|
|
||||||||||||||
| import json | ||||||||||||||
| import os | ||||||||||||||
| import tomllib | ||||||||||||||
| from pathlib import Path | ||||||||||||||
|
|
@@ -63,3 +64,26 @@ def test_adapter_install_does_not_touch_real_user_config(tmp_path: Path) -> None | |||||||||||||
| assert result.action == "added" | ||||||||||||||
| after = real_config.read_text(encoding="utf-8") if real_config.exists() else None | ||||||||||||||
| assert after == before | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def test_claude_code_install_writes_pre_compact_entry(tmp_path: Path) -> None: | ||||||||||||||
| brain_dir = tmp_path / "brain" | ||||||||||||||
| brain_dir.mkdir() | ||||||||||||||
| config_path = tmp_path / ".claude" / "settings.json" | ||||||||||||||
|
|
||||||||||||||
| adapter = get_adapter("claude-code") | ||||||||||||||
| first = adapter.install(brain_dir, config_path) | ||||||||||||||
| second = adapter.install(brain_dir, config_path) | ||||||||||||||
|
|
||||||||||||||
| assert first.action == "added" | ||||||||||||||
| assert second.action == "already_present" | ||||||||||||||
| settings = json.loads(config_path.read_text(encoding="utf-8")) | ||||||||||||||
| pre_compact = settings["hooks"]["PreCompact"] | ||||||||||||||
| commands = [ | ||||||||||||||
| hook.get("command", "") | ||||||||||||||
| for entry in pre_compact | ||||||||||||||
| for hook in entry.get("hooks", []) | ||||||||||||||
| ] | ||||||||||||||
| assert len(pre_compact) == 1 | ||||||||||||||
| assert any("BRAIN_DIR=" in command for command in commands) | ||||||||||||||
| assert any("gradata.hooks.pre_compact" in command for command in commands) | ||||||||||||||
|
Comment on lines
+88
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert both fragments on the same command entry. Line 88 and Line 89 can pass even if Suggested fix- assert any("BRAIN_DIR=" in command for command in commands)
- assert any("gradata.hooks.pre_compact" in command for command in commands)
+ assert any(
+ "BRAIN_DIR=" in command and "gradata.hooks.pre_compact" in command
+ for command in commands
+ )📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
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.
Guard against file-path
BRAIN_DIRbefore writing snapshots.Line 118 only checks existence. If
BRAIN_DIRpoints to a file, Line 121 can fail during snapshot directory creation.Suggested fix
def main(data: dict[str, Any]) -> None: resolved = resolve_brain_dir() if not resolved: return None brain_dir = Path(resolved) - if not brain_dir.exists(): + if not brain_dir.is_dir(): return None session_id = _session_id(data) _write_snapshot(_snapshot_path(brain_dir, session_id), _build_snapshot(brain_dir, data)) return None📝 Committable suggestion
🤖 Prompt for AI Agents