Skip to content

Commit 9e86e08

Browse files
authored
fix(test): isolate legacy agent directory overrides (#132874)
1 parent 5b532a7 commit 9e86e08

6 files changed

Lines changed: 129 additions & 52 deletions

File tree

‎src/test-utils/openclaw-test-state.test.ts‎

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe("openclaw test state", () => {
6262
await withEnvAsync(
6363
{
6464
OPENCLAW_AGENT_DIR: path.join(parent, "previous-agent"),
65+
PI_CODING_AGENT_DIR: path.join(parent, "previous-legacy-agent"),
6566
OPENCLAW_ACQUISITION_EMPTY: "",
6667
OPENCLAW_ACQUISITION_ABSENT: undefined,
6768
},
@@ -75,6 +76,7 @@ describe("openclaw test state", () => {
7576
"OPENCLAW_STATE_DIR",
7677
"OPENCLAW_CONFIG_PATH",
7778
"OPENCLAW_AGENT_DIR",
79+
"PI_CODING_AGENT_DIR",
7880
"OPENCLAW_ACQUISITION_EMPTY",
7981
"OPENCLAW_ACQUISITION_ABSENT",
8082
];
@@ -227,49 +229,71 @@ describe("openclaw test state", () => {
227229
);
228230
});
229231

230-
it("clears inherited agent-dir overrides by default", async () => {
231-
await withEnvAsync({ OPENCLAW_AGENT_DIR: "/tmp/outside-openclaw-agent" }, async () => {
232-
const state = await createOpenClawTestState({
233-
layout: "state-only",
234-
});
232+
it.each([
233+
{ agentEnv: undefined, applyEnv: true },
234+
{ agentEnv: undefined, applyEnv: false },
235+
{ agentEnv: "main", applyEnv: true },
236+
{ agentEnv: "main", applyEnv: false },
237+
] as const)(
238+
"isolates inherited agent selectors with $agentEnv and applyEnv=$applyEnv",
239+
async ({ agentEnv, applyEnv }) => {
240+
const inherited = {
241+
OPENCLAW_AGENT_DIR: "/tmp/outside-openclaw-agent",
242+
PI_CODING_AGENT_DIR: "/tmp/outside-legacy-agent",
243+
};
244+
await withEnvAsync(inherited, async () => {
245+
const state = await createOpenClawTestState({
246+
layout: agentEnv === "main" ? "home" : "state-only",
247+
agentEnv,
248+
applyEnv,
249+
});
235250

236-
try {
237-
expect(process.env.OPENCLAW_AGENT_DIR).toBeUndefined();
238-
expect(state.env.OPENCLAW_AGENT_DIR).toBeUndefined();
239-
expect(state.agentDir()).toBe(path.join(state.stateDir, "agents", "main", "agent"));
240-
} finally {
241-
await state.cleanup();
242-
}
251+
try {
252+
const expectedAgentDir = agentEnv === "main" ? state.agentDir() : undefined;
253+
expect(state.env.OPENCLAW_AGENT_DIR).toBe(expectedAgentDir);
254+
expect(state.env.PI_CODING_AGENT_DIR).toBeUndefined();
255+
expect(process.env.OPENCLAW_AGENT_DIR).toBe(
256+
applyEnv ? expectedAgentDir : inherited.OPENCLAW_AGENT_DIR,
257+
);
258+
expect(process.env.PI_CODING_AGENT_DIR).toBe(
259+
applyEnv ? undefined : inherited.PI_CODING_AGENT_DIR,
260+
);
261+
expect(state.agentDir()).toBe(path.join(state.stateDir, "agents", "main", "agent"));
262+
} finally {
263+
await state.cleanup();
264+
}
243265

244-
expect(process.env.OPENCLAW_AGENT_DIR).toBe("/tmp/outside-openclaw-agent");
245-
});
246-
});
266+
expect(process.env.OPENCLAW_AGENT_DIR).toBe(inherited.OPENCLAW_AGENT_DIR);
267+
expect(process.env.PI_CODING_AGENT_DIR).toBe(inherited.PI_CODING_AGENT_DIR);
268+
});
269+
},
270+
);
247271

248-
it("allows explicit agent-dir overrides when a test needs them", async () => {
249-
await withOpenClawTestState(
250-
{
251-
env: {
272+
it.each([undefined, "main"] as const)(
273+
"allows explicit agent-dir overrides with agentEnv=%s and restores absent or empty selectors",
274+
async (agentEnv) => {
275+
await withEnvAsync({ OPENCLAW_AGENT_DIR: undefined, PI_CODING_AGENT_DIR: "" }, async () => {
276+
const overrides = {
252277
OPENCLAW_AGENT_DIR: "/tmp/explicit-openclaw-agent",
253-
},
254-
},
255-
async (state) => {
256-
expect(process.env.OPENCLAW_AGENT_DIR).toBe("/tmp/explicit-openclaw-agent");
257-
expect(state.env.OPENCLAW_AGENT_DIR).toBe("/tmp/explicit-openclaw-agent");
258-
},
259-
);
260-
});
261-
262-
it("can route agent-dir env vars to the isolated main agent store", async () => {
263-
await withOpenClawTestState(
264-
{
265-
agentEnv: "main",
266-
},
267-
async (state) => {
268-
expect(process.env.OPENCLAW_AGENT_DIR).toBe(state.agentDir());
269-
expect(state.env.OPENCLAW_AGENT_DIR).toBe(state.agentDir());
270-
},
271-
);
272-
});
278+
PI_CODING_AGENT_DIR: "/tmp/explicit-legacy-agent",
279+
};
280+
const state = await createOpenClawTestState({ agentEnv, applyEnv: false, env: overrides });
281+
try {
282+
expect(state.env.OPENCLAW_AGENT_DIR).toBe(overrides.OPENCLAW_AGENT_DIR);
283+
expect(state.env.PI_CODING_AGENT_DIR).toBe(overrides.PI_CODING_AGENT_DIR);
284+
expect(process.env.OPENCLAW_AGENT_DIR).toBeUndefined();
285+
expect(process.env.PI_CODING_AGENT_DIR).toBe("");
286+
state.applyEnv();
287+
expect(process.env.OPENCLAW_AGENT_DIR).toBe(overrides.OPENCLAW_AGENT_DIR);
288+
expect(process.env.PI_CODING_AGENT_DIR).toBe(overrides.PI_CODING_AGENT_DIR);
289+
} finally {
290+
await state.cleanup();
291+
}
292+
expect(process.env.OPENCLAW_AGENT_DIR).toBeUndefined();
293+
expect(process.env.PI_CODING_AGENT_DIR).toBe("");
294+
});
295+
},
296+
);
273297

274298
it("writes scenario configs and auth profile stores", async () => {
275299
await withOpenClawTestState(

‎src/test-utils/openclaw-test-state.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ function buildEnvVars(params: {
237237
OPENCLAW_STATE_DIR: params.stateDir,
238238
OPENCLAW_CONFIG_PATH: params.configPath,
239239
...agentDirEnv,
240+
PI_CODING_AGENT_DIR: undefined,
240241
...params.scenarioEnv,
241242
...params.extraEnv,
242243
};

‎test/non-isolated-runner.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ function fixtureFiles(): Record<string, string> {
100100
const agentEventsPath = JSON.stringify(path.join(repoRoot, "src", "infra", "agent-events.ts"));
101101
const loggingConsolePath = JSON.stringify(path.join(repoRoot, "src", "logging", "console.ts"));
102102
const loggingStatePath = JSON.stringify(path.join(repoRoot, "src", "logging", "state.ts"));
103+
const testEnvPath = JSON.stringify(path.join(repoRoot, "src", "test-utils", "env.ts"));
103104
const payloadImports = [
104105
'import { createRequire } from "node:module";',
105106
'import { queryObjects } from "node:v8";',
@@ -145,6 +146,29 @@ function fixtureFiles(): Record<string, string> {
145146
"});",
146147
"",
147148
].join("\n"),
149+
"02-c-agent-env.test.ts": [
150+
`import { setTestEnvValue } from ${testEnvPath};`,
151+
'import { expect, it, vi } from "vitest";',
152+
'it("leaves agent selectors for file-completion env unstub", () => {',
153+
" expect(process.env.HOME).toBe(process.env.OPENCLAW_TEST_HOME);",
154+
" expect(process.env.OPENCLAW_TEST_HOME).toBeTruthy();",
155+
' for (const key of ["OPENCLAW_AGENT_DIR", "PI_CODING_AGENT_DIR"]) {',
156+
" setTestEnvValue(key, `/tmp/inherited-${key}`);",
157+
" vi.stubEnv(key, undefined);",
158+
" expect(process.env[key]).toBeUndefined();",
159+
" }",
160+
"});",
161+
"",
162+
].join("\n"),
163+
"02-d-agent-env.test.ts": [
164+
'import { expect, it } from "vitest";',
165+
'it("clears restored agent selectors before the next file", () => {',
166+
" expect(process.env.HOME).toBe(process.env.OPENCLAW_TEST_HOME);",
167+
" expect(process.env.OPENCLAW_AGENT_DIR).toBeUndefined();",
168+
" expect(process.env.PI_CODING_AGENT_DIR).toBeUndefined();",
169+
"});",
170+
"",
171+
].join("\n"),
148172
"03-a-runtime-store.test.ts": [
149173
`import { createPluginRuntimeStore } from ${runtimeStorePath};`,
150174
'import { expect, it } from "vitest";',
@@ -434,7 +458,7 @@ it("cleans every shared runner surface between files", async () => {
434458
// The collection failure is intentional. Every behavior test after it must
435459
// pass; any leaked surface turns the summary into a second failure.
436460
expect(output).toContain("synthetic collect failure");
437-
expect(output).toContain("1 failed | 32 passed");
461+
expect(output).toContain("1 failed | 34 passed");
438462
expect(output).not.toContain("first-file");
439463
} finally {
440464
await fs.rm(root, { recursive: true, force: true });

‎test/non-isolated-runner.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ function restoreSharedTestHomeAfterEnvUnstub(testHomeRaw: string | undefined): v
104104
delete process.env.OPENCLAW_CONFIG_PATH;
105105
delete process.env.OPENCLAW_STATE_DIR;
106106
delete process.env.OPENCLAW_AGENT_DIR;
107+
delete process.env.PI_CODING_AGENT_DIR;
107108
process.env.XDG_CONFIG_HOME = path.join(testHome, ".config");
108109
process.env.XDG_DATA_HOME = path.join(testHome, ".local", "share");
109110
process.env.XDG_STATE_HOME = path.join(testHome, ".local", "state");

‎test/test-env.test.ts‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ describe("installTestEnv", () => {
117117
OPENCLAW_HOME: realHome,
118118
OPENCLAW_STATE_DIR: path.join(realHome, ".openclaw"),
119119
OPENCLAW_CONFIG_PATH: configPath,
120+
OPENCLAW_AGENT_DIR: path.join(realHome, "caller-agent"),
121+
PI_CODING_AGENT_DIR: path.join(realHome, "caller-legacy-agent"),
120122
OPENCLAW_LIVE_TEST: "1",
121123
OPENCLAW_LIVE_USE_REAL_HOME: undefined,
122124
OPENCLAW_LIVE_TEST_QUIET: "1",
@@ -161,13 +163,17 @@ describe("installTestEnv", () => {
161163
const next = installTestEnv();
162164
cleanupFns.push(next.cleanup);
163165
expect(next.tempHome).not.toBe(failedHome);
166+
expect(process.env.OPENCLAW_AGENT_DIR).toBeUndefined();
167+
expect(process.env.PI_CODING_AGENT_DIR).toBeUndefined();
164168
expect(process.env.ACQUISITION_PROFILE_ADDED).toBe("from-profile");
165169
expect(process.env.ACQUISITION_PROFILE_EMPTY).toBe("from-profile");
166170
expect(
167171
fs.readFileSync(path.join(next.tempHome, ".claude", "settings.json"), "utf8"),
168172
).toBe("{}\n");
169173
next.cleanup();
170174
expect(process.env.HOME).toBe(realHome);
175+
expect(process.env.OPENCLAW_AGENT_DIR).toBe(callerEnv.OPENCLAW_AGENT_DIR);
176+
expect(process.env.PI_CODING_AGENT_DIR).toBe(callerEnv.PI_CODING_AGENT_DIR);
171177
expect(process.env.OPENCLAW_TEST_FAST).toBe("from-profile");
172178
expect(process.env.ACQUISITION_PROFILE_ADDED).toBe("from-profile");
173179
expect(fs.readdirSync(sandbox)).toEqual([]);
@@ -420,12 +426,21 @@ describe("installTestEnv", () => {
420426
setTestEnvValue("OPENCLAW_LIVE_TEST", "1");
421427
setTestEnvValue("OPENCLAW_LIVE_USE_REAL_HOME", "1");
422428
setTestEnvValue("OPENCLAW_LIVE_TEST_QUIET", "1");
429+
const agentDir = path.join(realHome, "caller-agent");
430+
const legacyAgentDir = path.join(realHome, "caller-legacy-agent");
431+
setTestEnvValue("OPENCLAW_AGENT_DIR", agentDir);
432+
setTestEnvValue("PI_CODING_AGENT_DIR", legacyAgentDir);
423433

424434
const testEnv = installTestEnv();
425435

426436
expect(testEnv.tempHome).toBe(realHome);
427437
expect(process.env.HOME).toBe(realHome);
428438
expect(process.env.TEST_PROFILE_ONLY).toBe("from-profile");
439+
expect(process.env.OPENCLAW_AGENT_DIR).toBe(agentDir);
440+
expect(process.env.PI_CODING_AGENT_DIR).toBe(legacyAgentDir);
441+
testEnv.cleanup();
442+
expect(process.env.OPENCLAW_AGENT_DIR).toBe(agentDir);
443+
expect(process.env.PI_CODING_AGENT_DIR).toBe(legacyAgentDir);
429444
});
430445

431446
it("keeps hermetic mode isolated when live flags request the real HOME", () => {
@@ -445,6 +460,8 @@ describe("installTestEnv", () => {
445460
setTestEnvValue("OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR", "1");
446461
setTestEnvValue("OPENCLAW_DISABLE_BUNDLED_PLUGINS", "1");
447462
setTestEnvValue("OPENCLAW_HOME", realHome);
463+
setTestEnvValue("OPENCLAW_AGENT_DIR", path.join(realHome, "caller-agent"));
464+
setTestEnvValue("PI_CODING_AGENT_DIR", path.join(realHome, "caller-legacy-agent"));
448465

449466
const testEnv = installTestEnv({ mode: "hermetic" });
450467
cleanupFns.push(testEnv.cleanup);
@@ -461,27 +478,34 @@ describe("installTestEnv", () => {
461478
expect(process.env.OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR).toBe("1");
462479
expect(process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS).toBeUndefined();
463480
expect(process.env.OPENCLAW_HOME).toBeUndefined();
481+
expect(process.env.OPENCLAW_AGENT_DIR).toBeUndefined();
482+
expect(process.env.PI_CODING_AGENT_DIR).toBeUndefined();
464483
expect(fs.existsSync(path.join(testEnv.tempHome, ".openclaw", "openclaw.json"))).toBe(false);
465484
expect(
466485
fs.existsSync(path.join(testEnv.tempHome, ".openclaw", "credentials", "token.txt")),
467486
).toBe(false);
468487
});
469488

470-
it("clears and restores OPENCLAW_HOME for normal isolated test runs", () => {
471-
const realHome = createTempHome();
472-
const configuredOpenClawHome = path.join(realHome, "custom-openclaw-home");
473-
setTestEnvValue("HOME", realHome);
474-
setTestEnvValue("USERPROFILE", realHome);
475-
setTestEnvValue("OPENCLAW_HOME", configuredOpenClawHome);
489+
it.each(["OPENCLAW_HOME", "OPENCLAW_AGENT_DIR", "PI_CODING_AGENT_DIR"])(
490+
"clears and restores %s for normal isolated test runs",
491+
(key) => {
492+
const realHome = createTempHome();
493+
const callerPath = path.join(realHome, "caller-override");
494+
setTestEnvValue("HOME", realHome);
495+
setTestEnvValue("USERPROFILE", realHome);
496+
setTestEnvValue(key, callerPath);
476497

477-
const testEnv = installTestEnv();
498+
const testEnv = installTestEnv();
499+
cleanupFns.push(testEnv.cleanup);
478500

479-
expect(testEnv.tempHome).not.toBe(realHome);
480-
expect(process.env.OPENCLAW_HOME).toBeUndefined();
501+
expect(testEnv.tempHome).not.toBe(realHome);
502+
expect(process.env[key]).toBeUndefined();
503+
setTestEnvValue(key, path.join(testEnv.tempHome, "explicit-override"));
481504

482-
testEnv.cleanup();
483-
expect(process.env.OPENCLAW_HOME).toBe(configuredOpenClawHome);
484-
});
505+
testEnv.cleanup();
506+
expect(process.env[key]).toBe(callerPath);
507+
},
508+
);
485509

486510
it.each([
487511
"TWILIO_ACCOUNT_SID",

‎test/test-env.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ function resolveRestoreEntries(): RestoreEntry[] {
219219
{ key: "OPENCLAW_CANVAS_HOST_PORT", value: process.env.OPENCLAW_CANVAS_HOST_PORT },
220220
{ key: "OPENCLAW_TEST_HOME", value: process.env.OPENCLAW_TEST_HOME },
221221
{ key: "OPENCLAW_AGENT_DIR", value: process.env.OPENCLAW_AGENT_DIR },
222+
{ key: "PI_CODING_AGENT_DIR", value: process.env.PI_CODING_AGENT_DIR },
222223
...ISOLATED_TEST_CREDENTIAL_ENV_KEYS.map((key) => ({ key, value: process.env[key] })),
223224
{ key: "NODE_OPTIONS", value: process.env.NODE_OPTIONS },
224225
];
@@ -239,7 +240,9 @@ function initializeIsolatedTestEnv(tempHome: string): void {
239240
// Derive all state, including SQLite, from this unique HOME so cleanup owns it.
240241
// Leave the override unset so nested HOME scopes also isolate their state.
241242
deleteTestEnvValue("OPENCLAW_STATE_DIR");
243+
// Model status still honors the shipped legacy selector; isolate both agent-dir keys.
242244
deleteTestEnvValue("OPENCLAW_AGENT_DIR");
245+
deleteTestEnvValue("PI_CODING_AGENT_DIR");
243246
// Prefer test-controlled ports over developer overrides (avoid port collisions across tests/workers).
244247
deleteTestEnvValue("OPENCLAW_GATEWAY_PORT");
245248
deleteTestEnvValue("OPENCLAW_BRIDGE_ENABLED");

0 commit comments

Comments
 (0)