Upgrade: workspace provider durability and destroy (core major)
This release makes every workspace provider say where its refs resolve and lets every provider delete what it opened. It is the workspace providers step (d2) of the session-scoped workspaces design (spec §3.2, §3.3, §7).
It is a major release of @helix-agents/core, @helix-agents/runtime-cloudflare and @helix-agents/workspace-docker, with minors of @helix-agents/runtime-js, @helix-agents/workspace-posix-core, @helix-agents/workspace-local-bash, @helix-agents/workspace-local-sandbox and @helix-agents/workspace-memory.
Who is affected:
- apps that use the Cloudflare sandbox's
destroyOnClose; - apps that use a per-session Cloudflare sandbox or filestore, or set an explicit sandbox
idor filestorenamespace(see Per-session ids); - apps whose sub-agents or companions declare their own Cloudflare filestore or sandbox workspace (see Behaviour changes);
- multi-host Node deployments of local-bash, local-sandbox or docker;
- anyone who wrote a custom
WorkspaceProvideror a customDockerEngine.
What to do
You use the SDK's providers and runtimes: upgrade them together with core, in one release.
You construct a
CloudflareSandboxWorkspaceProvider: pass the new requiredscopeoption, what owns the session store the provider serves. On the CF Durable Object, use the agent DO's own id; theworkspaceProvidersfactory receives(env, ctx):tsworkspaceProviders: (env, ctx) => new Map([ [ 'cloudflare-sandbox', new CloudflareSandboxWorkspaceProvider({ namespace: env.Sandbox, scope: ctx.id.toString(), }), ], ]);On another host, pass a value that uniquely names the store the sessions' state lives in, the same on every call. A missing or empty
scopeis a typedWorkspaceFailedErrorat construction.You set
destroyOnCloseon a Cloudflare sandbox: remove it. It no longer exists. A run's end is a release and never destroys the container. Delete a session's container when it is no longer needed. An executor that hosts the provider does it withdestroyWorkspace(sessionId)(the provider'sdestroy, which callssandbox.destroy()).On the CF Durable Object, until its own workspace operation ships: the DO exposes no
destroyWorkspaceto the app yet. A child's or consult's container is still destroyed at its end, but a root session's container is the app's to delete, in one line (a per-session container's sandbox id iscloudflareSandboxIdForSession(scope, sessionId), with the provider'sscope: the DO'sctx.id.toString()):tsimport { getSandbox } from '@cloudflare/sandbox'; import { cloudflareSandboxIdForSession } from '@helix-agents/runtime-cloudflare/sandbox'; await getSandbox(env.Sandbox, await cloudflareSandboxIdForSession(scope, sessionId)).destroy();The DO step of the design (d4) adds the DO's own workspace operation, which replaces this call. Until then a lost sandbox workspace is reset by default (the provider's
defaultOnUnavailableis'reset'), so a container whose disk was reset is replaced without an app call; see Behaviour changes.
You run local-bash, local-sandbox or docker on more than one host: route every request of a session to the host that opened its workspace (sticky routing). A ref from another host is now refused typed (
wrong_host). Set the provider'shostIdoption when several hosts (containers) share a hostname but not a filesystem. The defaulthostIdis the hostname, which changes when a container or pod is recreated: whentmpdirRootis on a persistent volume, sethostIdto a stable value, or the recreated host refuses its own workspaceswrong_host. Awrong_hostloss is never reset, whateveronUnavailablesays: the workspace still exists on the host that opened it.You use docker: schedule
provider.reap({ olderThan })on every host, as for local-bash and local-sandbox. PickolderThanlonger than your longest run plus the idle time a session's workspace should be kept.You wrote a custom
WorkspaceProvider: add the two required members,durabilityanddestroy(ref), to the contract below. See Building a provider.You wrote a custom
DockerEngine: implementlistContainers(label): every container (running or not) carrying the labelkey=value, with all its labels.Your sub-agents or companions keep files in their own Cloudflare workspace: read Behaviour changes. Those files are now deleted at the child's end.
You use a Cloudflare sandbox or filestore: read Per-session ids. A per-session workspace has a new id, and an explicit
id/namespacenow always needsshareAcrossSessions: true.
Per-session ids
Session and shared Cloudflare workspaces use separate ids, and per-session workspace ids are scoped to the owning Durable Object (or the configured scope).
- Per-session sandbox (no
id): the container's sandbox id iscloudflareSandboxIdForSession(scope, sessionId),helix-session-plus 24 hex characters of SHA-256 of the scope and the session id (lowercase, 38 characters). It is no longer the session id.cloudflareSandboxIdForSessionandSESSION_SANDBOX_ID_PREFIXare exported from@helix-agents/runtime-cloudflare/sandbox. - Per-session filestore (no
namespace): the namespace iscloudflareFileStoreNamespaceForSession(doId, sessionId),helix_session_plus 32 hex characters of SHA-256 of the Durable Object's id and the session id. It replaces "the session id when valid, elses_<hex>". Its R2 spill keys are scoped to the Durable Object too (<doId>/<namespace>/<path>).cloudflareFileStoreNamespaceForSessionandSESSION_FILESTORE_NAMESPACE_PREFIXare exported from@helix-agents/runtime-cloudflare/filestore. - An explicit
id/namespaceis always a shared workspace. It requiresshareAcrossSessions: true, also when it equals the session id, and it may not start withhelix-session-/helix_session_(case-insensitive). Either violation is a typedWorkspaceFailedErrorat open. If you setid: sessionId(ornamespace: sessionId) to get a per-session workspace, omitid/namespaceinstead. - Workspaces created before this release are not reattached. A per-session sandbox or filestore namespace created before this release was named by the session id (or
s_<hex>for a filestore session id that is not a valid namespace), so the new release opens a fresh one. There is no migration. Remove the old ones with platform tools:- sandbox:
await getSandbox(env.Sandbox, oldSessionId).destroy(); - filestore: drop the old namespace's
cf_workspace_<old namespace>tables from the Durable Object's storage, and delete its R2 spill objects (keys under<old namespace>/<old namespace>/).
- sandbox:
The provider contract
durability: WorkspaceDurability(required): one of'process': the ref resolves only in the process that opened it;'host': on the host that opened it, while its files exist; a multi-replica Node deployment needs sticky routing;'global': wherever the provider's bindings reach.
destroy(ref)(required; was optional) deletes the workspace's data and resources:- it works without a live handle;
- it is idempotent: a missing workspace is already destroyed;
- it never re-creates anything, and afterwards
resolve(ref)throwsWorkspaceEvictedError; - it validates the ref as
resolve()does, and refuses a ref it cannot validate withWorkspaceFailedError, deleting nothing; - a
'host'provider refuses a ref from another host withWorkspaceEvictedErrorreason: 'wrong_host', deleting nothing; - a provider with nothing to delete implements it as a no-op.
reap({ olderThan })stays optional.Workspace.verify()(optional): implement it whenresolvecan succeed against a reset resource. It throwsWorkspaceEvictedErrorreason: 'content_reset'when the live workspace is no longer the one its ref names.close()still releases and never deletes data.
@helix-agents/workspace-posix-core exports the shared host-binding helpers, resolveHostId(hostId?) and assertRefHost(refHost, hostId, providerId).
What changed
- core:
WorkspaceProvidergains the requireddurabilityanddestroy(above), and an optionaldefaultOnUnavailable?: 'fail' | 'reset': what a lost workspace does when itsWorkspaceConfig.onUnavailableis unset (else'fail'; an explicitonUnavailablealways wins).- A
wrong_hostloss is never reset: the workspace staysunavailable, typed, whateveronUnavailablesays. executor.destroyWorkspace(sessionId)andresetWorkspace(sessionId)are refused withframework_not_supportedonly when the session's provider is not registered on the executor. CoredestroySessionWorkspaceanswersunsupportedonly in that case.
- local-bash, local-sandbox, docker (
durability: 'host'):- each ref records the host that opened it: the provider's new
hostIdoption, elseos.hostname(); resolveordestroyof a ref from another host throwsWorkspaceEvictedErrorwithreason: 'wrong_host'and deletes nothing. The registry reports it asWorkspaceUnavailableError(workspace_unavailable) with reasonwrong_host, instead of a missing tmpdir (not_found).
- each ref records the host that opened it: the provider's new
- docker:
- new
reap({ olderThan }): it removes this provider's containers on this host whose tmpdir is gone or was last used at leastolderThanms ago, then its idle tmpdirs (the same rule as local-bash'sreap). It never touches another host's containers, anothertmpdirRoot's, one whose tmpdir is not one of the provider's own (helix-dk-…), or unlabelled ones; - containers carry the labels
helix.workspace.provider=docker,helix.workspace.tmpdirandhelix.workspace.host; - resolving and releasing a workspace mark its tmpdir used;
- release (
close()) still stops and removes the container and keeps the files; DockerEnginegainslistContainers(label).
- new
- workspace-memory:
durability: 'process'. - Cloudflare filestore (
durability: 'global'):- its ref carries the owning Durable Object's id, so
resolveordestroyin another DO is refusedwrong_hostand never opens empty tables; destroydeletes the namespace's files and their R2 spill objects; R2 spill keys are scoped to the Durable Object;- a marker table,
helix_workspace_filestore, records each namespace's current opening, so a destroyed or replaced opening resolvesnot_found(a session's namespace is reused by its next workspace). A destroy removes the namespace's rows and its marker in one step (a newer opening of the namespace always starts empty), then deletes the spilled R2 objects, keeping any object a newer opening references again. The table also records whether the namespace is shared: an open whose sharing disagrees with it is refused, typedWorkspaceFailedError, anddestroynever deletes a namespace marked shared.
- its ref carries the owning Durable Object's id, so
- Cloudflare sandbox (
durability: 'global'):destroyOnCloseis removed.close()is a release and never destroys the container;- the new required
scopeoption scopes per-session container ids to the owner of the session store (above); openwrites a marker file,<workspaceDir>/.helix/workspace-id, holding{ "id": <opening id>, "shared": <bool> }. An open whose sharing disagrees with the container's marker is refused, typedWorkspaceFailedError, before any configuration call (sleepAfter, env vars, bucket mounts);verify()reads it back (one file read per run). The registry calls it after everyresolve, and on a run's first get of a workspace still open from an earlier run. A container whose disk was reset (a sleep pastsleepAfter, a replacement) is reportedcontent_reset, instead of a silently empty workspace. What follows is the workspace'sonUnavailablesetting:'reset'(the sandbox's default, see below) destroys the old workspace and opens a fresh one;'fail'fails workspace tools withworkspace_unavailableuntil the app callsresetWorkspace(not yet callable on the CF DO);destroy(ref)callssandbox.destroy()on its own opening's per-session container: when the marker is its own, or missing (a reset disk). A container whose marker names another opening, or is shared, is left alone (logged at warn). A ref whose id does not match its sharing is refused, typed, and nothing is destroyed;- a snapshot restore or branch writes its own marker into the restored container; from a shared base the restored container is a per-session one, with an id of its own, so the session's destroy removes it;
- an app that needs files to survive a sleep uses the Cloudflare Sandbox SDK's
keepAliveor the snapshot tools.
- Cloudflare script runner (
durability: 'global'):destroyis a no-op (stateless isolates).
Behaviour changes
- A child's workspace is destroyed at its end on every provider. A child session's (an ephemeral sub-agent's) or a companion consult's own workspace is ended and destroyed at the child's terminal exit on every provider, the Cloudflare filestore and sandbox included, on the CF Durable Object too. The child's workspace contents are gone after its terminal exit. Before, the Cloudflare providers had no
destroy, so their child and companion workspaces were kept across consults. A root session's workspace is still kept until the app destroys it. An interrupted child counts as a terminal exit: a CF Durable Object child that is resumed afterwards starts with a fresh workspace. A new run of the session (the next consult, a resume, a retry) opens its workspace after the previous run's destroy has finished, waiting up toworkspaceEndWaitMs(default 30 s; a Stop ends the wait): within one runtime-js executor through its session workspaces, and on the CF Durable Object across drives (the next drive waits for the previous drive's settle, which follows its run's workspace release). Past the bound the run opens anyway, with a warning, and its open can overlap that destroy; across runtime-js processes the order is not guaranteed. - A lost Cloudflare sandbox workspace is reset by default. The sandbox provider's
defaultOnUnavailableis'reset': a container whose disk was reset (content_reset) or that is gone is destroyed and replaced by a fresh, empty workspace, firingonWorkspaceResetand logging a warning. Until the DO's own workspace operation (d4) gives the app a reset call,'fail'would leave a root session's workspace unusable on the DO. SetonUnavailable: 'fail'explicitly to keep the old behaviour (for example to inspect a lost container). d4 revisits this default. - A shared workspace is never destroyed. A filestore with an explicit
namespaceandshareAcrossSessions: true, or a sandbox with an explicitidandshareAcrossSessions: true, is shared by every session that opens it.destroynever removes its data: it logs a warning (ids only) and returns. An app that wants that data gone removes it on purpose itself:- filestore: delete the files in that namespace, for example by opening it and removing its entries with
ws.fs.rm(which also removes their R2 spill objects), or by dropping its tables from the DO's storage; - sandbox: call
getSandbox(env.Sandbox, id).destroy()from@cloudflare/sandbox, with the sharedid.
- filestore: delete the files in that namespace, for example by opening it and removing its entries with
- A ref written before this release fails validation. local-bash, local-sandbox and docker refs now carry the host, and filestore and sandbox refs carry their new ids. A ref written before this release fails payload validation with a typed
WorkspaceFailedError. There is no legacy-ref handling.
See also
- Workspaces: Lifecycle
- Building a provider: the durability and destroy contract.
- Cloudflare Sandbox — Lifecycle and Cloudflare Filestore — Lifecycle.
- Docker — Reaping.
- Runbook §14:
wrong_hostandcontent_reset. - Upgrade: the session's workspace binding (the previous workspace step).