Skip to content

fix: prevent attachment staging after worktree removal - #174104

Merged
shakkernerd merged 2 commits into
mainfrom
fix/staging-workspace-lifetime
Oct 16, 2026
Merged

shakkernerd merged 2 commits into
mainfrom
fix/staging-workspace-lifetime

Conversation

@shakkernerd

@shakkernerd shakkernerd commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #174029.

Removing a managed project worktree while an inbound attachment is being read can leave a successful attachment stage in its retired sandbox projection. The removal finishes before the delayed read, but staging then creates a private input directory and file using a path whose workspace owner has already been released.

User Impact

A pending attachment can no longer publish new files after its managed worktree is retired. Ordinary staging keeps the same attachment bytes and paths, and the managed original remains available when staging fails. The temporary workspace owner is released after success, a read failure, or cancellation.

This preserves the existing permission and removal behavior. It adds no configuration, data migration, retention policy, or backend requirement. Callers that only need workspace information retain their existing result and disposal behavior.

Why This Change Was Made

Sandbox workspace preparation already selects and validates the managed projection. Its information-only entry point disposed that owner before returning, while attachment staging still had asynchronous source work ahead of it.

The shared preparation now also exposes an owned result for writers. Attachment staging retains it for the whole operation and supplies its current-state assertion to the existing safe-filesystem root. The root checks that authority before creating both the privacy marker and the attachment, alongside the existing cancellation checks. The information-only entry point releases the same preparation immediately; selection and layout remain in one owner.

Evidence

The focused regression uses real managed-worktree creation and removal, persisted session metadata, sandbox preparation, and production attachment staging. A deferred source-read result makes removal finish before the stage resumes. Unchanged production code at c316eca reports one staged file after retirement and fails the required zero count.

The corrected owner regression passes all four cases: ordinary bytes, failed reads, cancellation, and retirement during the held read. It also verifies that the real owner stays current while reading and is disposed after settlement. The retirement case leaves no new input directory or file.

Remote validation passed changed-file formatting/lint, the affected production and test type checks, and 820 tests across 47 selected files. These cover existing source policy, history, SSH, workspace information, cancellation, and private-input reconciliation/restore. The native E2E runtime prerequisite also built successfully.

pnpm test src/gateway/server-methods/sandbox-media.workspace-lifetime.test.ts --maxWorkers=1 took 36.51 seconds wall time, including 14.57 seconds of cold worker preparation; its four test bodies took 9.49 seconds. The Git, registry, and session-owner composition is needed to exercise real retirement through its actual lifecycle owners. The remaining related batch took 785.18 seconds including its E2E prerequisite. Full PR CI passed on the current head.

@clawsweeper

clawsweeper Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Security sensitive changes

This maintainer PR changes sensitive security components.

No secops approval is required. This comment is informational because the PR author has Maintain or Admin access.

  • Current SHA: edbfb6644543a3afa977375f3e03d1fd40ee9f7e
  • Maintainer: @shakkernerd
  • Repository role: maintain

These security sensitive changes were made:

  • src/agents/sandbox.ts: Controls sandbox containment or tool execution authority. Check filesystem and network isolation, approval scope, and command allowlists.
  • src/agents/sandbox/context.ts: Controls sandbox containment or tool execution authority. Check filesystem and network isolation, approval scope, and command allowlists.

Carefully review these changes before merging.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime docker Docker and sandbox tooling agents Agent runtime and tooling size: M labels Oct 16, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 16, 2026
@clawsweeper

clawsweeper Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Attachment staging retains the managed workspace owner and checks its current authority before filesystem mutations.

Example: Remove a managed worktree while attachment.txt containing “ordinary attachment bytes” is being read.

  • Before: Staging publishes one attachment into the retired projection.
  • After: Staging publishes zero attachments, creates no input directory, and preserves the original attachment reference.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The focused patch is correct and exact-head checks pass; strongest behavior evidence is the production-owner harness rather than a shipped-entry-point capture.
Proof confidence 🦐 gold shrimp (3/6) Sufficient (none): For this maintainer-authored authority repair, the supplied production-owner regression exercises real session/worktree owners and filesystem effects in isolated Linux state: authorized staging preserves exact bytes, while completed worktree retirement during the held source read prevents both input-directory and attachment creation; failure and cancellation release ownership.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: An attachment can be reported successfully staged after its managed worktree has been removed.
Reason: The maintainer-authored change restores the existing workspace-lifecycle boundary without adding configuration, permissions, or another state owner.

Merge readiness

✅ Ready for maintainer review

This PR fixes a current-main workspace-lifetime defect; the prior mock-export finding is resolved, and no blocking introduced defect remains.

Priority: P2
Reviewed head: edbfb6644543a3afa977375f3e03d1fd40ee9f7e

Before merge

None.

Findings

None.

Agent review details

How this fits together

Inbound reply preparation passes attachment sources and session workspace metadata to sandbox staging, which produces private workspace files and rewritten media references.

flowchart LR
A[Inbound attachment] --> B[Session workspace preparation]
B --> C[Retained managed workspace owner]
C --> D[Source read]
D --> E[Guarded filesystem root]
C --> E
E --> F[Private attachment and media references]
Loading

Technical review

Best possible solution:

Land the focused repair that retains the existing workspace owner through guarded attachment publication.

Do we have a high-confidence way to reproduce the issue?

Current-main source establishes the lifetime gap, and the supplied deterministic regression records the matching base failure; this read-only review ran no target code.

Is this the best way to solve the issue?

The repair extends the existing owner's lifetime and reuses filesystem mutation guards while preserving information-only callers and SSH staging.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 28f8cab70da1.

Provenance checked

  • Workspace preparation ownership changes intended behavior with a stated reason (cbc53ac: The worker-authority refactor scoped local workspace disposal to preparation and required live checks at final effects; this PR explains why writers need ownership beyond preparation.)
  • SSH and local attachment staging keeps the original intent (af75a8d: Reused SSH sandboxes require guarded remote publication while preserving readable originals, source validation, exclusive creation, and cancellation.)
  • Captured sandbox policy preparation keeps the original intent (95baa6d: Media preparation must retain the requesting session's policy and captured source rather than select another session's state.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

  • Verified PR scope and resolved prior finding: The pinned introduced diff retains preparation ownership through staging. The subsequent commit adds specific isolation explanations to four mock factories and removes their obsolete baseline entries without adding exemptions. (src/auto-reply/reply/stage-sandbox-media.ts:104, edbfb6644543)
  • Current main still releases ownership before attachment I/O: Fetched main disposes selected.localWorkspace before returning workspace information; staging subsequently reads the attachment and creates files without consuming that owner's assertion. (src/agents/sandbox/context.ts:622, 28f8cab70da1)
  • Production-owner lifetime regression: The regression uses real managed-worktree creation/removal, persisted session metadata, sandbox preparation, source reading, and filesystem publication. It checks exact successful bytes, read failure, cancellation, and removal completed before the held read resumes; retirement must leave no input directory. The PR records the intended base failure and passing corrected cases. (src/gateway/server-methods/sandbox-media.workspace-lifetime.test.ts:110, edbfb6644543)
  • Verified dependency mutation contract: OpenClaw imports @openclaw/fs-safe through its filesystem facade and pins version 0.26.6, making its contract directly relevant. Inspection of that published npm artifact confirms root-impl.js composes root and per-call assertions, and create uses those composed options; docs/root.md specifies checks before parent creation, file creation, content writes, and publication. The privacy-marker helper cannot replace the workspace assertion with its cancellation callback. (dist/root-impl.js:216)
  • Existing authority remains authoritative: The retained local owner checks current session identity/lifecycle and the live registry binding; initial selection rejects removed worktrees, another session's ownership, repository/branch mismatches, and unrelated workspace paths. (src/gateway/worker-environments/local-workspace-projection.ts:343, edbfb6644543)
  • Linked report and distinct companion repair: Inbound attachments can stage after their managed worktree is removed #174029 documents this removal-during-read failure. fix: allow attachment follow-ups in managed project sessions #173956 repairs earlier workspace selection instead; its inspected before/after screenshots show retry delivery, not retirement during staging, so they are not proof of this PR's changed behavior.

PR surface

Source +35, Tests +183, Docs +3, Other -4. Total +217 across 13 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 3 53 18 +35
Tests 8 188 5 +183
Docs 1 3 0 +3
Config 0 0 0 0
Generated 0 0 0 0
Other 1 0 4 -4
Total 13 244 27 +217

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #174029
Summary: This PR repairs the reported post-retirement staging defect; the companion PR addresses a distinct workspace-selection failure.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit. Replaced prior rating: 🦪 silver shellfish.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🦐 gold shrimp, so this older rating label is no longer current.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: The demonstrated defect affects pending attachment staging during managed-worktree removal, with no demonstrated disclosure, production outage, or loss of the original attachment.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit. Replaced prior rating: 🦪 silver shellfish.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-16T10:32:29.177Z sha 2e4d27c :: needs changes before merge. :: [P2] Repair the changed mock factories' ratchet violations

Reviewed October 16, 2026, 6:52 AM ET / 10:52 UTC (Revision 2).

@shakkernerd

Copy link
Copy Markdown
Contributor Author

The four sandbox mock factories now explain their deliberate fixture isolation, and the generated baseline removes their four obsolete entries without adding exemptions. The mock-export ratchet passes on the current head.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 16, 2026
@shakkernerd
shakkernerd merged commit 4008c32 into main Oct 16, 2026
133 checks passed
@shakkernerd
shakkernerd deleted the fix/staging-workspace-lifetime branch October 16, 2026 13:55
@shakkernerd

Copy link
Copy Markdown
Contributor Author

Fixed: pending attachments no longer create files after their managed worktree is removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. docker Docker and sandbox tooling docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. security-sensitive-changed size: M status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inbound attachments can stage after their managed worktree is removed

1 participant