Skip to content

feat(plugins): add before_agent_start model/provider overrides before resolution#18568

Merged
steipete merged 3 commits intoopenclaw:mainfrom
natefikru:feat/plugin-model-override
Feb 16, 2026
Merged

feat(plugins): add before_agent_start model/provider overrides before resolution#18568
steipete merged 3 commits intoopenclaw:mainfrom
natefikru:feat/plugin-model-override

Conversation

@natefikru
Copy link
Contributor

@natefikru natefikru commented Feb 16, 2026

Summary

This PR extends the before_agent_start plugin hook so plugins can deterministically select provider/model before model resolution.

Clawforce needs this to enforce policy-driven routing (for example, forcing local models for sensitive prompts) at the correct point in the run pipeline.

What changed

  • Added modelOverride and providerOverride to PluginHookBeforeAgentStartResult.
  • Updated hook merge behavior so override precedence is stable and priority-safe.
  • Invoked before_agent_start earlier in run.ts so overrides are applied before resolveModel.
  • Passed earlyHookResult into attempt.ts to avoid double-firing before_agent_start.
  • Added focused test coverage for:
    • hook merger behavior,
    • early-invocation wiring,
    • graceful degradation when hooks fail,
    • compatibility with existing prependContext/systemPrompt behavior.

Scope boundaries

  • No changes to auth/token handling.
  • No new network integrations.
  • No changes to other hook contracts (message_sending, tool_result_persist, agent_end).
  • No UI changes.

Why this is needed

Previously, before_agent_start could enrich prompt context but could not reliably control provider/model selection at the point where selection actually occurs. This made hard policy routing non-deterministic. The new override contract closes that gap.

Test plan

  • pnpm vitest run src/plugins/hooks.before-agent-start.test.ts src/plugins/hooks.model-override-wiring.test.ts
  • Verify all tests pass (19/19).
  • Confirm override precedence behavior is explicit and covered by tests.

Compatibility

  • Backward compatible for existing plugins that only return prependContext and/or systemPrompt.
  • No config or migration changes required.

…rt hook

Enable plugins to override the model and provider for agent runs by
returning modelOverride/providerOverride from the before_agent_start
hook. The hook is now invoked early in run.ts (before resolveModel)
so overrides take effect. The result is passed to attempt.ts via
earlyHookResult to prevent double-firing.

This enables security-critical use cases like routing PII-containing
prompts to local models instead of cloud providers.
Layer 1: Hook merger tests verify modelOverride/providerOverride are
correctly propagated through the before_agent_start merger with
priority ordering, backward compatibility, and field isolation.

Layer 2: Pipeline wiring tests verify the earlyHookResult passthrough
contract between run.ts and attempt.ts, graceful error degradation,
and that overrides correctly modify provider/model variables.

19 tests total across 2 test files.
@openclaw-barnacle openclaw-barnacle bot added agents Agent runtime and tooling size: L labels Feb 16, 2026
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

7 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Make before_agent_start override merging preserve the first defined
model/provider override so higher-priority hooks cannot be overwritten by
lower-priority handlers, and align the corresponding test title and
expectation with the intended precedence behavior.

Co-authored-by: Cursor <[email protected]>
@natefikru natefikru changed the title Feat/plugin model override fix(plugins): enforce high-priority before_agent_start model/provider overrides Feb 16, 2026
@natefikru natefikru changed the title fix(plugins): enforce high-priority before_agent_start model/provider overrides feat(plugins): add before_agent_start model/provider overrides before resolution Feb 16, 2026
@steipete steipete merged commit 6d31d1e into openclaw:main Feb 16, 2026
23 checks passed
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: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments