Repository navigation
feat(codemode): Define Code Mode telemetry names - #2528
Merged
Merged
Conversation
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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
thomasjiangcy
approved these changes
Nov 2, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
codemodecall gets the normalgen_ai.execute_toolspan. Each nestedtools.x(...)call gets its own childgen_ai.execute_toolspan with the realgen_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:gen_ai.tool.dispatcher.name: codemode, the same attributeexecuteToolsets for catalog tools, so no new link attribute is neededgen_ai.tool.call.id=<codemode call id>/c<n>app.guardian.*decision andagent.tool_call.failedlogParenting rule for the bridge.
withSpanusesSentry.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 thecodemodespan active (Sentry.withActiveSpan, the waychat/pi/traced-stream.tsalready does).New
app.codemode.*attributes on thecodemodespan.chat/codemode/telemetry.tsaddsCodeModeTelemetryandcodeModeSpanAttributes(), following the same pattern asSandboxSearchTelemetryandsearchTelemetryAttributes. The attributes:script.bytescall.countcall.peak_concurrencycall.rejected_count(action review returned ask or deny)call.batch_limit_rejected_countvm.start_msrun_msoutput.bytes(before truncation)output.truncatederror.kind:script,timeout,memory,abortedorsandbox. These are pi-codemode's error kinds plusmemory, which marks ascriptfailure caused by the heap limit.The Guardian batch-size attribute is out of scope here; the Guardian batch track owns it.
Open questions
codeModeSpanAttributeshas no caller until the bridge lands. Is it OK to merge this before the bridge?gen_ai.tool.dispatcher.nameonly on theexecuteToolpath. The bridge needs a way to passcodemodeinto nested calls. Also, a nested catalog call that goes throughexecuteToolwould overwrite the value.chat/codemode/vs. wherever the tool track puts thecodemodetool.Validation:
pnpm typecheck,pnpm lintandpnpm file-length:checkpass. I couldn't run localjunior-qachat QA here: there are no model credentials and no Postgres. No runtime code path changed.via @thomasjiangcy.
--
View Session [Sentry]