Skip to content

Commit 679a46d

Browse files
fix(session): surface previous-transcript archive failures on /new rotation (#81984) (#85586)
Summary: - Adds an optional archive-error callback for session transcript archiving, wires `/new` reset rotation to log previous-transcript archive failures, adds regression coverage, and updates the changelog. - Reproducibility: yes. source-reproducible. Current main catches and ignores `archiveFileOnDisk` failures ins ... and the source PR proof exercises the same rename failure boundary with a real filesystem permission error. Automerge notes: - No ClawSweeper repair was needed after automerge opt-in. Validation: - ClawSweeper review passed for head 9d5f4c0. - Required merge gates passed before the squash merge. Prepared head SHA: 9d5f4c0 Review: #85586 (comment) Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com> Co-authored-by: clawsweeper[bot] <274271284+clawsweeper[bot]@users.noreply.github.com> Approved-by: takhoffman Co-authored-by: takhoffman <[email protected]>
1 parent a94f344 commit 679a46d

5 files changed

Lines changed: 134 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Docs: https://docs.openclaw.ai
4848
### Fixes
4949

5050
- Windows installer: fail Git checkout installs when `pnpm install` or `pnpm build` fails instead of writing a wrapper to a missing CLI build.
51+
- Sessions: surface previous-transcript archive failures during `/new` rotation so disk rename errors are logged instead of silently hiding stranded transcript files. Fixes #81984. (#85586, from #82081) Thanks @0xghost42.
5152
- Agents: keep parallel OpenAI-compatible tool-call deltas in separate argument buffers so interleaved tool calls no longer corrupt streamed arguments. (#82263) Thanks @luna-system.
5253
- Memory/doctor: report missing or unusable QMD workspace directories as workspace failures instead of generic binary failures. (#63167) Thanks @sercada.
5354
- Debug proxy: record CONNECT client-socket errors and destroy the paired upstream socket so abrupt client disconnects no longer leak tunnel resources. (#82444) Thanks @SebTardif.

‎src/auto-reply/reply/session.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,12 @@ export async function initSessionState(params: {
846846
sessionFile: previousSessionEntry.sessionFile,
847847
agentId,
848848
reason: "reset",
849+
onArchiveError: (error, sourcePath) => {
850+
log.warn(
851+
`failed to archive previous session transcript ${sourcePath} for session ${previousSessionEntry.sessionId}`,
852+
{ error: String(error) },
853+
);
854+
},
849855
});
850856
previousSessionTranscript = resolveStableSessionEndTranscript({
851857
sessionId: previousSessionEntry.sessionId,

‎src/gateway/session-reset-service.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export function archiveSessionTranscriptsForSession(params: {
112112
sessionFile?: string;
113113
agentId?: string;
114114
reason: "reset" | "deleted";
115+
onArchiveError?: (err: unknown, sourcePath: string) => void;
115116
}): string[] {
116117
return archiveSessionTranscriptsForSessionDetailed(params).map((entry) => entry.archivedPath);
117118
}
@@ -122,6 +123,7 @@ export function archiveSessionTranscriptsForSessionDetailed(params: {
122123
sessionFile?: string;
123124
agentId?: string;
124125
reason: "reset" | "deleted";
126+
onArchiveError?: (err: unknown, sourcePath: string) => void;
125127
}): ArchivedSessionTranscript[] {
126128
if (!params.sessionId) {
127129
return [];
@@ -132,6 +134,7 @@ export function archiveSessionTranscriptsForSessionDetailed(params: {
132134
sessionFile: params.sessionFile,
133135
agentId: params.agentId,
134136
reason: params.reason,
137+
onArchiveError: params.onArchiveError,
135138
});
136139
}
137140

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

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import fs from "node:fs";
22
import os from "node:os";
33
import path from "node:path";
4-
import { afterEach, describe, expect, it } from "vitest";
4+
import { afterEach, describe, expect, it, vi } from "vitest";
55
import {
66
onSessionTranscriptUpdate,
77
type SessionTranscriptUpdate,
88
} from "../sessions/transcript-events.js";
9-
import { archiveFileOnDisk } from "./session-transcript-files.fs.js";
9+
import {
10+
archiveFileOnDisk,
11+
archiveSessionTranscriptsDetailed,
12+
} from "./session-transcript-files.fs.js";
1013

1114
const subscriptions: Array<() => void> = [];
1215

1316
afterEach(() => {
17+
vi.restoreAllMocks();
1418
while (subscriptions.length > 0) {
1519
subscriptions.pop()?.();
1620
}
@@ -65,3 +69,113 @@ describe("archiveFileOnDisk transcript updates", () => {
6569
}
6670
});
6771
});
72+
73+
describe("archiveSessionTranscriptsDetailed failure surface", () => {
74+
it("invokes onArchiveError when fs.renameSync fails and returns only successful entries", () => {
75+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "oc-archive-failure-"));
76+
try {
77+
const sessionId = "11111111-1111-4111-8111-111111111111";
78+
const sessionFile = path.join(tmpDir, `${sessionId}.jsonl`);
79+
fs.writeFileSync(sessionFile, '{"type":"session-meta","agentId":"main"}\n');
80+
81+
const renameError = Object.assign(new Error("EACCES: permission denied"), {
82+
code: "EACCES",
83+
});
84+
const renameSpy = vi.spyOn(fs, "renameSync").mockImplementation(() => {
85+
throw renameError;
86+
});
87+
88+
const errors: Array<{ err: unknown; sourcePath: string }> = [];
89+
const archived = archiveSessionTranscriptsDetailed({
90+
sessionId,
91+
storePath: path.join(tmpDir, "store.json"),
92+
sessionFile,
93+
agentId: "main",
94+
reason: "reset",
95+
onArchiveError: (err, sourcePath) => {
96+
errors.push({ err, sourcePath });
97+
},
98+
});
99+
100+
renameSpy.mockRestore();
101+
102+
expect(archived).toEqual([]);
103+
expect(errors.length).toBeGreaterThan(0);
104+
expect(errors[0].err).toBe(renameError);
105+
expect(fs.existsSync(sessionFile)).toBe(true);
106+
} finally {
107+
fs.rmSync(tmpDir, { recursive: true, force: true });
108+
}
109+
});
110+
111+
it("archives normally when no onArchiveError is provided", () => {
112+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "oc-archive-success-"));
113+
try {
114+
const sessionId = "22222222-2222-4222-8222-222222222222";
115+
const sessionFile = path.join(tmpDir, `${sessionId}.jsonl`);
116+
fs.writeFileSync(sessionFile, '{"type":"session-meta","agentId":"main"}\n');
117+
118+
const archived = archiveSessionTranscriptsDetailed({
119+
sessionId,
120+
storePath: path.join(tmpDir, "store.json"),
121+
sessionFile,
122+
agentId: "main",
123+
reason: "reset",
124+
});
125+
126+
expect(archived.length).toBe(1);
127+
expect(archived[0].archivedPath).toContain(".jsonl.reset.");
128+
expect(fs.existsSync(archived[0].archivedPath)).toBe(true);
129+
expect(fs.existsSync(sessionFile)).toBe(false);
130+
} finally {
131+
fs.rmSync(tmpDir, { recursive: true, force: true });
132+
}
133+
});
134+
135+
it("surfaces real chmod archive failures through onArchiveError", () => {
136+
if (process.platform === "win32" || process.getuid?.() === 0) {
137+
return;
138+
}
139+
140+
const tmpDir = fs.realpathSync(
141+
fs.mkdtempSync(path.join(os.tmpdir(), "oc-archive-real-eacces-")),
142+
);
143+
try {
144+
const sessionId = "33333333-3333-4333-8333-333333333333";
145+
const sessionFile = path.join(tmpDir, `${sessionId}.jsonl`);
146+
fs.writeFileSync(sessionFile, '{"type":"session-meta","agentId":"main"}\n');
147+
fs.chmodSync(tmpDir, 0o555);
148+
149+
const errors: Array<{ code?: string; sourcePath: string }> = [];
150+
let archived: ReturnType<typeof archiveSessionTranscriptsDetailed> = [];
151+
try {
152+
archived = archiveSessionTranscriptsDetailed({
153+
sessionId,
154+
storePath: path.join(tmpDir, "store.json"),
155+
sessionFile,
156+
agentId: "main",
157+
reason: "reset",
158+
onArchiveError: (err, sourcePath) => {
159+
const code = (err as NodeJS.ErrnoException | undefined)?.code;
160+
errors.push({ code, sourcePath });
161+
},
162+
});
163+
} finally {
164+
fs.chmodSync(tmpDir, 0o755);
165+
}
166+
167+
expect(archived).toEqual([]);
168+
expect(errors.length).toBeGreaterThan(0);
169+
expect(errors[0].sourcePath).toBe(sessionFile);
170+
expect(errors[0].code).toMatch(/^(EACCES|EPERM)$/);
171+
expect(fs.existsSync(sessionFile)).toBe(true);
172+
} finally {
173+
try {
174+
fs.chmodSync(tmpDir, 0o755);
175+
} catch {
176+
// Already restored.
177+
}
178+
fs.rmSync(tmpDir, { recursive: true, force: true });
179+
}
180+
});
181+
});

‎src/gateway/session-transcript-files.fs.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export function archiveSessionTranscripts(opts: {
152152
* This prevents maintenance operations from mutating paths outside the agent sessions dir.
153153
*/
154154
restrictToStoreDir?: boolean;
155+
onArchiveError?: (err: unknown, sourcePath: string) => void;
155156
}): string[] {
156157
return archiveSessionTranscriptsDetailed(opts).map((entry) => entry.archivedPath);
157158
}
@@ -167,6 +168,11 @@ export function archiveSessionTranscriptsDetailed(opts: {
167168
* This prevents maintenance operations from mutating paths outside the agent sessions dir.
168169
*/
169170
restrictToStoreDir?: boolean;
171+
/**
172+
* Invoked when an individual transcript candidate fails to archive. The
173+
* caller decides whether to log, warn-deliver, or escalate.
174+
*/
175+
onArchiveError?: (err: unknown, sourcePath: string) => void;
170176
}): ArchivedSessionTranscript[] {
171177
const archived: ArchivedSessionTranscript[] = [];
172178
const storeDir =
@@ -194,8 +200,8 @@ export function archiveSessionTranscriptsDetailed(opts: {
194200
sourcePath: candidatePath,
195201
archivedPath: archiveFileOnDisk(candidatePath, opts.reason),
196202
});
197-
} catch {
198-
// Best-effort.
203+
} catch (err) {
204+
opts.onArchiveError?.(err, candidatePath);
199205
}
200206
}
201207
return archived;

0 commit comments

Comments
 (0)