-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Messages and abort #1345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Messages and abort #1345
Conversation
🦋 Changeset detectedLatest commit: 9d2a4a6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
Greptile OverviewGreptile SummaryThis PR introduces two major experimental features for Stagehand's agent system: abort signal support and message continuation. The abort signal functionality allows users to cancel long-running agent tasks using standard Both features are gated behind the Important Files Changed
Confidence score: 4/5
Sequence DiagramsequenceDiagram
participant User
participant V3
participant AgentHandler
participant AbortController
participant LLMClient
participant CombineSignals as combineAbortSignals
participant Validation as validateExperimentalFeatures
User->>V3: "agent(config)"
V3->>Validation: "validateExperimentalFeatures(config)"
Validation-->>V3: "validation passed"
V3-->>User: "agent instance"
User->>V3: "agent.execute({ instruction, signal, messages })"
V3->>Validation: "validateExperimentalFeatures(executeOptions)"
Validation-->>V3: "validation passed"
V3->>CombineSignals: "combineAbortSignals(instanceSignal, userSignal)"
CombineSignals-->>V3: "combinedSignal"
V3->>AgentHandler: "execute(options with combinedSignal)"
AgentHandler->>AgentHandler: "extractAbortSignal(options)"
AgentHandler->>AgentHandler: "prepareAgent(options)"
AgentHandler->>LLMClient: "generateText({ signal: combinedSignal })"
alt Execution completes normally
LLMClient-->>AgentHandler: "result with messages"
AgentHandler->>AgentHandler: "consolidateMetricsAndResult(messages)"
AgentHandler-->>V3: "AgentResult with messages"
V3-->>User: "AgentResult with messages"
else User aborts signal
User->>AbortController: "abort()"
AbortController->>LLMClient: "signal aborted"
LLMClient-->>AgentHandler: "error"
AgentHandler->>AgentHandler: "getAbortErrorReason(error, signal)"
AgentHandler->>AgentHandler: "throw AgentAbortError"
AgentHandler-->>V3: "AgentAbortError"
V3-->>User: "AgentAbortError"
else Stagehand closes
V3->>AbortController: "_agentAbortController.abort('closing')"
AbortController->>LLMClient: "signal aborted"
LLMClient-->>AgentHandler: "error"
AgentHandler->>AgentHandler: "getAbortErrorReason(error, signal)"
AgentHandler->>AgentHandler: "throw AgentAbortError"
AgentHandler-->>V3: "AgentAbortError"
V3-->>User: "AgentAbortError"
end
User->>V3: "agent.execute({ instruction, messages: previousMessages })"
Note over User,V3: Message continuation - using messages from previous result
V3->>AgentHandler: "execute(options with previous messages)"
AgentHandler->>AgentHandler: "combine input messages with new instruction"
AgentHandler->>LLMClient: "generateText({ messages: combinedMessages })"
LLMClient-->>AgentHandler: "result with full conversation"
AgentHandler-->>V3: "AgentResult with full conversation history"
V3-->>User: "AgentResult with updated messages"
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
13 files reviewed, 4 comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No issues found across 14 files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 issues found across 5 files (changes from recent commits).
Prompt for AI agents (all 2 issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="packages/core/lib/v3/handlers/v3AgentHandler.ts">
<violation number="1">
P2: The `stream()` method no longer catches errors from `prepareAgent()`. If an abort signal causes `prepareAgent()` to throw (e.g., null context from `stagehand.close()`), the error won't be converted to `AgentAbortError`. The `execute()` method handles this by wrapping everything in try-catch with abort detection.</violation>
<violation number="2" location="packages/core/lib/v3/handlers/v3AgentHandler.ts:335">
P2: The `onError` handler in `stream()` only checks `AgentAbortError.isAbortError(event.error)` but doesn't check `options.signal?.aborted` like the `execute()` method does. This inconsistency means abort-related errors that don't match the `isAbortError` pattern (e.g., null context errors from `stagehand.close()`) won't be properly converted to `AgentAbortError` in streaming mode.</violation>
</file>
Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4 issues found across 32 files (changes from recent commits).
Prompt for AI agents (all 4 issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name=".changeset/orange-garlics-brake.md">
<violation number="1">
P1: Changeset description doesn't match PR content. The PR adds abort signal support and message continuation for agents, but the changeset says "add support for page.addInitScript()" which is unrelated. This will result in an incorrect changelog entry.</violation>
</file>
<file name=".changeset/wide-doors-allow.md">
<violation number="1">
P2: Changeset message doesn't match the PR changes. The message says "Add support for 4.5 opus in cua agent" but this PR adds abort signal support and message continuation features. This will result in an incorrect changelog entry.</violation>
</file>
<file name=".changeset/tasty-teams-call.md">
<violation number="1">
P2: Changeset description doesn't match the PR content. The PR implements abort signals, message continuation, and auto-abort on close, but the changeset describes 'make act, extract, and observe respect user defined timeout param'. This will generate incorrect changelog entries. Consider updating the description to reflect the actual changes, e.g.:
`feat: add agent abort signal support and message continuation`</violation>
</file>
<file name=".changeset/fruity-clowns-pick.md">
<violation number="1">
P1: Changeset description doesn't match the PR content. The PR adds abort signal support and message continuation features, but the changeset says 'fix: don't attach to targets twice'. This will result in an incorrect changelog entry. Consider updating to something like:
feat: add agent abort signal support and message continuation
</file>
Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR
Agent Abort Signal and Message Continuation
Why
Enable users to cancel long-running agent tasks and continue conversations across multiple
execute()calls. Also ensures graceful shutdown whenstagehand.close()is called by automatically aborting any running agent tasks.What Changed
New Features (behind
experimental: true)Abort Signal Support
signaltoagent.execute()to cancel execution mid-runAbortControllerandAbortSignal.timeout()AgentAbortErrorwhen abortedMessage Continuation
execute()now returnsmessagesin the resultNew Utilities
combineAbortSignals.tsAbortSignal.any()on Node 20+, fallback for older)errorHandling.tsclose()may cause indirect errors (e.g., null context) that should still be treated as abortvalidateExperimentalFeatures.tsCUA Limitations
Abort signal and message continuation are not supported with CUA mode (throws
StagehandInvalidArgumentError). This matches existing streaming limitation.Tests Added
agent-abort-signal.spec.ts(7 tests)agent-message-continuation.spec.ts(4 tests)agent-experimental-validation.spec.ts(17 tests)Summary by cubic
Adds agent abort support and conversation continuation. You can cancel long runs, auto-abort on close, and carry messages across execute() calls. Feature is gated behind experimental: true and has clear CUA limitations.
New Features
Refactors
Written for commit 9d2a4a6. Summary will update automatically on new commits.