diff --git a/src/session-manager.mjs b/src/session-manager.mjs index 34d42fa..44a17f2 100644 --- a/src/session-manager.mjs +++ b/src/session-manager.mjs @@ -20,6 +20,9 @@ import { AcpClient } from "./acp-client.mjs"; import { ProxyMcpServer } from "./mcp-server.mjs"; export const SESSION_TTL_MS = parseInt(process.env.PROXY_SESSION_TTL_MS || "120000", 10); +/** Hard deadline for a single in-flight turn (thinking-heavy vision turns + * legitimately run long; the idle TTL must not kill them mid-turn). */ +export const TURN_DEADLINE_MS = parseInt(process.env.PROXY_TURN_DEADLINE_MS || "900000", 10); export const MAX_CONCURRENT_SESSIONS = parseInt(process.env.PROXY_MAX_CONCURRENT_SESSIONS || "4", 10); export const TOOL_CALL_BATCH_MS = parseInt(process.env.PROXY_TOOL_CALL_BATCH_MS || "50", 10); const REAL_AGENT = process.env.CURSOR_AGENT_BIN || "/usr/local/bin/agent.real"; @@ -297,10 +300,12 @@ export class CursorSession { if (content?.type === "text" && content.text) { this._handlers.onAssistantDelta(content.text); } + this._touchTimer(); return; } // agent_thought_chunk and other updates are ignored for OpenAI output. + this._touchTimer(); } /** @@ -309,16 +314,17 @@ export class CursorSession { runTurn(blocks, handlers) { this._handlers = handlers; this.lastActivityAt = Date.now(); - this._resetTimer(); if (blocks === null) { // Follow-up after tool_calls: same session/prompt is still in flight. this._hasToolCallInTurn = false; + this._resetTimer(); return; } this._hasToolCallInTurn = false; this._promptInFlight = true; + this._resetTimer(); this.acp .request("session/prompt", { sessionId: this.acpSessionId, @@ -401,16 +407,27 @@ export class CursorSession { _resetTimer() { if (this.timeoutHandle) clearTimeout(this.timeoutHandle); + // While a turn is in flight the TTL acts as a hard turn-deadline + // (longer than idle): thinking-heavy turns legitimately run for many + // minutes without emitting chunks. + const timeout = this._promptInFlight ? TURN_DEADLINE_MS : SESSION_TTL_MS; this.timeoutHandle = setTimeout(async () => { this.log(`[cursor:${this.sessionId}] TTL expired (idle ${SESSION_TTL_MS}ms)`); await this.destroy(); - }, SESSION_TTL_MS); + }, timeout); + } + + /** Re-arm the TTL timer after observed activity (mid-turn chunks etc). */ + _touchTimer() { + if (this.timeoutHandle) this._resetTimer(); } _finishTurn(reason, usage) { if (!this._handlers) return; const handlers = this._handlers; this._handlers = null; + this._promptInFlight = false; + this._resetTimer(); void Promise.resolve(handlers.onFinish(reason, usage)); } }