[FEATURE] Expose remaining session/container lifespan + advance-shutdown warning so long-running sessions can checkpoint and hand off
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Problem Statement
A Cowork/headless session runs inside an ephemeral container that is reclaimed after a wall-clock/operation-time limit (or on inactivity). When that happens mid-task, the session just ends — in Cowork it surfaces as "Cloud session stopped responding." The session has no way to (a) know how much runtime it has left, or (b) get advance warning before the container is reclaimed. So it can't checkpoint its state or schedule a continuation before it dies, and whatever wasn't already persisted is lost.
Please note this is distinct from context/compaction and should not be merged with it. This is orthogonal to context-window/compaction issues like #75203 and anthropics/claude-agent-sdk-python#772. Compaction is a token problem that happens within a living session. This is a wall-clock/lifespan problem: the container itself is reclaimed. You can hit either independently — a session well under its context limit still dies when its container's lifespan ends — so defending against compaction does nothing for this.
Proposed Solution
Two complementary capabilities:
- Lifespan visibility — let a session query its own remaining container runtime budget (even a coarse number/percentage), analogous to the remaining-context ask in #75203 but for wall-clock rather than tokens.
- Advance-shutdown warning — fire an event/hook (e.g. a threshold at N minutes remaining, or a
pre_reclaim/session_pausesignal) before the container is reclaimed, giving the session a bounded window to flush durable state, write a heartbeat, and schedule its own continuation.
Either one solves most of the problem; both together make durable multi-session orchestration dependable. The ideal UX: an agent running a long task can check how much runtime remains, and reliably receive a "you have ~N minutes left" signal early enough to checkpoint and hand off to a fresh session cleanly, instead of being cut off mid-work.
Alternative Solutions
Because a session can't observe its own state, the current workaround is to build an external process/MCP server that watches the session from outside and tries to infer when it's about to be reclaimed — an orchestrator observing state the session can't see for itself. It works, roughly, but it's fragile and everyone reinvents it. Other partial workarounds: scheduling continuation runs on a fixed timer (which risks firing while the previous run is still alive, or long after it died) and persisting state to disk after every step (heavy, and still blind to when the cutoff is actually coming). A first-class signal would retire this whole category of brittle workarounds.
Priority
High - Significant impact on productivity
Feature Category
Developer tools/SDK
Use Case Example
I orchestrate multi-hour business-development pipelines in Cowork that reliably exceed a single session's lifespan. A run sources, scores, and enriches a large candidate list — far more work than one session's container will live for, so it has to be done in session-sized pieces that each checkpoint durable state and hand off to a continuation. Today I can't size those pieces or trigger a clean hand-off, because the session can't see how much runtime it has left and gets no warning before it's cut off; it just hits the wall mid-run ("Cloud session stopped responding") and loses whatever wasn't already persisted. With this feature the running session would notice it's approaching its lifespan limit (or receive a "~N minutes left" event), flush its progress ledger, and schedule a continuation — so the next session resumes cleanly instead of me discovering a dead run and re-doing work.
Additional Context
Related issues (same family of pain, but none is a clean home for this specific lifespan ask): #63023 (background agents silently die on pause/resume, work lost — same failure mode, filed as a bug), #29096 (graceful shutdown signal for headless sessions, closed as a duplicate; cleanup-focused, not lifespan-aware), #29567 (graceful degradation on session interruption), and #28229 (native agent scheduling / daemon mode — the continuation side). Distinct from the context/compaction requests #75203 and anthropics/claude-agent-sdk-python#772, which this issue deliberately does not duplicate.