Skip to content

perf(hooks): hook_decisions logging cost/growth — per-call SQLite write, unbounded rows for deny/defer-heavy users #3657

Closed

Description

@KuSh

Found by `/code-review high` while iterating on #3206 (not fixed there — both are inherent tradeoffs of the ground-truth hook-decision-logging feature itself, not bugs introduced by that PR's fixup rounds, so flagged here for a deliberate design call rather than changed unilaterally).

1. `log_hook_decision` opens a full SQLite `Tracker` on every PreToolUse hook call

`src/hooks/hook_cmd.rs`, `log_hook_decision` (called from `run_claude()` for every `Rewrite` and `Skip` outcome — i.e. every single Bash tool call, not just RTK-covered ones). Before #3206 this hot path only wrote to an append-only JSON audit log (when `RTK_HOOK_AUDIT=1`). Now it unconditionally opens a `Tracker` (directory creation, `chmod` on the DB + WAL/SHM sidecars, WAL pragma, a `PRAGMA user_version` read, an INSERT) on every hook invocation, including commands that end up `Deny`/`Defer`d and never touch `rtk` at all. This is under the hook's <10ms latency budget documented in `cli-testing.md`.

This is arguably the intended cost of the feature (ground-truth logging requires writing on every call), but worth a deliberate perf pass — e.g. reusing a single `Tracker`/connection across the hook process's lifetime instead of opening fresh each time, or confirming the actual overhead is negligible with `hyperfine`.

2. `hook_decisions` can grow unbounded for a user whose commands are mostly denied/deferred

`src/core/tracking.rs`: `record_hook_decision()` deliberately skips `cleanup_old()` (by design — see its doc comment, avoiding a 3-table DELETE sweep on the hot path). Retention for `hook_decisions` piggybacks entirely on `record()`/`record_parse_failure()`'s cleanup cadence, which only fires when the user actually runs an `rtk ` or hits a parse failure. A user whose intercepted commands are mostly `Deny`/`Defer` (heavy `permissions.deny` rules, or frequent unattestable constructs) may rarely trigger either of those, so `hook_decisions` rows accumulate indefinitely. `hook_decisions_since` (used by `rtk discover`) also has no `LIMIT`, so a later `discover` run loads the entire unbounded result set into an in-memory `HashMap`.

Possible fixes: give `hook_decisions` its own periodic/probabilistic cleanup trigger independent of `record()`/`record_parse_failure()`, and/or add a row cap to `hook_decisions_since`'s query.

Neither of these is a correctness bug — flagging for a deliberate decision on whether/how to address.

Activity

  1. pszymkowiak commented on Aug 23, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟢 Priority low
    🎯 Confidence 62%

    Summary

    Design/perf tradeoffs in hook_decisions logging (per-call SQLite Tracker open, unbounded row growth for deny/defer-heavy users) flagged for deliberate decision, not a correctness bug.

    📁 Relevant files
    • src/hooks/hook_cmd.rs
    • src/core/tracking.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. KuSh commented on Aug 23, 2026

    @KuSh
    CollaboratorAuthor

    Should be handled by and in #3206, closing and will handle directly in the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions