Skip to content

fix(hooks): record what a CodeBuddy session does, and what it loads - #1547

Merged
SaulMoro merged 1 commit into
Tencent:mainfrom
Walternater:fix/1516-codebuddy-events
Oct 29, 2026
Merged

SaulMoro merged 1 commit into
Tencent:mainfrom
Walternater:fix/1516-codebuddy-events

Conversation

@Walternater

@Walternater Walternater commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Under the VSCode CodeBuddy plugin, every post-tool-use dispatch ran only local-agent-sync: the handlers that need a teamai scope (dashboard-report, skill-use) were dropped, so tool-use events and skill usage were never recorded for that host.

Three independent causes, all in teamai-cli:

  1. (root) The plugin sends the extension host's own directory as the payload cwd — it holds no teamai scope, so the scope resolution failed and the config-dependent handlers were dropped.
  2. The plugin's skill tool is use_skill, which was not among the tool's registered skill calls, so the call was never a skill use.
  3. A manual slash command lands as @command://name, which the /name prompt form cannot match.

What changed

  • The scope comes from the session when the payload names no scope (src/dashboard-collector.ts). hookScopeDir already falls back to the recorded scope for a cwd that is gone; a cwd that holds no teamai scope now falls back the same way — the workspace the session recorded. resolveHookScopeFor resolves the config and the directory together, so the dispatch works in the workspace (the payload's own cwd is replaced with it) instead of the extension host's directory. The recorded workspace is read once per process.
  • That fallback is for the host that sends its own directory, and that host only (HOSTS_SENDING_OWN_CWD). Every other host's payload names the workspace it works in, so its scope stays what that directory holds: a repository without teamai runs no team handlers ([bug] TeamAI hooks reach projects that never set it up: Stop nudge, and skill usage pushed to another team's stats #748), and another repository's work is never reported to the team the session started in. This is the change asked for in the review of the first version of this PR, where the fallback ran for every tool.
  • A member who installed at user scope and then joined a project gets the project's scope — the extension host's directory resolves to the user scope, so the fallback now also replaces a user-scope answer with the project the session started in.
  • use_skill is a CodeBuddy skill call (src/tools.ts).
  • @command://name is a prompt form (src/tools.ts, src/skill-use.ts). A tool's promptForm takes one form or several, so CodeBuddy keeps counting /name as well. The command URI is anchored at the start of the prompt, so a command URI quoted in the conversation is not a skill use.

Test Plan

Real-CLI verification

The built CLI (npm run build), three small team repos made with teamai init . --self, and a skill under ~/.codebuddy/skills and ~/.claude/skills. Every scenario sends the payload cwd the host sends, through teamai hook-dispatch <event> --tool <tool> --stdin.

  • S1 — the CodeBuddy plugin: the session starts with the workspace, then the post-tool-use and the UserPromptSubmit carry the extension host's own directory ("/").
  • S2 — a Claude session that starts in the team repo and then works in another repository (no teamai).
  • S3 — a member who installed at user scope and then joined a project; the payload is the extension host's directory.

main (5807b7d):

--- S1: the events the sessions recorded
    type=session_start tool=codebuddy cwd=/tmp/cb-fixture-main/s1/project toolName= prompt=
    type=session_start tool=codebuddy cwd=/tmp/cb-fixture-main/s1/project toolName= prompt=
--- S1: the skill usage the sessions recorded
    (no usage file)
--- S2: the events the sessions recorded
    type=session_start tool=claude cwd=/tmp/cb-fixture-main/s2/team-a toolName= prompt=
--- S2: the skill usage the sessions recorded
    (no usage file)
--- S3: the events the sessions recorded
    type=session_start tool=codebuddy cwd=/tmp/cb-fixture-main/s3/project toolName= prompt=
    type=tool_use tool=codebuddy cwd=/ toolName=Skill prompt=
    type=tool_use tool=codebuddy cwd=/ toolName=use_skill prompt=
--- S3: the skill usage the sessions recorded
    (/tmp/cb-fixture-main/s3/home/.teamai/user-usage.jsonl)
    skill=hello-skill tool=codebuddy

S1 records no tool use and no skill usage at all — the whole of the session's work is missing. S3 records the events with cwd: / and the usage in the user scope, not the project the session works in.

this branch:

--- S1: the events the sessions recorded
    type=session_start tool=codebuddy cwd=/tmp/cb-fixture-branch/s1/project toolName= prompt=
    type=tool_use tool=codebuddy cwd=/tmp/cb-fixture-branch/s1/project toolName=use_skill prompt=
    type=session_start tool=codebuddy cwd=/tmp/cb-fixture-branch/s1/project toolName= prompt=
    type=prompt_submit tool=codebuddy cwd=/tmp/cb-fixture-branch/s1/project toolName= prompt=@command://hello-skill review
--- S1: the skill usage the sessions recorded
    (/tmp/cb-fixture-branch/s1/home/.teamai/projects/private-tmp-cb-fixture-branch-s1-project-40bd155db9ca8d0b/usage.jsonl)
    skill=hello-skill tool=codebuddy
    skill=hello-skill tool=codebuddy
--- S2: the events the sessions recorded
    type=session_start tool=claude cwd=/tmp/cb-fixture-branch/s2/team-a toolName= prompt=
--- S2: the skill usage the sessions recorded
    (no usage file)
--- S3: the events the sessions recorded
    type=session_start tool=codebuddy cwd=/tmp/cb-fixture-branch/s3/project toolName= prompt=
    type=tool_use tool=codebuddy cwd=/tmp/cb-fixture-branch/s3/project toolName=Skill prompt=
    type=tool_use tool=codebuddy cwd=/tmp/cb-fixture-branch/s3/project toolName=use_skill prompt=
--- S3: the skill usage the sessions recorded
    (/tmp/cb-fixture-branch/s3/home/.teamai/projects/private-tmp-cb-fixture-branch-s3-project-3e811279ec6ab810/usage.jsonl)
    skill=hello-skill tool=codebuddy
    skill=hello-skill tool=codebuddy

S1 records both channels — the use_skill call and the @command:// prompt — with the workspace as the cwd. S2 is unchanged from main: the other repository's work is not reported to the team. S3 records the events with the workspace as the cwd and the usage in the project's scope.

The CodeBuddy plugin itself was not available here, so the plugin's own payload is reproduced from the report in #1516 (its cwd is the extension host's, its skill tool is use_skill, its manual slash command lands as @command://name); every other host's path is unchanged.

  • npx tsc --noEmit passes
  • npm run lint passes
  • npx vitest run over the hook, skill-use, usage, tool-support, contribute, scoring, doctor and codex-hook suites — 22 files, 827 tests, all pass
  • Added tests for the change

Notes for Reviewers

  • The fallback runs for CodeBuddy only. HOSTS_SENDING_OWN_CWD names the host whose payload is the extension host's directory; every other host resolves its scope from its own cwd, exactly as before this PR. S2 in the record above is the cross-repository case from the review: a Claude session that starts in a team repo and then works in another repository records nothing for that repository, as on main.
  • The user scope is recovered. resolveHookScopeFor replaces a user-scope answer with the project the session recorded, so a member who installed at user scope and then joined a project reports to the project (S3). A payload that holds a project scope of its own keeps it.
  • src/__tests__/hook-scope-1516.test.ts covers the scope resolution: the recorded scope when the payload names none, the payload's own scope when it holds one, the project scope when the payload holds only the user scope, and no scope at all for a session that recorded nothing — plus the cross-repository case, which must stay unanswered for every host but CodeBuddy. The last two fail on the previous head of this branch.
  • src/__tests__/skill-use.test.ts gains the use_skill call, the @command://name prompt, the command URI quoted in the conversation (counted by nobody) and the plain /name prompt for CodeBuddy. The first two fail on the previous head of this branch.
  • hook-dispatch-cli.test.ts mocks the scope resolution, which now answers with the config and the directory together.
  • docs/guide/reporting.md and its Chinese page list the prompt forms a skill use is counted by; the CodeBuddy one is added.
  • The use_skill call is marked evidence: 'source', not verified: the plugin's own bundle registers that name (see [bug] CodeBuddy host: tool_use events & skill usage never recorded — 3 independent causes in hook dispatch (v0.28.1) #1516), and a capture of a real CodeBuddy session is what would make it verified — the tool-support test only asks for a capture for the verified ones.
  • The new function is named resolveHookScopeFor, as resolveHookScope already names another one (src/types.ts).

Closes #1516.

@github-actions

Copy link
Copy Markdown

No confident findings in the specified diff.

The PR description includes sufficient testing: a built, real-CLI run reproduces the CodeBuddy payloads and records both events and skill usage. The unavailable plugin is explicitly disclosed; additional host coverage is not blocking.

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.

@Walternater Thanks for taking #1516. #1545 (opened earlier) fixes the same issue. We review both in full, the first one ready merges, and the other closes. This PR already avoids the two blockers we raised there: CodeBuddy /name prompts still count (one usage line on this branch and on main), and it merges cleanly with main at 793e0351, where tsc passes. use_skill is labelled source, and the tool-support pages are in sync. For a member with only a project-scope config, the CodeBuddy usage record and tool_use event now land in the project, where main records nothing. tsc, lint and the changed test files pass, and the three new tests fail on main.

What blocks approval:

  • [P1 blocking] The fallback to the session's workspace runs for every tool, so another repo's activity is recorded under a team. The new fallback in the hook-scope resolution doesn't check the tool. With the built CLI, a Claude session starts in team project A, then sends its hooks from repo B, which has no teamai:

    this branch: events
      "type":"session_start", "tool":"claude", "cwd":"<R>/team-a"
      "type":"tool_use",      "tool":"claude", "cwd":"<R>/team-a"
      "type":"prompt_submit", "tool":"claude", "cwd":"<R>/team-a"
      team-a/.teamai/usage.jsonl: 1 line   (B's /hello-skill)
    main: only the session_start
    

    B's activity is reported to team A, with its cwd rewritten to A. That breaks #748 (a directory without teamai runs no team handlers) and the issue's own "Claude channels unchanged" item. The body says every other host's path is unchanged. From the code, Codex team hooks (codex-team-hooks.ts:272) and scoring (scoring.ts:122) resolve through the same function, so they're affected too, though we didn't run them. Limiting the fallback to codebuddy, the host that sends an extension-host cwd, keeps every other tool as it is.

  • [P2 non-blocking on its own; we hold approval for an important item in scope] No recovery for a member with both a user-scope and a project-scope config. We raised this on #1545, and it reproduces here. The extension-host cwd / resolves to the user config, so the fallback never runs: the use_skill call is lost, and the tool_use event is recorded with cwd: "/". Members who installed at user scope and then joined a project are a common case. Recovery should also apply when the cwd resolves only to the user scope and the session started in a project.

Non-blocking:

  • [P2 non-blocking] resolveHookScope now names two unrelated exported functions: the existing one in src/types.ts:3425, and the new one in src/dashboard-collector.ts. A distinct name for the new one would keep a search from landing on the wrong one.
  • [P3 nit] Two neighbouring CodeBuddy comments in src/tools.ts disagree: one says the IDE sends skill, the next that its skill tool is use_skill.

The CI review found nothing. It ran no code, so it couldn't see the cross-tool case. E2E shard 2/3 and the Lint & Test jobs were still running when we checked.

@SaulMoro
SaulMoro self-requested a review October 29, 2026 06:41
Three causes, all in teamai-cli (Tencent#1516):

- The CodeBuddy plugin sends the extension host's own directory as the
  post-tool-use cwd, which holds no teamai scope, so every handler that needs
  one was dropped and the session's tool use was never recorded. The scope is
  now resolved from the workspace the session recorded, as it already is for a
  cwd that is gone — for that host alone, so every other host's payload keeps
  the scope its own directory holds (Tencent#748). A member who installed at user
  scope and then joined a project gets the project's scope back, not the user
  scope the extension host's directory resolves to.
- The plugin's skill tool is `use_skill`, which was not in the tool's
  registered skill calls, so the call was never a skill use.
- A manual slash command lands as `@command://name`, which the `/name` form
  cannot match, so the prompt named no skill.

The `@command://name` form is anchored at the start of the prompt, so a command
URI quoted in the conversation is not a skill use, and `/name` still counts for
CodeBuddy.
@Walternater
Walternater force-pushed the fix/1516-codebuddy-events branch from 1698832 to 5fcb64c Compare October 29, 2026 07:27
@Walternater

Copy link
Copy Markdown
Collaborator Author

Thanks for the review — all three points are addressed in 5fcb64c5 (the branch was force-pushed; the PR body has the full record).

  • [P1 blocking] the fallback ran for every tool — the scope fallback now belongs to the host that sends the extension host's directory (HOSTS_SENDING_OWN_CWD in src/dashboard-collector.ts), so every other host resolves its scope from its own cwd exactly as before. The cross-repository case from the review is now a test (does not report another repository's work to the team the session started in) and a scenario in the real-CLI record (S2): a Claude session that starts in a team repo and then works in another repository records nothing for that repository, as on main. Codex team hooks and scoring go through the same function and are untouched for the same reason.
  • [P2] no recovery for a member with a user-scope and a project-scope config — resolveHookScopeFor now also replaces a user-scope answer with the project the session recorded, so the extension host's directory (which resolves to the user scope) no longer swallows the session's scope. Scenario S3 in the record: on main the tool_use events carry cwd: / and the usage lands in ~/.teamai/user-usage.jsonl; on this branch the events carry the workspace and the usage lands in the project's own scope. A payload that holds a project scope of its own keeps it.
  • [P2] the name — the new function is resolveHookScopeFor; resolveHookScope keeps naming the one in src/types.ts.
  • [P3] the two comments — they now say which host sends what: the IDE sends the tool name skill, the VSCode plugin's own skill tool is use_skill.

One more change worth naming: the recorded-workspace cache is keyed by the session and the home the events live in, so two sessions in different homes cannot read each other's answer.

@github-actions

Copy link
Copy Markdown

No confident findings in the specified diff.

The PR description documents sufficient testing: a built, real-CLI reproduction records tool-use and prompt-submit events, plus skill usage through both CodeBuddy channels. The unavailable plugin is explicitly disclosed; additional host coverage is not blocking.

The earlier review reported no findings, so none require resolution.

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.

Thanks, approving 5fcb64c5. All four points from the last review are fixed. I checked each with the built CLI:

  • Fallback now limited to CodeBuddy. A Claude session that starts in team repo A and then works in repo B records only the session start in A, and nothing for B, the same as main.
  • User-scope plus project-scope install. When the plugin sends /, the events now carry the project directory, and the project's usage.jsonl gets the use_skill call and the @command:// prompt. On main the event says cwd: / and nothing is recorded.
  • Sessions that belong to the user scope stay there. A session opened in HOME, or in a git directory with no project config, still records to ~/.teamai/user-usage.jsonl. With no config at all, nothing is recorded, the same as main.
  • Other tools behave as on main. For tools other than CodeBuddy, resolveHookScopeFor returns what main's code did, and no cwd is rewritten. That covers Codex team hooks and scoring.
  • The name and the two comments are fixed.

The PR's three test files pass (110 tests), on this head and merged into current main, where tsc is clean too.

[P3 nit], optional: the CodeBuddy CLI shares the codebuddy id and sends its real working directory. If a CLI session starts in project P and later reports a directory that only the user scope covers, that activity now goes to P, with its cwd rewritten. On main it went to the user scope. This needs the CLI to leave the project directory, so it's rare, and no data is lost. One way to tighten it: skip the recovery when the reported directory is inside a git work tree, which the extension host's / never is.

@SaulMoro
SaulMoro merged commit 6a08c59 into Tencent:main Oct 29, 2026
18 checks passed
Walternater added a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…encent#1547)

CodeBuddy's plugin sends the extension host's directory instead of the workspace, so its tool-use and prompt events fell back to the user scope or were dropped. For CodeBuddy only, teamai now resolves the hook scope from the workspace the session recorded at its start, including when a user-scope install also exists, and records the IDE's skill tool and the plugin's use_skill. Other tools resolve their scope from their own cwd as before. Fixes Tencent#1516.
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.

[bug] CodeBuddy host: tool_use events & skill usage never recorded — 3 independent causes in hook dispatch (v0.28.1)

3 participants