fix(session): turn-deadline TTL separate from idle TTL
build-and-push / build (push) Successful in 3m55s

Long thinking-heavy turns (multi-image vision extraction) legitimately
run >120s without emitting chunks; the idle TTL killed cursor-agent
mid-turn, surfacing to HTTP clients as an empty assistant turn.

- TURN_DEADLINE_MS (default 900s) applies while a prompt is in flight;
  SESSION_TTL_MS (idle, 120s) applies between turns
- _touchTimer() re-arms on every session/update (chunks, thoughts)
- _finishTurn clears _promptInFlight and re-arms the idle TTL

Verified with the real 11-photo TTN replay that previously died with
'LLM stream returned empty assistant turn' after mid-turn TTL kills.
This commit is contained in:
Ubuntu
2026-09-27 13:43:58 +00:00
parent b96824a978
commit 15fa287e1b
+19 -2
View File
@@ -20,6 +20,9 @@ import { AcpClient } from "./acp-client.mjs";
import { ProxyMcpServer } from "./mcp-server.mjs"; import { ProxyMcpServer } from "./mcp-server.mjs";
export const SESSION_TTL_MS = parseInt(process.env.PROXY_SESSION_TTL_MS || "120000", 10); 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 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); 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"; 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) { if (content?.type === "text" && content.text) {
this._handlers.onAssistantDelta(content.text); this._handlers.onAssistantDelta(content.text);
} }
this._touchTimer();
return; return;
} }
// agent_thought_chunk and other updates are ignored for OpenAI output. // agent_thought_chunk and other updates are ignored for OpenAI output.
this._touchTimer();
} }
/** /**
@@ -309,16 +314,17 @@ export class CursorSession {
runTurn(blocks, handlers) { runTurn(blocks, handlers) {
this._handlers = handlers; this._handlers = handlers;
this.lastActivityAt = Date.now(); this.lastActivityAt = Date.now();
this._resetTimer();
if (blocks === null) { if (blocks === null) {
// Follow-up after tool_calls: same session/prompt is still in flight. // Follow-up after tool_calls: same session/prompt is still in flight.
this._hasToolCallInTurn = false; this._hasToolCallInTurn = false;
this._resetTimer();
return; return;
} }
this._hasToolCallInTurn = false; this._hasToolCallInTurn = false;
this._promptInFlight = true; this._promptInFlight = true;
this._resetTimer();
this.acp this.acp
.request("session/prompt", { .request("session/prompt", {
sessionId: this.acpSessionId, sessionId: this.acpSessionId,
@@ -401,16 +407,27 @@ export class CursorSession {
_resetTimer() { _resetTimer() {
if (this.timeoutHandle) clearTimeout(this.timeoutHandle); 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.timeoutHandle = setTimeout(async () => {
this.log(`[cursor:${this.sessionId}] TTL expired (idle ${SESSION_TTL_MS}ms)`); this.log(`[cursor:${this.sessionId}] TTL expired (idle ${SESSION_TTL_MS}ms)`);
await this.destroy(); 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) { _finishTurn(reason, usage) {
if (!this._handlers) return; if (!this._handlers) return;
const handlers = this._handlers; const handlers = this._handlers;
this._handlers = null; this._handlers = null;
this._promptInFlight = false;
this._resetTimer();
void Promise.resolve(handlers.onFinish(reason, usage)); void Promise.resolve(handlers.onFinish(reason, usage));
} }
} }