can1357/oh-my-pi · error

Cannot open a session writer before a session file exists

Error message

Cannot open a session writer before a session file exists

What it means

#appendWriter() lazily creates the storage writer used to append entries to the session file. It throws if #sessionFile is not yet set, because there is no file on disk to open a writer against. This is an internal invariant violation: writes must only happen after a session file has been created (new session persisted or a session resumed).

Source

Thrown at packages/coding-agent/src/session/session-manager.ts:790

						new Error("Authoritative session repair was superseded before verification."),
					]);
				}
			} while (this.#atomicRewriteDirty);

			this.#fileIsCurrent = true;
			this.#rewriteRequired = false;
			this.#hasTitleSlot = true;
			this.#clearDiskError();
		} catch (error) {
			if (error instanceof SessionPersistenceIndeterminateError) throw error;
			throw this.#latchIndeterminate(operationError, [toError(error)]);
		} finally {
			if (this.#atomicRewriteFenceEpoch === epoch) this.#atomicRewriteFenceEpoch = null;
		}
	}

	#appendWriter(): SessionStorageWriter {
		if (!this.#sessionFile) throw new Error("Cannot open a session writer before a session file exists");

		if (this.#writer?.isOpen()) return this.#writer;

		this.#writer = this.#storage.openWriter(this.#sessionFile, {
			flags: "a",
			onError: err => this.#noteDiskFailure(err),
		});
		return this.#writer;
	}

	#lineFor(entry: FileEntry): string {
		return `${stringifyJson(prepareEntryForPersistence(entry, this.#blobs)) ?? "null"}\n`;
	}

	#titleSlotLine(): string {
		return serializeTitleSlot({
			title: this.#sessionName,
			source: this.#titleSource,

View on GitHub (pinned to 9690622007)

Solutions

  1. Create or resume a session so a session file exists before appending (call the new-session/save path first).
  2. Check getSessionFile() before appending; if null, initialize the session.
  3. If a disk failure cleared the session file, recover or re-create the session rather than writing.
  4. Report a bug if the high-level API lets you append after a session was clearly created.

Example fix

// before
const mgr = new SessionManager(...);
mgr.appendMessage(msg, null); // throws
// after
const mgr = new SessionManager(...);
await mgr.newSession(); // creates session file
mgr.appendMessage(msg, null);
Defensive patterns

Strategy: validation

Validate before calling

if (mgr.getSessionFile() == null) {
  await mgr.newSession(); // or resume an existing file
}

Type guard

function hasSessionFile(mgr) {
  return mgr.getSessionFile() != null;
}

Try / catch

null

Prevention

When it happens

Trigger: Any append path (appendMessage, appendLabelChange, branch-related writes, etc.) invoked while #sessionFile is null — i.e. writing to a SessionManager that was constructed but whose session was never persisted to disk and never resumed from a file.

Common situations: Calling append APIs on a freshly constructed in-memory SessionManager before the first save/newSession creates the file; a previous disk failure cleared #sessionFile; SDK embedding where the caller skipped session creation.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/1290d2632ff4fda5. Report an issue: GitHub.