Skip to content

fix(agent): append tool-policy boundary after the cacheable prompt prefix - #5752

Closed
gac0812 wants to merge 2 commits into
tinyhumansai:mainfrom
gac0812:fix/tool-policy-boundary-prefix-cache
Closed

fix(agent): append tool-policy boundary after the cacheable prompt prefix#5752
gac0812 wants to merge 2 commits into
tinyhumansai:mainfrom
gac0812:fix/tool-policy-boundary-prefix-cache

Conversation

@gac0812

@gac0812 gac0812 commented Aug 24, 2026

Copy link
Copy Markdown

Summary

  • Append the session-varying ## Tool Policy Boundary block after the assembled system prompt, instead of prepending it.
  • This keeps the stable prefix (safety preamble, tool catalogue, workspace) byte-identical across agents/channels so the inference backend can reuse its prefix cache.
  • Matches the existing DateTimeSection convention: volatile content belongs at the tail, not at offset 0.
  • Regression test now asserts the boundary is present, is not the prompt prefix, and is the last markdown section.

Problem

build_system_prompt prepended render_tool_policy_boundary(...) in front of the fully assembled system prompt. That block is session-scoped (agent, channel, entrypoint, allowed tools). Putting it first moves the first diverging byte to offset 0 and defeats prefix caching for everything behind it. It also made every restricted agent open with the same heading instead of its persona.

Tracked in #5704. No existing open PR covers this.

Solution

Change the join from {boundary}\n\n{prompt} to {prompt}\n\n{boundary}. The model still sees the full boundary; only placement changes. The test helper make_agent_with_builder_and_dispatcher now calls install_for_tests() so the existing prompt test can run in isolation (same seam wiring as make_agent).

Submission Checklist

  • Tests added or updated (happy path + at least one failure / edge case) per Testing Strategy
  • Diff coverage ≥ 80% — new assertions cover the changed format string; helper wiring is the same path make_agent already used
  • Coverage matrix updated — N/A: behaviour-only change
  • All affected feature IDs from the matrix are listed in the PR description under ## Related — N/A: no coverage-matrix feature ID
  • No new external network dependencies introduced (mock backend used per Testing Strategy)
  • Manual smoke checklist updated if this touches release-cut surfaces (docs/RELEASE-MANUAL-SMOKE.md) — N/A: prompt assembly only
  • Linked issue closed via Closes #NNN in the ## Related section

Impact

  • Runtime: desktop/CLI agent turns. Restricted sessions still get the boundary; it is now after the stable prefix.
  • Performance: prefix cache can hit across agents/channels that share the stable sections.
  • No security, migration, or wire-format change.

Related


AI Authored PR Metadata (required for Codex/Linear PRs)

Linear Issue

  • Key: N/A
  • URL: N/A

Commit & Branch

  • Branch: fix/tool-policy-boundary-prefix-cache
  • Commit SHA: 268eb8a5113975f8e9f8ad70d80b8592dcfcaa74

Validation Run

  • pnpm --filter openhuman-app format:check — N/A: Rust-only change
  • pnpm typecheck — N/A: Rust-only change
  • Focused tests: cargo test --lib openhuman::agent::harness::session::turn::tests::system_prompt_includes_tool_policy_boundary -- --exact (pass); sibling set_agent_definition_name_refreshes_tool_policy_identity (pass)
  • Rust fmt/check (if changed): cargo fmt on the touched files
  • Tauri fmt/check (if changed): N/A: Tauri crate not touched

Validation Blocked

  • command: N/A
  • error: N/A
  • impact: N/A

Behavior Changes

  • Intended behavior change: tool-policy boundary is appended after the stable system prompt instead of prepended
  • User-visible effect: none in the UI; restricted agent turns keep the same policy text, later in the system prompt

Parity Contract

  • Legacy behavior preserved: unrestricted sessions still omit the boundary; allowed/restricted tool listing unchanged
  • Guard/fallback/dispatch parity checks: renderer still returns None when there are no restrictions

Duplicate / Superseded PR Handling

Summary by CodeRabbit

  • Improvements

    • Updated agent instructions so tool-policy boundaries appear after the standard system prompt, improving consistency and clarity in prompt handling.
    • Strengthened validation to ensure policy boundaries remain in the intended position and are not treated as the prompt prefix or followed by unrelated sections.
    • Improved configuration validation for runtime proxy overrides, including empty overlays and state preservation.
  • Documentation

    • Updated integration guidance to reflect the revised policy-boundary rendering behavior.

…efix

The session-varying Tool Policy Boundary was prepended to the system
prompt, so the first diverging byte sat at offset 0 and the inference
backend could not reuse the stable prefix (safety preamble, tool
catalogue, workspace). Append it instead, matching DateTimeSection
placement. Closes tinyhumansai#5704.
@gac0812
gac0812 requested a review from a team August 24, 2026 13:01

@tinysweeper tinysweeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tinysweeper found nothing blocking. Approving.

$0.0000 · 0 in / 0 out · 188 embedded · openrouter/openai/text-embedding-3-small

@tinysweeper

tinysweeper Bot commented Aug 24, 2026

Copy link
Copy Markdown

How this change flows

2 changed behaviours across 14 relationships. 5 surrounding behaviours are shown (60 graph nodes walked). 39 further behaviours left out to keep the diagram readable.

flowchart LR
  n0["make_agent_with_builder_and_dispatcher<br/>changed"]:::changed
  n1["system_prompt_includes_tool_policy_boundary<br/>changed"]:::changed
  n2["vec"]:::impacted
  n3["make_agent_with_builder"]:::impacted
  n4["openhuman"]:::impacted
  n5["AgentConfig"]:::impacted
  n6["ContextConfig"]:::impacted
  n0 -->|uses| n4
  n0 -->|uses| n5
  n0 -->|uses| n6
  n1 -->|calls| n2
  n1 -->|tests| n2
  n1 -->|calls| n3
  n1 -->|tests| n3
  n1 -->|uses| n4
  n1 -->|uses| n5
  n1 -->|uses| n6
  n3 -->|calls| n0
  n3 -->|uses| n4
  n3 -->|uses| n5
  n3 -->|uses| n6
  classDef changed fill:#0d4429,stroke:#238636,color:#e6edf3
  classDef impacted fill:#161b22,stroke:#6e7681,color:#c9d1d9
  classDef flagged fill:#5a1e02,stroke:#d93f0b,color:#ffffff
  classDef blocking fill:#67060c,stroke:#f85149,color:#ffffff
Loading

Green: changed behaviour. Grey: surrounding behaviour. Arrows name the call, use, implementation, or test relationship. Orange: has findings. Red: has a finding that blocks the merge.

tinysweeper 0.1.0

@tinysweeper tinysweeper Bot added the priority: p3 Whenever. Cosmetic, a nicety, or a cleanup with no user visible effect. label Aug 24, 2026
@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 25b9664f-ac6a-4dd6-9c92-28eb1cd77bda

📥 Commits

Reviewing files that changed from the base of the PR and between 268eb8a and 338ca86.

📒 Files selected for processing (1)
  • src/openhuman/config/schema/load_tests.rs

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The system prompt now appends the rendered tool-policy boundary after stable sections. Tests verify its position and final-section status. Test memory hosts, proxy test isolation, and integration documentation were updated.

Changes

Tool policy prompt ordering

Layer / File(s) Summary
Append the policy boundary and validate its position
src/openhuman/agent/harness/session/turn/context.rs, src/openhuman/agent/harness/session/turn_tests.rs, src/openhuman/tools/agent_policy/README.md
build_system_prompt appends the rendered policy boundary. Tests verify its placement and final-section status. The README identifies turn/context.rs as the rendering caller. The dispatcher-based test builder installs memory host implementations.

Runtime proxy test isolation

Layer / File(s) Summary
Isolate proxy configuration state in the test
src/openhuman/config/schema/load_tests.rs
The test uses HashMapEnv, serializes environment access, restores runtime proxy configuration with an RAII guard, and verifies that an empty overlay preserves and commits the configured proxy.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 338ca

The PR moves the session-varying tool-policy block to the end of the system prompt while preserving its contents and restricted-session behavior; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: senamakel

Poem

A rabbit nudged the prompt in line,
Stable words now lead and shine.
Policy waits at the ending gate,
Tests keep proxy state in a steady state.
Memory hosts hop into place.
Safe boundaries leave a trace.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The prompt-placement changes are in scope for issue #5704, but the runtime proxy environment test changes in src/openhuman/config/schema/load_tests.rs are unrelated to the linked issue objectives. Remove the unrelated runtime proxy test changes from this pull request, or link an issue that requires them.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: appending the tool-policy boundary after the cacheable prompt prefix.
Linked Issues check ✅ Passed The implementation changes tool-policy boundary placement from prepending to appending, preserving the stable prompt prefix and agent persona at the beginning as required by issue #5704. The regressio…
Full details: Linked Issues check

Explanation

The implementation changes tool-policy boundary placement from prepending to appending, preserving the stable prompt prefix and agent persona at the beginning as required by issue #5704. The regression test verifies the new ordering.

Warning

Your free Security trial is over. An organization admin can activate billing to continue.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Aug 24, 2026
Feature-Gate Smoke injects HTTP_PROXY into the job. apply_env_overrides()
re-overlays ProcessEnv, which can wipe the fixture URL, fail validate(),
and force enabled=false before set_runtime_proxy_config. Drive the same
commit path from an empty HashMapEnv instead.
@M3gA-Mind

Copy link
Copy Markdown
Collaborator

Thanks for this, @gac0812 — closing in favour of #5821 for #5704, with two pieces here that should come back as their own PRs.

Why #5821: it's MERGEABLE where this is CONFLICTING, it's one file, and it extracts append_tool_policy_boundary(prompt, boundary) so the placement becomes a unit test rather than something you need to stand up a session to exercise. Your diagnosis was the same and was reached first — the difference is scope and current state, not correctness.

Please do re-file these two, separately:

  1. The tools/agent_policy/README.md pointer. It still names the pre-kernelization session/turn.rs. That's a genuine doc-staleness bug you found on the way past, it's unrelated to Tool Policy Boundary is prepended to the system prompt, putting session-varying content ahead of the cacheable prefix #5704, and it'll merge on its own in a minute.
  2. The apply_env_overrides_commits_side_effects_to_runtime_proxy refactor in config/schema/load_tests.rs. A proxy env-var CI flake fix is worth having — it just can't be reviewed inside a prompt-ordering PR, because nobody looking at Tool Policy Boundary is prepended to the system prompt, putting session-varying content ahead of the cacheable prefix #5704 knows to check it.

Both are real work and neither is in #5821. Bundling them is the only reason this one is being closed rather than merged.

@M3gA-Mind M3gA-Mind closed this Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority: p3 Whenever. Cosmetic, a nicety, or a cleanup with no user visible effect.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tool Policy Boundary is prepended to the system prompt, putting session-varying content ahead of the cacheable prefix

3 participants