Skip to content

feat(remote-worker): per-session workspace directory on the container tier (#408) - #461

Merged
pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:fix/408-container-workspace-key
Oct 8, 2026
Merged

pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:fix/408-container-workspace-key

Conversation

@pdettori

@pdettori pdettori commented Oct 7, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #408. On the container tier, every session's turns ran in the one shared /workspace, so one user's agent saw another's git clone, and two sessions cloning the same repo collided on the same path.

The issue's worker-only proposal would not have fixed this. Every path the sandbox tools send is absolute under the harness's podCwd (cd '/workspace' && …, cat '/workspace/x'; see select-sandbox.ts and k8s-sandbox/src/operations.ts). So changing the directory BashRunner starts in changes nothing. The harness has to move podCwd too, and it does so only for workers that say they support it:

  • remote-worker: a new WorkspaceRunner wraps BashRunner.
    • A non-empty workspace_key runs in <SH_WORKSPACE_ROOT>/<key>, created on first use.
    • The key is validated with vmpool's rule: one path segment, invalid-workspace-key: … on refusal.
    • An empty key is passed through unchanged, so the proto change stays additive (spec §3.4).
    • The worker advertises the Hello capability workspace-subdir.
  • Cleanup is idle-only (SH_WORKSPACE_IDLE, default 30m, the same knob and default as the microVM worker).
    • A directory is renamed to a tombstone name under the lock and deleted outside it. A session that returns during the delete gets a fresh directory, not a half-deleted one.
    • A key with an Exec still running is never swept (P4 microVM tier: WorkspaceIdle=30min deletes a HITL session's workspace mid-session, silently #338).
    • A restarted worker does not adopt directories it finds under the root. The root is also the shared workspace, and its contents can't be told apart from a key's directory by name.
  • harness: a leased record that advertises workspace-subdir gets podCwd = <KAGENTI_SANDBOX_CWD>/<session id>. Pods, older workers and the microVM tier keep podCwd unchanged; on the microVM tier the guest already mounts the session's workspace at /workspace.

Not a security boundary. Every session runs in the same container as the same Unix user, so cd .. still reaches the others. Isolation remains MI1 S5 owner binding.

Deployment note: KAGENTI_SANDBOX_CWD (harness) must equal SH_WORKSPACE_ROOT (worker). Both default to /workspace.

Test plan

  • go test -race ./... in remote-worker. New tests check that:
    • two keys get separate directories, and a file persists across Execs under one key;
    • an empty key leaves Dir unset and creates nothing;
    • a/b, .., .hidden, a leading - and a 129-character key are all refused before anything runs;
    • the sweep reclaims only idle keys and leaves no tombstone behind;
    • a running Exec is never swept, and idle time counts from when an Exec ends.
  • Mutation check: with cmd.Dir disabled, the separate-directory and sweep tests fail.
  • pnpm --filter @moca/harness test: 583 passed. New cases check that podCwd moves only when the record has the capability.
  • make typecheck, make lint, go vet ./...
  • Live check on a cluster: two sessions on one container worker each git clone the same repo.

🤖 Generated with Claude Code

…on the container tier

On the container tier every session's turns ran in the one shared
/workspace, so user 2's agent saw user 1's `git clone`, and two sessions
cloning the same repo collided on the same path (rossoctl#408).

A worker-only change would not fix that: every path the sandbox tools
send is absolute under the harness's podCwd (`cd '/workspace' && ...`),
so the directory the child starts in does not matter. Both sides change,
gated by a new Hello capability:

- remote-worker: WorkspaceRunner wraps BashRunner. A non-empty
  workspace_key runs in <SH_WORKSPACE_ROOT>/<key>, created on first use;
  the key is validated to one path segment with vmpool's rule. An empty
  key is passed through unchanged. The worker advertises
  `workspace-subdir`.
- Cleanup is idle-only (SH_WORKSPACE_IDLE, default 30m, matching the
  microVM worker). A directory is renamed to a tombstone name under the
  lock, then deleted off it, so a session returning mid-delete gets a
  fresh directory. A key with a running Exec is never swept (rossoctl#338).
- harness: a leased record that advertises `workspace-subdir` gets
  podCwd = <KAGENTI_SANDBOX_CWD>/<session id>. Pods, older workers and
  the microVM tier keep podCwd unchanged.

This is not a security boundary: the same container and Unix user, so
`cd ..` still reaches other sessions. It fixes the collision and the
"B sees A's clone" problem; isolation remains MI1 S5 owner binding.

Fixes rossoctl#408

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Comment thread harness/src/select-sandbox.ts Fixed
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Oct 8, 2026
CodeQL js/polynomial-redos flagged /\/+$/ in sessionPodCwd. Move the
linear-scan stripTrailingSlashes out of run-leaf.ts into its own module
and use it in both places, so there is one implementation to keep.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This PR gives each session its own workspace directory on the container tier, gated by the workspace-subdir Hello capability. The worker and the harness agree on the key: take sends workspaceKey: sessionId and sessionPodCwd uses the same id. The lock ordering in Run/Sweep means a running Exec is never swept. The CodeQL js/polynomial-redos alert from the first commit is fixed in 830a60a. stripTrailingSlashes is now one shared linear-scan helper used by both run-leaf.ts and sessionPodCwd, and CodeQL is green. The inline comments are suggestions, not blockers: key validity, config drift between the two env vars, and reclaim behaviour.

Docs not in the diff that still say the container worker ignores workspace_key (now stale; the "no isolation between users" claim itself still holds): deploy/k8s/README.md:314, deploy/vm/README.md:691, docs/demos/vm-multi-user-demo.md:689.

The test plan's live cluster check (two sessions cloning the same repo on one container worker) is still unchecked.

Author: pdettori (MEMBER — maintainer)
Areas reviewed: TypeScript (harness), Go (remote-worker), Docs, Security
Agent/IDE config (.claude/.vscode): none
Commits: 2 commits, all signed-off: yes
CI status: passing (all 14 checks, including CodeQL)

sessionId: string,
): string {
if (!rec?.capabilities.includes(WORKSPACE_SUBDIR_CAPABILITY)) return podCwd;
return `${stripTrailingSlashes(podCwd)}/${sessionId}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: With the capability on, the container tier now refuses any key that fails ^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$, where before it ran them. Two producers can miss that rule:

  • leafSessionId → toSessionId sanitises characters but has no length cap, so a long tenant/run_id/item_id (over 128 chars) now fails every Exec on a capable container worker.
  • acquireTurnSandbox falls back to sessionId ?? holderId, which yields anon:<uuid> (the : is refused). executeTurn always passes getSessionId() now, so this looks unreachable today, but the function is still exported.

Consider capping or hashing in toSessionId, plus a test that the ids the harness generates satisfy the worker's rule.

Idle: wexec.DefaultWorkspaceIdle,
Inner: wexec.BashRunner{},
}
if v := get("SH_WORKSPACE_ROOT"); v != "" {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: If KAGENTI_SANDBOX_CWD and SH_WORKSPACE_ROOT disagree, the only symptom is cd: …: No such file or directory on every tool call. The name also means something different on microvm-worker, where SH_WORKSPACE_ROOT is a host directory (/srv/workspaces) that by design is not the harness cwd. On a P6 host running both workers, copying the microVM value breaks the container tier. Consider advertising the root in Hello (e.g. a label) so the harness can use it, or at least log a mismatch.


// DefaultWorkspaceIdle mirrors vmpool.DefaultWorkspaceIdle: an idle key's directory is
// the only thing a sweep deletes, and it is deleted only after this long with no Exec.
const DefaultWorkspaceIdle = 30 * time.Minute

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: This adds deletion where the container tier had none. Until now, files lived until the container restarted. A session that waits more than 30m on a person (e.g. require_approval) now comes back to an empty workspace, and the harness gets no workspaceReset-style signal it could surface. Consider a longer default on this tier, or call out the new behaviour in the deploy READMEs (deploy/k8s, deploy/vm).

// workspaceTombstonePrefix names a directory detached from its key and queued for
// removal. validWorkspaceKey requires an alphanumeric first character, so no key can
// resolve to one — the same trick, for the same reason, as vmpool's detachWorkspace.
const workspaceTombstonePrefix = ".reclaiming-"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Two kinds of leftover are never reclaimed:

  1. If the worker dies mid-RemoveAll, or RemoveAll fails (logged and dropped), the .reclaiming-* tombstone stays under Root, where empty-key sessions see it. Keys cannot start with ., so a startup pass (or each Sweep) that removes Root/.reclaiming-* is safe.
  2. Keys are tracked in memory only. On ocp-single the workspace is an emptyDir, which survives a container restart, so the directories of sessions that never return after a worker restart are orphaned. Worth a line in DESIGN.md even if adoption stays out of scope.

@pdettori
pdettori merged commit 57b6159 into rossoctl:main Oct 8, 2026
14 checks passed
cwiklik added a commit that referenced this pull request Nov 24, 2026
`mocactl follow-up SESSIONID "prompt"` is #381's last unbuilt command: a
second turn on a finished subagent's session, which keeps the first turn's
context, so `get` the result, read it, and ask for the next thing without
paying for the first turn again.

The result is copied out before the turn is dispatched. The sync path keeps
one result record per session and overwrites it unconditionally, so the copy
has to precede the dispatch or it races it; `cmdGet`'s write block is now a
shared `writeResultFiles` and both commands produce the same three files. An
async row is copied nothing: each of its turns keeps its own artifacts for
seven days (#840). A record that aged out of its 24 hours is not a refusal,
but a result that could not be *read* is, so a transport fault never costs
the result it promised to preserve.

The dispatch mirrors the row's backend, as a gate decision does: a row with
a `turnId` submits another turn, a row without one forks the same detached
worker `spawn` uses. The turn is always a prompt, including on a `solve`
row -- the session holds the repository workspace already (#461) -- and it
inherits the subagent's bundle and workload.

The ledger row is replaced by a fresh `record` line rather than updated:
`foldLines` keeps the later one, and an update line cannot carry an
envelope. That is what makes a gate on the follow-up answerable, since
`approve` re-sends the row's envelope and it has to hold the follow-up's
text, not the first turn's. Tag and label carry over -- one subagent, one
turn on.

Refused while the subagent is `running` (wait with `get --wait`) or parked
at a gate: a follow-up prompt is not an answer to a gate, and dispatching
one would leave it pending for good.

Tests: 14 new (12 on the command, 2 on the CLI surface). 189 green across
the 7 suites this touches; `make typecheck` and `make check-links` clean.

Refs #381
Assisted-By: Claude (Anthropic AI) <[email protected]>

Signed-off-by: cwiklik <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Container worker: honour workspace_key with a per-session working directory

3 participants