Skip to content

fix(plugins): preserve mutable context in plugin hook wrapper - #75249

Closed
hclsys wants to merge 3 commits into
openclaw:mainfrom
hclsys:fix/75245-plugin-hook-mutable-context
Closed

hclsys wants to merge 3 commits into
openclaw:mainfrom
hclsys:fix/75245-plugin-hook-mutable-context

Conversation

@hclsys

@hclsys hclsys commented Apr 30, 2026

Copy link
Copy Markdown

Summary

registerHook() wraps plugin handlers by shallow-cloning event.context to inject pluginConfig:

// Before (broken)
return handler({ ...evt, context: { ...evt.context, pluginConfig } });

This silently discards mutations that mutable hook contracts require. agent:bootstrap is the primary victim: core reads event.context.bootstrapFiles after triggerInternalHook returns, expecting plugins to have mutated it. Instead, the plugin's assignment lands on the discarded clone.

The fix passes the original event.context object and temporarily augments it with pluginConfig via a try/finally cleanup:

const context = evt.context ?? {};
const hadPluginConfig = Object.prototype.hasOwnProperty.call(context, "pluginConfig");
const previousPluginConfig = context.pluginConfig;
context.pluginConfig = pluginConfig;
try {
  return await handler({ ...evt, context });
} finally {
  if (hadPluginConfig) context.pluginConfig = previousPluginConfig;
  else delete context.pluginConfig;
}

Invariants preserved:

Pre-implement audit

  1. Existing-helper check: pluginConfig injection pattern is unique to this wrapper — no shared helper to reuse.
  2. Shared-helper caller check: registerHook closure is only called from within createPluginRegistry and its createApi path — no external caller contract affected.
  3. Rival PR scan: no open PRs targeting registerHook wrapper or wrappedHandler context mutation.

Test plan

  • src/agents/bootstrap-hooks.test.ts — 3 tests pass (1 existing + 2 regression)
  • src/plugins/loader.test.ts — 131/131 pass (including "injects plugin config into internal hook event context" which verifies pluginConfig is still injected)

Fixes #75245.

🤖 Generated with Claude Code

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Apr 30, 2026
@clawsweeper

clawsweeper Bot commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes:

The PR changes the plugin registerHook wrapper to pass the original event context with temporary pluginConfig injection, adds agent:bootstrap regression tests, and adds a changelog entry for the plugin hook fix.

Maintainer follow-up before merge:

No repair lane is needed: the source PR now contains the code fix, regression tests, and changelog; remaining work is maintainer review, CI, and resolving mergeability if GitHub still reports conflicts.

Security review:

Security review cleared: The diff only changes in-process plugin hook context handling, tests, and changelog text; it does not add dependency, workflow, secret, package, or supply-chain surface.

Review details

Best possible solution:

Land this PR or an equivalent narrow patch after mergeability and CI are green, preserving mutable hook-context behavior while restoring/removing per-handler pluginConfig after each plugin hook call.

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

Yes. Register an agent:bootstrap hook through api.registerHook that assigns event.context.bootstrapFiles, then call applyBootstrapHookOverrides; current main sends that assignment to the cloned wrapper context while core reads the original event context after dispatch.

Is this the best way to solve the issue?

Yes. Passing the original context and restoring pluginConfig in finally is the narrowest compatible repair for the existing mutable hook contract; an immutable return-value API would be a larger plugin SDK change.

What I checked:

  • Current main clones plugin hook context: registerHook wraps plugin handlers with context: { ...evt.context, pluginConfig }, so assignments to event.context.bootstrapFiles inside the handler affect only the clone. (src/plugins/registry.ts:555, df4136018ecb)
  • Bootstrap consumer reads original context after dispatch: applyBootstrapHookOverrides creates one event, awaits triggerInternalHook(event), then reads event.context.bootstrapFiles from the original event object. (src/agents/bootstrap-hooks.ts:27, df4136018ecb)
  • Hook docs define bootstrapFiles as mutable: The public automation hooks doc describes agent:bootstrap context.bootstrapFiles as a mutable array, supporting the issue’s expected mutation contract. Public docs: docs/automation/hooks.md. (docs/automation/hooks.md:125, df4136018ecb)
  • Existing cleanup test protects pluginConfig leak behavior: The loader test verifies a plugin hook can read event.context.pluginConfig and that the shared event context is {} after dispatch. (src/plugins/loader.test.ts:3712, df4136018ecb)
  • PR patch preserves original context and restores pluginConfig: The latest PR patch reuses evt.context, assigns context.pluginConfig for the handler call, and restores or deletes it in a finally block. (src/plugins/registry.ts:552, aadd7e4ca408)
  • PR patch adds regression tests and changelog: The latest PR patch adds plugin-wrapper bootstrap mutation coverage and a single-line CHANGELOG.md ### Fixes entry credited to the contributor. (src/agents/bootstrap-hooks.test.ts:46, aadd7e4ca408)

Likely related people:

  • Ayumi Server: Authored the commits that first injected pluginConfig into api.registerHook events and then changed the wrapper to shallow-copy context for per-handler isolation. (role: introduced wrapper behavior; confidence: high; commits: ed0b098d7596, c1187109c8fb; files: src/plugins/registry.ts)
  • steipete: Introduced the agent:bootstrap mutable hook path and appears in recent local/API history for the plugin registry and bootstrap hook files. (role: feature-history owner / recent maintainer; confidence: medium; commits: ad3c12a43a70, 36e687edf0ff; files: src/agents/bootstrap-hooks.ts, src/hooks/internal-hooks.ts, src/plugins/registry.ts)
  • 100yenadmin: Authored the broad generic plugin host-hook contract work that heavily touched src/plugins/registry.ts, hook docs, and SDK hook surfaces; also reported the linked regression with detailed reproduction evidence. (role: adjacent plugin hook contributor; confidence: medium; commits: 1adaa28dc86b; files: src/plugins/registry.ts, docs/plugins/hooks.md, docs/plugins/sdk-overview.md)

Remaining risk / open question:

  • Provided PR metadata reports mergeable: false for head aadd7e4ca408f0f68ce1165e206a22042f48e068, so landing may still require a rebase or conflict resolution even though the patch direction is correct.

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

HCL and others added 3 commits May 1, 2026 21:59
…er (openclaw#75245)

The registerHook wrapper shallow-cloned event.context to inject pluginConfig,
silently discarding mutations like event.context.bootstrapFiles = updated that
mutable hooks (agent:bootstrap) require. Pass the original context object and
temporarily augment it with pluginConfig via a try/finally cleanup instead.

Regression test uses createPluginRegistry({ activateGlobalSideEffects: true })
to exercise the real wrapper path: hook fires, bootstrapFiles replacement
survives, pluginConfig does not leak after handler returns.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
The `as Record<string, unknown>` casts on `context` were flagged by oxlint
as unnecessary — `evt.context` is already typed as `Record<string, unknown>`
in InternalHookEvent. Access pluginConfig directly without casts.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@hclsys

This comment was marked as low quality.

@hclsys

This comment was marked as low quality.

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

Labels

agents Agent runtime and tooling size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

registerHook wrapper drops mutable context changes for agent:bootstrap hooks

1 participant