Repository navigation
fix: keep speaker network policy out of shared system prompt - #11055
pcbeingused333 wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/astr_main_agent/computer_use.py" line_range="275-278" />
<code_context>
local_policy.allow_execution
and not local_policy.allow_network
and LOCAL_NETWORK_POLICY_NOTICE not in (req.system_prompt or "")
+ and not any(
+ isinstance(part, TextPart) and LOCAL_NETWORK_POLICY_NOTICE in part.text
+ for part in req.extra_user_content_parts
+ )
):
- runtime_prompt += f"\n## Execution policy\n{LOCAL_NETWORK_POLICY_NOTICE}\n"
</code_context>
<issue_to_address>
**Quoted text suppresses policy notice**
When a user-supplied text part contains `LOCAL_NETWORK_POLICY_NOTICE` along with other instructions, `any` treats every text part as an existing policy notice when it contains the notice string, including user-supplied or quoted content with additional contradictory instructions. The intended reminder is then omitted, so the model can attempt disallowed network operations and encounter avoidable denials.
Deduplicate using an exact match or an explicit marker for the generated notice rather than a substring search across arbitrary text parts.
</issue_to_address>Linked issues
#11054 [Bug] 「本地执行不可联网」提示按发言人权限 system prompt 中段,群聊共享会话里如果联网权限不同则会导致整轮缓存失效 · 2 of 2 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Keep the speaker-dependent local network notice out of the shared system prompt so alternating speakers preserve the reusable prompt prefix. | ✅ Met | apply_computer_use adds the notice as a temporary TextPart in the current request’s user content; the regression test checks that administrator and member system prompts remain equal. |
| 2 | Continue giving the notice to speakers who can execute locally but are not allowed network access. | ✅ Met | The existing permission condition is retained, and the regression test checks that a restricted member receives one temporary notice, excluded from saved history. |
Sourcery assessment
Needs a human reviewer. The network restriction is now supplied as temporary user content rather than part of the runtime prompt, so a model could give it less weight and invoke execution tools in ways the speaker’s policy forbids. Reverting restores the prior placement, but any network request or data exposure that already occurred cannot be undone.
| and not any( | ||
| isinstance(part, TextPart) and LOCAL_NETWORK_POLICY_NOTICE in part.text | ||
| for part in req.extra_user_content_parts | ||
| ) |
There was a problem hiding this comment.
⚪ Low · Quoted text suppresses policy notice
When a user-supplied text part contains LOCAL_NETWORK_POLICY_NOTICE along with other instructions, any treats every text part as an existing policy notice when it contains the notice string, including user-supplied or quoted content with additional contradictory instructions. The intended reminder is then omitted, so the model can attempt disallowed network operations and encounter avoidable denials.
Deduplicate using an exact match or an explicit marker for the generated notice rather than a substring search across arbitrary text parts.
Prompt for AI agents
In `astrbot/core/astr_main_agent/computer_use.py` at lines 275-278:
**Quoted text suppresses policy notice**
When a user-supplied text part contains `LOCAL_NETWORK_POLICY_NOTICE` along with other instructions, `any` treats every text part as an existing policy notice when it contains the notice string, including user-supplied or quoted content with additional contradictory instructions. The intended reminder is then omitted, so the model can attempt disallowed network operations and encounter avoidable denials.
Deduplicate using an exact match or an explicit marker for the generated notice rather than a substring search across arbitrary text parts.|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Linked issuesChecked at commit 74e3655 #11054 [Bug] 「本地执行不可联网」提示按发言人权限 system prompt 中段,群聊共享会话里如果联网权限不同则会导致整轮缓存失效 · 1 of 1 met
|
Fixes #11054
Modifications / 改动点
The local network policy notice depends on the current speaker's permissions. Adding it to the system prompt changes the shared conversation prefix whenever administrators and members alternate, invalidating the provider's prefix cache for the subsequent history.
Move the notice to a temporary TextPart in the current user content, and check existing content parts before inserting it again. Runtime instructions and permission enforcement stay intact. This preserves the intent of #10134: the notice is not repeated in tool results and is excluded from saved history. The next turn can still lose cache reuse from the previous turn's temporary part onward, but the shared system prompt and preceding history remain stable.
Screenshots or Test Results / 运行截图或测试结果
Run
python -m pytest tests/unit/test_astr_main_agent.py tests/unit/test_star_context.py -q -p no:cacheproviderwith the existing Python 3.12 test environment.With the updated tests and computer_use.py restored from master (26fdd1b), the command produced 4 failed and 215 passed. The new regression reaches the administrator/member system_prompt equality assertion and fails because master inserts the member network policy into the system prompt. With the fix restored, all 219 tests pass.
Coverage checks identical administrator/member system prompts, one temporary member notice, persistence filtering, and duplicate prevention on request rebuild. Ruff checks, Ruff formatting checks, and git diff --check pass for the changed files.
Checklist / 检查清单
docs/zhanddocs/enagainst the changed navigation, page structure, and labels, and updated them in this PR (or explained why no documentation update is needed): backend prompt change, no WebUI entry points change, so no documentation update is needed.This change was prepared with AI assistance (Codex).
Summary by Sourcery
Move local network policy notices into temporary per-request user content while preserving runtime enforcement and shared prompt stability.
Bug Fixes:
Enhancements:
Tests: