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.
This commit is contained in:
nicegui
2026-10-16 15:39:10 +08:00
committed by GitHub
parent a7614aa2ff
commit 0b821908bc
3 changed files with 32 additions and 2 deletions
+4 -1
View File
@@ -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
+19 -1
View File
@@ -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.
+9
View File
@@ -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) {