diff --git a/src/agents/system-prompt.test.ts b/src/agents/system-prompt.test.ts index 7b2d97188..15262ddb1 100644 --- a/src/agents/system-prompt.test.ts +++ b/src/agents/system-prompt.test.ts @@ -301,6 +301,23 @@ describe("buildAgentSystemPrompt", () => { expect(prompt).toContain("Bravo"); }); + it("ignores context files with missing or blank paths", () => { + const prompt = buildAgentSystemPrompt({ + workspaceDir: "/tmp/openclaw", + contextFiles: [ + { path: undefined as unknown as string, content: "Missing path" }, + { path: " ", content: "Blank path" }, + { path: "AGENTS.md", content: "Alpha" }, + ], + }); + + expect(prompt).toContain("# Project Context"); + expect(prompt).toContain("## AGENTS.md"); + expect(prompt).toContain("Alpha"); + expect(prompt).not.toContain("Missing path"); + expect(prompt).not.toContain("Blank path"); + }); + it("adds SOUL guidance when a soul file is present", () => { const prompt = buildAgentSystemPrompt({ workspaceDir: "/tmp/openclaw", diff --git a/src/agents/system-prompt.ts b/src/agents/system-prompt.ts index 36ab37060..6fe11cc4f 100644 --- a/src/agents/system-prompt.ts +++ b/src/agents/system-prompt.ts @@ -550,8 +550,11 @@ export function buildAgentSystemPrompt(params: { } const contextFiles = params.contextFiles ?? []; - if (contextFiles.length > 0) { - const hasSoulFile = contextFiles.some((file) => { + const validContextFiles = contextFiles.filter( + (file) => typeof file.path === "string" && file.path.trim().length > 0, + ); + if (validContextFiles.length > 0) { + const hasSoulFile = validContextFiles.some((file) => { const normalizedPath = file.path.trim().replace(/\\/g, "/"); const baseName = normalizedPath.split("/").pop() ?? normalizedPath; return baseName.toLowerCase() === "soul.md"; @@ -563,7 +566,7 @@ export function buildAgentSystemPrompt(params: { ); } lines.push(""); - for (const file of contextFiles) { + for (const file of validContextFiles) { lines.push(`## ${file.path}`, "", file.content, ""); } }