Skip to content

feat(hooks): replay recent commands through the hooks - #199

Merged
furedea merged 1 commit into
mainfrom
feat/replay-commands
Oct 17, 2026
Merged

furedea merged 1 commit into
mainfrom
feat/replay-commands

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Add agents/hooks/replay_commands.py, a manual tool that measures the hooks. It is not registered as a hook. It collects the Bash commands run in recent sessions and judges them with the current guards, without running them. It then prints allow / ask / deny totals, the count for each reason, and the asked and denied commands.

  • Usage: replay_commands.py --since YYYY-MM-DD[THH:MM] [--hooks DIR]. --since is in local time. --hooks defaults to the script's own directory, so a checkout judges with its own hooks. For a checkout, the script falls back to agents/command_permissions.json, because the generated prefix list is missing from hooks/rules/ there.
  • Sources:
    • Claude transcripts: ~/.claude/projects/*/*.jsonl, using assistant tool_use entries named Bash.
    • Codex rollouts: ~/.codex/sessions/**/*.jsonl[.zst], read with the stdlib compression.zstd. Codex logs come in two formats: older logs record function_call exec_command with {"cmd": ...}, and newer ones record item_completed CommandExecution items, where the script is taken from the zsh -lc argv. A file whose mtime is before --since is skipped.
  • Judging: guards run in the PreToolUse order of agents/hooks.json (forbidden, git, allowed), and the first non-allow verdict counts. AGENT_PROJECT_DIR is set to each command's recorded cwd, so the project rules that apply are the ones from where the command ran. audit_log.blocked and audit_log.escalated are silenced during the replay.
  • Privacy: output goes only to stdout. Command text is never written to files. Each listed command shows its first line, at most 200 characters.

Closes #188

Verification

  • uv run --frozen pytest tests: 1518 passed, 4 failed. The 4 failures are in test_command_policy.py (3 multi-line/GNU-grep contracts) and test_allowed_command.py[Herdr prompts]. They fail the same way on origin/main in this session, because only BSD /usr/bin/grep is on the sandbox PATH. They are unrelated to this change.
  • New tests in tests/agents/hooks/test_replay_commands.py (6) use synthetic transcripts in tmp_path. They cover:
    • Claude counts per verdict and reason, the --since cutoff, and non-Bash tools;
    • both Codex formats, zstd-compressed;
    • an untouched audit log;
    • --hooks pointing at a copied checkout with a changed rule;
    • --since missing or malformed.
  • ruff check, ruff format, ty: passed.
  • Manual run on real sessions since 2026-10-16: 1404 commands (allow 1299, ask 48, deny 57) in about 90 seconds.

🤖 Generated with Claude Code

Add agents/hooks/replay_commands.py, a manual tool that collects the Bash
commands run since --since from Claude transcripts and Codex session logs,
judges them with the guards in --hooks without running them, and prints
allow/ask/deny totals, counts per reason, and the asked and denied commands.
Audit-log writes are suppressed and nothing is written outside the terminal.

Refs #188

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@furedea
furedea merged commit 61746c0 into main Oct 17, 2026
11 checks passed
@furedea
furedea deleted the feat/replay-commands branch October 17, 2026 12:13
furedea added a commit that referenced this pull request Nov 16, 2026
agent-config の PostToolUse フックが ADR 編集後に実行する `adr-check` を PATH
に置く.`jev-lint` と同じ方法で `home.packages` に追加し,`agent-config` 入力を #225
を含むリビジョンに更新する.

Refs furedea/agent-config#169

## 変更内容

- `nix/home/default.nix`: `adrCheckPackage =
agent-config.packages.${system}.adr-check;` を `jevLintPackage`
の隣に追加し,`home.packages` に加えた(furedea/agent-config#225).
- `flake.lock`: `agent-config` を `2ab23eb` → `138c542` に更新した(`nix flake
update agent-config`).

## agent-config の更新内容(2ab23eb → 138c542,23 コミット)

- lint: go-style / rust-style / python-style / TypeScript / bash-style /
github-actions-style / agents/AGENTS.md のルールを ast-grep・jev-lint・model
rules へ移行(#190, #191, #192, #193, #194, #195),Go の `util`・`common`
パッケージ検出(#202),ブロッキング ast-grep ルールの欠落修正(#216)
- hooks: 静的ルールエラーでの編集ブロックと ast-grep-ignore の保護(#203),ask
プレフィックスの限定(#199),リポジトリフック回避オプションの拒否(#212),コードセルの判定修正(#224)
- skills: issue / git / setup ワークフローを `development-cycle`
に統合(#211),agent 実行の verify ステップ削除(#213),範囲外指摘への回答と all-green 待機(#223)
- docs/ADR: MADR の採用(#215),スキル責務の配置(#208),コマンド判定の方針(#198),ast-grep
のブロック方針(#214),AGENTS.md の整理(#205)
- build/ci: マイナーバージョン上げの設定(#217),jev-lint モデルルールテストを CI で実行(#218)
- feat: `adr-check` コマンドの追加(#225)

## 検証

- `nix build '.#homeConfigurations."kaito@mbp".activationPackage'`:
成功.生成物の `home-path/bin` に `adr-check` が含まれることを確認した.
- `darwin-rebuild switch` は実行していない.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: furedea <[email protected]>
Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

フックの効果を測る:過去のコマンドを通し直して集計する

1 participant