Skip to content

fix(agent): continue unattended turns after transient upstream stream failures#3395

Merged
tatoalo merged 1 commit into
mainfrom
posthog-code/retry-unattended-turn-upstream-failure
Jul 13, 2026
Merged

fix(agent): continue unattended turns after transient upstream stream failures#3395
tatoalo merged 1 commit into
mainfrom
posthog-code/retry-unattended-turn-upstream-failure

Conversation

@tatoalo

@tatoalo tatoalo commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Problem

handleTurnFailure only treats follow-up turns in interactive mode as recoverable, so a single transient upstream stream death during the initial or resume turn fails the entire run, tears down the sandbox, and throws away the whole turn's work (and its token spend)

Changes

  • add promptWithUpstreamRetry for initial and resumed turns. On transient upstream failures, wait 5 seconds and retry automatically: stream interruptions resume with a hidden "continue from where you left off" prompt, while connection, timeout, and provider failures re-send the original prompt
  • re-read the session before every retry so retries always target the current session, even if it was recreated during the delay
  • limit retries to two attempts per turn. Non-upstream errors, exhausted retry budgets, and follow-up turns keep their existing behavior

@trunk-io

trunk-io Bot commented Jul 13, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 13, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit cac7efd.

@tatoalo tatoalo self-assigned this Jul 13, 2026
@tatoalo tatoalo marked this pull request as ready for review July 13, 2026 13:59
@greptile-apps

greptile-apps Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "fix(agent): continue unattended turns af..." | Re-trigger Greptile

Comment thread packages/agent/src/server/agent-server.ts
Comment thread packages/agent/src/server/agent-server.ts Outdated
Comment thread packages/agent/src/server/agent-server.ts Outdated
@tatoalo tatoalo force-pushed the posthog-code/retry-unattended-turn-upstream-failure branch from 9845016 to 8fb35f0 Compare July 13, 2026 14:12
@tatoalo tatoalo added the Stamphog This will request an autostamp by stamphog on small changes label Jul 13, 2026
github-actions[bot]
github-actions Bot previously approved these changes Jul 13, 2026

@github-actions github-actions 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.

Well-bounded retry wrapper with correct per-turn counter scoping, session null-guard on every attempt, and proper differentiation between mid-stream deaths (continuation) and pre-stream failures (re-send original prompt). All three bot-identified concerns are demonstrably resolved in the code, with test coverage for each scenario.

@tatoalo tatoalo requested a review from a team July 13, 2026 14:18
… failures

Initial and resume turns run unattended in cloud sandboxes — there is no
user watching who could retry — yet handleTurnFailure only treats
follow-up turns in interactive mode as recoverable. A single transient
upstream stream death during the initial or resume turn therefore failed
the whole task run and tore down the sandbox.

Give those turns a bounded (2 per turn) number of automatic retries when
the failure classifies as a transient upstream failure. A stream that
died mid-response already delivered the original prompt into the session
history, so that case retries with a hidden continuation prompt; failures
where the request may never have been processed re-send the original
prompt instead. The session is re-read on every attempt so a teardown or
re-initialization during the retry delay cannot route the retry to a
stale session. Non-upstream errors and exhausted budgets fail exactly as
before.
@tatoalo tatoalo force-pushed the posthog-code/retry-unattended-turn-upstream-failure branch from 8fb35f0 to cac7efd Compare July 13, 2026 14:36
@github-actions github-actions Bot dismissed their stale review July 13, 2026 14:36

New commits pushed (delta classified non_linear_history) — stamphog approval dismissed; re-review running automatically.

@github-actions github-actions 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.

Well-scoped resilience fix with per-turn-local retry counter, session re-read on each attempt, and correct differentiation between mid-stream deaths (continuation) and pre-stream failures (original prompt re-send). All three bot-flagged concerns were addressed and the implementation is backed by good test coverage.

@tatoalo tatoalo merged commit f4f299d into main Jul 13, 2026
26 checks passed
@tatoalo tatoalo deleted the posthog-code/retry-unattended-turn-upstream-failure branch July 13, 2026 14:46
charlesvien pushed a commit to ricardo-leiva/code that referenced this pull request Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Stamphog This will request an autostamp by stamphog on small changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant