WIP: Context policy and out-of-band message design#272
Draft
esafwan wants to merge 16 commits into
Draft
Conversation
…messages Detailed, code-grounded plan to prevent large tool/result payloads from being replayed as conversation history. Includes: - Data model changes (7 new fields on Agent Message) - History loader refactor (single policy-aware entry point) - Tool result persistence updates (sync + stream paths) - Complete test coverage for acceptance criteria - Risk analysis and rollout strategy Estimated implementation time: 2.5 hours for Phase 1. https://claude.ai/code/session_01JYBGo1Ap9ybbsyPLUEgxNa
Adds context_policy field to Agent Message to control how persisted content re-enters model context on future turns. Large tool outputs now stored as compact reference lines instead of being replayed verbatim, keeping input tokens O(1) per result rather than O(payload). Changes: - agent_message.json: 7 new nullable fields (context_policy, record_kind, context_summary, reference_doctype, reference_name, visibility, token_estimate). NULL policy = include_full (backward compatible). - conversation_manager.py: get_conversation_history() now filters by policy via _message_to_context(); add_message() accepts all new fields. - agent_integration.py: sync tool-result persistence uses include_reference for payloads >2000 chars, include_full otherwise. - tests/test_context_policy.py: 7 acceptance tests covering all policies, backward compat, and token growth bound. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Updated get_conversation_history to query tool_call and kind fields. - Updated _message_to_context to process include_reference policies. - Implemented logic to dynamically split single database Tool Result rows into strict assistant and tool API objects during history replay.
- Injected a global architectural rule into the system prompt compiler. - Instructed all agents on how to parse and fetch reference handles using the get_result_context tool.
- Created handle_get_result_context native function to retrieve large truncated payloads from the database. - Registered the get_result_context tool globally so all agents can fetch data regardless of memory settings.
- Programmatically generated and synced the Agent Context Artifact DocType schema. - Established foundational database storage layer for future massive non-tool payloads (e.g., file attachments).
- Added @frappe.whitelist() to add_message in agent_chat.py. - Enabled out-of-band context injection for frontend applications and external integrations.
- Added max_context_chars Integer field directly to the Agent DocType schema. - Enabled precise, per-bot configuration for tool result truncation limits.
- Modified run_stream loop to automatically flag tool results > 2000 chars as references. - Applied context policy fields directly onto the mutated message row to preserve existing UI rendering.
- Removed hardcoded 2000 character magic number in tool result length checks. - Refactored truncation logic to dynamically fetch 'max_context_chars' from the executing Agent object. - Preserved a robust fallback to 2000 if the agent setting is missing or empty.
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.
Summary
Adds a WIP design spec for a HUF context-policy layer and out-of-band/reference-only message pattern.
This is intended to solve cases where large runtime payloads, such as search visible result snapshots, retrieved documents, large tool outputs, browser state, or debug traces, are useful for audit/replay but should not be blindly persisted and replayed as normal LLM conversation history.
What is included
Agent Messageand replayed in later turns.record_kind,visibility,context_policy,context_summary, and references.Notes
This PR is intentionally documentation-only and WIP. It should be used to discuss the direction before backend/runtime changes are implemented.