@@ -14,6 +14,7 @@ import {
1414import type { ContextEngine } from "../../context-engine/types.js" ;
1515import { getGlobalHookRunner } from "../../plugins/hook-runner-global.js" ;
1616import { clearMemoryPluginState , registerMemoryPromptSection } from "../../plugins/memory-state.js" ;
17+ import { captureEnv , setTestEnvValue } from "../../test-utils/env.js" ;
1718import { testing as cliBackendsTesting } from "../cli-backends.js" ;
1819import { hashCliSessionText } from "../cli-session.js" ;
1920import { resetContextWindowCacheForTest } from "../context.js" ;
@@ -28,6 +29,7 @@ import {
2829} from "./prepare.js" ;
2930
3031const getRuntimeConfigMock = vi . hoisted ( ( ) => vi . fn ( ( ) => ( { } ) ) ) ;
32+ let sessionFileEnvSnapshot : ReturnType < typeof captureEnv > | undefined ;
3133
3234vi . mock ( "../../config/config.js" , ( ) => ( {
3335 getRuntimeConfig : getRuntimeConfigMock ,
@@ -180,7 +182,8 @@ function createSessionFile() {
180182 // Prepare tests use canonical OpenClaw session paths because several cases
181183 // assert that external or stale transcript paths are ignored.
182184 const dir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-cli-prepare-" ) ) ;
183- vi . stubEnv ( "OPENCLAW_STATE_DIR" , dir ) ;
185+ sessionFileEnvSnapshot ??= captureEnv ( [ "OPENCLAW_STATE_DIR" ] ) ;
186+ setTestEnvValue ( "OPENCLAW_STATE_DIR" , dir ) ;
184187 const sessionFile = path . join ( dir , "agents" , "main" , "sessions" , "session-test.jsonl" ) ;
185188 fs . mkdirSync ( path . dirname ( sessionFile ) , { recursive : true } ) ;
186189 fs . writeFileSync (
@@ -263,6 +266,8 @@ describe("shouldSkipLocalCliCredentialEpoch", () => {
263266 resetContextWindowCacheForTest ( ) ;
264267 clearMemoryPluginState ( ) ;
265268 vi . unstubAllEnvs ( ) ;
269+ sessionFileEnvSnapshot ?. restore ( ) ;
270+ sessionFileEnvSnapshot = undefined ;
266271 } ) ;
267272
268273 it ( "skips local cli auth only when a profile-owned execution was prepared" , ( ) => {
0 commit comments