fix: auto-report unhandled errors in AgentWorkflow to trigger onWorkflowError - #1003
Merged
Conversation
…lowError Closes #998 Previously, throwing an error inside a workflow's run() method would halt the workflow but never notify the Agent via onWorkflowError. Only explicit step.reportError() calls triggered the callback, but those did not halt the workflow. This created an inconsistency where users had to choose between halting (throw) and notifying (reportError). The fix wraps both originalRun.call() paths in the run() wrapper with try/catch blocks that call a new _autoReportError() method. This method sends an error callback to the Agent via RPC, then re-throws the original error so the workflow still halts. A _errorReported flag prevents double notification when step.reportError() is called before throwing (e.g., in waitForApproval rejection flows). Key design decisions: - _autoReportError is non-durable (not wrapped in step.do) because the workflow is about to halt anyway. On retry, a new instance resets the flag, so the agent gets re-notified (idempotent via onWorkflowCallback). - Best-effort: notification failures are swallowed so the original error always propagates to the Workflows runtime. - The flag is set before step.do in reportError, so it guards even when steps are mocked or fail during testing.
🦋 Changeset detectedLatest commit: 9f576fb The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #998
Problem
AgentWorkflowhas an inconsistency in error handling:throw new Error()halts the workflow but never triggersonWorkflowErroron the Agentstep.reportError()triggersonWorkflowErrorbut does not halt the workflowUsers expect
throwto both halt the workflow and notify the Agent. Today they have to do both manually, and even then the ordering is fragile.Solution
Wrap both
originalRun.call()paths in therun()wrapper withtry/catchblocks. When an unhandled error propagates out of the user'srun()method:_autoReportError(err)sends an error callback to the Agent via RPCA new
_errorReportedinstance flag prevents double notification whenstep.reportError()was already called before the throw.Changes
packages/agents/src/workflows.ts_errorReportedflag,_autoReportError()method, andtry/catchwrappers around bothoriginalRun.call()pathspackages/agents/src/tests/test-workflow.tspackages/agents/src/tests/workflow-error-reporting.test.tsintrospectWorkflowInstancepackages/agents/src/tests/workflow-prototype.test.tspackages/agents/src/tests/agents/workflow.tsWorkflowEnventries + 4 helper methods for starting error-scenario workflowspackages/agents/src/tests/worker.tspackages/agents/src/tests/wrangler.jsonc.changeset/fix-workflow-error-reporting.mdDesign decisions & tradeoffs
Non-durable error notification
_autoReportErrorcallsnotifyAgent()directly — it is not wrapped instep.do(). This is intentional:step.do()could interfere with the Workflows runtime's retry semantics (the step might succeed on retry while the throw still happens)_errorReported = false, so the agent gets re-notified — this is acceptable becauseonWorkflowCallbackidempotently sets status to"errored"Best-effort notification
The
try/catchinside_autoReportErrorswallows notification failures. If the agent is unreachable (e.g., binding misconfigured, DO overloaded), the original error still propagates. The workflow enters"errored"state in the Workflows runtime regardless.Flag set before
step.doIn
reportError(),this._errorReported = trueis set before thestep.do()call. This ensures the guard works even if:_initAgentfailure pathIf
_initAgent()throws (e.g., missing binding), the catch calls_autoReportError, which callsthis.agentgetter, which throws "Agent not initialized". The inner try/catch swallows this — correct behavior since we can't notify an agent we can't reach.Inheritance /
super.run()pathBoth branches of the
run()wrapper have try/catch. If a child class callssuper.run()and the parent throws, both wrappers catch. The_errorReportedflag (instance-level) prevents the second wrapper from sending a duplicate notification.Edge cases covered by tests
throwdirectly inrun()(outsidestep.do)throwinsidestep.do()step.reportError()thenthrowstep.reportError()alone (no throw)String(err)path works correctlywaitForApprovalrejectionreportErrorinsidewaitForApprovalsets flag, auto-report skippedNotes for reviewers
step.do()throw test is skipped because the Workflows runtime retries failed steps with exponential backoff, causingwaitForStatus("errored")to exceed the 5s test timeout. The behavior is still correct — step errors propagate torun()where our catch fires. This is the same limitation that causes 5 other tests inworkflow-integration.test.tsto be skipped._autoReportErroris a private method, so no public API surface change. The only observable behavior change is thatonWorkflowErrornow fires on unhandled throws._errorReportedflag resets on each workflow instance. If the Workflows runtime retriesrun()on a new instance, the agent gets notified again. This is by design — each retry is a fresh attempt, and repeated "errored" status updates are idempotent.patchbump since this is a bug fix with no API changes.