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