Skip to content

fix(agents): keep agent_end preparation within the turn lifetime - #197947

Closed
ericcaiwx-star wants to merge 2 commits into
openclaw:mainfrom
ericcaiwx-star:codex/fix-197940-agent-end-lifetime
Closed

ericcaiwx-star wants to merge 2 commits into
openclaw:mainfrom
ericcaiwx-star:codex/fix-197940-agent-end-lifetime

Conversation

@ericcaiwx-star

@ericcaiwx-star ericcaiwx-star commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Closed in favor of #197990, opened by the issue author for the same fix. No merge or author-delivery completion is claimed for this Draft.

Related: #197940

What Problem This Solves

After an embedded turn calls dispatch_action, deferred terminal-message hydration can outlive the plugin invocation scope. The reply succeeds, but plugin agent_end handlers fail before executing.

User Impact

The candidate hydrates terminal messages within the awaited turn preparation owner, then starts handlers without waiting for their completion. Existing admission checks, no-subscriber behavior and best-effort failure handling remain intact.

Why This Change Was Made

Implementation head: 3e27ee9c18fd0dde03f0fb5539292df5583c0056. The repair stays in runAgentEndSideEffectsAsync. This Draft is being closed because the issue author is carrying the same repair in #197990.

Evidence

Actual named-base failure and exact-head post-fix proof were executed through ocpr prove:

  • Base 4efd662cc2c6f9f2afa8330ac813e48c9bd9b0a2: built isolated Gateway, existing local Ollama qwen3.6:35b-a3b, and two real plugin registrations. The chat-completions request returned HTTP 200 after tool_search → dispatch_action → session_status; neither terminal handler executed, and both logged Plugin invocation scope is closed. Required-hook assertion exited 1.
  • Head 3e27ee9c18fd0dde03f0fb5539292df5583c0056: the same shipped entry executed both terminal handlers exactly once, each receiving 8 messages, with zero closed-scope errors. A fresh direct-tool control executed both once with 5 messages. Both task-owned Gateways were terminated and joined. The formal post command exited 0.
  • Deterministic real PluginInstance / PluginInvocationScope regression: baseline failed with zero handler entries; candidate passed. Pending handlers do not block foreground completion. No-subscriber and rejected-hydration controls passed.
  • Six affected test files / 55 tests passed; changed-test type graph, targeted lint and format passed. Gateway qaRuntime build passed. Independent local P0–P2 review reported no actionable findings.
  • Coverage is the isolated local embedded Gateway/model path. Other providers, channels and operating systems were not exercised. Final hosted proof re-review, Ready CI and author WATCH gates were not completed.

Executed runtime driver commands (each starts the built Gateway using pnpm openclaw gateway run):

python3 /Users/ericstudio/repos/oc-pr-management/.tmp/openclaw-p0-p1/one-more-evidence/197940-runtime-preflight.py /Volumes/StudioData/Development/OpenClaw/worktrees/preflight-197940 require-hooks
python3 /Users/ericstudio/repos/oc-pr-management/.tmp/openclaw-p0-p1/one-more-evidence/197940-runtime-preflight.py /Volumes/StudioData/Development/OpenClaw/worktrees/preflight-197940 direct require-hooks

AI-assisted.

@clawsweeper

clawsweeper Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S and removed size: XS labels Nov 12, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 12, 2026
@clawsweeper

clawsweeper Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

This PR awaits terminal-message hydration before releasing the turn scope, while leaving plugin agent_end handlers in the background.

Example: An embedded turn runs tool_search, dispatch_action, and session_status before completing.

  • Before: The reply succeeds, but delayed hydration starts terminal handlers after scope release, producing "Plugin invocation scope is closed".
  • After: Hydration finishes and terminal handlers start before scope release, without waiting for those handlers to complete.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The source repair is focused and coherent, but execution evidence for the changed head remains pending.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🦐 gold shrimp (3/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Plugins miss their end-of-turn handlers after an otherwise successful embedded turn using dispatch_action.
Reason: Restoring terminal hooks has clear value and preserves the designed lifecycle contract without adding configuration or expanding scope.

Merge readiness

⛔ Blocked before merge - 1 item remains

Keep this PR: it addresses a current lifecycle defect with a focused repair in the existing owner. No definite introduced bug was found; the PR remains a draft.

Priority: P1
Reviewed head: 3e27ee9c18fd0dde03f0fb5539292df5583c0056

Before merge

  • Add real behavior proof - The captured Gateway evidence exercises the failing base through a real model and HTTP entry point, but the body explicitly leaves exact-head post-fix execution pending; the added regression test has no supplied execution result. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.

Findings

None.

Tests

  • Missing end-to-end proof: Provide exact-head Gateway output showing both terminal handlers execute after dispatch_action, plus base-red/head-green regression results, with credentials and private endpoints redacted.
Agent review details

How this fits together

The agent-end harness receives completed-turn facts and a deferred transcript loader, prepares plugin hook input, and dispatches handlers within the turn's plugin invocation lifetime.

flowchart TD
  A[Embedded turn completes] --> B[Prepare completed turn anchor]
  B --> C[Hydrate terminal messages]
  C --> D[Admit plugin handlers]
  D --> E[Release turn scope]
  D --> F[Background handlers finish]
Loading

Technical review

Best possible solution:

Keep terminal hydration within the existing awaited preparation owner and dispatch handlers before releasing the turn scope.

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

Current source exposes the delayed-hydration admission gap, matching the supplied named-base Gateway failure; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The patch repairs the owning lifetime boundary with no new state or API and preserves subscriber filtering, best-effort errors, and background handler execution.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against d30e45c3f48e.

Provenance checked

  • Awaited completed-turn preparation and background hook dispatch keeps the original intent (f9dfa9a: Prepare transcript anchors under retained turn custody while keeping interactive plugin hooks nonblocking.)
  • Completed-turn hook context keeps the original intent (7d69dfc: Resolve the final transcript anchor once under the turn lease and expose it to background plugin reviewers.)
  • Deferred transcript hydration and loader release keeps the original intent (42d9921: Release consumed loader closures before waiting for hooks so pending handlers retain hydrated messages without retaining completed attempt state.)

Testing

Proof path: none.

Security

None.

Evidence

What I checked:

  • Introduced repair: The pinned introduction diff moves deferred hydration into runAgentEndSideEffectsAsync before background dispatch, retaining subscriber checks and observational failure handling. (src/agents/harness/agent-end-side-effects.ts:178, 3e27ee9c18fd)
  • Current-main failure mechanism: Current main starts executeAgentHarnessAgentEndHook without awaiting it; that function awaits deferred messages before invoking handlers, allowing caller cleanup to close admission first. (src/agents/harness/lifecycle-hook-helpers.ts:76, d30e45c3f48e)
  • Production caller: Embedded finalization already awaits runAgentEndSideEffectsAsync and binds a loader that reads nested tool activity, so the repair belongs at this existing preparation boundary. (src/agents/embedded-agent-runner/run/attempt-finalize.ts:374, 3e27ee9c18fd)
  • Focused regression coverage: The added test uses real hook dispatch, wrapped plugin instances, and PluginInvocationScope to cover delayed hydration, admission before release, and background handler completion. (src/agents/harness/agent-end-side-effects.scope.test.ts:11, 3e27ee9c18fd)
  • Historical intent: The awaited preparation path originated in the transcript-anchor migration; later completed-turn work preserved background hooks. The loader-retention commit explicitly requires releasing loader closures while retaining hydrated messages. (src/agents/harness/agent-end-side-effects.ts:174, f9dfa9a36f15)
  • Captured runtime evidence: The supplied body, sourceRevision 1acf6db36449fef2a1c5110d6651f65b38940f12d1e5b67c1cb85a183e09fb19, records a named-base isolated Gateway failure after dispatch_action and a successful direct-tool control; it explicitly claims no post-fix success. (4efd662cc2c6)

PR surface

Source +15, Tests +105. Total +120 across 3 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 1 17 2 +15
Tests 2 105 0 +105
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 122 2 +120

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

No label changes.

Label justifications:

  • P1: The reported embedded workflow completes its reply while refusing every terminal plugin handler after dispatch_action, breaking plugin completion workflows.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 12, 2026, 6:23 PM ET / 23:23 UTC.

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

Labels

agents Agent runtime and tooling P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant