Skip to content

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 id or filestore namespace (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 WorkspaceProvider or a custom DockerEngine.

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 required scope option, what owns the session store the provider serves. On the CF Durable Object, use the agent DO's own id; the workspaceProviders factory receives (env, ctx):

    ts
    workspaceProviders: (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 scope is a typed WorkspaceFailedError at construction.

  • You set destroyOnClose on 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 with destroyWorkspace(sessionId) (the provider's destroy, which calls sandbox.destroy()).

    • On the CF Durable Object, until its own workspace operation ships: the DO exposes no destroyWorkspace to 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 is cloudflareSandboxIdForSession(scope, sessionId), with the provider's scope: the DO's ctx.id.toString()):

      ts
      import { 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 defaultOnUnavailable is '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's hostId option when several hosts (containers) share a hostname but not a filesystem. The default hostId is the hostname, which changes when a container or pod is recreated: when tmpdirRoot is on a persistent volume, set hostId to a stable value, or the recreated host refuses its own workspaces wrong_host. A wrong_host loss is never reset, whatever onUnavailable says: 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. Pick olderThan longer 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, durability and destroy(ref), to the contract below. See Building a provider.

  • You wrote a custom DockerEngine: implement listContainers(label): every container (running or not) carrying the label key=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 / namespace now always needs shareAcrossSessions: 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 is cloudflareSandboxIdForSession(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. cloudflareSandboxIdForSession and SESSION_SANDBOX_ID_PREFIX are exported from @helix-agents/runtime-cloudflare/sandbox.
  • Per-session filestore (no namespace): the namespace is cloudflareFileStoreNamespaceForSession(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, else s_<hex>". Its R2 spill keys are scoped to the Durable Object too (<doId>/<namespace>/<path>). cloudflareFileStoreNamespaceForSession and SESSION_FILESTORE_NAMESPACE_PREFIX are exported from @helix-agents/runtime-cloudflare/filestore.
  • An explicit id / namespace is always a shared workspace. It requires shareAcrossSessions: true, also when it equals the session id, and it may not start with helix-session- / helix_session_ (case-insensitive). Either violation is a typed WorkspaceFailedError at open. If you set id: sessionId (or namespace: sessionId) to get a per-session workspace, omit id / namespace instead.
  • 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>/).

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) throws WorkspaceEvictedError;
    • it validates the ref as resolve() does, and refuses a ref it cannot validate with WorkspaceFailedError, deleting nothing;
    • a 'host' provider refuses a ref from another host with WorkspaceEvictedError reason: '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 when resolve can succeed against a reset resource. It throws WorkspaceEvictedError reason: '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:
    • WorkspaceProvider gains the required durability and destroy (above), and an optional defaultOnUnavailable?: 'fail' | 'reset': what a lost workspace does when its WorkspaceConfig.onUnavailable is unset (else 'fail'; an explicit onUnavailable always wins).
    • A wrong_host loss is never reset: the workspace stays unavailable, typed, whatever onUnavailable says.
    • executor.destroyWorkspace(sessionId) and resetWorkspace(sessionId) are refused with framework_not_supported only when the session's provider is not registered on the executor. Core destroySessionWorkspace answers unsupported only in that case.
  • local-bash, local-sandbox, docker (durability: 'host'):
    • each ref records the host that opened it: the provider's new hostId option, else os.hostname();
    • resolve or destroy of a ref from another host throws WorkspaceEvictedError with reason: 'wrong_host' and deletes nothing. The registry reports it as WorkspaceUnavailableError (workspace_unavailable) with reason wrong_host, instead of a missing tmpdir (not_found).
  • docker:
    • new reap({ olderThan }): it removes this provider's containers on this host whose tmpdir is gone or was last used at least olderThan ms ago, then its idle tmpdirs (the same rule as local-bash's reap). It never touches another host's containers, another tmpdirRoot'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.tmpdir and helix.workspace.host;
    • resolving and releasing a workspace mark its tmpdir used;
    • release (close()) still stops and removes the container and keeps the files;
    • DockerEngine gains listContainers(label).
  • workspace-memory: durability: 'process'.
  • Cloudflare filestore (durability: 'global'):
    • its ref carries the owning Durable Object's id, so resolve or destroy in another DO is refused wrong_host and never opens empty tables;
    • destroy deletes 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 resolves not_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, typed WorkspaceFailedError, and destroy never deletes a namespace marked shared.
  • Cloudflare sandbox (durability: 'global'):
    • destroyOnClose is removed. close() is a release and never destroys the container;
    • the new required scope option scopes per-session container ids to the owner of the session store (above);
    • open writes 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, typed WorkspaceFailedError, before any configuration call (sleepAfter, env vars, bucket mounts);
    • verify() reads it back (one file read per run). The registry calls it after every resolve, and on a run's first get of a workspace still open from an earlier run. A container whose disk was reset (a sleep past sleepAfter, a replacement) is reported content_reset, instead of a silently empty workspace. What follows is the workspace's onUnavailable setting: 'reset' (the sandbox's default, see below) destroys the old workspace and opens a fresh one; 'fail' fails workspace tools with workspace_unavailable until the app calls resetWorkspace (not yet callable on the CF DO);
    • destroy(ref) calls sandbox.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 keepAlive or the snapshot tools.
  • Cloudflare script runner (durability: 'global'): destroy is 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 to workspaceEndWaitMs (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 defaultOnUnavailable is 'reset': a container whose disk was reset (content_reset) or that is gone is destroyed and replaced by a fresh, empty workspace, firing onWorkspaceReset and 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. Set onUnavailable: '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 namespace and shareAcrossSessions: true, or a sandbox with an explicit id and shareAcrossSessions: true, is shared by every session that opens it. destroy never 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 shared id.
  • 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 ​

Released under the MIT License.