Skip to content

fix: keep speaker network policy out of shared system prompt - #11055

Open
pcbeingused333 wants to merge 4 commits into
AstrBotDevs:masterfrom
pcbeingused333:fix/c2059-network-policy
Open

pcbeingused333 wants to merge 4 commits into
AstrBotDevs:masterfrom
pcbeingused333:fix/c2059-network-policy

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

Run python -m pytest tests/unit/test_astr_main_agent.py tests/unit/test_star_context.py -q -p no:cacheprovider with 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 / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc. (not applicable: bug fix, no new feature)
  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 📚 I checked the affected WebUI instructions and screenshots in docs/zh and docs/en against 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.
  • 🤓 I have ensured that no new dependencies are introduced.
  • 😮 My changes do not introduce malicious code.

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:

  • Keep speaker-specific local network policy notices out of the shared system prompt to preserve prompt-prefix cache reuse when permissions change between speakers.

Enhancements:

  • Represent the network policy notice as a temporary user-content part that is excluded from saved history and is not duplicated when requests are rebuilt.

Tests:

  • Add regression coverage for stable system prompts, temporary notice insertion, persistence filtering, and duplicate prevention.

@sourcery-ai sourcery-ai 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.

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.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +275 to +278
and not any(
isinstance(part, TextPart) and LOCAL_NETWORK_POLICY_NOTICE in part.text
for part in req.extra_user_content_parts
)

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.

⚪ 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-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@sourcery-ai

sourcery-ai Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit 74e3655

#11054 [Bug] 「本地执行不可联网」提示按发言人权限 system prompt 中段,群聊共享会话里如果联网权限不同则会导致整轮缓存失效 · 1 of 1 met

# Requirement Status Notes
1 Keep the speaker-dependent local network notice out of the shared system prompt so permission changes do not invalidate the shared conversation prefix. ✅ Met The notice is added as a temporary part of the current user content, and the regression test checks that administrator and member requests retain the same system prompt.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

2 participants