Skip to content

feat(server-utils): Add instrumentation for pi-durable - #24993

Open
JPeer264 wants to merge 8 commits into
developfrom
jp/pi-durable-instrumentation
Open

JPeer264 wants to merge 8 commits into
developfrom
jp/pi-durable-instrumentation

Conversation

@JPeer264

@JPeer264 JPeer264 commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Adds piDurableIntegration for @earendil-works/pi-durable, Earendil's new durable agent harness. It is on by default in Node, Bun and Deno, and in a Worker built with @sentry/cloudflare/vite, which covers pi-durable in a Durable Object through the Agents SDK PiHarness. Each run becomes its own invoke_agent trace with chat and execute_tool children, and the first run of a subagent conversation nests under the tool call that created it.

pi-durable has no telemetry hooks (pi-telemetry exists, but no pi package emits spans through it yet), so the integration hooks Harness.open() and wraps the models and registry it receives. The wrapped options keep the caller's options as prototype, because pi-durable reads settings, env and conversationCreated at every use.

Decisions worth a look:

  • Runs are tracked per conversation. pi-durable hands run control to a new pi.generation task after every tool round, so no single task spans a run. The run span ends when the commit that removes pi.live.run goes through, or when the Harness closes with the run in flight.
  • Each run starts a new trace in clean scopes. The scheduler runs task phases in whatever async context last woke it, often an unrelated request. Forking that context attached runs to the wrong request and the wrong conversation. Only the client of that context is kept, because @sentry/cloudflare binds the client to the scope of each request, not to the default scope.
  • gen_ai.conversation.id is <harness id>:<conversation id>. pi-durable numbers conversations per storage from 1, so the bare id would merge every root conversation into one. The cost is a new id after a restart (on Cloudflare, after every restart of the Durable Object), until pi-durable has a stable storage id.
  • Tool spans record the result the model receives, read from the committed result entry. That covers output streamed through api.output() (all of bash) and results an afterTool hook redacted.
  • Errors: failures pi-durable only passes to onReport (throwing hooks or sections) and task phases that throw are captured. Throws of the built-in coding tools are not, because bash throws for every non-zero exit, a normal result for a coding agent. The built-in tools are recognized by their factories (createBashTool() and friends), so an app that registers them in an extension of its own gets the same treatment. The span keeps its error status.
  • The provider SDK integrations are skipped process-wide from the first run on, as the Flue and LangChain integrations do: pi-ai sends its requests through the openai, @anthropic-ai/sdk and @google/genai clients, or through the Workers AI binding with createAI() of agents/models/pi-ai, and those integrations cannot tell pi-ai's calls from the app's own. Bedrock requests go through the AWS SDK, which awsIntegration still reports twice; that skip is a follow-up.

Messages, token usage and finish reasons go through the pi-ai mapper from the Flue fix, and the system prompt and tools are replayed from pi-durable's positional system messages.

Not covered yet: spans for tool calls pi-durable answers without running execute(), spans for custom tasks, agent names (pi-durable has none), a link between a run and the request that submitted it, a marker for compaction requests, and an error event for a failed model request (the chat span carries the status).

🤖 Generated with Claude Code

@JPeer264
JPeer264 added this pull request to stack #25002 October 2, 2026 13:14
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 29.6 kB - -
@sentry/browser - with treeshaking flags 27.75 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.65 kB - -
@sentry/browser (incl. Tracing) 51.52 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 51.52 kB - -
@sentry/browser (incl. Tracing, Profiling) 54.5 kB - -
@sentry/browser (incl. Tracing, Replay) 91.14 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.12 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 95.83 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 108.83 kB - -
@sentry/browser (incl. Feedback) 47.12 kB - -
@sentry/browser (incl. sendFeedback) 34.65 kB - -
@sentry/browser (incl. FeedbackAsync) 39.76 kB - -
@sentry/browser (incl. Metrics) 30.61 kB - -
@sentry/browser (incl. Logs) 30.89 kB - -
@sentry/browser (incl. Metrics & Logs) 31.55 kB - -
@sentry/react 31.43 kB - -
@sentry/react (incl. Tracing) 53.84 kB - -
@sentry/vue 37.55 kB - -
@sentry/vue (incl. Tracing) 54.4 kB - -
@sentry/svelte 29.63 kB - -
@sentry/remix (Remix 3 client bundle) 55.85 kB - -
CDN Bundle 31.33 kB - -
CDN Bundle (incl. Tracing) 52.07 kB - -
CDN Bundle (incl. Logs, Metrics) 33.56 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 54.03 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 74.3 kB - -
CDN Bundle (incl. Tracing, Replay) 89.67 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 91.62 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 95.83 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 97.8 kB - -
CDN Bundle - uncompressed 92.46 kB - -
CDN Bundle (incl. Tracing) - uncompressed 154.77 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.04 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 160.72 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 228.66 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 274.58 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 280.52 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 288.28 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 294.21 kB - -
@sentry/nextjs (client) 56.19 kB - -
@sentry/sveltekit (client) 51.9 kB - -
@sentry/core/server 40.52 kB - -
@sentry/core/browser 13.51 kB - -
@sentry/node 148.78 kB +2.6% +3.76 kB 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.37 kB +0.18% +149 B 🔺
@sentry/node - without tracing 93.61 kB +0.18% +159 B 🔺
@sentry/node - without channel injection 126.96 kB +3.09% +3.8 kB 🔺
@sentry/aws-serverless 101.84 kB +0.15% +148 B 🔺
@sentry/cloudflare (withSentry) - minified 209.04 kB - -
@sentry/cloudflare (withSentry) 517.75 kB - -

View base workflow run

@JPeer264
JPeer264 removed this pull request from stack #25002 October 2, 2026 13:31
@JPeer264
JPeer264 changed the base branch from jp/flue-map-messages-conventions to jp/bun-conversation-id-integration October 2, 2026 13:39
@JPeer264
JPeer264 force-pushed the jp/pi-durable-instrumentation branch from 1dad424 to 91a0b35 Compare October 2, 2026 13:39
@JPeer264
JPeer264 added this pull request to stack #25004 October 2, 2026 13:57
@JPeer264
JPeer264 force-pushed the jp/pi-durable-instrumentation branch from 91a0b35 to 26d39c9 Compare October 2, 2026 16:59
@JPeer264
JPeer264 force-pushed the jp/pi-durable-instrumentation branch from 26d39c9 to 4fc5960 Compare October 3, 2026 16:23
@JPeer264
JPeer264 removed this pull request from stack #25004 October 5, 2026 07:21
@JPeer264
JPeer264 added this pull request to stack #25029 October 5, 2026 07:22
@JPeer264

JPeer264 commented Oct 5, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor 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.

Stale Bugbot comment from a previous run.

Comment thread packages/server-utils/src/ai/pi-durable/index.ts
Comment thread packages/server-utils/src/integrations/index.ts
@JPeer264
JPeer264 force-pushed the jp/pi-durable-instrumentation branch from 4fc5960 to f8eb62c Compare October 5, 2026 09:44
@JPeer264
JPeer264 force-pushed the jp/pi-durable-instrumentation branch from f8eb62c to c8a1208 Compare October 5, 2026 11:22
@JPeer264

JPeer264 commented Oct 5, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

1 issue from previous review remains unresolved.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit c8a1208. Configure here.

@JPeer264
JPeer264 force-pushed the jp/pi-durable-instrumentation branch from c8a1208 to 189004f Compare October 5, 2026 12:53
@JPeer264
JPeer264 marked this pull request as ready for review October 5, 2026 13:24
@JPeer264
JPeer264 requested review from a team as code owners October 5, 2026 13:24
@JPeer264
JPeer264 requested review from chargome and s1gr1d and removed request for a team October 5, 2026 13:24
@JPeer264
JPeer264 requested review from andreiborza, isaacs and mydea and removed request for isaacs and mydea October 5, 2026 13:24
@JPeer264
JPeer264 force-pushed the jp/pi-durable-instrumentation branch from 189004f to aa37def Compare October 6, 2026 07:08
Base automatically changed from jp/bun-conversation-id-integration to develop October 6, 2026 07:55
pi-durable has no telemetry hooks, so `piDurableIntegration` wraps the
`models` and `registry` passed to `Harness.open()`. Each run becomes its
own `invoke_agent` trace with `chat` and `execute_tool` children, and a
subagent run nests under the tool call that started it.

Messages are mapped to the gen_ai conventions, and the system prompt and
tools are read from pi-durable's positional system messages. Tool spans
record the result the model receives. Failures pi-durable only reports,
and task phases that throw, are captured. Throws of the built-in coding
tools are not, because they report expected results to the model.

On Cloudflare it also covers pi-durable in a Durable Object through the
Agents SDK `PiHarness`, including requests that pi-ai sends through the
Workers AI binding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@JPeer264
JPeer264 force-pushed the jp/pi-durable-instrumentation branch from aa37def to e779efc Compare October 6, 2026 07:55
Comment thread packages/server-utils/src/ai/pi-durable/models.ts Outdated

@andreiborza andreiborza 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.

Thanks for all this effort! A couple of points:

...(harnessOptions.registry ? { registry: own(instrumentRegistry(harnessOptions.registry, options, runs)) } : {}),
// pi-durable passes failures of extension code here, such as a throwing hook, and keeps going.
onReport: own((error: unknown) => {
captureException(error, { mechanism: { handled: true, type: PI_DURABLE_ORIGIN } });

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.

m: I'd make this dependent on if users set an onReport handler. If they did not, I don't think these errors should be classified as handled, wdyt?

Suggested change
captureException(error, { mechanism: { handled: true, type: PI_DURABLE_ORIGIN } });
captureException(error, { mechanism: { handled: !!harnessOptions.onReport, type: PI_DURABLE_ORIGIN } });

return startSpanManual(
{
name: model.id ? `chat ${model.id}` : 'chat',
op: getGenAiSpanOp('chat'),

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.

m: Use GEN_AI_CHAT from sentry conventions instead please.

const startRunSpan = (): Span =>
startInactiveSpan({
name: 'invoke_agent',
op: getGenAiSpanOp('invoke_agent'),

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.

m: Replace with GEN_AI_INVOKE_AGENT please from sentry conventions.

return startSpanManual(
{
name: `execute_tool ${tool.name}`,
op: getGenAiSpanOp('execute_tool'),

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.

m: Please us GEN_AI_EXECUTE_TOOL from sentry conventions instead.

Comment on lines +149 to +150
if (!BUILT_IN_TOOLS.has(tool)) {
captureException(error, { mechanism: { handled: true, type: PI_DURABLE_ORIGIN } });

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.

Suggested change
if (!BUILT_IN_TOOLS.has(tool)) {
captureException(error, { mechanism: { handled: true, type: PI_DURABLE_ORIGIN } });
if (!BUILT_IN_TOOLS.has(tool)) {
captureException(error, { mechanism: { handled: false, type: PI_DURABLE_ORIGIN } });

m: Should be false here no? User has no chance to handle it?

Comment on lines +401 to +407
expect(Object.keys(chat!)).not.toContain('gen_ai.input.messages');
expect(Object.keys(chat!)).not.toContain('gen_ai.output.messages');
expect(Object.keys(chat!)).not.toContain('gen_ai.system_instructions');
expect(Object.keys(chat!)).not.toContain('gen_ai.tool.definitions');
expect(Object.keys(execute!)).not.toContain('gen_ai.tool.call.arguments');
expect(Object.keys(execute!)).not.toContain('gen_ai.tool.call.result');
expect(execute!['gen_ai.tool.description']).toBe('Get the weather.');

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.

l: Let's use constants from sentry conventions here too.

Comment on lines +428 to +433
expect(recorded).not.toContain('gen_ai.input.messages');
expect(recorded).not.toContain('gen_ai.output.messages');
expect(recorded).not.toContain('gen_ai.system_instructions');
expect(recorded).not.toContain('gen_ai.tool.definitions');
expect(recorded).not.toContain('gen_ai.tool.call.arguments');
expect(recorded).not.toContain('gen_ai.tool.call.result');

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.

l: Let's use constants from sentry conventions here too.

Comment on lines +549 to +558
expect(execute.attributes).toMatchObject({
'sentry.op': 'gen_ai.execute_tool',
'sentry.origin': 'auto.ai.pi_durable',
'gen_ai.tool.name': 'get_weather',
'gen_ai.tool.description': 'Get the weather.',
'gen_ai.tool.call.id': 'call_1',
'gen_ai.conversation.id': `${runs.harnessId}:4`,
'gen_ai.tool.call.arguments': '{"city":"Berlin"}',
'gen_ai.tool.call.result': 'sunny',
});

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.

l: Let's use constants from sentry conventions here too.

Comment on lines +598 to +601
expect(errored!.attributes['sentry.status.message']).toBe('internal_error');
expect(errored!.attributes['gen_ai.tool.call.result']).toBe('not found');
expect(thrown!.attributes['sentry.status.message']).toBe('internal_error');
expect(bash!.attributes['sentry.status.message']).toBe('internal_error');

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.

l: Let's use constants from sentry conventions here too.

Comment thread CHANGELOG.md Outdated

- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott

- **feat(server-utils)**: The new `piDurableIntegration` traces runs of pi-durable and is on by default where the Sentry runtime hook or a Sentry bundler plugin injects its channel, for example in Node and in Workers built with `@sentry/cloudflare/vite`. From the first pi-durable model request on, the `openai`, `@anthropic-ai/sdk`, `@google/genai` and Workers AI integrations stop reporting requests in the whole process, because pi-ai sends its requests through these clients. Requests that your app sends to these providers directly are then no longer traced either. The Flue integration now also stops the Workers AI integration. To keep these integrations, remove the pi-durable one: `integrations: integrations => integrations.filter(integration => integration.name !== 'PiDurable')`.

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.

m: This entry is too technical. Just announce the integration and what it does (tracing Pi Durable).

Comment on lines +179 to +182
}
if (message.isError && !call.failed) {
call.span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
}

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.

Bug: The function piAiContentToString returns an empty string for undefined input, violating its string | undefined return type and causing an empty attribute to be set on spans.
Severity: LOW

Suggested Fix

Modify piAiContentToString to return undefined when its input is undefined. This can be done by adding a check at the beginning of the function, like if (content === undefined) { return undefined; }, to align its behavior with its type signature.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/server-utils/src/ai/pi-durable/tools.ts#L179-L182

Potential issue: The function `piAiContentToString` has a return type of `string |
undefined`, but it incorrectly returns an empty string (`''`) when the input `content`
is `undefined`. The calling function, `finishToolCall`, checks `if (result !==
undefined)` to decide whether to set a span attribute. Because `'' !== undefined` is
true, an empty string is set as the `GEN_AI_TOOL_CALL_RESULT` attribute on the span.
This is misleading, as it represents 'no content' as 'empty content' in telemetry,
instead of omitting the attribute altogether.

This branch has not been deployed

No deployments
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.

2 participants