earendil-works/pi · error · AggregateError

Failed to replace remote session attachment

Error message

Failed to replace remote session attachment

What it means

Wrapping error from RemoteSession.#prepareReplacement: detaching the previous session lease failed while replacing the attachment with a new session. If re-detaching the new lease also failed, both errors are combined into an AggregateError under this message.

Solutions

  1. Inspect the inner error(s) for the underlying transport/server cause
  2. Retry the session replacement once the connection is stable
  3. Reconnect the client if detach calls keep failing
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at packages/coding-agent/src/client/remote-session.ts:286 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24). Data as JSON: /api/errors/21801164bd552de0. Report an issue: GitHub.

Appendix: source

Thrown at packages/coding-agent/src/client/remote-session.ts:286

		const next = await prepare();
		await this.#assertNotDisposedAfterAwait(next);
		const snapshot = next.snapshot;
		if (!snapshot) {
			await this.#detach(next);
			throw new Error(`Session ${next.id} did not provide a snapshot`);
		}
		if (previous && previous.id !== next.id && previous.attached && this.phase !== "idle") {
			await this.#detach(next);
			throw new Error(`Cannot ${operation} a session while session is ${this.phase ?? "unavailable"}`);
		}
		if (previous && previous.id !== next.id && previous.attached) {
			try {
				await previous.detach();
			} catch (error) {
				try {
					await this.#detach(next);
				} catch (cleanupError) {
					throw new AggregateError([error, cleanupError], "Failed to replace remote session attachment");
				}
				throw error;
			}
		}
		await this.#assertNotDisposedAfterAwait(next);
		this.#bind(next, snapshot);
	}

	async #runIdleOperation(
		operation: "setModel" | "setThinking",
		description: string,
		run: () => Promise<void>,
	): Promise<void> {
		this.#assertAvailable();
		this.#requireHandle();
		if (this.phase !== "idle") {
			throw new Error(`Cannot ${description} while session is ${this.phase ?? "unavailable"}`);
		}

View on GitHub (pinned to 4af9d21d3b)