refactor(memory): use XML markers for injected context#178
Open
GCS-ZHN wants to merge 1 commit into
Open
Conversation
Wrap the memory block returned to the model in explicit <memory_context>...</memory_context> tags so the agent can clearly distinguish injected reference material from the user's message. A short header inside the wrapper reminds the model that the contents are background context, not instructions. - src/services/context.ts: replace the [MEMORY] header with a <memory_context> wrapper; nest <user_profile> and <project_knowledge> sections, with each memory item as <memory relevance=N%>...</memory>. - src/services/user-profile/profile-context.ts: emit the four profile categories as <user_preferences>, <user_patterns>, <user_workflows>, and <learning_paths> with their items as nested elements carrying category / topic / frequency attributes. Add escapeXmlText / escapeXmlAttr helpers and apply them to all user-supplied strings so profile content cannot break out of the tags. Calling logic (forEach + parts.push, the parts array name, return contract, and the workflow steps formatting expression) is preserved; only the pushed string contents change.
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
Replace the plain-text
[MEMORY]header in injected context with explicit XML tags so the model can clearly distinguish reference material from the user's message.Changes
src/services/context.ts— wrap the injected block in<memory_context>...</memory_context>with a header reminding the model the contents are background context. Nest<user_profile>and<project_knowledge>sections; each memory item becomes<memory relevance="N%">...</memory>.src/services/user-profile/profile-context.ts— emit profile categories as<user_preferences>,<user_patterns>,<user_workflows>, and<learning_paths>with their items as nested elements carryingcategory/topic/frequencyattributes. AddescapeXmlText/escapeXmlAttrhelpers and apply them to all user-supplied strings so profile content cannot break out of the tags.Behavior preserved
formatContextForPromptstill returns""when there is nothing to inject, so theif (memoryContext)guard insrc/index.ts:279behaves identically.getUserProfileContextstill returnsstring | null;nullis returned when no profile or no injectable items exist.partsarray name,forEach+parts.pushcalling style, and the workflow steps formatting expression are kept as-is. Only the pushed string contents change.Motivation
Models that consume injected context benefit from explicit structural markers: tags make it trivial to identify the start/end of the injected region, attributes carry similarity / category / frequency without parsing prose, and escaping prevents profile content (some of which is written by AI cleanup passes) from breaking out of the wrapper.
Test plan
formatContextForPromptoutput today. Suggested follow-up: a unit test asserting the XML structure (see PR comment).<memory_context>and closes with</memory_context>.