kink-discovery / scripts /codex_baseline.py
Perplexed7675's picture
Sync from kink_cli (Docker Space)
6ff91d6 verified
Raw
History Blame Contribute Delete
1.77 kB
#!/usr/bin/env python3
"""sessionStart hook: snapshot git tree for codex review threshold gating.
Saves a git tree reference representing the working-tree state at session
start. The companion stop hook (codex_review.py) diffs against this
baseline to measure how much the session changed, independent of commits.
"""
import subprocess
import sys
from pathlib import Path
project_root = Path(__file__).resolve().parent.parent
state_dir = project_root / ".cursor" / "hooks" / "state"
baseline_path = state_dir / "codex_review_baseline"
def save_baseline() -> None:
"""Record the working-tree state so the review hook can diff against it.
Uses git add -N to include untracked files in the snapshot, so new
files created during the session appear in the stop-time diff.
"""
state_dir.mkdir(parents=True, exist_ok=True)
subprocess.run(
["/usr/bin/git", "add", "-N", "."],
capture_output=True,
text=True,
timeout=10,
check=False,
cwd=str(project_root),
)
stash = subprocess.run(
["/usr/bin/git", "stash", "create"],
capture_output=True,
text=True,
timeout=10,
check=False,
cwd=str(project_root),
)
sha = stash.stdout.strip()
if not sha:
head = subprocess.run(
["/usr/bin/git", "rev-parse", "HEAD"],
capture_output=True,
text=True,
timeout=5,
check=False,
cwd=str(project_root),
)
sha = head.stdout.strip()
baseline_path.write_text(sha)
def main() -> None:
"""Entry point: read hook input, snapshot baseline, emit empty JSON."""
sys.stdin.read()
save_baseline()
print("{}")
if __name__ == "__main__":
main()