Skip to content

[plugin sdk] Harden finalize retry and run context cleanup#75600

Merged
jalehman merged 20 commits into
openclaw:mainfrom
electricsheephq:split/plugin-sdk-finalize-run-context
May 4, 2026
Merged

[plugin sdk] Harden finalize retry and run context cleanup#75600
jalehman merged 20 commits into
openclaw:mainfrom
electricsheephq:split/plugin-sdk-finalize-run-context

Conversation

@100yenadmin

Copy link
Copy Markdown
Contributor

Summary

  • Adds bounded before_agent_finalize retry handling so retry instructions are keyed by run id, capped by maxAttempts, and clearable per run.
  • Keeps plugin run context available until terminal agent-event subscriptions settle, while preventing late non-terminal handlers from resurrecting closed runs.
  • Wraps session/runtime/scheduler cleanup hooks in a bounded timeout and preserves live scheduler jobs during cleanup.

This replaces the consolidated #73384 / #74483 stack with a smaller fresh-main slice. The previous consolidated branch became extremely brittle under constant upstream churn: rebases repeatedly invalidated generated baselines, review threads, and CI assumptions, which made the PR harder to review than the underlying changes warranted. This PR files one hook/seam area at a time so maintainers can review, test, and merge each contract independently without re-opening the whole workflow stack.

Links

Non-goals

  • Does not claim to close the broader Control UI/native-page RFC scope.
  • Does not include session actions, attachments, scheduled turns, path facts, SessionEntry projection, or advanced workflow fixtures; those are separate fresh-main slices.

Test Plan

  • pnpm test src/plugins/hooks.before-agent-finalize.test.ts src/agents/harness/lifecycle-hook-helpers.test.ts src/plugins/contracts/host-hooks.contract.test.ts src/plugins/contracts/run-context-lifecycle.contract.test.ts
  • pnpm test src/plugins/contracts/host-hooks.contract.test.ts src/plugins/contracts/run-context-lifecycle.contract.test.ts
  • pnpm check:test-types
  • pnpm lint:core
  • git diff --check
  • npm run check:no-conflict-markers

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: L labels May 1, 2026
@clawsweeper

clawsweeper Bot commented May 1, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

Summary
The PR adds bounded before_agent_finalize retry metadata, terminal-aware plugin run-context cleanup, bounded plugin host cleanup timeouts, and related docs, changelog, Matrix test, onboarding type, and API-baseline updates.

Reproducibility: not applicable. This is an additive SDK PR rather than a user bug report; source inspection shows current main lacks the proposed retry and cleanup behavior, and the PR adds focused regression coverage.

Next step before merge
No narrow automation repair surfaced; the remaining action is maintainer review of the public SDK contract plus exact-head merge validation.

Security
Cleared: The diff changes TypeScript SDK/runtime cleanup logic, docs, changelog, tests, and a generated API hash; it adds no dependency, workflow, install, secret, or package-resolution surface.

Review details

Best possible solution:

Review and land this focused SDK slice after maintainer acceptance of the public retry metadata contract, keeping the broader workflow and recipe follow-ups in their separate PRs.

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

Not applicable. This is an additive SDK PR rather than a user bug report; source inspection shows current main lacks the proposed retry and cleanup behavior, and the PR adds focused regression coverage.

Is this the best way to solve the issue?

Yes, pending maintainer acceptance. The patch keeps the behavior in the plugin SDK host/runtime boundary, documents the new contract, and addresses prior review concerns with targeted tests instead of reopening the larger consolidated stack.

Acceptance criteria:

  • pnpm test src/plugins/hooks.before-agent-finalize.test.ts src/agents/harness/lifecycle-hook-helpers.test.ts src/plugins/contracts/host-hooks.contract.test.ts src/plugins/contracts/run-context-lifecycle.contract.test.ts
  • pnpm plugin-sdk:api:check
  • pnpm check:test-types
  • pnpm lint:core
  • git diff --check

What I checked:

  • PR state checked: Public GitHub API shows this PR is open, non-draft, mergeable, based on a90be47, and headed at a7ff7186f65968ca2219dc8d3c5de38d7c8249cb with 22 changed files. (a7ff7186f659)
  • Current main lacks retry metadata: On current main, PluginHookBeforeAgentFinalizeResult only has action and reason, and harness normalization only trims result.reason for revise decisions. (src/plugins/hook-types.ts:294, a90be474f441)
  • Retry implementation inspected: PR head adds retry metadata, per-run retry budget keys, max-attempt normalization, instruction-hash fallback keys, and retry-candidate iteration before returning a revise outcome. (src/agents/harness/lifecycle-hook-helpers.ts:140, a7ff7186f659)
  • Run-context cleanup implementation inspected: PR head waits for live terminal event handlers up to a bounded grace period, allows terminal handlers to see context while active, and blocks late resurrection after timeout or non-terminal closed-run writes. (src/plugins/host-hook-runtime.ts:120, a7ff7186f659)
  • Cleanup hardening inspected: PR head wraps session, runtime, and scheduler cleanup callbacks with the shared timeout helper, records config fallback failures, preserves restart jobs, and supports stale-cleanup guards. (src/plugins/host-hook-cleanup.ts:109, a7ff7186f659)
  • Stale API guard inspected: PR head adds registry retirement tracking and guards captured plugin API run-context mutations unless the registry is active or the plugin is currently activating. (src/plugins/registry.ts:2196, a7ff7186f659)

Likely related people:

  • 100yenadmin: Co-authored the merged generic plugin host-hook foundation that introduced the run-context, cleanup, scheduler, and event-subscription surfaces now being hardened. (role: introduced behavior; confidence: high; commits: 1adaa28dc86b; files: src/plugins/host-hook-runtime.ts, src/plugins/host-hook-cleanup.ts, src/plugins/registry.ts)
  • jalehman: The merged host-hook foundation commit records jalehman as co-author and reviewer, and the PR timeline shows active follow-up pushes on this cleanup-hardening branch. (role: reviewer and adjacent owner; confidence: high; commits: 1adaa28dc86b; files: src/plugins/host-hook-runtime.ts, src/plugins/host-hook-cleanup.ts, src/plugins/registry.ts)
  • vincentkoc: Introduced the current before_agent_finalize hook path and recently maintained plugin hook docs and timeout behavior around the same public SDK surface. (role: recent hook maintainer; confidence: high; commits: f3accc753cf2, 1d34564de9ba, 7d2d8732d0a1; files: src/agents/harness/lifecycle-hook-helpers.ts, src/plugins/hooks.ts, docs/plugins/hooks.md)
  • steipete: Recently maintained Matrix DM allowlist, owner-pin, and shared channel routing behavior adjacent to the PR's live Matrix dmScope fix. (role: recent Matrix/channel maintainer; confidence: medium; commits: 7e8d95b41360, 392897304cc5, ffe67e9cdc9e; files: extensions/matrix/src/matrix/monitor/handler.ts, extensions/matrix/src/matrix/monitor/handler.test.ts)

Remaining risk / open question:

  • The retry metadata is a public Plugin SDK contract, so maintainers still need to accept the semantics before merge.

Codex review notes: model gpt-5.5, reasoning high; reviewed against a90be474f441.

@100yenadmin
100yenadmin force-pushed the split/plugin-sdk-finalize-run-context branch from 16eb912 to b4bee82 Compare May 1, 2026 11:07
@openclaw-barnacle openclaw-barnacle Bot added the docs Improvements or additions to documentation label May 1, 2026
@100yenadmin
100yenadmin force-pushed the split/plugin-sdk-finalize-run-context branch from b4bee82 to f1737af Compare May 1, 2026 11:52
@100yenadmin
100yenadmin force-pushed the split/plugin-sdk-finalize-run-context branch 2 times, most recently from 2664e2b to 24434fb Compare May 1, 2026 12:21
@100yenadmin

Copy link
Copy Markdown
Contributor Author

Fixed in 24434fbb509. Added the missing public docs and changelog entry for the bounded before_agent_finalize retry contract, including instruction, idempotencyKey, and maxAttempts. Validation: pnpm test src/plugins/hooks.before-agent-finalize.test.ts src/agents/harness/lifecycle-hook-helpers.test.ts src/plugins/contracts/host-hooks.contract.test.ts src/plugins/contracts/run-context-lifecycle.contract.test.ts, pnpm plugin-sdk:api:check, pnpm docs:list, pnpm lint:core, git diff --check, npm run check:no-conflict-markers. pnpm check:test-types is currently blocked by unrelated fresh-main errors in src/commands/onboard-non-interactive.ts.

@100yenadmin
100yenadmin marked this pull request as ready for review May 1, 2026 12:39
Copilot AI review requested due to automatic review settings May 1, 2026 12:39
@100yenadmin
100yenadmin force-pushed the split/plugin-sdk-finalize-run-context branch from 24434fb to bb90aef Compare May 1, 2026 12:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens plugin SDK lifecycle behavior around before_agent_finalize retry handling and plugin run-context/session cleanup, aiming to prevent runaway retries and to avoid leaking or resurrecting per-run state while cleanup is in progress.

Changes:

  • Add bounded, per-run retry budgeting for before_agent_finalize via retry metadata (instruction/idempotencyKey/maxAttempts) and related tests/docs.
  • Keep plugin run context available until terminal agent-event subscriptions settle, while preventing late non-terminal handlers from writing into closed runs.
  • Add a bounded timeout wrapper for plugin cleanup hooks and refine scheduler-job cleanup to preserve live jobs and avoid stale cleanup callbacks.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/plugins/host-hook-runtime.ts Prevent closed-run run-context resurrection; wait/log for terminal subscriptions; improve scheduler cleanup controls.
src/plugins/host-hook-cleanup.ts Wrap cleanup hooks in timeout; allow cfg to default; preserve scheduler jobs and avoid stale cleanup callbacks.
src/plugins/host-hook-cleanup-timeout.ts New helper to apply a bounded timeout to cleanup callbacks.
src/plugins/host-hook-cleanup-timeout.test.ts Unit test for timeout timer unref() behavior.
src/plugins/hooks.ts Merge logic for before_agent_finalize now normalizes/propagates retry metadata.
src/plugins/hooks.before-agent-finalize.test.ts Adds coverage for merging revise decisions with empty/valid retry instructions.
src/plugins/hook-types.ts Extends PluginHookBeforeAgentFinalizeResult with optional retry metadata.
src/plugins/contracts/run-context-lifecycle.contract.test.ts Contract coverage for closed-run behavior, terminal subscription settling, and cleanup timeout behavior.
src/agents/harness/lifecycle-hook-helpers.ts Implements finalize-retry budgeting keyed by runId and bounded by maxAttempts.
src/agents/harness/lifecycle-hook-helpers.test.ts Tests retry budgeting and per-run clearing behavior.
docs/plugins/hooks.md Documents before_agent_finalize retry metadata for plugins.
docs/.generated/plugin-sdk-api-baseline.sha256 Updates SDK API baseline hashes for the new surface.
CHANGELOG.md Changelog entry for the new SDK retry/cleanup hardening.

Comment thread src/plugins/hooks.ts
Comment thread src/agents/harness/lifecycle-hook-helpers.ts
Comment thread docs/plugins/hooks.md
Comment thread src/plugins/host-hook-cleanup-timeout.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 24434fbb50

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/harness/lifecycle-hook-helpers.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c5d4ec3231

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/plugins/host-hook-runtime.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b813d2bc17

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/harness/lifecycle-hook-helpers.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Comment thread src/plugins/host-hook-runtime.ts
Comment thread src/plugins/contracts/run-context-lifecycle.contract.test.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Comment thread src/plugins/host-hook-cleanup.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5627a4a692

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/harness/lifecycle-hook-helpers.ts Outdated
@100yenadmin
100yenadmin requested a review from Copilot May 1, 2026 14:55
@jalehman
jalehman force-pushed the split/plugin-sdk-finalize-run-context branch from a7ff718 to ec58a62 Compare May 4, 2026 13:58
@jalehman
jalehman merged commit 8afc9ef into openclaw:main May 4, 2026
108 checks passed
@jalehman

jalehman commented May 4, 2026

Copy link
Copy Markdown
Contributor

Merged via squash.

Thanks @100yenadmin!

lxe pushed a commit to lxe/openclaw that referenced this pull request May 6, 2026
…75600)

Merged via squash.

Prepared head SHA: ec58a62
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
…75600)

Merged via squash.

Prepared head SHA: ec58a62
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…75600)

Merged via squash.

Prepared head SHA: ec58a62
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
…75600)

Merged via squash.

Prepared head SHA: ec58a62
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
…75600)

Merged via squash.

Prepared head SHA: ec58a62
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling channel: matrix Channel integration: matrix commands Command implementations docs Improvements or additions to documentation size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants