
























@@ -30,6 +30,8 @@ import { createMemoryCoreTestHarness } from "./test-helpers.js";
3030const { createTempWorkspace } = createMemoryCoreTestHarness();
3131const DREAMING_TEST_BASE_TIME = new Date("2026-04-05T10:00:00.000Z");
3232const DREAMING_TEST_DAY = "2026-04-05";
33+const originalDreamingTestFast = process.env.OPENCLAW_TEST_FAST;
34+const originalDreamingStateDir = process.env.OPENCLAW_STATE_DIR;
3335const EMPTY_SESSION_CONTENT_HASH =
3436"75a11da44c802486bc6f65640aa48a730f0f684c5c07a42ba3cd1735eb3fb070";
3537const LIGHT_DREAMING_TEST_CONFIG: OpenClawConfig = {
@@ -59,6 +61,24 @@ const LIGHT_DREAMING_TEST_CONFIG: OpenClawConfig = {
5961},
6062};
616364+function setDreamingTestEnv(stateDir: string): void {
65+Reflect.set(process.env, "OPENCLAW_TEST_FAST", "1");
66+Reflect.set(process.env, "OPENCLAW_STATE_DIR", stateDir);
67+}
68+69+function restoreDreamingTestEnv(): void {
70+if (originalDreamingTestFast === undefined) {
71+Reflect.deleteProperty(process.env, "OPENCLAW_TEST_FAST");
72+} else {
73+Reflect.set(process.env, "OPENCLAW_TEST_FAST", originalDreamingTestFast);
74+}
75+if (originalDreamingStateDir === undefined) {
76+Reflect.deleteProperty(process.env, "OPENCLAW_STATE_DIR");
77+} else {
78+Reflect.set(process.env, "OPENCLAW_STATE_DIR", originalDreamingStateDir);
79+}
80+}
81+6282function requireCandidateByKey<T extends { key: string }>(candidates: T[], key: string): T {
6383const candidate = candidates.find((entry) => entry.key === key);
6484if (!candidate) {
@@ -947,8 +967,7 @@ describe("memory-core dreaming phases", () => {
947967948968it("checkpoints session transcript ingestion and skips unchanged transcripts", async () => {
949969const workspaceDir = await createDreamingWorkspace();
950-vi.stubEnv("OPENCLAW_TEST_FAST", "1");
951-vi.stubEnv("OPENCLAW_STATE_DIR", path.join(workspaceDir, ".state"));
970+setDreamingTestEnv(path.join(workspaceDir, ".state"));
952971const sessionsDir = resolveSessionTranscriptsDirForAgent("main");
953972await fs.mkdir(sessionsDir, { recursive: true });
954973const transcriptPath = path.join(sessionsDir, "dreaming-main.jsonl");
@@ -1022,7 +1041,7 @@ describe("memory-core dreaming phases", () => {
10221041([target]) => typeof target === "string" && target === transcriptPath,
10231042).length;
10241043readSpy.mockRestore();
1025-vi.unstubAllEnvs();
1044+restoreDreamingTestEnv();
10261045}
1027104610281047expect(transcriptReadCount).toBeLessThanOrEqual(1);
@@ -1051,8 +1070,7 @@ describe("memory-core dreaming phases", () => {
10511070it("keeps primary session transcripts out of configured subagent workspaces", async () => {
10521071const workspaceDir = await createDreamingWorkspace();
10531072const subagentWorkspaceDir = await createDreamingWorkspace();
1054-vi.stubEnv("OPENCLAW_TEST_FAST", "1");
1055-vi.stubEnv("OPENCLAW_STATE_DIR", path.join(workspaceDir, ".state"));
1073+setDreamingTestEnv(path.join(workspaceDir, ".state"));
1056107410571075const mainSessionsDir = resolveSessionTranscriptsDirForAgent("main");
10581076const subagentSessionsDir = resolveSessionTranscriptsDirForAgent("agi-ceo");
@@ -1122,7 +1140,7 @@ describe("memory-core dreaming phases", () => {
11221140await triggerLightDreaming(beforeAgentReply, workspaceDir, 5);
11231141});
11241142} finally {
1125-vi.unstubAllEnvs();
1143+restoreDreamingTestEnv();
11261144}
1127114511281146const mainCorpus = await fs.readFile(
@@ -1141,8 +1159,7 @@ describe("memory-core dreaming phases", () => {
1141115911421160it("redacts sensitive session content before writing session corpus", async () => {
11431161const workspaceDir = await createDreamingWorkspace();
1144-vi.stubEnv("OPENCLAW_TEST_FAST", "1");
1145-vi.stubEnv("OPENCLAW_STATE_DIR", path.join(workspaceDir, ".state"));
1162+setDreamingTestEnv(path.join(workspaceDir, ".state"));
11461163const sessionsDir = resolveSessionTranscriptsDirForAgent("main");
11471164await fs.mkdir(sessionsDir, { recursive: true });
11481165const transcriptPath = path.join(sessionsDir, "dreaming-main.jsonl");
@@ -1198,7 +1215,7 @@ describe("memory-core dreaming phases", () => {
11981215await triggerLightDreaming(beforeAgentReply, workspaceDir, 5);
11991216});
12001217} finally {
1201-vi.unstubAllEnvs();
1218+restoreDreamingTestEnv();
12021219}
1203122012041221const corpusPath = path.join(
@@ -1215,8 +1232,7 @@ describe("memory-core dreaming phases", () => {
1215123212161233it("skips dreaming-generated narrative transcripts during session ingestion", async () => {
12171234const workspaceDir = await createDreamingWorkspace();
1218-vi.stubEnv("OPENCLAW_TEST_FAST", "1");
1219-vi.stubEnv("OPENCLAW_STATE_DIR", path.join(workspaceDir, ".state"));
1235+setDreamingTestEnv(path.join(workspaceDir, ".state"));
12201236const sessionsDir = resolveSessionTranscriptsDirForAgent("main");
12211237await fs.mkdir(sessionsDir, { recursive: true });
12221238const transcriptPath = path.join(sessionsDir, "dreaming-narrative.jsonl");
@@ -1291,7 +1307,7 @@ describe("memory-core dreaming phases", () => {
12911307{ trigger: "heartbeat", workspaceDir },
12921308);
12931309} finally {
1294-vi.unstubAllEnvs();
1310+restoreDreamingTestEnv();
12951311}
1296131212971313await expectPathMissing(
@@ -1308,8 +1324,7 @@ describe("memory-core dreaming phases", () => {
1308132413091325it("skips dreaming transcripts when the session store identifies them before bootstrap lands", async () => {
13101326const workspaceDir = await createDreamingWorkspace();
1311-vi.stubEnv("OPENCLAW_TEST_FAST", "1");
1312-vi.stubEnv("OPENCLAW_STATE_DIR", path.join(workspaceDir, ".state"));
1327+setDreamingTestEnv(path.join(workspaceDir, ".state"));
13131328const sessionsDir = resolveSessionTranscriptsDirForAgent("main");
13141329await fs.mkdir(sessionsDir, { recursive: true });
13151330const transcriptPath = path.join(sessionsDir, "dreaming-narrative.jsonl");
@@ -1387,7 +1402,7 @@ describe("memory-core dreaming phases", () => {
13871402{ trigger: "heartbeat", workspaceDir },
13881403);
13891404} finally {
1390-vi.unstubAllEnvs();
1405+restoreDreamingTestEnv();
13911406}
1392140713931408await expectPathMissing(
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。