Skip to content

perf(hooks): reuse a session's bot lookup across PreToolUse fires once a scoped hook exists #47818

Todo

Description

@buluoray

Goal: a scoped hook does not make every PreToolUse fire rebuild the config to name the caller's bot.

Found in review of #47124 (question on _member_id_for). Today hook_scope.scope_of is computed once per fire, lazily, and only when a scoped hook is stored (ScriptHookStore.fire, kas_wire, kas_agents.pre_tool_hook_matchers). It is not cached across fires. Each computation calls KiroCrewConfig.load(), which serves the parsed document from a stat-keyed cache but rebuilds every section dataclass with the GIL held (see config/load_memo.py), then crewmate_for_session and, when that finds no bot, read_session_execution. It runs off the event loop (asyncio.to_thread), so it does not block the loop, but every tool call pays it once a scoped hook exists.

Proposed change: answer the bot half of scope_of through config.load_memo.memoized, keyed on the config fingerprint and the session key, so a fire reuses the last answer while the config files are unchanged. The project half reads the live slot table and stays per fire. A worker record written after a session's first lookup must still be seen, so the memo must also miss when the session's execution record changes (or the record read stays outside the memo).

Not a goal: caching the project lookup, or changing what a scope matches.

Test: a fire with a scoped hook calls KiroCrewConfig.load at most once across N fires for one session while the config is unchanged, and again after a config edit.

Activity

  1. added
    tier:T1
    P99
    on Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P99tier:T1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions