Skip to content

perf(discover): estimate_hook_coverage double-scans for unattestable constructs #3650

Open

Description

@KuSh

Found by `/code-review high` while iterating on #3206 (not fixed there — pre-existing, and a real fix touches a shared function's contract used by other callers).

In `src/discover/mod.rs`, the estimate fallback path scans the same command with `lexer::contains_unattestable_construct` twice per call:

  1. `permissions::check_command_with_rules` (called from `estimate_hook_coverage`) already calls it internally, to force an `Ask` verdict on unattestable constructs.
  2. `estimate_hook_coverage_with_verdict` calls it again explicitly, because a `check_command_with_rules`-returned `Ask` verdict is ambiguous — it doesn't distinguish "unattestable construct" from "an ask_rules pattern matched" — and the real hook (`hooks::hook_cmd::decide_from_verdict`) unconditionally defers on unattestable constructs regardless of verdict, so `estimate_hook_coverage_with_verdict` needs to know that fact directly, not just that the verdict was `Ask`.

This runs in `rtk discover`'s per-command scan loop — over a large history (tens of thousands of commands) this is avoidable duplicate work in the exact path #3206 was optimizing.

Possible fix: have `check_command_with_rules` (or a new variant) return whether it hit the unattestable-construct branch alongside the verdict, so callers don't need a second scan. This touches `src/hooks/permissions.rs`'s public(crate) contract and its other callers (`hooks::hook_cmd`), so it's a slightly bigger change than a one-file fix — worth scoping as its own PR rather than folding into #3206.

Activity

  1. added
    bug
    help wanted
    on Aug 23, 2026
  2. pszymkowiak commented on Aug 23, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟢 Priority low
    🎯 Confidence 88%

    Summary

    estimate_hook_coverage redundantly scans commands twice for unattestable constructs in the discover per-command loop; fix requires widening check_command_with_rules's contract and should be its own PR.

    📁 Relevant files
    • src/discover/mod.rs
    • src/hooks/permissions.rs
    • src/hooks/hook_cmd.rs

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

  3. k4its1t commented on Aug 24, 2026

    @k4its1t

    I’d like to work on this. I’ll make the permission check return the unattestable-construct fact alongside its verdict, thread that result through the existing hook/discover callers, and add regression coverage showing the discover estimate does not rescan the command. I’ll keep the change scoped to this contract/performance issue.

  4. k4its1t commented on Aug 24, 2026

    @k4its1t

    Update after checking the latest develop: the estimate_hook_coverage path this issue targets only exists on the still-open #3206 branch, so a fix today would have to be a stacked PR and would not be independently mergeable into develop. I’m stepping back for now rather than creating that dependency; this should be picked up once #3206 lands (or if maintainers explicitly want a stacked change).

  5. ZXLyyds100 commented on Oct 17, 2026

    @ZXLyyds100
    Contributor

    Implemented in #4637, independently based on current develop now that #3206 has landed.

    Discover reuses a command-bound permission result, so an ordinary estimate attests once instead of twice. Existing verdict-only APIs and permission precedence remain unchanged. An early wrapper Ask retains an unknown attestation and still scans once when needed; it is not treated as safe.

    The PR includes the original failing call-count regression, fixed/short-circuit/contract tests, and a negative control that rejects an intentionally unsafe cache value. Full workspace tests passed (4,965; all 8 ignored tests also passed separately), as did 4,505 no-default-feature tests.

    Twenty native CLI scenarios preserve discover report values, rewrite stdout and exit codes. The linked screenshot and 14.36-second recording run actual local binaries on isolated synthetic transcripts, not product UI or hosted CI. Timing observations are not a claim of a 50% overall speedup.

    Maintainer security review and the account holder's CLA signature remain outstanding.

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

    area:performancebughelp wantedpriority:low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions