From 0b821908bc1daf5945fcd4fedc21f8141988bf7f Mon Sep 17 00:00:00 2001 From: nicegui <147186267+nicegui@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:39:10 +0800 Subject: [PATCH] cursor: a tool-result continuation in a Run's last minute rebuilds before writing results, not 502 at the lifetime (#157, nicegui) The 15-minute absolute lifetime spans all of a retained Run's tool roundtrips. A tool-result continuation that arrives with no more than min(60s, lifetime/10) left now retires the owner (410 "retired before lifetime deadline") and rebuilds from the supplied history before writing any result, instead of resuming and failing mid-segment with "native Run lifetime expired". Replays of an already submitted batch keep their path. Merging main resolved a conflict with a7614aa (#143): expireIn sets expiresAt as well as the timer. Reviewed at 75e481a (nicegui's 6f03753 plus the merge of main): - packages/cursor `bun test` (isolated HOME) 267 pass, 0 fail, three times; `bun scripts/check.mjs` ok; PR CI run 38868447170 green, attempt 1. - The near-expiry test fails on the source without the reserve check. - Near-deadline injected by shifting Date.now() from the owner's expiresAt, not by waiting: 62s left resumes on the same Run (1 Run, 1 result written); 59s and 0.5s left rebuild (2 Runs, no result written). With the check removed by hand, 59s and 0.5s go red. --- packages/cursor/README.md | 5 ++++- packages/cursor/native-e2e.test.mjs | 20 +++++++++++++++++++- packages/cursor/native-session.mjs | 9 +++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/cursor/README.md b/packages/cursor/README.md index da00fda..f81bb96 100644 --- a/packages/cursor/README.md +++ b/packages/cursor/README.md @@ -94,7 +94,10 @@ Tool results can continue a retained Run in the same plugin process. The owner uses the same session/cache key and first user message as `conversation_id`, so Task subagents sharing their parent's session can run independently. Retention waits at most 120 seconds for a result batch and 15 minutes for a Run's total -lifetime. The pool holds at most 8 owners per account and 32 per process, with +lifetime. A continuation arriving in the last minute of that lifetime rebuilds +from supplied history before submitting results to the old Run. The absolute +deadline still applies to an active segment; it is never extended by activity. +The pool holds at most 8 owners per account and 32 per process, with bounded history, blobs, arguments, and pending writes. When pending writes reach their bound (a long history asks for its blobs at once, and a slow link drains them slowly), the Run waits for the link to drain, and fails only if it diff --git a/packages/cursor/native-e2e.test.mjs b/packages/cursor/native-e2e.test.mjs index 38f6bb0..d7a0917 100644 --- a/packages/cursor/native-e2e.test.mjs +++ b/packages/cursor/native-e2e.test.mjs @@ -1,5 +1,5 @@ import "./nonet.mjs" -import { afterEach, beforeEach, expect, test } from "bun:test" +import { afterEach, beforeEach, expect, spyOn, test } from "bun:test" import http2 from "node:http2" import { randomUUID } from "node:crypto" import { getEventListeners } from "node:events" @@ -320,6 +320,24 @@ test("native retention: Task parent awaiting results permits a child sharing ses expect(a.runs).toHaveLength(2) }) +test("native retention: a near-expiry Run rebuilds before submitting results or exposing output", async () => { + const a = await agent(), account = auth(), chat = initial() + const first = await a.ask(account, chat) + expect(first.status).toBe(200) + const owner = [...nativeSessions.entries][0] + const now = Date.now.bind(Date) + const clock = spyOn(Date, "now").mockImplementation(() => now() + NATIVE_LIMITS.lifetime - 1000) + try { + const marker = "result-before-lifetime-boundary" + rebuilt(await a.ask(account, continueWith(chat, first.body, [marker])), marker) + expect(a.runs).toHaveLength(2) + expect(a.results).toEqual([]) + expect(owner.state).toBe("expired") + expect(nativeSessions.entries.has(owner)).toBe(false) + expect(a.errors).toEqual([]) + } finally { clock.mockRestore() } +}) + for (const limit of ["resultWait", "lifetime"]) test(`native retention: ${limit} expiry rebuilds supplied results on a fresh stateless Run`, async () => { // #143: the lifetime counts from the Run's start, so a short one set before // the first ask expired a first Run slower than it on a loaded machine. diff --git a/packages/cursor/native-session.mjs b/packages/cursor/native-session.mjs index 16a6ef5..e739143 100644 --- a/packages/cursor/native-session.mjs +++ b/packages/cursor/native-session.mjs @@ -381,6 +381,14 @@ export class NativeSessionPool { return { owner, batch, replay: true, segment: batch.next } } if (owner.state !== "awaiting-results" || owner.segment !== batch || !batch.clean) return fallback("owner busy") + // A retained Run's absolute timer spans all tool roundtrips. Rebuild + // near its deadline before writing this batch: otherwise a new Chat + // segment can lose its transport after already exposing output. + const reserve = Math.min(60_000, owner.limits.lifetime / 10) + if (owner.expiresAt - Date.now() <= reserve) { + owner.fail({ status: 410, message: "native Run retired before lifetime deadline" }, "expired") + return fallback("owner near lifetime deadline") + } return { owner, batch, results, encoded, digest, chat } } catch (e) { if (!(e instanceof NativeSessionError)) throw e @@ -469,6 +477,7 @@ class NativeSession { // the whole lifetime; a test brings it in after the first answer expireIn(ms) { clearTimeout(this.lifetimeTimer) + this.expiresAt = Date.now() + ms this.lifetimeTimer = later(() => this.fail({ status: 410, message: "native Run lifetime expired" }, "expired"), ms) } retain(n) {