Skip to content

fix(guardrails): close a blocked stream even when its refusal cannot be saved - #387

Merged
jddunn merged 3 commits into
masterfrom
fix/blocked-stream-closed-when-refusal-save-fails
Oct 13, 2026
Merged

jddunn merged 3 commits into
masterfrom
fix/blocked-stream-closed-when-refusal-save-fails

Conversation

@jddunn

@jddunn jddunn commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Summary

A follow-up to #384 from its review: the Codex thread on ExternalToolResultHandler.ts L772, confirmed in the code at master df4f0b2, with a test on the real path that fails before the fix.

A block whose refusal the store refused to save left the stream open

Evidence (df4f0b2). When an output guard blocks the call a stream stopped for, discardPendingExternalToolRequest() records the refused tool result, clears the pending request in memory and then awaits the conversation's save with no catch (ExternalToolResultHandler.ts L765-773). discardBlockedToolCalls() deletes the stream's context and closes the stream only after that call returns (AgentOSOrchestrator.ts L724-737), and AgentOS's onOutputVerdict logs the error and goes on (AgentOS.ts L1515-1521). A store that refused the save left the stream open with its context: handleToolResults() registered on it (AgentOS.ts L2293), the orchestrator found the context, the one thing it checks (ExternalToolResultHandler.ts L208-219), and the GMI received a result for the blocked call.

Fix. discardPendingExternalToolRequest() now does the whole drop: once it has recorded the refusal and cleared the pending request, it removes the stream's context, saves the conversation, and closes the stream in a finally, so the stream is closed whether or not the store takes the save; a refused save rejects after that. discardBlockedToolCalls() returns what the handler reports. The warning onOutputVerdict logs says what a refused save leaves: the stored conversation keeps the pause until the conversation is saved again.

Test. tests/api/AgentOS.processRequest.toolCallGuards.integration.spec.ts, "a block whose refusal the store fails to save still closes the stream, so the call cannot be continued on it": the real path through processRequest() and handleToolResult() with the GMI that stops for a host call, and a conversation manager whose saveConversation refuses the save that records the refusal, as a store does during an outage. The case asserts that the store refused that save, that the conversation in memory has no pause and answers the call with the refusal, and that handleToolResult() on the blocked stream yields one error without reaching the GMI.

What a refused save leaves in the store

The thread also notes that a restart may offer the stored pause again. The conversation in memory holds the refusal and no pause, and every later save of it stores both: the next turn's inbound save (TurnExecutionPipeline.ts L418-450), the save before the conversation manager evicts it (ConversationManager.ts L466-468) and the save at shutdown (L812-822). A restart finds the pause stored only when the process stopped before any of those saves succeeded. A store that refuses writes cannot take a durable record of the refusal either, so this change does not try; the warning names the stream and the calls, and the Block bullet of docs/safety/GUARDRAILS_USAGE.md ("Tool Calls") says when the stored pause can come back.

Linked issues

Follows #384 (for #344).

Type of change

  • Bug fix
  • Feature
  • Performance
  • Documentation
  • Refactor
  • Tests
  • Build or CI

How this was verified

CI on this pull request.

  • Run 38360934868 on the test commit (232ec52): 1 failed, 10876 passed. The new case fails at expect(gmi.handleToolResult).not.toHaveBeenCalled(): the GMI received the blocked call's result (call-shell-1, { exitCode: 0 }). The continuation then ended in an error only because the same store refused the continuation's own save, after the GMI had taken the result.
  • Run 38361452923 on the fix (c97c20d): every job green. Test & Lint ran 833 test files, 10877 tests passed and none failed, the spec's 9 cases among them.

Migration notes

None. A store that takes the save sees no change; with one that refuses it, the stream is closed as it is after a save that succeeds.

Checklist

  • Tests cover the changed behavior.
  • Docs are updated.
  • The title follows Conventional Commits, with ! before the colon for a change that breaks users (feat!: or feat(api)!:).
  • No secrets or private data are included.
  • Every automated review thread is settled (how).
  • I have read this change and can explain every line.

Summary by CodeRabbit

  • Bug Fixes

    • Blocked tool-call streams now close even when saving the refusal fails. The failed save does not prevent stream closure, and the blocked request cannot be continued on that stream.
    • If saving fails, the conversation may retain the refusal until a later save succeeds.
  • Documentation

    • Clarified how hosts wrapping streams can access the IDs of blocked tool calls.

…t be saved

With conversational persistence on and a store that refuses the save
recording the refusal, a block on the call a turn stops for must still
close the stream: handleToolResult() on it must end in an error and never
reach the GMI. The conversation in memory must still drop the pause and
answer the call with the refused tool result.
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: framerslab/agentos/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 7f51a05f-4098-485d-a80c-367cff5025fe
📥 Commits

Reviewing files that changed from the base of the PR and between 6fd43c4 and c97c20d.

📒 Files selected for processing (5)
  • docs/safety/GUARDRAILS_USAGE.md
  • src/api/AgentOS.ts
  • src/api/runtime/AgentOSOrchestrator.ts
  • src/api/runtime/ExternalToolResultHandler.ts
  • tests/api/AgentOS.processRequest.toolCallGuards.integration.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

📝 Walkthrough

Blocked tool-call discards now close the stream and remove its active context even when saving the conversation fails. The save error propagates after closure. Documentation and an integration test describe the behavior.

Changes: Blocked tool-call discard

Layer / File(s) Summary
Discard cleanup and failure behavior
src/api/runtime/ExternalToolResultHandler.ts, src/api/runtime/AgentOSOrchestrator.ts, src/api/AgentOS.ts, tests/api/AgentOS.processRequest.toolCallGuards.integration.spec.ts, docs/safety/GUARDRAILS_USAGE.md
The handler removes the active stream context before saving and closes the stream in a finally block. The orchestrator delegates the discard to the handler. If saving fails, the error propagates after closure. The integration test checks the failed-save behavior, and the documentation describes the resulting conversation state.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to c97c2

Blocked tool-call streams now close even when saving the refusal fails, so the blocked call cannot be continued. A failed save leaves the stored pause in place until a later save succeeds. No merge-blocking risk is evident.

Pre-merge checks · Passed 5
Passed Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 4…
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.
Description check Passed The description is complete and directly related to the change. It includes the summary, linked issue, change types, verification results, migration notes, and completed checklist.
Title check Passed The title is concise, specific, and follows Conventional Commits. It accurately describes closing a blocked stream when saving the refusal fails.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@sourcery-ai

sourcery-ai Bot commented Oct 13, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR adds a real-path integration regression test covering the guardrail failure case where saving a refusal throws, ensuring the blocked stream is still finalized and cannot be continued while the in-memory refusal state remains correct.

File-Level Changes

Change Details Files
Add an integration regression test for blocked streams when refusal persistence fails.
  • Mock refusal-result conversation saves to fail like a storage outage.
  • Verify the refusal is applied in memory and the pending request is cleared despite the failed save.
  • Verify the blocked stream closes and later tool-result handling returns an error without invoking the GMI.
tests/api/AgentOS.processRequest.toolCallGuards.integration.spec.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

…be saved

When an output guard blocked the call a stream stopped for, the pause was
dropped in memory and the conversation saved before the orchestrator
removed the stream's context and closed the stream. A store that refused
the save threw past both steps; AgentOS logged the error, and the stream
stayed open with its context, so handleToolResult() continued it with a
result for the blocked call.

discardPendingExternalToolRequest() now removes the stream's context
before the save and closes the stream whether or not the store takes it;
a refused save rejects once the stream is closed. The warning AgentOS
logs says the stored conversation keeps the pause until the conversation
is saved again.
The Block bullet of the usage guide's Tool Calls section says the stream
is closed even when the conversation store refuses the save, and when the
stored conversation can still hold the pause after a restart.
@jddunn
jddunn marked this pull request as ready for review October 13, 2026 07:16
@jddunn
jddunn requested a review from Victor-Evogor as a code owner October 13, 2026 07:16
@sourcery-ai

sourcery-ai Bot commented Oct 13, 2026

Copy link
Copy Markdown

Sorry @jddunn, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 3 days and 23 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T07:19:47.826216Z c97c20d Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@jddunn
jddunn merged commit 864941b into master Oct 13, 2026
12 checks passed
@jddunn
jddunn deleted the fix/blocked-stream-closed-when-refusal-save-fails branch October 13, 2026 07:21
github-actions Bot pushed a commit that referenced this pull request Oct 13, 2026
## [0.13.88](v0.13.87...v0.13.88) (2026-10-13)

### fix

* **guardrails:** close a blocked stream even when its refusal cannot be saved (#387) ([864941b](864941b))
* **meetings:** the follow-ups' forward fixes: the interlock's order on the worker's clock, the loop's two passes, handlers before destinations, the late notice first, finals during startCapture (#388) ([16dce24](16dce24))
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