Skip to content

feat(pi): track mid-stream agent stream stalls - #1618

Open
gewenyu99 wants to merge 2 commits into
mainfrom
feat/stream-stall-telemetry
Open

gewenyu99 wants to merge 2 commits into
mainfrom
feat/stream-stall-telemetry

Conversation

@gewenyu99

@gewenyu99 gewenyu99 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

4 of 10 gpt-6.2-sol orchestrator runs in #1617 stalled mid-stream until ai-gateway's 600s deadline killed them. Early user cancels look like normal ones, and there's no PostHog event to track stalls in prod.

Changes

  • Emit wizard: agent stream stalled when a Pi model turn goes silent for 120s or fails mid-stream on a deadline or drop. Redact secrets from error_message.
  • Send a recovered or aborted event if a silent turn finishes or gets cancelled.
  • Include model, stream progress, and concurrent Pi session counts, so you can slice stalls by model and parallel load.

Test plan

  • pnpm typecheck, pnpm test src/agent/runner/harness/pi/, pnpm lint, and pnpm build pass.

@gewenyu99
gewenyu99 requested a review from a team as a code owner November 26, 2026 23:35
@github-actions

Copy link
Copy Markdown

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci ai-observability
  • /wizard-ci basic-integration
  • /wizard-ci feature-flags
  • /wizard-ci mcp-analytics
  • /wizard-ci replay-vision
  • /wizard-ci revenue
  • /wizard-ci self-driving
  • /wizard-ci warehouse
  • /wizard-ci warehouse-seeded

Test an individual app:

  • /wizard-ci ai-observability/anthropic
  • /wizard-ci ai-observability/google-adk
  • /wizard-ci ai-observability/groq
Show more apps
  • /wizard-ci ai-observability/manual-capture
  • /wizard-ci ai-observability/openai
  • /wizard-ci ai-observability/openai-agents
  • /wizard-ci ai-observability/opentelemetry
  • /wizard-ci ai-observability/vercel-ai
  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/flutter
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci feature-flags/django
  • /wizard-ci feature-flags/next-js
  • /wizard-ci mcp-analytics/custom-dispatcher
  • /wizard-ci mcp-analytics/go-sdk
  • /wizard-ci mcp-analytics/python-sdk
  • /wizard-ci mcp-analytics/ruby-sdk
  • /wizard-ci mcp-analytics/rust-sdk
  • /wizard-ci mcp-analytics/typescript-sdk
  • /wizard-ci replay-vision/javascript-node
  • /wizard-ci replay-vision/next-js
  • /wizard-ci replay-vision/react-vite
  • /wizard-ci revenue/stripe
  • /wizard-ci self-driving/astro
  • /wizard-ci self-driving/fastapi
  • /wizard-ci self-driving/nuxt
  • /wizard-ci self-driving/react-router
  • /wizard-ci self-driving/sveltekit
  • /wizard-ci warehouse/monorepo-env
  • /wizard-ci warehouse/multi-source-next
  • /wizard-ci warehouse/stripe-node
  • /wizard-ci warehouse/zero-source
  • /wizard-ci warehouse-seeded/next-stripe
  • /wizard-ci warehouse-seeded/next-stripe-declined

Test against a Context Mill branch:

  • /wizard-ci all context-mill:my-branch

Add context-mill:<branch> to any command above to pin the Context Mill branch. It defaults to main.

Results will be posted here when complete.

@veria-ai

veria-ai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

Reviews

@posthog-security-review-bot posthog-security-review-bot Bot added the security-review Request a security review label Nov 26, 2026
@posthog-security-review-bot

posthog-security-review-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Security review complete — f71f52d40ccf

No findings above the confidence threshold, in 5 min.

Add the security-review label to run again on the latest commit.

@posthog-security-review-bot posthog-security-review-bot 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.

Agent-driven security review - no findings.

@posthog-security-review-bot posthog-security-review-bot Bot removed the security-review Request a security review label Nov 26, 2026
@gewenyu99
gewenyu99 force-pushed the feat/stream-stall-telemetry branch from f71f52d to 9222e91 Compare November 27, 2026 00:59
@gewenyu99
gewenyu99 changed the base branch from perf/aio-runtime-opt to main November 27, 2026 00:59

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

Verdict: REQUEST CHANGES

See inline comments.

Automated review by Glimmervoid. Not written by a human

const idleMs =
turnStartedAt > 0 ? Math.max(0, now() - lastActivityAt) : 0;

if (msg.stopReason === '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.

[convergent: boundary + architecture] HIGH

This reports a drop Pi is about to retry as a final failed, so a drop Pi recovers from looks like a real stall in the dashboard.

Pi's auto-retry is on by default (settings-manager.js:673, retry?.enabled ?? true) and we don't turn it off. Its retryable pattern in pi-ai/dist/utils/retry.js matches terminated, socket hang up, connection.?lost and timed? out, the same strings classifyStreamStallError maps to upstream_closed, connection_reset and deadline_exceeded. Pi emits them in this order: message_end with stopReason: 'error', then agent_end (with willRetry), then auto_retry_start from _handlePostAgentRun (agent-session.js:1426). This branch sets turnState = 'failed' and sends report('failed') before auto_retry_start arrives. The auto_retry_start / auto_retry_end pause branches only run from waiting, so they never fire here, and no recovered follows when the retry works.

The tests at stream-stall.test.ts:318-331 and :399-405 send auto_retry_start right after turn_start with no message_end. Pi never sends that order, which is why they pass.

Suggested fix: on an error message_end, hold the report until agent_end. With willRetry: false, report failed. With willRetry: true, wait for auto_retry_end: success: false reports failed, and a later successful message_end reports recovered. Reading Pi's own willRetry also keeps this from drifting when a ~1.1.x bump changes Pi's retry regex. Then rewrite those two tests in Pi's real order.

This also overlaps #1363, which sends agent turn retried from the same auto_retry_start. Worth picking one owner for Pi retry telemetry, or giving both events a shared turn id so they can be joined.

Automated review by Glimmervoid. Not written by a human

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 32614a1.

stream-stall.ts now holds stall errors on message_end until agent_end reports willRetry.

  • willRetry: false emits failed once.
  • willRetry: true moves to retrying without emitting failed, keeps silentReported across the retry turn_start, emits recovered if a silent turn finishes on retry, and emits failed once on retry exhaustion or cancelled backoff (auto_retry_end with success: false).
  • Updated stream-stall.test.ts and structured-run.test.ts to match Pi's real event order and cover retry recovery, retry exhaustion, and cancelled backoff.

@gewenyu99
gewenyu99 requested review from a team and johncwaters and removed request for johncwaters November 27, 2026 19:15
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