fix: keep per-wakeup content out of proactive agent system prompts - #9398
Open
shentry wants to merge 1 commit into
Open
fix: keep per-wakeup content out of proactive agent system prompts#9398shentry wants to merge 1 commit into
shentry wants to merge 1 commit into
Conversation
Both proactive wakeup paths built the system prompt by appending the conversation history dump and the triggering payload before calling build_main_agent. build_main_agent then appends the persona, skills and tool declarations to that same string, so every static block ended up behind content that changes on every single wakeup. The provider's prefix cache could never match and cron runs reported cached_tokens=None. Split the two wake prompts into a static system half and a dynamic user half, and move the conversation history and the job payload into the user turn: - cron/manager.py, the scheduled wakeup reported in the issue. - astr_agent_tool_exec.py, the background-task-result wakeup, which had the same construction and the same problem. The static wake rules stay in the system prompt, so instructions keep their system role and only the per-run parts move. The system prompt is now byte-identical across wakeups of the same session. Tests cover that the history and the payload reach req.prompt but not req.system_prompt, that two wakeups differing in both still produce an identical system prompt, and that the static rules are still present. Closes AstrBotDevs#8473
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #8473
Problem
Both proactive wakeup paths assembled
req.system_promptlike this before callingbuild_main_agent:build_main_agentthen appends the persona, skills and tool declarations to that same string. So the whole static tail sits behind content that differs on every single run, the prefix cache can never match, and the cron runs in the issue reportcached_tokens=None.The issue reports the cron path. The background-task-result path in
astr_agent_tool_exec.pyhad the identical construction and the identical problem, so this PR fixes both — fixing only one would have left the same bug in place one file over.Change
Split the two wake prompts into a static system half and a dynamic user half, and move the per-run content into the user turn:
The static rules deliberately stay in the system prompt, so the instructions keep their system role and
"Proceed according to your system instructions"in the user turn stays accurate. Only the parts that actually vary move.Result: for a given session the system prompt is byte-identical across wakeups, so persona + tools + rules form a stable cacheable prefix, and everything that varies rides in the last message where it was never cacheable anyway.
Tests
Added
TestWokeMainAgentPromptCachingtotests/unit/test_cron_manager.py, which drives_woke_main_agentand captures theProviderRequesthanded tobuild_main_agent:req.prompt, notreq.system_promptreq.prompt, notreq.system_promptsystem_prompt(and differentprompt)The first three fail against the previous implementation and pass with this change.
pytest tests/unit/-> 734 passedruff format --check/ruff check(0.15.22) on the four touched files -> cleanNote on the full suite:
pytest tests/has 3 pre-existing failures intests/test_dashboard.py(test_t2i_*), andruff format --checkflags 7 pre-existing test files. I verified both are present on a cleanmastercheckout without my changes and are unrelated to this PR.Follow-up not done here
The cron system prompt still starts with the wake rules, so its prefix differs from a normal chat turn's and the two paths cache separately. Sharing one prefix would mean letting
build_main_agentappend the wake rules after the persona/tool blocks, which is a bigger refactor than this fix needs. Happy to do it in a follow-up if you want it.Summary by Sourcery
Ensure proactive agent wakeups keep dynamic per-run data out of the system prompt so provider prefix caching can be reused across runs.
Enhancements:
Tests: