Repository navigation
feat(system-prompt): trim prompt noise, gate sections on tool availability, filter skills per agent - #25569
feat(system-prompt): trim prompt noise, gate sections on tool availability, filter skills per agent#25569rgr4y wants to merge 2 commits into
Conversation
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]>
| const filtersMatch = | ||
| agentFilter === undefined || | ||
| (snapshotFilter !== undefined && | ||
| snapshotFilter.length === agentFilter.length && | ||
| snapshotFilter.every((s, i) => s === agentFilter[i])); |
There was a problem hiding this 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:
| 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.
Additional Comments (1)
Consider deferring the unfiltered call or computing only once: Prompt To Fix With AIThis 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. |
There was a problem hiding this comment.
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) fromtoolNames === [](explicitly empty after filtering) - Wire per-agent skill filtering through
resolveSkillsPromptForRun()so agents withskills: ["coding-agent"]only see those skills - Add
messagetoDEFAULT_TOOL_ALLOWfor sandboxed messaging agents - Apply sandbox tool-policy filter in
/contextcommand 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 |
| // 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 : []), |
There was a problem hiding this comment.
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:
- Adjusting the condition to check if skillsPrompt is provided rather than gating on the read tool
- Updating the test and verifying that cron sessions don't need skills in minimal mode
- Adding a special case for when skillsPrompt is explicitly provided by the caller
| // 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 : []), |
|
This pull request has been automatically marked as stale due to inactivity. |
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
Summary
hasGateway && !isMinimal— subagents and sandboxed agents were receiving gateway CLI docs they cannot use## Tool Call Style,## Workspace,## Sandbox, and skills sections on actual tool availability — agents with no filesystem/shell tools no longer receive irrelevant workspace guidancetoolNames === undefined(legacy path, use fallback list) fromtoolNames === [](explicitly empty after policy filtering → show "No tools available")!isMinimal && availableTools.has("read")— agents withoutreadcannot load SKILL.md regardless of what the prompt saysresolveSkillsPromptForRun()now acceptsskillFilter?: string[];attempt.tsresolvesresolveAgentSkillsFilter(config, sessionAgentId)and passes it through so agents configured withskills: ["coding-agent"]only see those skills in their promptmessagetoDEFAULT_TOOL_ALLOW— sandboxed agents usingprofile: "messaging"were silently losing themessagetool at the sandbox policy stepresolveCommandsSystemPromptBundleso/contextestimates match what the model run actually receives for sandboxed sessionsTest plan
profile: "messaging",sandbox: {mode: "all"}) receivesmessagetool## Workspaceor## Sandboxsections in system promptskills: ["coding-agent"]only sees that skill in its prompt; other skills are filteredtoolNames: [](empty after policy) sees "No tools are available" rather than the hardcoded fallback list/contexttool-count estimate matches actual model run for sandboxed sessions🤖 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:
hasWorkspaceAccess,hasGateway, andavailableTools.has("read"). The "No tools are available" message correctly handlestoolNames === [](post-policy empty) vstoolNames === undefined(legacy Pi path).attempt.tsresolves an agent-specificskillFilterand passes it through toresolveSkillsPromptForRun(), which can rebuild from snapshotresolvedSkillsif the snapshot was built without this agent's filter.messagetool fix: Adds"message"toDEFAULT_TOOL_ALLOWso sandboxed messaging agents retain the tool./contextaccuracy:commands-system-prompt.tsnow applies sandbox tool policy filtering so/contextestimates match what the model run actually receives.Confidence Score: 4/5
messagetool,/contextmismatch). The two style suggestions (order-sensitive filter comparison, redundant unfiltered prompt computation) are minor and non-blocking. Theundefinedvs[]distinction fortoolNamesis correctly handled throughout. No security concerns introduced.src/agents/system-prompt.tshas 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!