CH 05 · Context Engineering and Memory · 13/18
Read as one pageReal code: OpenClaw stacks workspace files in order
OpenClaw keeps the standing workspace facts as separate Markdown files, then builds them into the system prompt. These files are the standing-instructions stack: rules, persona, identity, user preferences, and durable memory, in a fixed order. Task instructions, history, and the current message are added by other code. Watch the sort keys, then the short note in front of only three files.
const CONTEXT_FILE_ORDER = new Map<string, number>([ // [1]
["agents.md", 10],
["soul.md", 20],
["identity.md", 30],
["user.md", 40],
["tools.md", 50],
["bootstrap.md", 60],
["memory.md", 70], // [2]
]);
// ... (40 lines: lowercase the file name, then look it up in this map)
.toSorted((a, b) => { // [3]
if (a.order !== b.order) {
return a.order - b.order;
}
if (a.basename !== b.basename) {
return a.basename.localeCompare(b.basename);
}
// Preserve loader precedence for shared USER defaults and the personal overlay.
if (a.basename === "user.md") {
return 0; // [4]
}
Source: openclaw/openclaw, src/agents/system-prompt-context-files.ts (MIT)
lines.push("Loaded project context:");
for (const [basename, guidance] of Object.entries({
"soul.md": "SOUL.md: persona/tone. Follow it unless higher-priority instructions override.",
"memory.md":
"MEMORY.md: durable non-profile facts and decisions; use when relevant unless higher-priority instructions override.",
"user.md":
"USER.md: durable user preferences and profile directives; follow unless higher-priority instructions override.",
})) {
if (files.some((file) => file.basename === basename)) { // [5]
lines.push(guidance);
}
}
// ... (4 lines: a three-sentence note when a personal USER.md is present, then a blank line)
for (const { file } of files) {
lines.push(`## ${file.path}`, "", sanitizeContextFileContentForPrompt(file.content), ""); // [6]
}
Source: openclaw/openclaw, src/agents/system-prompt-context-files.ts (MIT)
What to notice
- [1]
CONTEXT_FILE_ORDERmaps a lowercased file name to a number.prepareContextFilesForPrompt, in the skipped lines, stores that number on eachEmbeddedContextFile(a path plus the file's text). A missing name sorts last. - [2]
AGENTS.mdis first andMEMORY.mdis last. Persona, identity, user preferences, andBOOTSTRAP.mdsit between them.tools.mdis in the map, but it is not in the default file list. - [3]
toSortedreturns a new array, so the loader's order stays as it was read. Comparison uses the map number, then the file name. - [4] Two
USER.mdfiles stay in loader order (return 0means tied), so the shared file and a personal overlay keep the order the loader produced. - [5] A one-line role is added only for soul, memory, and user, and only when that file is present. Those lines say they give way to higher-priority instructions.
AGENTS.mdgets no yield sentence. It sorts first. - [6] Each file becomes a heading (its path) and then its text.
sanitizeContextFileContentForPromptstrips the default heartbeat prompt out of old templates. The comment says that prompt would otherwise route Claude subscriptions to paid extra usage. The scheduled heartbeat turn still carries the real instructions.
buildSystemPrompt writes this under "Workspace Files (injected)", then appends this run's tools. A naive prompt concatenates files in directory order, with no labels, so a persona file and a memory file look the same.
Read it yourself: open the test "filters blank paths and renders typed project context in canonical order" in src/agents/system-prompt.test.ts. It passes the files out of order. Expect ## AGENTS.md before ## ./SOUL.md, that heading before ## MEMORY.md, a persona line, a memory line, and no "Blank path".
Finished this idea?
Your learning progress stays in this browser.