From 43c6b56dfa2093b7ef5b1cb67ed1dae905c6d6a7 Mon Sep 17 00:00:00 2001 From: Harivansh Rathi Date: Mon, 9 Mar 2026 10:01:59 -0700 Subject: [PATCH] fix chat --- .../coding-agent/src/core/gateway/runtime.ts | 55 +++++++++++++------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/packages/coding-agent/src/core/gateway/runtime.ts b/packages/coding-agent/src/core/gateway/runtime.ts index 77c15b1..a509fba 100644 --- a/packages/coding-agent/src/core/gateway/runtime.ts +++ b/packages/coding-agent/src/core/gateway/runtime.ts @@ -39,6 +39,7 @@ import { buildGatewaySessionStateMessages, messageContentToHistoryParts, } from "./session-state.js"; +import { findMostRecentSession } from "../session-manager.js"; export { createGatewaySessionManager, sanitizeSessionKey, @@ -204,7 +205,7 @@ export class GatewayRuntime { sessionKey: string, listener: (event: GatewayEvent) => void, ): Promise<() => void> { - const managedSession = await this.ensureSession(sessionKey); + const managedSession = await this.requireExistingSession(sessionKey); managedSession.listeners.add(listener); listener({ type: "hello", @@ -274,6 +275,32 @@ export class GatewayRuntime { return session ? this.createSnapshot(session) : undefined; } + private async getOrLoadExistingSession( + sessionKey: string, + ): Promise { + const found = this.sessions.get(sessionKey); + if (found) { + found.lastActiveAt = Date.now(); + return found; + } + + if (!findMostRecentSession(this.getGatewaySessionDir(sessionKey))) { + return null; + } + + return this.ensureSession(sessionKey); + } + + private async requireExistingSession( + sessionKey: string, + ): Promise { + const managedSession = await this.getOrLoadExistingSession(sessionKey); + if (!managedSession) { + throw new HttpError(404, `Session not found: ${sessionKey}`); + } + return managedSession; + } + private async ensureSession( sessionKey: string, existingSession?: AgentSession, @@ -377,14 +404,6 @@ export class GatewayRuntime { } } - private getManagedSessionOrThrow(sessionKey: string): ManagedGatewaySession { - const managedSession = this.sessions.get(sessionKey); - if (!managedSession) { - throw new HttpError(404, `Session not found: ${sessionKey}`); - } - return managedSession; - } - private rejectQueuedMessages( managedSession: ManagedGatewaySession, error: string, @@ -807,7 +826,7 @@ export class GatewayRuntime { const action = sessionMatch[2]; if (!action && method === "GET") { - const session = await this.ensureSession(sessionKey); + const session = await this.requireExistingSession(sessionKey); this.writeJson(response, 200, { session: this.createSnapshot(session) }); return; } @@ -852,13 +871,13 @@ export class GatewayRuntime { } if (action === "abort" && method === "POST") { - this.getManagedSessionOrThrow(sessionKey); + await this.requireExistingSession(sessionKey); this.writeJson(response, 200, { ok: this.abortSession(sessionKey) }); return; } if (action === "reset" && method === "POST") { - this.getManagedSessionOrThrow(sessionKey); + await this.requireExistingSession(sessionKey); await this.resetSession(sessionKey); this.writeJson(response, 200, { ok: true }); return; @@ -875,7 +894,7 @@ export class GatewayRuntime { } if (action === "state" && method === "GET") { - const session = await this.ensureSession(sessionKey); + const session = await this.requireExistingSession(sessionKey); this.writeJson(response, 200, this.createSessionState(session)); return; } @@ -1121,7 +1140,7 @@ export class GatewayRuntime { provider: string, modelId: string, ): Promise<{ ok: true; model: { provider: string; modelId: string } }> { - const managed = await this.ensureSession(sessionKey); + const managed = await this.requireExistingSession(sessionKey); const found = managed.session.modelRegistry.find(provider, modelId); if (!found) { throw new HttpError(404, `Model not found: ${provider}/${modelId}`); @@ -1137,7 +1156,7 @@ export class GatewayRuntime { if (limit !== undefined && (!Number.isFinite(limit) || limit < 1)) { throw new HttpError(400, "History limit must be a positive integer"); } - const managed = await this.ensureSession(sessionKey); + const managed = await this.requireExistingSession(sessionKey); const rawMessages = managed.session.messages; const messages: HistoryMessage[] = []; for (const [index, msg] of rawMessages.entries()) { @@ -1162,7 +1181,7 @@ export class GatewayRuntime { sessionKey: string, patch: { name?: string }, ): Promise { - const managed = await this.ensureSession(sessionKey); + const managed = await this.requireExistingSession(sessionKey); if (patch.name !== undefined) { // Labels in pi-mono are per-entry; we label the current leaf entry const leafId = managed.session.sessionManager.getLeafId(); @@ -1180,7 +1199,7 @@ export class GatewayRuntime { if (sessionKey === this.primarySessionKey) { throw new HttpError(400, "Cannot delete primary session"); } - const managed = this.sessions.get(sessionKey); + const managed = await this.requireExistingSession(sessionKey); if (managed) { if (managed.processing) { await managed.session.abort(); @@ -1239,7 +1258,7 @@ export class GatewayRuntime { } private async handleReloadSession(sessionKey: string): Promise { - const managed = await this.ensureSession(sessionKey); + const managed = await this.requireExistingSession(sessionKey); // Reloading config by calling settingsManager.reload() on the session managed.session.settingsManager.reload(); }