Skip to content

feat(codemode): Define Code Mode telemetry names - #2528

Merged
thomasjiangcy merged 1 commit into
mainfrom
codemode/telemetry
Nov 2, 2026
Merged

thomasjiangcy merged 1 commit into
mainfrom
codemode/telemetry

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Defines the telemetry for Code Mode (#1987) before the bridge emits it. No emission and no runtime wiring in this PR.

Nested tool calls keep the existing tool telemetry. The codemode call gets the normal gen_ai.execute_tool span. Each nested tools.x(...) call gets its own child gen_ai.execute_tool span with the real gen_ai.tool.name, so the existing "Tool failures or slow tool calls" query still finds Notion or Linear calls made from a script. Nested spans:

  • set gen_ai.tool.dispatcher.name: codemode, the same attribute executeTool sets for catalog tools, so no new link attribute is needed
  • use gen_ai.tool.call.id = <codemode call id>/c<n>
  • keep their own app.guardian.* decision and agent.tool_call.failed log

Parenting rule for the bridge. withSpan uses Sentry.startSpan, which parents under the active span. pi-codemode reports calls from a worker callback (onCall), and that callback can lose the async context. The bridge has to run each nested call with the codemode span active (Sentry.withActiveSpan, the way chat/pi/traced-stream.ts already does).

New app.codemode.* attributes on the codemode span. chat/codemode/telemetry.ts adds CodeModeTelemetry and codeModeSpanAttributes(), following the same pattern as SandboxSearchTelemetry and searchTelemetryAttributes. The attributes:

  • script.bytes
  • call.count
  • call.peak_concurrency
  • call.rejected_count (action review returned ask or deny)
  • call.batch_limit_rejected_count
  • vm.start_ms
  • run_ms
  • output.bytes (before truncation)
  • output.truncated
  • error.kind: script, timeout, memory, aborted or sandbox. These are pi-codemode's error kinds plus memory, which marks a script failure caused by the heap limit.

The Guardian batch-size attribute is out of scope here; the Guardian batch track owns it.

Open questions

  • codeModeSpanAttributes has no caller until the bridge lands. Is it OK to merge this before the bridge?
  • Today the adapter sets gen_ai.tool.dispatcher.name only on the executeTool path. The bridge needs a way to pass codemode into nested calls. Also, a nested catalog call that goes through executeTool would overwrite the value.
  • Directory: chat/codemode/ vs. wherever the tool track puts the codemode tool.

Validation: pnpm typecheck, pnpm lint and pnpm file-length:check pass. I couldn't run local junior-qa chat QA here: there are no model credentials and no Postgres. No runtime code path changed.

via @thomasjiangcy.

--

View Session [Sentry]

Define the span attributes for a codemode tool call and the rules for its
nested tool spans, before the bridge emits them. Nested calls keep the normal
gen_ai.execute_tool span with the real tool name, set
gen_ai.tool.dispatcher.name to codemode, and parent under the codemode span.

Refs #1987

Co-Authored-By: Thomas Jiang <[email protected]>
@vercel

vercel Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 2, 2026 9:29am UTC

Request Review

@sentry-junior
sentry-junior Bot marked this pull request as ready for review November 2, 2026 10:57
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 2, 2026
@thomasjiangcy
thomasjiangcy merged commit ffd5e9b into main Nov 2, 2026
45 checks passed
@thomasjiangcy
thomasjiangcy deleted the codemode/telemetry branch November 2, 2026 11:11
thomasjiangcy added a commit that referenced this pull request Nov 2, 2026
Add `runCodeModeScript()` in
`packages/junior/src/chat/codemode/bridge.ts`. It runs a model-written
script in pi-codemode's `start()` mode and sends each `tools.x()` call
through the normal tool pipeline. Nothing calls it yet except tests. A
later change registers the `codemode` tool and removes the placeholder
import in `app.ts`.

```ts
runCodeModeScript({
  toolCallId,    // id of the codemode call; nested ids are `${toolCallId}/c${n}`
  source,        // script body
  tools,         // scriptTools from planToolExposure
  pipeline,      // createToolPipeline(...)
  countToolCall, // runResume.countToolCall
  signal,        // the Run's abort signal
}): Promise<CodeModeResult>
```

**Behavior**
- The calls of one VM drain (between `idle` messages) go to
`reviewToolActions` as one batch. Each proposal carries `script: {
source, calls }` with the script's earlier calls and their status. If a
drain has more than 10 calls, the whole drain fails with the gate's
error before any hook or review runs.
- Each nested call takes the direct path: argument preparation and
schema check, plugin `beforeToolExecute` hooks, the batch review,
credentials and auth handling, then the executor under its own
`gen_ai.execute_tool` span with `gen_ai.tool.dispatcher.name: codemode`.
Nested calls run under the `codemode` span through
`Sentry.withActiveSpan`, and the bridge sets the `app.codemode.*`
attributes from #2528.
- Concurrency is decided in `admit()`. Tools with `executionMode:
"sequential"` run one at a time. All other tools run in parallel,
including `auto`/`review` tools: direct calls serialize those only
because each one is reviewed alone.
- The script deadline is `CODE_MODE_SCRIPT_TIMEOUT_MS` (120s). The Run's
abort signal stops the VM and every pending nested call. Each nested
call counts toward `maxToolCallsPerTurn`. Output and return value share
a 40k-character cap (pi's 10k-token default at 4 characters per token).
- The result has the return value, the capped output, and every nested
call with its status (`ok | error | rejected | interrupted |
not_started`). It still lists them after a timeout or abort. A rejected
call carries Guardian's instructions and the gate's rejection marker.
`guardian/history.ts` reads those markers from `codemode` results, so
prior-rejection tracking survives a resume.
- An authorization pause from a nested call stops the script and is
rethrown, so the Run can pause.

**Adapter change.** The per-call flow in `createPiAgentTools()` moves
into `createToolPipeline()`. It splits preparation (checks, hooks) from
reviewed execution, so a script can review per batch and execute per
call. The tool list, the execution modes, and the direct and
`executeTool` paths are unchanged. The existing adapter suites pass
without edits.

**Tests** (`tests/integration/codemode/bridge.test.ts`, real pipeline
and gate; only Guardian's reviewer is faked): `Promise.all` over 3
reviewed tools runs in parallel in one batch and adds to the turn count;
a sequential tool queues; `handoff` from `scriptTools` is refused; a
`deny` rejects only that call, and its marker is in the result and
restored by history; an 11-call drain fails before any review or hook;
the 120s deadline, using fake timers; abort settles a running call
(`interrupted`) and a queued one (`not_started`).

`tests/integration/annotation-cards.test.ts` times out locally on
unmodified `main` too.

<!-- junior-request-attribution:start -->
via @thomasjiangcy.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A47491ad5f19b808aa555580a70751617 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A47491ad5f19b808aa555580a70751617)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A47491ad5f19b808aa555580a70751617/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Thomas Jiang <[email protected]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 1be82e43 Deployed Nov 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant