Skip to content

fix(agent): preserve output after trailing model failure - #3533

Merged
kojiwakayama merged 3 commits into
mainfrom
fix/issue-338-trailing-model-error
Aug 10, 2026
Merged

kojiwakayama merged 3 commits into
mainfrom
fix/issue-338-trailing-model-error

Conversation

@kojiwakayama

@kojiwakayama kojiwakayama commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • preserve a delivered assistant response when a later provider request fails after a completed model step
  • keep empty, incomplete, cancelled, lifecycle-error, and mid-step responses on their existing failure paths
  • cover both hosted finalization paths and the shared provider-stream classifier

This is the runtime portion of veryfront/veryfront-issue-inbox#338. No API persistence change is required because the runtime now reports the canonical run as completed. The Agent consumer must be updated to the released package before the issue is closed.

Verification

  • deno test --no-check --allow-all src/agent/hosted/hosted-chat-finalization.test.ts src/agent/hosted/stream-finalization.test.ts src/agent/streaming/stream-outcome.test.ts
  • deno task test:unit (3,778 passed, 28,062 steps; 1 ignored)
  • deno task fmt:check
  • deno task lint
  • deno task typecheck

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of provider failures that occur after a response has completed or handed off to a tool.
    • Preserved completed responses and tool-handoff outcomes when trailing stream errors occur.
    • Ensured timeout failures reach a failed state with a clear timeout message.
    • Ensured fallback output, mirrored content, and cleanup are finalized correctly.
  • Tests

    • Added regression coverage for provider failures, tool handoffs, completed responses, and stream timeouts.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@kojiwakayama, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 45 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 45aad2ab-85fe-4ec3-94d9-41aa9b2e8767

📥 Commits

Reviewing files that changed from the base of the PR and between f7ba44d and 19aeb29.

📒 Files selected for processing (1)
  • docs/api-reference/veryfront/agent.md
📝 Walkthrough

Walkthrough

The change preserves completed streamed output after non-timeout provider failures follow a completed tool handoff. Timeout errors now produce failed terminal states. Shared timeout classification covers stream, idle, bootstrap, and chat stream timeout messages.

Changes

Stream completion handling

Layer / File(s) Summary
Shared timeout classification
src/agent/streaming/stream-outcome.ts, src/agent/hosted/stream-terminal-error.ts
The shared isStreamTimeoutError helper recognizes timeout messages. Hosted terminal-error handling uses the shared classifier.
Stream outcome classification
src/agent/streaming/stream-outcome.ts, src/agent/streaming/stream-outcome.test.ts
Completed output with a finish signal suppresses non-timeout provider failures and resolves to tool_handoff. Idle timeouts remain failed.
Hosted response finalization
src/agent/hosted/hosted-chat-finalization.ts, src/agent/hosted/hosted-chat-finalization.test.ts, src/agent/hosted/stream-finalization.test.ts
Hosted finalization preserves completed output after trailing provider failures. Tests verify terminal dispatch, fallback output, flushing, model identification, cleanup, and timeout failure.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: kwakayama

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: preserving delivered output after a trailing model failure.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-338-trailing-model-error

Comment @coderabbitai help to get the list of available commands.

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/agent/streaming/stream-outcome.ts (1)

130-143: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Apply the completed-output gate to providerError.

Lines 142-143 return a failed outcome whenever providerError is set without thrownError. This bypasses the completed-output gate. A completed tool handoff can therefore fail when the caller reports the trailing provider failure as providerError.

  • src/agent/streaming/stream-outcome.ts#L130-L143: apply the output-and-finish-signal exception before both thrownError and providerError failure paths.
  • src/agent/streaming/stream-outcome.test.ts#L135-L142: add a completed tool_handoff case with providerError and no thrownError; expect tool_handoff.

As per coding guidelines, “For behavior changes, add or update a focused failing test before changing implementation.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/agent/streaming/stream-outcome.ts` around lines 130 - 143, Apply the
completed-output gate in the stream-outcome classification flow before handling
either thrownError or providerError, so completed output with a valid finish
signal returns the successful tool_handoff outcome even when only providerError
is present. First add a focused completed tool_handoff test with providerError
and no thrownError in src/agent/streaming/stream-outcome.test.ts lines 135-142,
then update the logic around the existing failure paths in
src/agent/streaming/stream-outcome.ts lines 130-143.

Source: Coding guidelines

🧹 Nitpick comments (1)
src/agent/hosted/hosted-chat-finalization.ts (1)

12-12: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use the internal import alias.

This import crosses from the hosted module to the streaming module. Replace the relative path with the configured #veryfront/* alias.

Based on learnings, use #veryfront/* aliases when an import crosses a module boundary. As per coding guidelines, use #veryfront/* for internal source imports.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/agent/hosted/hosted-chat-finalization.ts` at line 12, Update the import
of hasCompletedStepSignal in hosted-chat-finalization.ts to use the configured
`#veryfront/`* internal alias for the streaming module instead of the relative
path, leaving the imported symbol unchanged.

Sources: Coding guidelines, Learnings

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/agent/streaming/stream-outcome.ts`:
- Around line 130-143: Apply the completed-output gate in the stream-outcome
classification flow before handling either thrownError or providerError, so
completed output with a valid finish signal returns the successful tool_handoff
outcome even when only providerError is present. First add a focused completed
tool_handoff test with providerError and no thrownError in
src/agent/streaming/stream-outcome.test.ts lines 135-142, then update the logic
around the existing failure paths in src/agent/streaming/stream-outcome.ts lines
130-143.

---

Nitpick comments:
In `@src/agent/hosted/hosted-chat-finalization.ts`:
- Line 12: Update the import of hasCompletedStepSignal in
hosted-chat-finalization.ts to use the configured `#veryfront/`* internal alias
for the streaming module instead of the relative path, leaving the imported
symbol unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 263c2631-ffec-48e0-b459-af04c819b860

📥 Commits

Reviewing files that changed from the base of the PR and between d68fe07 and d83e58c.

📒 Files selected for processing (5)
  • src/agent/hosted/hosted-chat-finalization.test.ts
  • src/agent/hosted/hosted-chat-finalization.ts
  • src/agent/hosted/stream-finalization.test.ts
  • src/agent/streaming/stream-outcome.test.ts
  • src/agent/streaming/stream-outcome.ts

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d83e58cce1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agent/hosted/hosted-chat-finalization.ts Outdated

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

🧹 Nitpick comments (1)
src/agent/hosted/stream-terminal-error.ts (1)

4-4: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use the internal import alias for this cross-module import.

This import crosses from src/agent/hosted to src/agent/streaming. Use the repository’s #veryfront/* alias instead.

As per coding guidelines, internal src/**/*.ts imports use #veryfront/*. Based on learnings, relative imports are reserved for same-directory siblings, and this import crosses module boundaries.

Proposed fix
-import { isStreamTimeoutError } from "../streaming/stream-outcome.ts";
+import { isStreamTimeoutError } from "`#veryfront/agent/streaming/stream-outcome.ts`";
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/agent/hosted/stream-terminal-error.ts` at line 4, Update the import of
isStreamTimeoutError in stream-terminal-error.ts to use the repository’s
`#veryfront/`* internal alias for the streaming module instead of the relative
cross-module path; leave same-directory relative imports unchanged.

Sources: Coding guidelines, Learnings

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/agent/hosted/stream-terminal-error.ts`:
- Line 4: Update the import of isStreamTimeoutError in stream-terminal-error.ts
to use the repository’s `#veryfront/`* internal alias for the streaming module
instead of the relative cross-module path; leave same-directory relative imports
unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b2cc01c7-7d22-477f-b973-d5759d98c1bf

📥 Commits

Reviewing files that changed from the base of the PR and between d83e58c and f7ba44d.

📒 Files selected for processing (5)
  • src/agent/hosted/hosted-chat-finalization.test.ts
  • src/agent/hosted/hosted-chat-finalization.ts
  • src/agent/hosted/stream-terminal-error.ts
  • src/agent/streaming/stream-outcome.test.ts
  • src/agent/streaming/stream-outcome.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/agent/hosted/hosted-chat-finalization.ts
  • src/agent/streaming/stream-outcome.test.ts

@kojiwakayama
kojiwakayama added this pull request to the merge queue Aug 10, 2026
Merged via the queue into main with commit d110ee2 Aug 10, 2026
31 checks passed
@kojiwakayama
kojiwakayama deleted the fix/issue-338-trailing-model-error branch August 10, 2026 13:14
@kojiwakayama kojiwakayama mentioned this pull request Aug 10, 2026
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.

1 participant