Skip to content

feat: record unrecognised AI_AGENT values as unknown agents - #747

Open
MattieTK wants to merge 1 commit into
mainfrom
feat/ai-agent-fallback
Open

MattieTK wants to merge 1 commit into
mainfrom
feat/ai-agent-fallback

Conversation

@MattieTK

Copy link
Copy Markdown
Member

AI_AGENT is becoming the shared way for coding agents to identify themselves to the tools they run. Claude Code, Copilot in VS Code, Pi and Crush set it for child processes, and gh, the Stripe CLI and the Netlify CLI read it before any vendor-specific variable (agents.md#136 tracks the proposal). At this point cf ignores any AI_AGENT value it has no detector for, so those agents look like human use.

Changes

  • Unknown agents. An AI_AGENT value that no detector explains now marks the run as agentic, with harness ID unknown. That covers agent help, whoami defaults, the X-CF-CLI-Agent header and telemetry's agent field. A detector explains a value when it read AI_AGENT itself, or when the value equals its harness ID or starts with that ID plus _ (for example, OpenCode's opencode). False-like values (0, false, no, off) are still ignored.
  • Announced name. Telemetry records the unrecognised value as agentAnnouncedName, but only when it matches the format gh accepts ([A-Za-z0-9_-]), limited to 64 characters. Other values still count as an unknown agent, but their name is not sent. The header keeps the fixed unknown value, so it stays a closed set.
  • Claude Code version. Claude Code sets AI_AGENT=claude-code_<major>-<minor>-<patch>_agent (since 2.1.120). cf now recognises Claude Code from that value and records the version it contains as agentVersion.
  • Disclosure. packages/cli/telemetry.md describes both new properties.

When a recognised agent and an unrecognised one are both present (for example, an outer agent's AI_AGENT inherited by Codex), the recognised agent stays primary. The unknown match is kept in matches, and its name is still recorded.

Notes for reviewers

  • New telemetry properties fall into Town Lake's PII-masked properties_residual column. agentVersion and agentAnnouncedName can be read in the raw sparrow_events table, but not in fct.events until the data model gives them their own columns.
  • With AI_AGENT now read, a test that sets CLAUDECODE="" to simulate a human user still detects Claude Code when run inside a Claude Code session. Only the dispatcher test relies on that, and it now sets both variables.

Testing

  • pnpm test in packages/cli, with agent variables removed from the environment as in CI, passes.
  • pnpm check passes.

AI_AGENT is the cross-agent marker that Claude Code, Copilot in VS Code,
Pi, Crush and others set for child processes, and gh, Stripe and Netlify
read first. cf ignored any value it had no detector for, so agents
without a dedicated detector looked like humans.

Treat any AI_AGENT value that no detector explains as harness `unknown`,
and record the announced name in telemetry when it matches the format gh
accepts, bounded to 64 characters. Also recognise Claude Code from its
AI_AGENT value and record the version it embeds as `agentVersion`.
@MattieTK
MattieTK requested a review from a team as a code owner November 23, 2026 15:36
@MattieTK
MattieTK requested a review from NuroDev November 23, 2026 15:36
@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/cf@747

commit: 6c706a6

};

// Claude Code announces itself as `claude-code_<major>-<minor>-<patch>_agent`.
const CLAUDE_CODE_ANNOUNCEMENT = /^claude-code(?:_(\d+(?:-\d+)*))?(?:_|$)/;

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.

CLAUDE_CODE_ANNOUNCEMENT accepts values outside the documented Claude Code announcement format, such as claude-code_1-2-3-4_agent and claude-code_1-2-3_agentic. With no other signal those are classified as Claude Code and their arbitrary numeric suffix is sent as agentVersion, instead of remaining an unknown AI_AGENT value. Restrict the matcher to the three-component, _agent-terminated form while retaining the existing versionless claude-code_agent case.

Suggested change
const CLAUDE_CODE_ANNOUNCEMENT = /^claude-code(?:_(\d+(?:-\d+)*))?(?:_|$)/;
const CLAUDE_CODE_ANNOUNCEMENT =
/^claude-code(?:_(\d+-\d+-\d+)_agent|_agent)?$/;

@ask-bonk

ask-bonk Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Review: 1 findings.

Posted 1 inline suggestion.

github run

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💬 Approved with comments — No correctness, security or performance problems. The only note is an INFO about the changeset wording, which overstates which AI_AGENT values count as an agent.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📝 Documentation: 💬 1 info

The changeset title overstates which AI_AGENT values count as an agent.

Recommendation: 💬 Optional Nits

  • [INFO] Changeset title says any AI_AGENT value marks an agent, but false-like values are ignored and names are recorded only when they match the allowed format. (.changeset/detect-unknown-agents.md:5)
View full review

The changeset says "any" AI_AGENT value is treated as an agent, and that the name it gives is recorded. Empty and false-like values are ignored, and the name is recorded only when it matches the allowed format.


✅ All other checks passed

🚀 Release: ✅ LGTM

Overall risk: Low. The pull request adds unknown-agent detection and Claude Code version telemetry. It includes a minor changeset and updates packages/cli/telemetry.md.


🔒 Security: ✅ LGTM

No security issues. The announced name is checked against a strict, length-limited pattern before it reaches telemetry, and the header keeps a closed set of values.


📖 AGENTS.md: ✅ LGTM

The pull request follows the repository conventions. It includes a minor changeset for the user-facing change and updates the telemetry disclosure.


📋 Code Quality: ✅ LGTM

I found no correctness or convention problems.

  • Fallback: detectUnknownAgent runs after every detector, so it only claims AI_AGENT values that no detector explains. A recognised agent stays primary.
  • Announced name: a name is recorded only if it matches the 64-character [A-Za-z0-9_-] format. The X-CF-CLI-Agent header always sends the fixed value unknown.
  • Tests: they cover version parsing, unknown agents, withheld names, values a detector already explains, and a recognised agent staying primary over an unknown one.

⚡ Performance: ✅ LGTM

No performance issues.


Reviewed commit: 6c706a65

"cf": minor
---

Treat any `AI_AGENT` value as an AI coding agent.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ℹ️ INFO · Documentation

Changeset title says any AI_AGENT value marks an agent, but false-like values are ignored and names are recorded only when they match the allowed format.

This changelog entry says any AI_AGENT value is treated as an AI coding agent. The code ignores empty and false-like values (0, false, no, off), and it records the announced name only when the name is at most 64 characters of [A-Za-z0-9_-]. Consider qualifying the title and body, for example "Treat unrecognised AI_AGENT values as an AI coding agent", and saying that the name is recorded only when it is in the accepted format.

@github-actions

Copy link
Copy Markdown
Contributor

cf --help startup

mean stddev min max
PR 281.0 ms 3.6 ms 275.4 ms 286.3 ms
base 279.6 ms 3.7 ms 275.6 ms 286.7 ms

Δ (PR vs base): ↑ +1.3 ms (+0.5%)

Measured with hyperfine on ubuntu-latest (3 warmup runs, 20 timed runs). CI numbers carry ±a few ms of runner jitter; treat small deltas as noise.

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.

1 participant