Skip to content

feat(system-prompt): trim prompt noise, gate sections on tool availability, filter skills per agent - #25569

Closed
rgr4y wants to merge 2 commits into
openclaw:mainfrom
rgr4y:feat/better-system-prompt-clean
Closed

rgr4y wants to merge 2 commits into
openclaw:mainfrom
rgr4y:feat/better-system-prompt-clean

Conversation

@rgr4y

@rgr4y rgr4y commented Feb 24, 2026 •

Copy link
Copy Markdown

Summary

  • Gate the "OpenClaw CLI Quick Reference" section on hasGateway && !isMinimal — subagents and sandboxed agents were receiving gateway CLI docs they cannot use
  • Gate ## Tool Call Style, ## Workspace, ## Sandbox, and skills sections on actual tool availability — agents with no filesystem/shell tools no longer receive irrelevant workspace guidance
  • Fix hardcoded tool fallback bug: distinguish toolNames === undefined (legacy path, use fallback list) from toolNames === [] (explicitly empty after policy filtering → show "No tools available")
  • Gate skills section on !isMinimal && availableTools.has("read") — agents without read cannot load SKILL.md regardless of what the prompt says
  • Wire per-agent skill filtering: resolveSkillsPromptForRun() now accepts skillFilter?: string[]; attempt.ts resolves resolveAgentSkillsFilter(config, sessionAgentId) and passes it through so agents configured with skills: ["coding-agent"] only see those skills in their prompt
  • Add message to DEFAULT_TOOL_ALLOW — sandboxed agents using profile: "messaging" were silently losing the message tool at the sandbox policy step
  • Apply sandbox tool-policy filter in resolveCommandsSystemPromptBundle so /context estimates match what the model run actually receives for sandboxed sessions

Test plan

  • Sandboxed messaging agent (profile: "messaging", sandbox: {mode: "all"}) receives message tool
  • Agent with no filesystem tools does not receive ## Workspace or ## Sandbox sections in system prompt
  • Agent with skills: ["coding-agent"] only sees that skill in its prompt; other skills are filtered
  • Agent with toolNames: [] (empty after policy) sees "No tools are available" rather than the hardcoded fallback list
  • /context tool-count estimate matches actual model run for sandboxed sessions
  • Subagents do not receive gateway CLI quick reference

🤖 Generated with Claude Code

Greptile Summary

This PR gates system prompt sections on actual tool availability, preventing subagents and sandboxed agents from receiving irrelevant instructions. Key changes:

  • System prompt gating: Workspace, Sandbox, CLI Quick Reference, Tool Call Style, and Skills sections are now conditionally included based on hasWorkspaceAccess, hasGateway, and availableTools.has("read"). The "No tools are available" message correctly handles toolNames === [] (post-policy empty) vs toolNames === undefined (legacy Pi path).
  • Per-agent skill filtering: attempt.ts resolves an agent-specific skillFilter and passes it through to resolveSkillsPromptForRun(), which can rebuild from snapshot resolvedSkills if the snapshot was built without this agent's filter.
  • Sandbox message tool fix: Adds "message" to DEFAULT_TOOL_ALLOW so sandboxed messaging agents retain the tool.
  • /context accuracy: commands-system-prompt.ts now applies sandbox tool policy filtering so /context estimates match what the model run actually receives.
  • Both inline comments found are style/performance suggestions, not correctness issues. The logic is sound and the gating conditions are well-chosen.

Confidence Score: 4/5

  • This PR is safe to merge — it reduces prompt noise for capability-limited agents without changing behavior for full-featured sessions.
  • Score of 4 reflects that the changes are logically sound, well-commented, and fix real bugs (missing message tool, /context mismatch). The two style suggestions (order-sensitive filter comparison, redundant unfiltered prompt computation) are minor and non-blocking. The undefined vs [] distinction for toolNames is correctly handled throughout. No security concerns introduced.
  • src/agents/system-prompt.ts has the most complex conditional nesting and should be verified against diverse agent configurations (no-tools, sandbox-only, full gateway).

Last reviewed commit: f7096cd

(5/5) You can turn off certain types of comments like style here!

rgr4y and others added 2 commits February 24, 2026 07:30
Gate sections that should not appear for all agents:

- "OpenClaw CLI Quick Reference" (gateway subcommands) is now only
  included when the agent has the gateway tool AND is in full mode.
  Subagents and sandboxed agents were receiving gateway CLI docs even
  though gateway is in SUBAGENT_TOOL_DENY_ALWAYS for those contexts.

- Skills section is now gated on !isMinimal && availableTools.has("read").
  Subagents receive targeted context via their spawn prompt and do not
  need to scan workspace skills. Agents without the read tool cannot
  load SKILL.md regardless of what the prompt says.

Wire per-agent skill filtering through to the prompt:

- resolveSkillsPromptForRun() now accepts skillFilter?: string[] and
  passes it through to buildWorkspaceSkillsPrompt/filterSkillEntries,
  which already supported filtering but had no caller passing the value.
- Snapshot path: re-uses pre-built prompt when filters match; falls back
  to rebuilding from resolvedSkills when the snapshot predates the filter.
- attempt.ts resolves resolveAgentSkillsFilter(config, sessionAgentId)
  after sessionAgentId is known and passes it as skillFilter, so an
  agent configured with skills: ["coding-agent", "summarize"] only sees
  those two skills in its system prompt.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…stimate

Two related fixes for sandboxed agents using profile: "messaging":

1. Add `message` to DEFAULT_TOOL_ALLOW in sandbox constants.
   The sandbox allowlist previously excluded `message` even though it is
   a core channel-communication tool and explicitly part of the "messaging"
   profile. Agents configured with profile: "messaging" and sandbox: {mode:
   "all"} would silently lose the message tool at the sandbox policy step.
   `sessions_send` / `sessions_list` / `sessions_history` were already
   present; `message` belongs alongside them.

2. Apply sandbox tool-policy filter in resolveCommandsSystemPromptBundle.
   The /context fresh-estimate path called createOpenClawCodingTools without
   a sandbox context (no docker available during a command), so the sandbox
   policy step was skipped — causing /context to overestimate the tool list
   for sandboxed sessions. Now, when sandboxRuntime.sandboxed is true, we
   apply resolveSandboxToolPolicyForAgent as a pure policy filter so the
   estimate matches what the actual model run receives.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Copilot AI review requested due to automatic review settings February 24, 2026 15:33
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M labels Feb 24, 2026

@greptile-apps greptile-apps 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.

5 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +538 to +542
const filtersMatch =
agentFilter === undefined ||
(snapshotFilter !== undefined &&
snapshotFilter.length === agentFilter.length &&
snapshotFilter.every((s, i) => s === agentFilter[i]));

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.

Order-sensitive filter comparison is fragile

This comparison uses positional index matching (snapshotFilter.every((s, i) => s === agentFilter[i])) rather than the existing matchesSkillFilter utility from ./filter.ts, which normalizes, deduplicates, and sorts before comparing. While both arrays currently flow through normalizeSkillFilter (which preserves config order), this creates a subtle coupling to ordering. If any future code path reorders the filter (e.g., deduplication, user editing), the comparison will spuriously fail and trigger an unnecessary rebuild from resolvedSkills.

Consider using the existing utility for consistency:

Suggested change
const filtersMatch =
agentFilter === undefined ||
(snapshotFilter !== undefined &&
snapshotFilter.length === agentFilter.length &&
snapshotFilter.every((s, i) => s === agentFilter[i]));
const filtersMatch =
agentFilter === undefined || matchesSkillFilter(snapshotFilter, agentFilter);
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/skills/workspace.ts
Line: 538-542

Comment:
**Order-sensitive filter comparison is fragile**

This comparison uses positional index matching (`snapshotFilter.every((s, i) => s === agentFilter[i])`) rather than the existing `matchesSkillFilter` utility from `./filter.ts`, which normalizes, deduplicates, and sorts before comparing. While both arrays currently flow through `normalizeSkillFilter` (which preserves config order), this creates a subtle coupling to ordering. If any future code path reorders the filter (e.g., deduplication, user editing), the comparison will spuriously fail and trigger an unnecessary rebuild from `resolvedSkills`.

Consider using the existing utility for consistency:

```suggestion
    const filtersMatch =
      agentFilter === undefined || matchesSkillFilter(snapshotFilter, agentFilter);
```

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps

greptile-apps Bot commented Feb 24, 2026

Copy link
Copy Markdown
Contributor
Additional Comments (1)

src/agents/pi-embedded-runner/run/attempt.ts
Redundant unfiltered skills prompt computation

skillsPromptUnfiltered is computed unconditionally, but when agentSkillFilter !== undefined (lines 370-379), a second call to resolveSkillsPromptForRun is made with the filter, and skillsPromptUnfiltered is discarded. Since resolveSkillsPromptForRun may rebuild from skill entries or snapshots, this is wasted work.

Consider deferring the unfiltered call or computing only once:

const agentSkillFilter = params.config && sessionAgentId
  ? resolveAgentSkillsFilter(params.config, sessionAgentId)
  : undefined;

const skillsPrompt = resolveSkillsPromptForRun({
  skillsSnapshot: params.skillsSnapshot,
  entries: shouldLoadSkillEntries ? skillEntries : undefined,
  config: params.config,
  workspaceDir: effectiveWorkspace,
  skillFilter: agentSkillFilter,
});
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/run/attempt.ts
Line: 337-342

Comment:
**Redundant unfiltered skills prompt computation**

`skillsPromptUnfiltered` is computed unconditionally, but when `agentSkillFilter !== undefined` (lines 370-379), a second call to `resolveSkillsPromptForRun` is made with the filter, and `skillsPromptUnfiltered` is discarded. Since `resolveSkillsPromptForRun` may rebuild from skill entries or snapshots, this is wasted work.

Consider deferring the unfiltered call or computing only once:

```
const agentSkillFilter = params.config && sessionAgentId
  ? resolveAgentSkillsFilter(params.config, sessionAgentId)
  : undefined;

const skillsPrompt = resolveSkillsPromptForRun({
  skillsSnapshot: params.skillsSnapshot,
  entries: shouldLoadSkillEntries ? skillEntries : undefined,
  config: params.config,
  workspaceDir: effectiveWorkspace,
  skillFilter: agentSkillFilter,
});
```

How can I resolve this? If you propose a fix, please make it concise.

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 optimizes the system prompt by conditionally gating sections based on actual tool availability and agent configuration. It fixes several issues where agents were receiving irrelevant documentation and resolves a bug with tool fallback logic when tools are explicitly filtered to an empty list.

Changes:

  • Gate system prompt sections (OpenClaw CLI, Tool Call Style, Workspace, Sandbox, Skills) on actual tool availability to reduce prompt noise for agents without relevant tools
  • Fix hardcoded tool fallback bug by distinguishing toolNames === undefined (legacy path with fallback) from toolNames === [] (explicitly empty after filtering)
  • Wire per-agent skill filtering through resolveSkillsPromptForRun() so agents with skills: ["coding-agent"] only see those skills
  • Add message to DEFAULT_TOOL_ALLOW for sandboxed messaging agents
  • Apply sandbox tool-policy filter in /context command to match actual run behavior

Reviewed changes

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

Show a summary per file
File Description
src/auto-reply/reply/commands-system-prompt.ts Apply sandbox tool policy filtering to match actual model run behavior for /context command estimates
src/agents/system-prompt.ts Gate sections (CLI reference, tool style, workspace, sandbox, skills) on actual tool availability; fix toolNames fallback bug
src/agents/skills/workspace.ts Add per-agent skill filtering support with skillFilter parameter and snapshot filter matching logic
src/agents/sandbox/constants.ts Add "message" to DEFAULT_TOOL_ALLOW for sandboxed messaging agents
src/agents/pi-embedded-runner/run/attempt.ts Wire agent skill filter resolution and add trace logging for system prompt debugging

Comment on lines +497 to +499
// Only show skills when the agent has the read tool (required to load SKILL.md)
// and is not a subagent (minimal mode) — subagents receive targeted context via spawn prompt
...(!isMinimal && availableTools.has("read") ? skillsSection : []),

Copilot AI Feb 24, 2026

Copy link

Choose a reason for hiding this comment

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

The new gating logic for skills (!isMinimal && availableTools.has("read")) will break the existing test in system-prompt.test.ts at line 112. The test "omits extended sections in minimal prompt mode" provides toolNames: ["message", "memory_search"] (which doesn't include "read") and expects skills to be shown with expect(prompt).toContain("## Skills"). With this change, skills will not be shown since availableTools.has("read") will be false in minimal mode without the read tool.

The comment at line 111 in the test states "Skills are included even in minimal mode when skillsPrompt is provided (cron sessions need them)". This suggests that cron sessions in minimal mode may rely on having skills available even without the read tool. The new logic would break this behavior.

Consider either:

  1. Adjusting the condition to check if skillsPrompt is provided rather than gating on the read tool
  2. Updating the test and verifying that cron sessions don't need skills in minimal mode
  3. Adding a special case for when skillsPrompt is explicitly provided by the caller
Suggested change
// Only show skills when the agent has the read tool (required to load SKILL.md)
// and is not a subagent (minimal mode) — subagents receive targeted context via spawn prompt
...(!isMinimal && availableTools.has("read") ? skillsSection : []),
// Show skills when:
// - skillsPrompt is explicitly provided (needed for some minimal/cron sessions), OR
// - the agent has the read tool (required to load SKILL.md) and is not a subagent (minimal mode)
...((params.skillsPrompt || (!isMinimal && availableTools.has("read"))) ? skillsSection : []),

Copilot uses AI. Check for mistakes.
@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 3, 2026
@Takhoffman
Takhoffman requested a review from a team as a code owner March 24, 2026 20:17
@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label Mar 25, 2026
@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 30, 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.

@openclaw-barnacle openclaw-barnacle Bot closed this Apr 3, 2026
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: M stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants