Durable sessions and isolated agent workers
hard 7 minA long-running task outlives individual model calls and workers.
Video lesson · Chapter 1 of 4
The grant expires during the lunch break
Theory
Read or listen first. About 2 min.
What it is
A long-running task outlives individual model calls and workers. Persist user intent, tool outcomes, approvals and task progress in a durable session. Derive a bounded context for the next call from that record. A worker can restart and a context summary can be replaced while the authoritative events remain available. External services retain authority over their own state.
Why it exists
Treat a model proposal as a proposed action. Before an effect, verify current identity, approval scope, parameters, expiry and prior execution. Reconstruct the job from its durable events if a worker disappears. Context compaction should preserve unresolved commitments and links to evidence; grade it by the resumed task’s outcome. A fluent summary alone cannot demonstrate safe recovery.
Resume a draft after approval expires
Inside this chapter
Chapter sources- Context compaction
- Retain unsettled user constraints, resource versions and references to durable tool results when summarizing earlier turns. Compare compressed and uncompressed continuations on the same frozen task, including restrictions that only matter near completion.
Compaction replaces old turns with a summary so the next model call fits the context window. Summaries naturally keep the main goal and drop conditions that seem minor, but those conditions are often what makes a later action safe. Keep pending commitments, approvals, resource versions and references to durable tool results as structured fields rather than prose, and evaluate compaction by running the rest of a frozen task with and without it.
Example. A summary preserves “draft the report” but drops “send after I approve.” A continuation that emails it fails even if the text is excellent.
Another example. A travel agent's summary keeps "book Lisbon trip" but drops "budget under 800". The resumed run books a 1,100 itinerary. A structured constraints field carried across compaction prevents it.
What the summary must keep
Item from earlier turns Kept in summary? Consequence Task: draft the report yes draft continues Constraint: send only after I approve no email sent early, task fails Pointer to draft v4 in storage yes no need to regenerate Old tool chatter no fine, nothing depends on it Unresolved commitments outrank conversational detail. - Delegated execution
- The orchestrator should grant a worker only the tools and resources needed for its attempt. The service owning a side effect rechecks the grant at commit time. A checkpoint containing an earlier approval is evidence about history.
Give each worker the narrowest set of tools and resources its attempt needs, scoped to the current task and version. The service that performs the side effect checks the grant itself at the moment of commit: who, what exact parameters, until when, and whether it already ran. That way a stale checkpoint, a replayed message or a confused worker cannot turn old approval into a new action.
Example. An approval covered sending version 3 before 15:00. A replacement worker prepares version 4 at 15:04 and needs a fresh decision.
Another example. A finance agent was approved to pay invoice 881 for 1,200 before Friday. On Monday a resumed worker tries the same payment; the payments service rejects it because the grant expired, and the request returns to the approver.
Approval checked at commit time
The service that owns the effect makes the final check. A checkpoint saying approved is only history. - Outcome-based UI evaluation
- In a disposable workspace, observe the current UI, perform an approved action, then inspect the application state. Multiple action sequences may meet the same goal. Grade both final state and protected invariants.
UI agents act through screenshots, clicks and typing, and many action sequences can reach the same goal. Grading by matching a click path is brittle; grading by what the UI displays is unreliable, because a toast can appear before a save fails. Reset a disposable environment per trial, then query the application's real state (database rows, files, outbox) for both the goal and the things that must not happen.
Example. The interface displays “saved.” Reopen the draft to check its contents and confirm the outbox is empty.
Another example. A form-filling agent clicks Submit and sees "Thank you". The evaluation queries the backend and finds the date field stored as blank because of a locale format mismatch, so the trial fails.
Act, then inspect real state - 1Fresh workspacedisposable app instance
- 2Observe UIscreenshot of the editor
- 3Approved actiontype text, click Save
- 4UI says savednot trusted on its own
- 5Inspect statereopen draft, check outbox empty
Grade the stored draft and the protected invariant, not the toast message.
- Durable event log as source of truth
- The session service appends every user message, tool result, approval and decision as an immutable event. Model context, summaries and worker memory are all derived from this log, so any of them can be rebuilt after a crash.
Because events are immutable and ordered, you can audit exactly what the agent knew at each step and replay it with a new context builder. Store large tool outputs by reference so the log stays small, and make external services, not the log, the authority on whether an effect actually happened.
Example. A worker dies mid-task. A new worker replays events 1 to 57 for session s-19, rebuilds the context, sees that the send in event 55 is still pending, and continues from there.
Everything else is derived from the log
Workers write events back; they never hold the only copy of state. - Sandboxed worker isolation
- Each agent attempt runs in an isolated environment (container or micro-VM) with its own filesystem, limited network egress and short-lived credentials scoped to the task. A compromised or confused worker cannot reach other sessions or long-lived secrets.
Isolation is what lets you run model-chosen commands at all. Pair it with the commit-time checks above: the sandbox limits what a worker can touch, and the effect-owning service decides whether a specific action is allowed.
Example. A coding agent running untrusted repository scripts gets a fresh sandbox with network access only to the package mirror and a token that can push to one branch for one hour.
What the sandbox limits
Filesystem
Fresh per attempt, discarded afterwards.
Network
Allow-list of hosts the task needs.
Credentials
Short-lived, scoped to one task.
Resources
CPU, memory and time caps.
How it works
Step through the idea, or press Play.
Event ledger. Draft stored with a pending send
Understand it
Worked example
Resume a draft after approval expires
- 1
Draft stored with a pending send
A durable event records the draft version, operation key and original approval expiry. It can be read after the worker vanishes.
- 2
Resume with an unresolved action
The builder restores the draft pointer and identifies the send as pending. It includes the user’s constraint and latest tool acknowledgement.
- 3
Propose sending version four
A newly provisioned worker uses the reconstructed context. Its proposal goes through the effect boundary like any other attempt.
- 4
Expired grant blocks send
The gate looks up current scope and the effect ledger. It leaves the draft intact and records a new request for approval.
Follow the handoffs
Session state across the worker loss
Summary
The whole topic in one minute.
The durable session records history. Only the currently valid grant can authorize this specific external effect.
Treat a model proposal as a proposed action. Before an effect, verify current identity, approval scope, parameters, expiry and prior execution. Reconstruct the job from its durable events if a worker disappears. Context compaction should preserve unresolved commitments and links to evidence; grade it by the resumed task’s outcome. A fluent summary alone cannot demonstrate safe recovery.