Skip to content

Commit f55d2a8

Browse files
committed
test(session-archive): drive a real chmod 0o555 EACCES through archiveSessionTranscriptsDetailed to address clawsweeper real-environment ask on #81984
1 parent 94334af commit f55d2a8

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

‎src/gateway/session-transcript-files.fs.archive-events.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,66 @@ describe("archiveSessionTranscriptsDetailed failure surface (#81984)", () => {
130130
fs.rmSync(tmpDir, { recursive: true, force: true });
131131
}
132132
});
133+
134+
it("surfaces a REAL EACCES (chmod 0o555 parent dir) through onArchiveError, no fs spy", () => {
135+
// clawsweeper feedback on PR #82081: the failure-surface assertion used a
136+
// spy on fs.renameSync. This case removes the spy entirely and induces the
137+
// EACCES on a real macOS/Linux filesystem by chmod'ing the parent dir to
138+
// 0o555 (read+execute, no write) so the in-place sibling rename that
139+
// archiveSessionTranscriptsDetailed performs hits the real kernel errno.
140+
if (process.platform === "win32") {
141+
return;
142+
}
143+
// realpath resolves the /var → /private/var symlink macOS uses for tmp, so
144+
// the assertion against errors[0].sourcePath compares the same shape the
145+
// production code would emit.
146+
const tmpDir = fs.realpathSync(
147+
fs.mkdtempSync(path.join(os.tmpdir(), "oc-archive-real-eaccess-")),
148+
);
149+
try {
150+
const sessionId = "33333333-3333-4333-8333-333333333333";
151+
const sessionFile = path.join(tmpDir, `${sessionId}.jsonl`);
152+
fs.writeFileSync(sessionFile, '{"type":"session-meta","agentId":"main"}\n');
153+
154+
// Real, on-disk EACCES — no module mocks anywhere in this test.
155+
fs.chmodSync(tmpDir, 0o555);
156+
157+
const errors: Array<{ code?: string; sourcePath: string }> = [];
158+
let archived: ReturnType<typeof archiveSessionTranscriptsDetailed>;
159+
try {
160+
archived = archiveSessionTranscriptsDetailed({
161+
sessionId,
162+
storePath: path.join(tmpDir, "store.json"),
163+
sessionFile,
164+
agentId: "main",
165+
reason: "reset",
166+
onArchiveError: (err, sourcePath) => {
167+
const code = (err as NodeJS.ErrnoException | undefined)?.code;
168+
errors.push({ code, sourcePath });
169+
},
170+
});
171+
} finally {
172+
// Restore writeable mode so afterEach cleanup can rmSync the dir.
173+
fs.chmodSync(tmpDir, 0o755);
174+
}
175+
176+
expect(archived).toEqual([]);
177+
expect(errors.length).toBeGreaterThan(0);
178+
// The exact errno depends on platform/fs; EACCES on macOS/Linux,
179+
// sometimes EPERM elsewhere. Assert the source path is the real session
180+
// file and the error carries a system errno (i.e. it's not synthesised).
181+
expect(errors[0].sourcePath).toBe(sessionFile);
182+
expect(errors[0].code).toMatch(/^(EACCES|EPERM)$/);
183+
// The original session file is left in place when the archive rename
184+
// fails, so downstream code can retry on the next /new rotation.
185+
expect(fs.existsSync(sessionFile)).toBe(true);
186+
} finally {
187+
try {
188+
fs.chmodSync(tmpDir, 0o755);
189+
} catch {
190+
// already restored
191+
}
192+
fs.rmSync(tmpDir, { recursive: true, force: true });
193+
}
194+
});
133195
});

0 commit comments

Comments
 (0)