Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(session): add error logging and remove TOCTOU in ensureSessionDir

yushene23462dc7253
diff --git a/src/agent/session/session-manager.ts b/src/agent/session/session-manager.ts
index 9ff1961462..1df2ac4431 100644
--- a/src/agent/session/session-manager.ts
+++ b/src/agent/session/session-manager.ts
@@ -280,9 +280,10 @@ export class SessionManager {
}
private enqueue(task: () => Promise<void>) {
- this.queue = this.queue.then(task, task).catch(() => {
- // Swallow storage errors to prevent unhandled rejections.
- // The queue must remain settled so subsequent writes can proceed.
+ this.queue = this.queue.then(task, task).catch((err) => {
+ // Swallow to keep the queue settled so subsequent writes can proceed,
+ // but log for debuggability.
+ console.error("[SessionManager] storage write failed:", err);
});
return this.queue;
}
diff --git a/src/agent/session/storage.ts b/src/agent/session/storage.ts
index b578f3ab32..9b104a242b 100644
--- a/src/agent/session/storage.ts
+++ b/src/agent/session/storage.ts
@@ -23,16 +23,16 @@ export function resolveSessionPath(sessionId: string, options?: SessionStorageOp
export function ensureSessionDir(sessionId: string, options?: SessionStorageOptions) {
const dir = resolveSessionDir(sessionId, options);
- if (!existsSync(dir)) {
- try {
+ // mkdirSync with recursive is idempotent (no-op if dir exists),
+ // so skip the existsSync check to avoid a TOCTOU race.
+ try {
+ mkdirSync(dir, { recursive: true });
+ } catch (err) {
+ // Retry once on transient ENOENT (macOS APFS race condition)
+ if ((err as NodeJS.ErrnoException).code === "ENOENT") {
mkdirSync(dir, { recursive: true });
- } catch (err) {
- // Retry once on transient ENOENT (macOS APFS race condition)
- if ((err as NodeJS.ErrnoException).code === "ENOENT") {
- mkdirSync(dir, { recursive: true });
- } else {
- throw err;
- }
+ } else {
+ throw err;
}
}
}