Skip to content

[Feature]: Extend before_agent_start hook context with Model, Tools, and Identity fields#14873

Closed
akv2011 wants to merge 4 commits intoopenclaw:mainfrom
akv2011:main
Closed

[Feature]: Extend before_agent_start hook context with Model, Tools, and Identity fields#14873
akv2011 wants to merge 4 commits intoopenclaw:mainfrom
akv2011:main

Conversation

@akv2011
Copy link
Copy Markdown

@akv2011 akv2011 commented Feb 12, 2026

Summary

The current before_agent_start plugin hook provides a sanitized context (prompt, messages), which is insufficient for plugins that require "Agentic" capabilities. Specifically, external plugins cannot access the LLM instance to perform intent planning, nor can they see the tools available to the agent, or robustly identify the user (missing senderId, senderE164). This prevents the creation of advanced security and governance plugins that need to verify intent before execution.

Proposed solution

Extend PluginHookAgentContext and PluginHookBeforeAgentStartEvent to expose the following fields, which are already available in attempt.ts:

In ctx (PluginHookAgentContext):

Model: The Model instance (enables the plugin to call the LLM for planning/verification).
modelRegistry: For model configuration access.

runId: To correlate the start event with subsequent tool calls (for caching plans).
senderId, senderE164, senderUsername: For identity verification and audit logging.
messageChannel

accountId: For full context awareness.
In event (PluginHookBeforeAgentStartEvent):

Tools: The list of tools available for this run (crucial for verifying if a user's intent matches allowed tools).

Alternatives considered

Building a custom agent: This bypasses the plugin system entirely and fragments the ecosystem.

Using before_tool_call only: This is too late for "Intent Planning". By the time a tool is called, the "plan" has already been decided by the LLM. Security plugins need to verify the plan against policy before any tool is even attempted.

Additional context

I have a working implementation in a fork that successfully powers an "ArmorIQ" security plugin. This plugin uses the exposed ctx.model to generate a secure execution plan and event.tools to validate it, preventing prompt injection and unauthorized actions.

Greptile Overview

Greptile Summary

This PR extends the before_agent_start plugin hook payload so plugins can do richer pre-run governance/intent checks. Specifically:

  • src/agents/pi-embedded-runner/run/attempt.ts now passes a tools summary on the event and adds additional fields (run/account/sender identity, channel, model, modelRegistry) on the hook context when running runBeforeAgentStart.
  • src/plugins/types.ts updates the public hook type definitions to include these new fields.

These changes integrate with the existing hook runner in src/plugins/hooks.ts (sequential modifying hook for before_agent_start) and are surfaced to external plugins via the re-export in src/plugin-sdk/index.ts.

Confidence Score: 3/5

  • This PR is likely safe to merge after addressing the plugin-SDK type dependency and tool naming ambiguity.
  • Core runtime behavior changes are limited to passing additional context into an existing hook call, but the hook type exports are part of the public plugin SDK and may break external plugin builds if the new pi-* types aren’t resolvable. Additionally, the emitted tool list may be unreliable due to duplicate fallback names.
  • src/plugins/types.ts, src/agents/pi-embedded-runner/run/attempt.ts

(4/5) You can add custom instructions or style guidelines for the agent here!

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Feb 12, 2026
Copy link
Copy Markdown
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.

2 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment thread src/plugins/types.ts Outdated
Comment thread src/agents/pi-embedded-runner/run/attempt.ts Outdated
@plc
Copy link
Copy Markdown

plc commented Feb 16, 2026

hope this gets merged! i am working on a skill to provide intelligent model selection (eg: use haiku for simple tasks, opus for coding, sonnet for writing) and it's reliant on before_agent_start having the ability to override models, this PR would be 50% of the functionality i require, and I'd like to submit a follow up PR to provide the rest of the implementation.

this would provide significant token efficiency improvements and is well worth the relatively small change IMHO. it'd open up all manner of innovation about model selection.

@akv2011
Copy link
Copy Markdown
Author

akv2011 commented Feb 16, 2026

hope this gets merged! i am working on a skill to provide intelligent model selection (eg: use haiku for simple tasks, opus for coding, sonnet for writing) and it's reliant on before_agent_start having the ability to override models, this PR would be 50% of the functionality i require, and I'd like to submit a follow up PR to provide the rest of the implementation.

this would provide significant token efficiency improvements and is well worth the relatively small change IMHO. it'd open up all manner of innovation about model selection.

Thanks man i don't know why they removed it these were already there in previous version even more fields

Ps: Let me know if you want extra hands on ur pr

@plc
Copy link
Copy Markdown

plc commented Feb 17, 2026

@steipete wdyt about this stuff?

@akv2011
Copy link
Copy Markdown
Author

akv2011 commented Feb 17, 2026

@ngutman Any inputs for this

plc added a commit to plc/ModelSmartSelect that referenced this pull request Feb 17, 2026
References openclaw/openclaw#14873 as prerequisite.
Includes exact code diffs, auth re-resolution notes, and test cases.
@tygent0
Copy link
Copy Markdown

tygent0 commented Mar 10, 2026

Would love for this PR to make it in.

akv2011 added 3 commits March 11, 2026 04:12
…r info

Extend PluginHookAgentContext with messageChannel, accountId, senderId,
senderName, senderUsername, senderE164, runId, model, and modelRegistry
fields so plugins can access the full runtime context during the
before_agent_start hook.

Also extend PluginHookBeforeAgentStartEvent with a tools array so
plugins can inspect available tool definitions.

These additions are fully backward-compatible: all new fields are
optional, so existing plugins continue to work unchanged.

Resolves the gap where plugins needing model access or sender identity
(e.g. for intent planning or per-user policy) had no way to obtain
this information from the hook context.
- Replace Model<Api>/ModelRegistry with 'unknown' in PluginHookAgentContext
  to avoid forcing external plugins to depend on @mariozechner/pi-ai and
  @mariozechner/pi-coding-agent at type-check time. Plugins that need
  the concrete type can cast internally.

- Filter out unnamed tools instead of using 'tool' fallback, preventing
  ambiguous duplicate names that would break allowlist-based governance
  plugins.
…gent_start, remove legacy pre-attempt hook call
@openclaw-barnacle
Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Mar 16, 2026
@openclaw-barnacle
Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #pr-thunderdome-dangerzone on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

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 stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants