|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { prepareEmbeddedAttemptBootstrap } from "./attempt-bootstrap-prepare.js"; |
| 6 | +import type { EmbeddedRunAttemptParams } from "./types.js"; |
| 7 | + |
| 8 | +const tempDirs: string[] = []; |
| 9 | + |
| 10 | +afterEach(async () => { |
| 11 | + await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true }))); |
| 12 | +}); |
| 13 | + |
| 14 | +describe("prepareEmbeddedAttemptBootstrap", () => { |
| 15 | + async function prepare(params: { agentWorkspace: string; sessionWorkspace: string }) { |
| 16 | + return await prepareEmbeddedAttemptBootstrap({ |
| 17 | + attempt: { |
| 18 | + sessionId: "session-1", |
| 19 | + sessionKey: "agent:main:session-1", |
| 20 | + trigger: "user", |
| 21 | + isCanonicalWorkspace: params.agentWorkspace === params.sessionWorkspace, |
| 22 | + config: { agents: { defaults: { workspace: params.agentWorkspace } } }, |
| 23 | + } as EmbeddedRunAttemptParams, |
| 24 | + bootstrapWorkspaceDir: params.agentWorkspace, |
| 25 | + effectiveWorkspace: params.sessionWorkspace, |
| 26 | + hasReadTool: true, |
| 27 | + isRawModelRun: false, |
| 28 | + markStage: () => undefined, |
| 29 | + resolvedWorkspace: params.sessionWorkspace, |
| 30 | + sessionAgentId: "main", |
| 31 | + sessionLabel: "agent:main:session-1", |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + it("layers execution project instructions after agent bootstrap files", async () => { |
| 36 | + const agentWorkspace = await fs.realpath( |
| 37 | + await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-agent-workspace-")), |
| 38 | + ); |
| 39 | + const sessionWorkspace = await fs.realpath( |
| 40 | + await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-workspace-")), |
| 41 | + ); |
| 42 | + tempDirs.push(agentWorkspace, sessionWorkspace); |
| 43 | + await fs.writeFile(path.join(agentWorkspace, "AGENTS.md"), "Canonical agent instructions"); |
| 44 | + await fs.writeFile(path.join(agentWorkspace, "SOUL.md"), "Canonical agent soul"); |
| 45 | + await fs.writeFile(path.join(sessionWorkspace, "AGENTS.md"), "Execution project context"); |
| 46 | + await fs.writeFile(path.join(sessionWorkspace, "SOUL.md"), "Execution soul must stay private"); |
| 47 | + |
| 48 | + const result = await prepare({ agentWorkspace, sessionWorkspace }); |
| 49 | + const executionAgentsIndex = result.contextFiles.findIndex( |
| 50 | + (file) => file.path === path.join(sessionWorkspace, "AGENTS.md"), |
| 51 | + ); |
| 52 | + const lastAgentFileIndex = result.contextFiles.findLastIndex((file) => |
| 53 | + file.path.startsWith(`${agentWorkspace}${path.sep}`), |
| 54 | + ); |
| 55 | + |
| 56 | + expect(executionAgentsIndex).toBeGreaterThan(lastAgentFileIndex); |
| 57 | + expect(result.contextFiles[executionAgentsIndex]).toEqual( |
| 58 | + expect.objectContaining({ |
| 59 | + path: path.join(sessionWorkspace, "AGENTS.md"), |
| 60 | + content: "Execution project context", |
| 61 | + }), |
| 62 | + ); |
| 63 | + expect(result.contextFiles).toContainEqual( |
| 64 | + expect.objectContaining({ |
| 65 | + path: path.join(agentWorkspace, "SOUL.md"), |
| 66 | + content: "Canonical agent soul", |
| 67 | + }), |
| 68 | + ); |
| 69 | + expect(result.contextFiles).not.toContainEqual( |
| 70 | + expect.objectContaining({ path: path.join(sessionWorkspace, "SOUL.md") }), |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it("keeps same-workspace bootstrap output byte-identical", async () => { |
| 75 | + const workspace = await fs.realpath( |
| 76 | + await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-same-workspace-")), |
| 77 | + ); |
| 78 | + tempDirs.push(workspace); |
| 79 | + await fs.writeFile(path.join(workspace, "AGENTS.md"), "Same workspace instructions"); |
| 80 | + await fs.writeFile(path.join(workspace, "SOUL.md"), "Same workspace soul"); |
| 81 | + |
| 82 | + const explicit = await prepare({ agentWorkspace: workspace, sessionWorkspace: workspace }); |
| 83 | + const omitted = await prepareEmbeddedAttemptBootstrap({ |
| 84 | + attempt: { |
| 85 | + sessionId: "session-1", |
| 86 | + sessionKey: "agent:main:session-1", |
| 87 | + trigger: "user", |
| 88 | + isCanonicalWorkspace: true, |
| 89 | + config: { agents: { defaults: { workspace } } }, |
| 90 | + } as EmbeddedRunAttemptParams, |
| 91 | + effectiveWorkspace: workspace, |
| 92 | + hasReadTool: true, |
| 93 | + isRawModelRun: false, |
| 94 | + markStage: () => undefined, |
| 95 | + resolvedWorkspace: workspace, |
| 96 | + sessionAgentId: "main", |
| 97 | + sessionLabel: "agent:main:session-1", |
| 98 | + }); |
| 99 | + |
| 100 | + expect(explicit).toEqual(omitted); |
| 101 | + }); |
| 102 | +}); |
0 commit comments