Skip to content

fix(agents): settle aborted runs through after-turn so agent_end fires - #116087

Merged
steipete merged 1 commit into
mainfrom
steipete/aborted-run-agent-end
Jul 29, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/aborted-run-agent-end

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

User-aborted agent runs unwound with a raw AbortError out of the embedded run's lane task, skipping completeEmbeddedAttemptAfterTurn entirely. No agent-end side effects ever fired for aborted turns: the interrupted-turn experience review (#115887) was unreachable on the primary abort path, deterministic correction autocapture never ran (its own doc says failed-turn corrections are the most worth keeping), and plugin agent_end hooks were starved. Live-verified on a scratch gateway: lane task error: lane=main ... error="AbortError: This operation was aborted" / "Error: attempt aborted before prompt submission", with zero scheduling activity.

Why This Change Was Made

Three escape hatches let aborts bypass the settle → after-turn pipeline that already handles aborted lifecycles everywhere else (finalizeAttemptContextEngineTurn takes aborted, bootstrap persistence checks it, the agent-end event computes success = !aborted && !promptError):

  1. attempt-stream-finalize.ts — releaseForPrompt() throws "attempt aborted before prompt submission" when abort blocks the next round's submission; an aborted attempt never submits another prompt, so this is expected teardown noise, now tolerated when the lifecycle is aborted.
  2. attempt-stream-settle.ts — the completion-required async-task wait rethrew AbortError unless the lifecycle was timedOut; user aborts now settle the same way timeout-aborts always have. An aborted run's unfinished async tasks no longer stamp a "Timed out waiting for async task completion" prompt error (that would reclassify the abort as an errored completion).
  3. attempt-after-turn.ts — teardown races can leave an AbortError in promptError; abort outranks failure in terminal-outcome precedence, so the agent-end event no longer attaches error when the lifecycle is aborted. The scheduler's abort-vs-error split then classifies correctly.

The abort-race test that pinned the old unwind ("preserves a run-budget timeout when abort blocks prompt submission") now asserts the run resolves with its run-budget timeout attribution preserved on the terminal — convergent with the normal timeout contract, which always settled through this path.

User Impact

Interrupting a deep agent turn now feeds self-learning and plugin hooks instead of silently dropping the turn. No config or API changes.

Evidence

  • Live before/after on a scratch dev gateway (real OpenAI, gpt-5.6-luna, native runtime): before — SIGINT mid-turn produced a lane-task AbortError rejection and no scheduling; after — a 22-iteration aborted turn logs experience review scheduled: session=agent:main:abort-live-3 iterations=22 aborted=true, the review runs cleanly (isolated incognito session, correct self-exclusion), zero lane task errors. A completed short turn logs the below-depth-bar skip, proving the event path fires for every turn.
  • node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/attempt-phase-lifecycle.test.ts src/agents/embedded-agent-runner/run/attempt-abort.test.ts src/agents/embedded-agent-runner/run/attempt.abort-race.test.ts src/agents/embedded-agent-runner/run/attempt-session-settle.test.ts — 16 passed, incl. two new regressions: aborted async-task wait settles without reclassification, and a teardown-raced AbortError yields an abort-classified agent-end event (success:false, no error).
  • node scripts/check-changed.mjs green (local fallback; Blacksmith backend unreachable).

@steipete
steipete marked this pull request as ready for review July 29, 2026 21:18
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Jul 29, 2026
@steipete
steipete force-pushed the steipete/aborted-run-agent-end branch from 509a885 to f117fc3 Compare July 29, 2026 21:20
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels Jul 29, 2026
@clawsweeper

clawsweeper Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 29, 2026, 5:24 PM ET / 21:24 UTC.

ClawSweeper review

What this changes

The PR treats expected abort errors during embedded-run teardown as aborted terminal outcomes, allowing after-turn processing, agent-end hooks, and interrupted-turn experience review to run.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep this PR open for normal maintainer review. The supplied diff narrowly restores the established settle → after-turn lifecycle for user-aborted embedded runs, and its stated live gateway proof plus focused regressions support the change; the maintainer label requires explicit human handling rather than automated cleanup.

Priority: P1
Reviewed head: f117fc3f3934426afad7915db9d6726e6816b62e
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused lifecycle fix with concrete live before/after evidence and targeted regressions; final current-main integration and required checks remain the meaningful merge gate.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The PR body provides an after-fix real scratch-gateway run with a real provider, a concrete scheduled-review log, and the absence of the former lane-task abort error; focused automated regressions supplement that proof.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body provides an after-fix real scratch-gateway run with a real provider, a concrete scheduled-review log, and the absence of the former lane-task abort error; focused automated regressions supplement that proof.
Evidence reviewed 5 items Abort path is contained at lifecycle boundaries: The proposed branch catches a runner abort only when lifecycle state is already aborted before continuing to settlement, and permits aborts from the completion-required async-task wait to continue into after-turn processing.
Agent-end classification preserves abort precedence: The branch suppresses the agent-end error field when the lifecycle is aborted, matching the documented distinction that an aborted event is unsuccessful but not a provider/prompt failure.
Focused regressions cover both escapes: The branch adds coverage for an AbortError during completion-required async-task waiting, abort-raced agent-end classification, and the former prompt-release unwind path.
Findings None None.
Security None None.

How this fits together

An embedded agent run streams model work, waits for completion-required asynchronous tasks, and then executes after-turn processing that flushes state and emits an agent-end event. Plugins and the experience-review feature consume that event to distinguish normal completion, errors, timeouts, and user interruptions.

flowchart LR
  A[User stops an agent turn] --> B[Embedded run stream]
  B --> C[Async-task settlement]
  C --> D[After-turn processing]
  D --> E[Agent-end event]
  E --> F[Plugin hooks]
  E --> G[Experience review]
  D --> H[Terminal outcome]
Loading

Decision needed

Question Recommendation
Does the current main branch still preserve the intended single-settlement ordering when this abort repair is merged, so newly delivered aborted agent-end events are safe for existing hook consumers? Confirm and land after current-head checks: Verify the branch against current main and merge if the after-turn event remains single-delivery and required checks pass.

Why: The code change deliberately expands delivery of an existing terminal event to aborted runs; the repository marks this PR for maintainer handling, and the final integration decision depends on current-main behavior beyond the supplied branch diff.

Before merge

  • Resolve merge risk (P1) - The branch intentionally changes aborted terminal delivery: agent-end consumers that previously saw no event will now run, so maintainer review should confirm the current main integration still preserves the intended single-settlement and terminal-outcome ordering.
Agent review details

Security

None.

PR surface

Source +24, Tests +135. Total +159 across 5 files.

View PR surface stats
Area Files Added Removed Net
Source 3 28 4 +24
Tests 2 144 9 +135
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 172 13 +159

Review metrics

None.

Merge-risk options

Maintainer options:

  1. Verify the current-main terminal sequence before merge (recommended)
    Rebase or merge-test the exact head and confirm an aborted run performs one after-turn settlement with the expected terminal outcome and agent-end event.
  2. Accept the event-delivery expansion
    Land with the documented behavior that existing agent-end consumers now receive aborted turns, accepting the intentional hook and transcript-side-effect change.
Copy recommended automerge instruction
@clawsweeper automerge

Special instructions:
Rebase onto current main, run the focused embedded-run abort lifecycle tests, and confirm one abort-classified agent-end event with no synthetic error.

Technical review

Best possible solution:

Land the focused lifecycle repair after confirming it rebases cleanly onto current main and the remaining required checks validate the exact head; retain abort precedence so plugins and experience review receive a consistent aborted agent-end event without a synthetic error.

Do we have a high-confidence way to reproduce the issue?

Yes, at source level: an abort thrown from prompt release or completion-required task settlement bypasses after-turn in the reported pre-change path, and the branch adds focused regression coverage for both routes. The PR also supplies a described real gateway before/after run, but I did not independently execute it in this read-only review.

Is this the best way to solve the issue?

Yes, provisionally: continuing expected teardown aborts into the existing settle and after-turn owner is narrower than adding a parallel hook or self-learning-only workaround. The final check is that current-main integration still keeps terminal delivery single-shot.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against d98e984ec2ae.

Labels

Label changes:

  • add P1: User-aborted agent turns currently can end silently without the post-turn hooks and learning path that other terminal outcomes receive.
  • add merge-risk: 🚨 session-state: The PR changes terminal settlement and after-turn execution, which owns transcript/session-side effects for interrupted embedded runs.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides an after-fix real scratch-gateway run with a real provider, a concrete scheduled-review log, and the absence of the former lane-task abort error; focused automated regressions supplement that proof.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides an after-fix real scratch-gateway run with a real provider, a concrete scheduled-review log, and the absence of the former lane-task abort error; focused automated regressions supplement that proof.

Label justifications:

  • P1: User-aborted agent turns currently can end silently without the post-turn hooks and learning path that other terminal outcomes receive.
  • merge-risk: 🚨 session-state: The PR changes terminal settlement and after-turn execution, which owns transcript/session-side effects for interrupted embedded runs.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body provides an after-fix real scratch-gateway run with a real provider, a concrete scheduled-review log, and the absence of the former lane-task abort error; focused automated regressions supplement that proof.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body provides an after-fix real scratch-gateway run with a real provider, a concrete scheduled-review log, and the absence of the former lane-task abort error; focused automated regressions supplement that proof.

Evidence

What I checked:

  • Abort path is contained at lifecycle boundaries: The proposed branch catches a runner abort only when lifecycle state is already aborted before continuing to settlement, and permits aborts from the completion-required async-task wait to continue into after-turn processing. (src/agents/embedded-agent-runner/run/attempt-stream-finalize.ts:71, f117fc3f3934)
  • Agent-end classification preserves abort precedence: The branch suppresses the agent-end error field when the lifecycle is aborted, matching the documented distinction that an aborted event is unsuccessful but not a provider/prompt failure. (src/agents/embedded-agent-runner/run/attempt-after-turn.ts:184, f117fc3f3934)
  • Focused regressions cover both escapes: The branch adds coverage for an AbortError during completion-required async-task waiting, abort-raced agent-end classification, and the former prompt-release unwind path. (src/agents/embedded-agent-runner/run/attempt-phase-lifecycle.test.ts:103, f117fc3f3934)
  • Related merged consumer establishes the expected event contract: The merged interrupted-turn experience-review change depends on aborted turns reaching agent-end without an error, so this PR repairs its upstream delivery path rather than introducing a parallel feature. (src/skills/workshop/experience-review.ts, 095f30ae269d)
  • Real runtime evidence was provided: The PR body reports a before/after scratch-gateway run with a real OpenAI model: the prior abort produced a lane-task error and no scheduling; the proposed branch scheduled the aborted-turn review with no lane-task error. (f117fc3f3934)

Likely related people:

  • steipete: This person authored both the merged interrupted-turn experience-review work that consumes aborted agent-end events and the current lifecycle repair. (role: feature owner and recent area contributor; confidence: high; commits: 095f30ae269d, f117fc3f3934; files: src/agents/embedded-agent-runner/run/attempt-after-turn.ts, src/agents/embedded-agent-runner/run/attempt-stream-settle.ts, src/skills/workshop/experience-review.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Confirm the exact head against current main and let the remaining required checks complete.
  • Verify one aborted terminal event reaches after-turn without changing timeout precedence or causing duplicate hook delivery.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete merged commit 8fefe62 into main Jul 29, 2026
129 of 135 checks passed
@steipete
steipete deleted the steipete/aborted-run-agent-end branch July 29, 2026 21:44
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant