Skip to content

perf(hooks)!: cut the wait on each tool call and prompt from ~180 ms to ~30 ms - #1801

Merged
SaulMoro merged 14 commits into
Tencent:mainfrom
hz89Dev:feat/issue-1788-hook-wait
Nov 9, 2026
Merged

SaulMoro merged 14 commits into
Tencent:mainfrom
hz89Dev:feat/issue-1788-hook-wait

Conversation

@hz89Dev

@hz89Dev hz89Dev commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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.

Hook (Claude Code, median of 25 interleaved runs) before after git processes per event
tool call in a scope 176.9 ms 32.2 ms 10 → 0
prompt in a scope 200.2 ms 30.5 ms 6 → 0
tool call outside any scope 154.1 ms 36.1 ms 0 → 0
 teamai hook-dispatch post-tool-use --tool claude          (index.js, before the compile cache)
+  runHookFastPath                                          src/hook-fast-path.ts
+    argv is the exact installed command, POSIX, no team hooks / workflow step / copilot / agy / git
+    payload has a session id and is not TodoWrite
+    prompt: no package or contribute hint waiting, machine not synced over HTTP
+    → spawn the same --bg-only child, log "fast path", exit
   import('./cli.js')                                       anything else, with the stdin already read handed over
     hookDispatchCli → foreground handlers → --bg-only child
-      loadLocalConfig → git rev-parse ×2, git show manifest/members.yaml
+      loadLocalConfig → members.yaml answer kept per (repo, origin/HEAD ref, sha)
+      no teamai scope → "no scope", no handler (unless the machine syncs over HTTP)

Each commit is one slice of #1788, in this order:

Commit Slice
perf(roles): read manifest/members.yaml once per commit of the team repo 1
perf(git): keep git answers between runs on Windows too 1, on Windows
feat(recall)!: let the recall switch stop the recall hooks 5
refactor(logger): move the debug.log transport into a module of its own prepares 2: the fast path logs without the logger
perf(hooks): exit a PostToolUse hook before the CLI bundle loads 2
perf(hooks): run no background handler for a tool call or prompt outside any scope 3
perf(hooks): give the TodoWrite hint from the PostToolUse wildcard entry 4
perf(hooks): exit a UserPromptSubmit hook before the CLI bundle loads 6
build(lint): keep what runs before the CLI bundle free of its dependencies guards 2 and 6
docs: say when a hook keeps the agent waiting, and record #1788 docs, CHANGELOG
fix(recall): give no TodoWrite reminder while recall is off 5, the hint too
refactor(tools): record which tools' hooks need the CLI bundle in TOOLS guards 2 and 6: a new tool must say whether its hook may skip the bundle
test(e2e): wait for the detached pass that records a prompt 6: tests read events the background child now writes
fix(doctor): note an ignored TodoWrite timeout override instead of failing 4: an old override must not fail teamai doctor

Nothing from #1788 is left for a follow-up.

Slice 4 drops one entry from every tool that had it. Claude Code's settings.json after the next pull:

         "description": "[teamai] Hook dispatch post-tool-use wildcard"
-      },
-      {
-        "matcher": "TodoWrite",
-        "hooks": [
-          {
-            "type": "command",
-            "command": "bash -lc \"teamai hook-dispatch post-tool-use --tool claude --matcher TodoWrite 2>/dev/null\" || true"
-          }
-        ],
-        "description": "[teamai] Hook dispatch post-tool-use TodoWrite"
       }

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature causing existing behavior to change)
  • Documentation only
  • Refactor / internal cleanup

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. A builtin.overrides timeout a team set for Hook dispatch post-tool-use TodoWrite now applies to nothing. teamai doctor prints a note saying so and still passes:

ℹ hooks/hooks.yaml sets a timeout under `builtin.overrides` for `Hook dispatch post-tool-use TodoWrite`, an entry teamai no longer installs: the TodoWrite hint runs in `Hook dispatch post-tool-use wildcard`, so this timeout is ignored. Remove it from hooks/hooks.yaml; a timeout for `Hook dispatch post-tool-use wildcard` applies to every tool call.
✔ All checks passed!

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 TodoWrite reminder. They used to check only TEAMAI_RECALL_DISABLED, so a team that never turned recall on still collected votes, and its agents were told to use a teamai-recall agent that pull had removed.

Test Plan

  • npx tsc --noEmit passes
  • npm run lint passes, documentation checks included
  • The test files that cover what I changed pass
  • Added/updated tests for the change

Timings. bench.mjs ran 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:

before  [hook-dispatch] post-tool-use/claude background: 43ms total, 5 handlers, slowest local-agent-sync=42ms | ...
after   [hook-dispatch] post-tool-use/claude fast path
        [hook-dispatch] post-tool-use/claude background: 16ms total, 5 handlers, slowest dashboard-report=15ms | ...

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:

before  [hook-dispatch] prompt-submit/claude foreground: 44ms total, 5 handlers, slowest local-agent-sync=44ms | local-agent-sync=44ms(ok) dashboard-report=22ms(ok) skill-mention=21ms(ok) package-pending-hint=14ms(ok) pending-hint=10ms(ok)
after   [hook-dispatch] prompt-submit/claude fast path
        [hook-dispatch] prompt-submit/claude background: 13ms total, 2 handlers, slowest dashboard-report=13ms | dashboard-report=13ms(ok) skill-mention=12ms(ok)

debug.log for a tool call outside any scope:

before  [hook-dispatch] post-tool-use/claude background: 14ms total, 1 handlers, slowest local-agent-sync=13ms | local-agent-sync=13ms(ok)
after   [hook-dispatch] post-tool-use/claude fast path
        [hook-dispatch] post-tool-use/claude no 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.js with a sandboxed HOME, a cloned team repo and Claude Code payloads:

$ teamai hook-dispatch post-tool-use --tool claude   # Read in scope: exit 0, 44 ms
  [hook-dispatch] post-tool-use/claude fast path
  [hook-dispatch] post-tool-use/claude background: 14ms total, 5 handlers, slowest dashboard-report=14ms | ...
$ teamai hook-dispatch prompt-submit --tool claude   # prompt in scope: exit 0, 47 ms
  [hook-dispatch] prompt-submit/claude fast path
  [hook-dispatch] prompt-submit/claude background: 12ms total, 2 handlers, slowest dashboard-report=12ms | ...
$ teamai hook-dispatch post-tool-use --tool claude   # TodoWrite, recall off: exit 0, 125 ms, no output
  [hook-dispatch] post-tool-use/claude foreground: 3ms total, 1 handlers, slowest todowrite-hint=3ms | todowrite-hint=3ms(ok)
$ teamai hook-dispatch post-tool-use --tool claude   # TodoWrite, recall on: exit 0, 149 ms
stdout: {"hookSpecificOutput":{"hookEventName":"PostToolUse","additionalContext":"[teamai:todowrite-hint] Task plan detected. ...
  [hook-dispatch] post-tool-use/claude foreground: 4ms total, 1 handlers, slowest todowrite-hint=4ms | todowrite-hint=4ms(ok)

teamai hooks inject then teamai doctor, without and with the retired override in hooks/hooks.yaml:

--- PostToolUse entries in ~/.claude/settings.json
{"matcher":"*","command":"teamai hook-dispatch post-tool-use --tool claude 2>/dev/null\" || true"}
--- without the override
exit 0
✔ All checks passed!
--- with the override  (builtin.overrides: Hook dispatch post-tool-use TodoWrite: { timeout: 5 })
exit 0
ℹ hooks/hooks.yaml sets a timeout under `builtin.overrides` for `Hook dispatch post-tool-use TodoWrite`, an entry teamai no longer installs: ...
✔ All checks passed!

Tests per slice. Where a test was seen failing before its change, it says so:

role-assignments-cache   a later process at the same commit runs no git helper        (3 failed on origin/main)
                         a fetch reads again; a failed read is not remembered; nothing written in the TeamAI home
                         Windows: kept inside the profile, nothing kept outside it    (failed before the Windows commit)
                         single-repo team read once at the fetched commit; linked worktree with packed refs
hook-fast-path           every full-dispatch condition; each tool's verdict equal to TOOLS hooks.needsBundle
                         (failed before the field existed); session id equal to deriveSessionId's;
                         prompt hint files checked against the real writers and readers
hook-dispatch-cli        stdin handed over in-process; the child fills teamai_run / teamai_launch
hook-handlers            prompt-submit foreground per platform; "no scope" leaves only local-agent-sync
todowrite-hint           hint from the * pass for claude, codebuddy, cursor, codex, dsh; none for opencode, pi, omp, agy;
                         builtin.disabled on either key; no hint with recall off (failed before the fix)
hooks-skill-retire       pull removes the old TodoWrite entry from a team-only file
doctor-retired-builtin   a timeout override for the retired key is a note, not a failed check (failed before the fix)
recall-attribution       recall off: nothing written or pushed; disable takes effect at the next event
uncached-entry (lint)    an import of zod, the logger or the tool table in what runs before the bundle fails lint
e2e/hook-fast-path-1788  10/10 on the built CLI: fast-path line, background total, events.jsonl runMode,
                         no scope, HTTP enrolment, multi-repo child, a package hint printed on each of 20 runs
e2e stats-recall-dry-run, contribute-namespace   pass (a dry run still writes nothing in the TeamAI home)
e2e deleted-worktree-scope-810, copilot-global-hooks, slash-skill-paths
                         47/47 once each test waits for the detached child before it reads events.jsonl

The slice 2 module was written before its tests. The tests came straight after, and mutation testing then killed 26 of 27 argvVerdict mutants (the survivor is equivalent) and 9 of 9 childPayload mutants.

Not checked here:

  • No Windows timings. On Windows only slice 1, and its Windows commit, change what the agent waits for; the fast paths are POSIX only, as [feat] Agent hooks: keep the agent waiting less #1788 decided. hook-fast-path.test.ts and role-assignments-cache.test.ts are in the Windows CI selection.
  • Real CLI runs used Claude Code payloads only. Codex, CodeBuddy and OpenCode take the same code path; CI covers the matrix.
  • The full unit suite ran once locally under parallel load. The only failures were tests that also failed the same way on origin/main at that moment, or that passed when rerun alone: 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 pendingHint on 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.yaml for the old HTTP enrolment. The loader reads ~/.teamai/config.yaml with an HTTP repo, so the fast path checks that.

  • Once local-agent/config.json exists, its disabled: true wins 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-run promise to write nothing there.

  • Until the next pull, an old --matcher TodoWrite entry 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 TodoWrite call 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 after hook-dispatch returns. The three E2E files that failed in CI read events.jsonl right after the call and now wait for the child first, with the same assertions. For example, deleted-worktree-scope-810 saw a third prompt_submit because the setup's prompt landed after the test had counted.

  • An ignored override is a doctor note, not a failed informational check. Taking informational checks out of allPassed would change the exit code for every such check, and src/doctor.ts documents that they count.

Things worth a close look:

  • src/hook-fast-path.ts keeps 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 required hooks.needsBundle in TOOLS.
  • src/utils/git-ref-files.ts reads origin/HEAD and its sha from the git dir's files: loose refs, then packed-refs, with .git files and commondir followed. Anything unexpected falls back to asking git.
  • teamai_launch is now forwarded as null rather than deleted, so the background child can tell "the foreground said no launch" from "the fast path forwarded nothing".

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

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 SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 session pendingHint, CURSOR_VERSION, and a payload with no session id.
  • The members cache is dropped when the team repo's origin/HEAD moves.

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 TodoWrite hook 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.

@SaulMoro

SaulMoro commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

One more blocker at 8b8fe5b. E2E shard 3/3 fails in src/__tests__/e2e/deleted-worktree-scope-810.test.ts, which exercises the hooks this PR moves onto the fast path:

records them under the worktree's project, not where the session moved on
  expected [ 'prompt_submit', …(6) ] to deeply equal [ 'prompt_submit', …(5) ]   (a third prompt_submit)
recovers an omitted cwd after deletion, user scope true, moved on true
  expected '/tmp/teamai-issue810-e2e-…/b' to be '/tmp/teamai-issue810-e2e-…/home/…'

A late prompt is recorded a second time, and an event whose cwd was omitted is attributed to the directory the session moved to rather than to the deleted worktree's scope. That points at the fast path. The background child starts in the hook's working directory instead of the directory the full dispatch resolves for the scope. It may also record a prompt the full dispatch would have deduplicated. I could not reproduce a difference in my own run, but this test hits that case directly. Please look at it with the CHANGELOG item.

@SaulMoro

SaulMoro commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

E2E shard 2/3 is red at 8b8fe5b as well: 13 failures, all of them hook events that are no longer recorded.

  • src/__tests__/e2e/copilot-global-hooks.test.ts: 12 cases in "Copilot built-in hooks in project scope". For example:
    • before the upgrade: legacy copy: expected +0 to be 1 at line 246;
    • no prompt_submit in events.jsonl at line 300, where one is expected.
  • src/__tests__/e2e/slash-skill-paths.test.ts: hook-dispatch ignores paths and records a valid skill.

Copilot is one of the tools the fast path is meant to leave to the full dispatch. That suggests the needsBundle check, or the Copilot detection it replaced, no longer sends these payloads to the full dispatch, so nothing is written. Together with the shard 3/3 failure in deleted-worktree-scope-810, these block the merge along with the CHANGELOG item.

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.
@hz89Dev
hz89Dev force-pushed the feat/issue-1788-hook-wait branch from 8b8fe5b to c305ef4 Compare November 9, 2026 08:46
@hz89Dev

hz89Dev commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

All three are fixed at c305ef4, which is rebased on main.

Doctor exit code (P1). An ignored Hook dispatch post-tool-use TodoWrite override is now a doctor note, not a check, so teamai doctor still passes (fix(doctor): note an ignored TodoWrite timeout override instead of failing). I did not take informational checks out of allPassed. That would change the exit code for every such check, and src/doctor.ts documents that they count. Built CLI, hooks inject and then doctor, without and with the override:

--- without the override
exit 0
✔ All checks passed!
--- with the override
exit 0
ℹ hooks/hooks.yaml sets a timeout under `builtin.overrides` for `Hook dispatch post-tool-use TodoWrite`, an entry teamai no longer installs: the TodoWrite hint runs in `Hook dispatch post-tool-use wildcard`, so this timeout is ignored. Remove it from hooks/hooks.yaml; a timeout for `Hook dispatch post-tool-use wildcard` applies to every tool call.
✔ All checks passed!

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 TodoWrite call prints nothing.

E2E shards 2/3 and 3/3. These were tests reading events.jsonl before the background child wrote to it, not the fast path recording the wrong thing. Slice 6 moves dashboard-report and skill-mention to the background child on every POSIX path, the full dispatch included. Copilot still takes the full dispatch (hook-fast-path.test.ts pins every tool's verdict to hooks.needsBundle), but its prompt is now recorded after hook-dispatch returns.

  • In deleted-worktree-scope-810, the setup's moved-on prompt landed after the test had counted, which is the third prompt_submit.
  • The omitted-cwd case read the event before it, from b.

test(e2e): wait for the detached pass that records a prompt makes the three files wait for the tracked detached children before reading. The assertions are unchanged. Locally: those files pass 47/47, and the full E2E suite passed 2056/0 before the rebase onto main.

#1797. Noted, thanks.

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

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 SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was generated by AI during triage.

Approving at c305ef4.

  • Doctor. An ignored Hook dispatch post-tool-use TodoWrite timeout is now a note, so teamai doctor still passes. Keeping informational checks in allPassed is 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.md pages 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-hooks and slash-skill-paths read events.jsonl before it was written. d9e21c39 makes them wait for the tracked detached children, and the assertions are unchanged. Copilot still takes the full dispatch, and hook-fast-path.test.ts pins every tool's verdict to hooks.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.

@SaulMoro
SaulMoro merged commit 595c37d into Tencent:main Nov 9, 2026
17 checks passed
hz89Dev added a commit to hz89Dev/teamai-cli that referenced this pull request Nov 9, 2026
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.
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.

[feat] Agent hooks: keep the agent waiting less

3 participants