Skip to content

Commit 2fc40b3

Browse files
committed
refactor(memory-core): use replaceFileAtomic for dreaming file writes
Align with the sibling dreaming-dreams-file.ts which already uses replaceFileAtomic from openclaw/plugin-sdk/security-runtime. Replaces three fs.writeFile calls (inline daily, separate report, deep report) with replaceFileAtomic for consistent file I/O patterns within the dreaming module. Test: existing 9 tests pass unchanged, no behavioral change.
1 parent 68ead4d commit 2fc40b3

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

extensions/memory-core/src/dreaming-markdown.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
replaceManagedMarkdownBlock,
1212
withTrailingNewline,
1313
} from "openclaw/plugin-sdk/memory-host-markdown";
14+
import { replaceFileAtomic } from "openclaw/plugin-sdk/security-runtime";
1415
import { updateDeepDreamsFile } from "./dreaming-dreams-file.js";
1516
import { resolveMemoryCoreNowMs, resolveMemoryCoreTimestamp } from "./time.js";
1617

@@ -88,7 +89,14 @@ export async function writeDailyDreamingPhaseBlock(params: {
8889
endMarker: markers.end,
8990
body,
9091
});
91-
await fs.writeFile(inlinePath, withTrailingNewline(updated), "utf-8");
92+
await replaceFileAtomic({
93+
filePath: inlinePath,
94+
content: withTrailingNewline(updated),
95+
mode: 0o600,
96+
preserveExistingMode: true,
97+
tempPrefix: "dreaming-inline",
98+
throwOnCleanupError: true,
99+
});
92100
}
93101

94102
if (shouldWriteSeparate(params.storage)) {
@@ -105,7 +113,13 @@ export async function writeDailyDreamingPhaseBlock(params: {
105113
body,
106114
"",
107115
].join("\n");
108-
await fs.writeFile(reportPath, report, "utf-8");
116+
await replaceFileAtomic({
117+
filePath: reportPath,
118+
content: report,
119+
mode: 0o600,
120+
tempPrefix: "dreaming-report",
121+
throwOnCleanupError: true,
122+
});
109123
}
110124

111125
await appendMemoryHostEvent(params.workspaceDir, {
@@ -141,7 +155,13 @@ export async function writeDeepDreamingReport(params: {
141155
if (shouldWriteSeparate(params.storage)) {
142156
reportPath = resolveSeparateReportPath(params.workspaceDir, "deep", nowMs, params.timezone);
143157
await fs.mkdir(path.dirname(reportPath), { recursive: true });
144-
await fs.writeFile(reportPath, `# Deep Sleep\n\n${body}\n`, "utf-8");
158+
await replaceFileAtomic({
159+
filePath: reportPath,
160+
content: `# Deep Sleep\n\n${body}\n`,
161+
mode: 0o600,
162+
tempPrefix: "dreaming-deep",
163+
throwOnCleanupError: true,
164+
});
145165
}
146166
await appendMemoryHostEvent(params.workspaceDir, {
147167
type: "memory.dream.completed",

0 commit comments

Comments
 (0)