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) {