Repository navigation
perf(hooks)!: cut the wait on each tool call and prompt from ~180 ms to ~30 ms - #1801
Conversation
|
No confident findings in the specified PR diff. The description documents sufficient testing: built-CLI verification with Claude Code payloads, lint, type checks, targeted tests, and E2E results. Deferred Windows timings and additional provider/agent coverage are not blocking under the trusted review rules. Review was read-only; I did not run, build, or install anything from the PR. |
SaulMoro
left a comment
There was a problem hiding this comment.
This was generated by AI during triage.
Requesting changes at 8b8fe5b for one CHANGELOG item. The speed-up itself holds up.
I ran it with a built CLI against main, with the same fixture on both builds:
- A tool call in a scope goes from 8 to 10 git processes to none once the cache is warm. The median falls from 182 ms to 37 ms.
- A prompt falls from 272 ms to 37 ms.
- The full dispatch still runs for a waiting package hint,
TEAMAI_HTTP_ENDPOINT, a sessionpendingHint,CURSOR_VERSION, and a payload with no session id. - The members cache is dropped when the team repo's
origin/HEADmoves.
I read 8b8fe5b1 (the needsBundle field) after the run, and it changes none of this.
[P1 blocking] A team that keeps the old TodoWrite timeout override now fails teamai doctor, and the Breaking Changes section does not say so. On main, a hooks/hooks.yaml with builtin.overrides for Hook dispatch post-tool-use TodoWrite ends with ✔ All checks passed!. At this head the same file ends with
✖ Team built-in hook overrides name hooks teamai installs
⚠ Some checks failed. See suggestions above.
and doctor exits 1, so a team's CI job that runs teamai doctor turns red after the upgrade. The check is marked informational, but allPassed in src/doctor.ts:1223 still counts it. The changes that come with it are listed only under Features:
- the override now does nothing;
- the
--matcher TodoWritehook entry is no longer installed; - on Cursor, Codex and WorkBuddy, the hint's timeout goes from 3 s to 10 s.
Either exclude informational checks from allPassed, or keep the exit 1. In both cases, add a Breaking Changes entry naming the retired override and what a team should do: remove it, or move the timeout to Hook dispatch post-tool-use wildcard.
[P2 non-blocking] docs/guide/sharing.md:253, and its Chinese page, say a TodoWrite call waits "for the reminder, even with recall off". With recall off there is no reminder now, so that sentence needs updating.
The recall gate in d3add68c overlaps #1797. We'll take it through this PR, and I've told #1797 so.
The CI review found nothing. I agree on the code, but it missed the doctor exit code above. CI is still running at this head.
One more blocker at 8b8fe5b. E2E shard 3/3 fails in A late prompt is recorded a second time, and an event whose |
E2E shard 2/3 is red at 8b8fe5b as well: 13 failures, all of them hook events that are no longer recorded.
Copilot is one of the tools the fast path is meant to leave to the full dispatch. That suggests the |
Every config load read members.yaml at origin/<default> with three git processes (default branch, tip, show), and a hook loads the config on every tool call: about 650 git processes and 3.4 s of a typical session. The answer is now kept with the other git answers (GitAnswers, in the per-user temporary directory), keyed by the team repo's real path and stamped with the ref origin/HEAD names and its commit. The ref and commit come from the git dir's files (loose ref, then packed-refs; .git files and commondir followed), so any fetch or pull reads again. Where anything is unexpected, teamai asks git as before. A failed read is never remembered as an absent file, an HTTP source is untouched, and a dry run leaves the TeamAI home as it found it. Tested: role-assignments-cache.test.ts (no git helper runs in a later process at the same commit; a fetch reads again; nothing written in the TeamAI home; failed read, unparsable file, linked worktree and packed refs), role-assignments.test.ts, stats-recall-dry-run and contribute-namespace E2E. Refs Tencent#1788
Windows kept no git answer past its run: the answers sit in the temporary directory only where its owner can be checked, and Windows has no uid to check it by. So every Windows hook, which takes no fast path, still started the three git processes a config load reads manifest/members.yaml with. Windows now keeps them in `%TEMP%\teamai` when the temporary directory is inside the user's profile, as `%LOCALAPPDATA%\Temp` is by default and no other user can write. A TEMP set anywhere else keeps nothing, as before. Tested: role-assignments-cache.test.ts, now also in the Windows CI selection (an answer kept inside the profile, none outside it, and a single-repo team read once at the fetched commit). Refs Tencent#1788
recall-attribution, votes-sync and votes-push checked only TEAMAI_RECALL_DISABLED, so they recorded and pushed votes for every team, recall on or off. They now skip their work unless recall is on for the hook's scope, as isRecallEnabled resolves it: the environment switch, then the member's recallEnabled, then the team's sharing.recall.enabled. Nothing is installed or removed, so `teamai recall disable` and `enable` take effect at the next event. BREAKING CHANGE: recall is off by default, so a team that never enabled it stops recording recall attribution and votes. Tested: recall-attribution.test.ts (nothing recorded, credited or pushed with recall off; crediting stops at the next event once the member turns it off), hook-handlers.test.ts. Refs Tencent#1788
The hook fast path writes debug.log before the CLI bundle loads, and the logger imports chalk, which costs a hook about 5 ms to load. The file transport (append, rotation, its test hooks) moves unchanged into utils/debug-log.ts, which needs only node:fs, node:path and utils/home.ts. Tested: the eight test files that set the log path, unchanged apart from their import. Refs Tencent#1788
A tool call kept the agent waiting about 160 ms for a process that runs no handler for any tool but TodoWrite: it only started the --bg-only child. Most of that was loading the bundle. bin.ts now asks hook-fast-path.ts first. For the exact command teamai installs, `hook-dispatch post-tool-use --tool <t> [--matcher <m>]`, it reads the payload, starts the same --bg-only child with it, writes `[hook-dispatch] post-tool-use/<tool> fast path` to debug.log, and exits. The full dispatch still runs on Windows, for team hooks, TodoWrite, Copilot, agy and git, a Claude-format hook under Cursor or Copilot CLI, a workflow step, and a payload without a session id; the stdin already read is handed to it in-process. The fast path forwards neither teamai_run nor teamai_launch, so the --bg-only child reads both from the session's environment, which a POSIX child inherits. The full dispatch now forwards "no launch" as null, so the child never reads one from its own environment there. Tested: hook-fast-path.test.ts (every full-dispatch condition, the payload rules, the copies of launch-context's names), hook-dispatch-cli.test.ts (stdin handed over; the child fills the run and launch), and e2e/hook-fast-path-1788.test.ts on the built CLI. Refs Tencent#1788
…ide any scope Every tool call in a folder outside any teamai scope still ran a background pass, because the built-ins sit in the member's user-level tool settings. Outside a scope, the only handler PostToolUse and UserPromptSubmit keep is the HTTP local agent's, which does nothing unless the machine syncs over HTTP. The background child now resolves the scope as before and, when there is none and the machine is not HTTP-enrolled, writes `[hook-dispatch] <event>/<tool> no scope` to debug.log and exits. Enrolled means local-agent/config.json without "disabled": true, a member config with an HTTP repo (the v0.17 enrolment loadLocalAgentConfig backfills from), or an endpoint variable. The check reads files only, so the fast path can use it too, and a test holds it to never be narrower than loadLocalAgentConfig. Other events are unchanged. Tested: hook-dispatch-cli.test.ts (no handler and the no-scope line for both events), hook-fast-path.test.ts (each enrolment, checked against the loader), hook-handlers.test.ts (outside a scope only local-agent-sync is left in the background for these events), and e2e/hook-fast-path-1788.test.ts: no scope; enrolled through config.json and TEAMAI_HTTP_ENDPOINT; a partition project with no .teamai folder and a legacy <project>/.teamai install keep their handlers. Refs Tencent#1788
Every tool that is installed the full set of built-ins had a second PostToolUse entry for TodoWrite alone. That is one more process on each TodoWrite call, for a hint the wildcard pass can give itself. The hint now runs in the wildcard pass, answering only a TodoWrite call: - only for the tools that had the entry; an adapter tool (OpenCode, Pi, OMP, Antigravity, ...) still gets none; - off where the team lists `Hook dispatch post-tool-use TodoWrite` or the wildcard key under builtin.disabled, read at run time; an unreadable hooks file keeps it off, as the retired Skill key does; - with its 2.5 s budget; the entry's timeout is now the wildcard's 10 s. A timeout under builtin.overrides for the old key applies to nothing, and `teamai doctor` says so. The old entry is in a list of retired built-ins, so cleanup of a project's team-only file still recognises the copies an earlier release wrote there; user settings drop it by its description. The fast path hands a TodoWrite call to the full dispatch, and an old `--matcher TodoWrite` entry still installed runs no handler until the next pull removes it. Tested: todowrite-hint.test.ts (the hint per tool, builtin.disabled cases), hooks-skill-retire.test.ts (team-only cleanup), doctor-retired- builtin.test.ts, hook-fast-path.test.ts; golden fixtures change only by the removed entry. Refs Tencent#1788
On macOS and Linux a prompt waits on no handler unless something is left for it to print. The prompt's records, skill-mention and dashboard-report, now run in the detached child there, and the fast path from the tool-call hook takes a prompt too: it starts that child and exits before cli.js loads. Windows keeps them inline, so no WMI spawn is added. The full dispatch still runs when: - any condition of the tool-call fast path holds, Copilot and agy included; - a package hint waits in ~/.teamai/package-hints/<sid>.pending.json; - the session's ~/.teamai/sessions/<sid>.json holds a pendingHint, looked for on every tool rather than only where Stop output is ignored, the only place one is written: one read, and no copy of the tool table; - the machine syncs over HTTP, so the local agent can print its notices. <sid> is the session id with every character outside [a-zA-Z0-9._-] replaced, as both hints name their files. On a host that sends no turn id, a skill used in the turn's first tool call can now be credited to the turn before; docs/guide/dashboard.md says so. Tested: hook-fast-path.test.ts (argv, the session id, each pending hint written or read by its real producer or consumer), hook-handlers.test.ts (the foreground of a prompt per platform), hook-fast-path-1788 E2E (fast path and the child's records; a waiting hint printed on 20 of 20 prompts; 20 background passes complete). Refs Tencent#1788
…ncies The hook fast path saves its time by loading nothing of the CLI bundle, which held only by a comment: one import of the logger, zod or the tool table in bin.ts, hook-fast-path.ts or a helper they load would put the bundle back on every tool call, and no test would fail. The lint rule teamai/uncached-entry now fails any import there but a node: module or another file of that set (UNCACHED). readStdin and the stdin hand-off move into utils/hook-stdin.ts, one module of that set, which the team hooks import too instead of reaching into the fast path. The fast path's session id is now checked against deriveSessionId, as the hint files the prompt fast path looks for are named by it. Tested: uncached-entry.test.ts, hook-fast-path.test.ts, hook-dispatch-cli.test.ts. Refs Tencent#1788
sharing.md, in both languages, shows the debug.log lines a fast path, its background pass and a hook outside any scope write, from a real run. The CHANGELOG records the timings, the git calls a tool call no longer starts, the merged TodoWrite entry, and the recall switch now stopping the recall hooks. tool-launch.md names the fast path's own launch check. Refs Tencent#1788
The TodoWrite hint tells the agent to use the teamai-recall agent, and it checked only TEAMAI_RECALL_DISABLED. Recall is off by default, and with it off pull removes that agent and its rule, so on every team that never turned recall on the hint sent the agent to an agent it does not have. The hint now reads the same switch as the other recall hooks and `teamai recall status`: the variable, then the member's recallEnabled, then the team's sharing.recall.enabled. Tested: todowrite-hint.test.ts (no hint with recall off, which fails without this change; the hint as before with recall on), hook-handlers, recall-attribution and hook-dispatch-cli tests. Refs Tencent#1788
The hook fast path keeps its own list of the tools whose hook it must leave to the full dispatch (Copilot CLI, Antigravity CLI), because it runs before the tool table can load. Nothing checked that list: a new tool whose payload the bundle translates would have taken the fast path, and its background child would have read a payload it cannot parse. Each tool now states `hooks.needsBundle` in TOOLS; the field is required, so a new tool does not type-check until it decides. A test checks the fast path's verdict for every tool, on both events, against that field. No tool's behaviour changes. Tested: hook-fast-path.test.ts (seen failing before the field existed), tools, tools-channels, locate-matrix, builtin-hooks, hooks, copilot and agy tests. Refs Tencent#1788
Since the UserPromptSubmit handlers moved to the detached pass, a test that counts recorded prompts right after hook-dispatch returns can read the file before the child writes it. Wait for tracked detached children first, with a sync variant for the tests that run the CLI with spawnSync.
…iling A team timeout for `Hook dispatch post-tool-use TodoWrite`, which teamai no longer installs, made doctor exit 1. It is now a note that says where the hint runs and what to do, and doctor still passes. The CHANGELOG lists the entry's removal under breaking changes, and the sharing guide says a TodoWrite call with recall off prints nothing.
8b8fe5b to
c305ef4
Compare
|
All three are fixed at c305ef4, which is rebased on main. Doctor exit code (P1). An ignored The CHANGELOG now lists, under Breaking Changes: the entry is no longer installed, the override does nothing, what to do instead, and the 3 s to 10 s timeout on Cursor, Codex and WorkBuddy. sharing.md (P2). Both pages now say that with recall off a E2E shards 2/3 and 3/3. These were tests reading
#1797. Noted, thanks. |
|
No confident findings in the specified PR diff. The PR description documents sufficient testing: built-CLI verification with Claude Code payloads, lint, type checks, targeted tests, and E2E results. Deferred Windows timings and additional provider/agent coverage are not blocking under the trusted review rules. The earlier pass reported no findings, so there are none to mark resolved. This review was read-only; I did not run, build, or install anything from the PR. |
SaulMoro
left a comment
There was a problem hiding this comment.
This was generated by AI during triage.
Approving at c305ef4.
- Doctor. An ignored
Hook dispatch post-tool-use TodoWritetimeout is now a note, soteamai doctorstill passes. Keepinginformationalchecks inallPassedis fine, since doctor documents that they count. - CHANGELOG. The Breaking Changes section now says the entry is gone, that its override applies to nothing and what to do instead, and that the timeout goes from 3 s to 10 s on Cursor, Codex and WorkBuddy.
- Guide. Both
sharing.mdpages now say a TodoWrite call with recall off prints nothing. - E2E failures. Your explanation fits. The prompt is now recorded by the detached pass, so
deleted-worktree-scope-810,copilot-global-hooksandslash-skill-pathsreadevents.jsonlbefore it was written.d9e21c39makes them wait for the tracked detached children, and the assertions are unchanged. Copilot still takes the full dispatch, andhook-fast-path.test.tspins every tool's verdict tohooks.needsBundle.
Recording prompts in the background is what the speed-up rests on. It also means an event lands a moment after the hook returns, and anything that reads events.jsonl right away has to allow for that.
Conflicts: - src/tools.ts: keep both hook fields, projectDirVariable (Tencent#1787) and needsBundle (Tencent#1801). - src/__tests__/helpers/windows-tests.ts: keep both added test files, in order. - CHANGELOG.md: keep the folder-marker entry, which replaces the Tencent#1791 one, and add Tencent#1801's two entries. Tencent#1801 gives the TodoWrite hint only while recall is on, so the test that a team's replacement text is read from a data home with spaces turns recall on.
Summary
Every tool call and every prompt kept the agent waiting on teamai's hook, even when that hook had nothing to say. The time went on loading the CLI bundle, and on three git processes per config load to read
manifest/members.yaml. On macOS and Linux, a hook with nothing to print now starts its background process and exits before the bundle loads. The members answer is kept per commit of the team repo.Each commit is one slice of #1788, in this order:
perf(roles): read manifest/members.yaml once per commit of the team repoperf(git): keep git answers between runs on Windows toofeat(recall)!: let the recall switch stop the recall hooksrefactor(logger): move the debug.log transport into a module of its ownperf(hooks): exit a PostToolUse hook before the CLI bundle loadsperf(hooks): run no background handler for a tool call or prompt outside any scopeperf(hooks): give the TodoWrite hint from the PostToolUse wildcard entryperf(hooks): exit a UserPromptSubmit hook before the CLI bundle loadsbuild(lint): keep what runs before the CLI bundle free of its dependenciesdocs: say when a hook keeps the agent waiting, and record #1788fix(recall): give no TodoWrite reminder while recall is offrefactor(tools): record which tools' hooks need the CLI bundle in TOOLStest(e2e): wait for the detached pass that records a promptfix(doctor): note an ignored TodoWrite timeout override instead of failingteamai doctorNothing from #1788 is left for a follow-up.
Slice 4 drops one entry from every tool that had it. Claude Code's
settings.jsonafter the next pull:Type of Change
Breaking, two changes, both under Breaking Changes in the CHANGELOG.
The TodoWrite hint has no hook entry of its own; it runs in the
PostToolUse*entry. Abuiltin.overridestimeout a team set forHook dispatch post-tool-use TodoWritenow applies to nothing.teamai doctorprints a note saying so and still passes:On Cursor, Codex and WorkBuddy the hint now has the wildcard's 10 s timeout where its entry had 3 s.
With recall off, which is the default, the hooks record no recall adoption, cast or push no vote, and give no
TodoWritereminder. They used to check onlyTEAMAI_RECALL_DISABLED, so a team that never turned recall on still collected votes, and its agents were told to use ateamai-recallagent that pull had removed.Test Plan
npx tsc --noEmitpassesnpm run lintpasses, documentation checks includedTimings.
bench.mjsran the origin/main build and this branch's build alternately on the same machine: 25 runs per scenario, a 0.6 s pause between samples, one warm-up each. A git shim counted the git processes; the debug.log lines come from one counted run of each. Node v24.21.0, Apple M4 Pro, load average 7.51 6.37 11.97 at the start and 16.37 9.44 12.35 at the end.debug.log for one tool call in a scope:
debug.log for one prompt in a scope. The foreground ran 5 handlers; now the fast path runs and the 2 that print nothing run in the background:
debug.log for a tool call outside any scope:
The 10 git processes one tool call in a scope started before:
git --version×2,rev-parse --abbrev-ref origin/HEAD×2,rev-parse --verify origin/main^{commit}×3,show origin/main:./manifest/members.yaml×3. After: none.Real CLI on this branch's build. One pass through the built
dist/index.jswith a sandboxed HOME, a cloned team repo and Claude Code payloads:teamai hooks injectthenteamai doctor, without and with the retired override inhooks/hooks.yaml:Tests per slice. Where a test was seen failing before its change, it says so:
The slice 2 module was written before its tests. The tests came straight after, and mutation testing then killed 26 of 27
argvVerdictmutants (the survivor is equivalent) and 9 of 9childPayloadmutants.Not checked here:
hook-fast-path.test.tsandrole-assignments-cache.test.tsare in the Windows CI selection.local-agent*,home,uninstall,check-docs,doctor-rules-delivery, 659/659 on rerun. CI runs the full suite.Related Issues
Closes #1788
Notes for Reviewers
Where this differs from the issue:
The prompt fast path looks for a contribute
pendingHinton every tool, not only on tools whose Stop reply is ignored. Those are the only tools one is written for, so this costs one small read and keeps no copy of the tool table.The issue names
local-agent/config.yamlfor the old HTTP enrolment. The loader reads~/.teamai/config.yamlwith an HTTP repo, so the fast path checks that.Once
local-agent/config.jsonexists, itsdisabled: truewins over the endpoint variables, as in the loader ("a removed source must not reconnect").The fast path has no check for the ephemeral config. That config is only set together with
TEAMAI_WORKFLOW_ATTEMPT, which already takes the full dispatch.The members answer is not one file per scope in the data home. It lives with teamai's other git answers in the per-user temporary directory, one file per CLI version (
$TMPDIR/teamai-<uid>/git-answers-<version>.json; on Windows%TEMP%\teamai\, and only when that directory is inside the user's profile). A file in the TeamAI home broke the--dry-runpromise to write nothing there.Until the next pull, an old
--matcher TodoWriteentry still runs a full dispatch, but no handler answers it. The*entry already gives the hint for the same call.On macOS and Linux a
TodoWritecall still takes the full dispatch, even with recall off, because the fast path cannot read the switch without loading the bundle.Slice 6 moves the prompt handlers that print nothing (
dashboard-report,skill-mention) to the background child on every POSIX path, the full dispatch included. Copilot still takes the full dispatch, but its prompt is now recorded afterhook-dispatchreturns. The three E2E files that failed in CI readevents.jsonlright after the call and now wait for the child first, with the same assertions. For example,deleted-worktree-scope-810saw a thirdprompt_submitbecause the setup's prompt landed after the test had counted.An ignored override is a doctor note, not a failed
informationalcheck. Takinginformationalchecks out ofallPassedwould change the exit code for every such check, andsrc/doctor.tsdocuments that they count.Things worth a close look:
src/hook-fast-path.tskeeps copies of a few names the bundle owns, because it runs before the bundle loads. A test checks each one against its owner, including the tools whose hook needs the bundle, which is the new requiredhooks.needsBundleinTOOLS.src/utils/git-ref-files.tsreadsorigin/HEADand its sha from the git dir's files: loose refs, then packed-refs, with.gitfiles andcommondirfollowed. Anything unexpected falls back to asking git.teamai_launchis now forwarded asnullrather than deleted, so the background child can tell "the foreground said no launch" from "the fast path forwarded nothing".