Skip to content

Scope telemetry hooks with tool matchers - #22084

Merged
Ella Hathaway (ellahathaway) merged 8 commits into
ellahathaway-clarify-telemetry-hook-messagefrom
ellahathaway-telemetry-hook-matcher-scoping
Nov 13, 2026
Merged

Ella Hathaway (ellahathaway) merged 8 commits into
ellahathaway-clarify-telemetry-hook-messagefrom
ellahathaway-telemetry-hook-matcher-scoping

Conversation

@ellahathaway

@ellahathaway Ella Hathaway (ellahathaway) commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Telemetry hooks now use tool-name matchers, so agent clients start the aspire CLI only for tools the classifier tracks.

  • Copilot: CopilotAgentClient.TelemetryHookMatcher adds a matcher for skill and the Aspire MCP tools. Copilot matchers see only the tool name, so there is no Read matcher; one would start the CLI for every file read in every repository.
  • Claude Code: ClaudeCodeAgentClient registers three PostToolUse groups: Skill (every skill, since Claude hook if rules don't match Skill input yet, [BUG] PreToolUse hook "if" field (permission-rule syntax) never matches Skill tool invocations (v2.1.278) anthropics/claude-code#95819, so the CLI classifier drops non-Aspire skills), the Aspire MCP tools, and Read limited to Aspire skill directories by an if rule.
  • Each matcher lives with its agent client. AgentTelemetryHook.McpMatcher is built from the classifier's MCP tool prefixes, so the matchers cannot drift from the classifier.
  • The spec documents the matchers. Clients that ignore matchers still work because the classifier stays the authoritative filter.

IMPORTANT: Copilot limitation

This doesn't fully solve the invocation problem for Copilot. Copilot hook matchers only see the tool name, and Copilot has no equivalent of Claude Code's if rules for filtering on tool input. So for Copilot:

  • Every skill call starts the aspire CLI, including non-Aspire skills. The classifier drops those events, but the process still launches.
  • File reads can't be scoped to Aspire skill directories, so the Copilot hook doesn't match view at all. Clients that honor the matcher don't report reference_file_read events.
  • Copilot CLI versions earlier than 1.0.63 ignore matcher and still start the CLI for every tool.

Fully scoping Copilot needs input-based filtering in Copilot hooks.

Validation: dotnet test --project tests\Aspire.Cli.Tests\Aspire.Cli.Tests.csproj --no-launch-profile -- --filter-class "*.AgentInit*" --filter-class "*TelemetryHook*" --filter-class "*AgentTelemetry*" --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true" passed (449/449). The Aspire.Cli.EndToEnd.Tests build succeeds, but the E2E tests were not run.

Fixes #20480

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

@ellahathaway Ella Hathaway (ellahathaway) changed the title Scope agent telemetry hooks to tracked tools with matchers Scope telemetry hooks with tool matchers Nov 10, 2026
@github-actions

This comment has been minimized.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The Claude Read gate is tested only as serialized text, so client-level validation is still needed to confirm rule evaluation triggers plugin, user, and project skill reads, including Windows paths.

No open issues 🎉

What changed in this PR

This PR narrows Aspire’s agent telemetry hooks to reduce unnecessary CLI launches, addressing the noisy per-tool invocation reported in #20480. It builds on #22078’s per-client hook registration while keeping the classifier as the final filter.

Changes:

  • Adds tool-name matchers for Copilot and Claude Code, with a path-filtered Claude Read hook for Aspire skills.
  • Extracts shared settings merge and hook-identification logic into TelemetryHookSettings.
  • Updates matcher tests, registration expectations, and the telemetry specification.
File Description
tests/​Aspire.Cli.Tests/​Agents/​TelemetryHookConfiguratorTests.cs Verifies matcher registration, migration, and preservation of existing hooks.
tests/​Aspire.Cli.Tests/​Agents/​AgentTelemetryHookTests.cs Tests tool matching and canonical MCP tool coverage.
tests/​Aspire.Cli.EndToEnd.Tests/​AgentCommandTests.cs Updates expected Claude hook configuration.
src/​Aspire.Cli/​Agents/​Hooks/​TelemetryHookSettings.cs Centralizes settings merging and Aspire hook identification.
src/​Aspire.Cli/​Agents/​Hooks/​AgentTelemetryHook.cs Defines client matchers and the Claude skill-path condition.
src/​Aspire.Cli/​Agents/​Copilot/​CopilotAgentClient.cs Registers the Copilot tool matcher.
src/​Aspire.Cli/​Agents/​ClaudeCode/​ClaudeCodeAgentClient.cs Registers separate tool and filtered-read hook groups.
docs/​specs/​aspire-product-telemetry.md Documents matcher scope and fallback behavior.

Review effort: Balanced

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 22084

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 22084"

@github-actions

This comment has been minimized.

@ellahathaway
Ella Hathaway (ellahathaway) force-pushed the ellahathaway-telemetry-hook-matcher-scoping branch from 84a23d3 to a46114c Compare November 10, 2026 23:50
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Tests inspect configuration and emulate matching but do not verify that supported Copilot/Claude versions suppress unrelated hook launches while preserving tracked skill, MCP, and Claude reference-read events.

No open issues 🎉

Review effort: Balanced

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

This comment has been minimized.

@IEvangelist David Pine (IEvangelist) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No actionable findings.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

Register the Copilot user hook with a tool-name matcher for skill and Aspire
MCP tools, and register Claude PostToolUse hooks for the Skill and Aspire MCP
tools plus Read calls limited to Aspire skill paths through an if rule. Share
settings merge helpers through TelemetryHookSettings.

Co-authored-by: Copilot App <[email protected]>
Add a test that every canonical skill and reference path matches ClaudeReadIf, and split the merged matcher paragraph in the telemetry spec.

Co-authored-by: Copilot App <[email protected]>
Move the Copilot and Claude matcher constants into their agent clients, drop the shared settings helper, and restore the clients to their base shape plus the matcher behavior.

Co-authored-by: Copilot App <[email protected]>
Claude Code does not evaluate hook if rules against Skill input (anthropics/claude-code#95819), so Skill(aspire*) blocked every Skill hook. Match every skill and let the classifier drop non-Aspire skills.

Co-authored-by: Copilot App <[email protected]>
@ellahathaway
Ella Hathaway (ellahathaway) force-pushed the ellahathaway-telemetry-hook-matcher-scoping branch from 7159cf2 to 27a91a9 Compare November 12, 2026 22:31
@github-actions

Copy link
Copy Markdown
Contributor

Tests selector

2 / 101 PR test projects · 4 PR jobs, from 6 changed files.

Selected PR test projects (2 / 101)

Aspire.Cli.EndToEnd.Tests, Aspire.Cli.Tests

Selected PR jobs (4)

cli-starter-validation, extension-e2e, polyglot, typescript-api-compat


How these were chosen — grouped by what changed

📦 affected project Aspire.Cli
→ 1 test: Aspire.Cli.EndToEnd.Tests

🧪 tests/Aspire.Cli.EndToEnd.Tests/AgentCommandTests.cs (changed test)
→ 1 directly: Aspire.Cli.EndToEnd.Tests

🧪 tests/Aspire.Cli.Tests/Agents/AgentTelemetryHookTests.cs (changed test)
→ 1 directly: Aspire.Cli.Tests

🧪 tests/Aspire.Cli.Tests/Agents/TelemetryHookConfiguratorTests.cs (changed test)
→ 1 directly: Aspire.Cli.Tests

Job reasons

Job Triggered by
cli-starter-validation affected project Aspire.Cli
extension-e2e • src/Aspire.Cli/Agents/ClaudeCode/ClaudeCodeAgentClient.cs, src/Aspire.Cli/Agents/Copilot/CopilotAgentClient.cs, src/Aspire.Cli/Agents/Hooks/AgentTelemetryHook.cs
• affected project Aspire.Cli
polyglot affected project Aspire.Cli
typescript-api-compat affected project Aspire.Cli

Selection computed for commit 27a91a9.

@ellahathaway
Ella Hathaway (ellahathaway) enabled stacked auto-merge (squash) November 12, 2026 23:15
@ellahathaway
Ella Hathaway (ellahathaway) merged commit dbcb523 into main Nov 13, 2026
221 checks passed
Ella Hathaway (ellahathaway) added a commit that referenced this pull request Nov 13, 2026
## Description

Stack 3/3, on #22084. `aspire agent init --scope project` now installs
telemetry hooks too. The hooks are written at user scope
(`~/.copilot/hooks` and `~/.claude/settings.json`) no matter which scope
was requested. Skills and MCP configuration still follow the requested
scope, and #22078's per-client gating still decides which clients get
hooks.

Because the hooks live in user-level configuration, they apply in every
repository where that client runs, not only the one where `aspire agent
init` was run. The PostToolUse matcher from #22084 makes the client
launch `aspire` only for matching tool calls, and only Aspire skill, MCP
tool, and reference-file usage is reported. Users can opt out with
`ASPIRE_CLI_TELEMETRY_OPTOUT=true`. The install message now says the
hooks were written to user-level configuration.

### Why we can't install project-level hooks today

Every hook launches a separate `aspire` process, so it must fire exactly
once per event. Today a project-level hook would make it fire more than
once (see #20480):

- **Copilot runs hooks from every source and doesn't dedupe them.** It
loads the repo's `.github/hooks/*.json`, `~/.copilot/hooks`,
`.github/copilot/settings(.local).json`, `~/.copilot/settings.json`, and
plugins, then runs all of them. A project hook in `.github/hooks` plus
the user hook that global init installs would launch `aspire` twice for
each event. Claude Code dedupes identical handlers across its settings
files, so this is a Copilot problem.
- **Copilot also runs Claude's project hooks.** Copilot reads the repo's
`.claude/settings(.local).json` and runs those hooks too, ignoring
Claude-only fields such as `if`. A Claude project hook would fire in
Copilot sessions on top of Copilot's own hook, even if the user set up
only Claude for the project.
- **Install time can't avoid the duplicates.** Project hook files are
committed and shared, but user hooks belong to each machine. We can't
delete a user hook to make room for a project hook, because it also
serves the user's other repos. Skipping the project hook when a user
hook exists only helps the developer who ran init, not teammates who
clone the repo with their own user hooks.
- **Runtime dedupe is heavy.** Each invocation is an independent process
with no shared event ID. Deduping would mean hook processes coordinating
through shared state on every tool call, which is the hot path.

Installing hooks only at user scope gives exactly one hook per client on
each machine. We can revisit project hooks if Copilot starts deduping
identical hook commands or applying precedence across hook sources.

Validation: `dotnet test --project
tests\Aspire.Cli.Tests\Aspire.Cli.Tests.csproj --no-launch-profile --
--filter-class "*.AgentInit*" --filter-class "*.TelemetryHook*"
--filter-class "*.AgentTelemetry*" --filter-not-trait "quarantined=true"
--filter-not-trait "outerloop=true"` passed (464/464). The E2E tests
were not run.

## Checklist

- Is this feature complete?
  - [x] Yes. Ready to ship.
  - [ ] No. Follow-up changes expected.
- Are you including unit tests for the changes and scenario tests if
relevant?
  - [x] Yes
  - [ ] No
- Did you add public API?
  - [ ] Yes
    - If yes, did you have an API Review for it?
      - [ ] Yes
      - [ ] No
- Did you add `<remarks />` and `<code />` elements on your triple slash
comments?
      - [ ] Yes
      - [ ] No
  - [x] No
- Does the change make any security assumptions or guarantees?
  - [ ] Yes
    - If yes, have you done a threat model and had a security review?
      - [ ] Yes
      - [ ] No
  - [x] No

---------

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: b39716fb-e8ff-45f3-a048-a710d4f3052e
Jose Perez Rodriguez (joperezr) pushed a commit that referenced this pull request Nov 13, 2026
## Description

Backports of the PRs from
https://github.com/microsoft/aspire/stack/22118:
- #22078
- #22084
- #22093

## Checklist

- Is this feature complete?
  - [ ] Yes. Ready to ship.
  - [ ] No. Follow-up changes expected.
- Are you including unit tests for the changes and scenario tests if
relevant?
  - [ ] Yes
  - [ ] No
- Did you add public API?
  - [ ] Yes
    - If yes, did you have an API Review for it?
      - [ ] Yes
      - [ ] No
- Did you add `<remarks />` and `<code />` elements on your triple slash
comments?
      - [ ] Yes
      - [ ] No
  - [ ] No
- Does the change make any security assumptions or guarantees?
  - [ ] Yes
    - If yes, have you done a threat model and had a security review?
      - [ ] Yes
      - [ ] No
  - [ ] No

---------

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: b39716fb-e8ff-45f3-a048-a710d4f3052e
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.

Telemetry hook makes incorrect assumptions

3 participants