Skip to content

fix(gateway): Files saves can overwrite a replacement workspace after a same-chat reset - #178348

Closed
potoior wants to merge 1 commit into
openclaw:mainfrom
potoior:fix-files-replacement-workspace
Closed

potoior wants to merge 1 commit into
openclaw:mainfrom
potoior:fix-files-replacement-workspace

Conversation

@potoior

@potoior potoior commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Closes #178310

What Problem This Solves

Fixes: a session Files save can overwrite a replacement managed workspace after the same chat is reset, when the replacement holds the same relative path and identical bytes as the file the editor originally opened.

User Impact

User impact: an editor opened before the reset gets session_file_conflict on save (its draft is preserved and a reload is offered) instead of silently overwriting the replacement workspace's file; a fresh read of the replacement authorizes a new edit as before.

Why This Change Was Made

The Files CAS token was a plain content hash, so identical bytes in two different workspaces produced the same token; the write-target contract requires a user-bound write to retain its original target. The session Files read and save now derive the CAS token from the resolved session workspace root, so a token minted in the replaced worktree cannot authorize a save into its replacement. Only the Gateway session Files RPC opts in: the node workspace-inspection protocol and stored repository artifacts keep the unbound content hash, and a workspace-root path being recycled for a new worktree within one editor's lifetime is an accepted edge (named in the binding helper).

Evidence

  • Regression in src/gateway/server.sessions.create.workspace-policy.test.ts through the registered Gateway handlers and the real session/worktree lifecycle: read → same-chat reset → replacement worktree (new id, identical base\n bytes) → stale-hash save now fails with session_file_conflict and leaves the replacement file untouched; a fresh read's save succeeds (red before: the stale save returned success and overwrote the replacement).
  • Sibling suites green: repository files (16/16, node inspection protocol unchanged), workspace-fs, agents-files, claws-drafts, mcp-app-host-files, method scopes, managed-image-attachments, mcp-app-form-resources, and the UI chat-session-workspace contract tests; format, oxlint, and tsgo core/extensions/test lanes clean.

A same-chat reset replaces a session's managed worktree under the same
key. An open editor retains only its content hash, so a save after the
reset succeeded into the replacement workspace whenever the replacement
held identical bytes, silently overwriting its file and violating the
write-target contract. The session Files CAS token is now bound to the
resolved session workspace root on both the read and the save, so a
stale editor gets session_file_conflict and keeps its draft; a fresh
read of the replacement authorizes a new edit. Node workspace
inspection and stored repository artifacts keep the unbound content
hash.

Fixes openclaw#178310
@clawsweeper

clawsweeper Bot commented Oct 20, 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.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S labels Oct 20, 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. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 20, 2026
@clawsweeper

clawsweeper Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

What this changes

This PR binds session Files save tokens to the workspace root so stale editors cannot overwrite a replacement workspace with identical file contents.

Example: Read README.md containing "base\n", reset the same chat, create a replacement worktree with identical README.md bytes, then save the original draft.

  • Before: The retained content hash matches and the draft overwrites the replacement README.md.
  • After: The workspace-bound token differs, so Save returns session_file_conflict and leaves the replacement unchanged; a fresh read permits saving.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The implementation targets the right invariant with a meaningful lifecycle regression, but existing consumer tests fail and the required shipped-entry-point proof is absent.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: An editor opened before a chat reset can silently save into the replacement workspace when the filename and initial bytes match.
Reason: The patch restores the documented write-target invariant without adding settings, persisted state, or a competing writer.

Merge readiness

⛔ Blocked before merge - 2 items remain

Keep open: this PR addresses a real workspace-target bug, but its token change breaks existing Files tests; the competing fixes do not yet establish a safely landable replacement.

Priority: P2
Reviewed head: 8001c04b1c5f83cfba6c82b170bbc3b29b178d8b

Before merge

  • Add real behavior proof - The supplied evidence exercises registered handlers and real worktree allocation in an in-process test, including stale-target rejection before disk mutation and fresh-target success; no shipped Gateway or UI execution is supplied, so contributor proof must show replacement rejection, draft preservation, and fresh saving, with IPs, API keys, phone numbers, and non-public endpoints redacted. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.
  • Update Files consumer tests for workspace-bound tokens (P2) - Binding every local session token changes ordinary get/set results, but sessions-files.test.ts still builds expectedHash from plain SHA-256 in saveFile and expects plain hashes in preview, BOM, and concurrent-save assertions. The exact-head checks-node-changed-compact-small-3 annotations confirm mismatched preview hashes, rejected happy-path saves, zero successful concurrent saves, and an identity-read join that is never reached. Update save fixtures to use tokens returned by get, and retain independent assertions for successful saves, stale conflicts, BOM bytes, concurrency, and identity publication rather than weakening these checks.

Findings

  • [P2] Update Files consumer tests for workspace-bound tokens — src/gateway/server-methods/sessions-files.ts:577

Tests

  • Missing end-to-end proof: Provide a shipped Gateway or Control UI run showing stale-save rejection after real workspace replacement, preserved draft and replacement bytes, and successful fresh-read saving, including recovery of an editor retained across the token-format update.
Agent review details

How this fits together

The Gateway resolves session Files RPCs to a workspace, produces preview tokens, and passes conditional saves through the shared filesystem writer, which checks content and live authority before mutation.

flowchart TD
  A[Files editor] --> B[Session Files RPC]
  B --> C[Current session workspace]
  C --> D[Preview and workspace token]
  D --> A
  B --> E[Shared conditional writer]
  E --> F[Workspace file]
Loading

Technical review

Best possible solution:

Keep the workspace precondition in the existing conditional-write owner and update its consumer tests without weakening content-conflict or live-authority checks.

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

Current-main source and the maintainer's recorded lifecycle reproduction establish the identical-content overwrite path; this read-only review executed no target code.

Is this the best way to solve the issue?

Workspace-bound tokens repair the missing read-time target precondition using the existing writer and preserve unbound node/artifact behavior, but the existing Files test consumers must follow the changed token contract.

Full review comments:

  • [P2] Update Files consumer tests for workspace-bound tokens — src/gateway/server-methods/sessions-files.ts:577
    Binding every local session token changes ordinary get/set results, but sessions-files.test.ts still builds expectedHash from plain SHA-256 in saveFile and expects plain hashes in preview, BOM, and concurrent-save assertions. The exact-head checks-node-changed-compact-small-3 annotations confirm mismatched preview hashes, rejected happy-path saves, zero successful concurrent saves, and an identity-read join that is never reached. Update save fixtures to use tokens returned by get, and retain independent assertions for successful saves, stale conflicts, BOM bytes, concurrency, and identity publication rather than weakening these checks.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.95

AGENTS.md: found and applied where relevant.

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

Provenance checked

Testing

Proof path: in-process harness.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test src/gateway/server-methods/sessions-files.test.ts
  • pnpm test src/gateway/server-methods/workspace-fs.test.ts src/gateway/server-methods/sessions-files.repository.test.ts
  • pnpm test src/gateway/server.sessions.create.workspace-policy.test.ts
  • git diff --check

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 6b5279a adds src/gateway/server-methods/sessions-files.ts:167 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 6b5279a65123; files: src/gateway/server-methods/sessions-files.ts)
  • Shakker: Raw commit c4ccdcb adds src/gateway/server-methods/session-file-read-authority.ts:27 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: c4ccdcb8b4d2; files: src/gateway/server-methods/session-file-read-authority.ts)

PR surface

Source +35, Tests +79. Total +114 across 4 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 3 41 6 +35
Tests 1 79 0 +79
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 120 6 +114

Review metrics

Metric Value Why it matters
Production versus test changes Production +41/-6 lines; tests +79/-0 lines The production growth threads one token binding through existing owners, while the regression exercises replacement rejection and a subsequent successful save.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #178310
Summary: The issue and three open fixes address stale Files saves after same-chat workspace replacement; no replacement is established as safely landable.

Members:

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

Labels

Label changes:

  • add P2: A bounded reset-and-replacement sequence can misapply a file edit, and this focused repair currently has attributable consumer-test failures.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Label justifications:

  • P2: A bounded reset-and-replacement sequence can misapply a file edit, and this focused repair currently has attributable consumer-test failures.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

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.

Reviewed October 20, 2026, 7:16 AM ET / 11:16 UTC.

@potoior

potoior commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Both review asks are addressed on the updated head (9c90e2b3c49, pushed after the review ran):

  • Update Files consumer tests for workspace-bound tokens (P2) — fixed in test(gateway): assert session Files hashes through the workspace binding. saveFile fixtures now compute expectedHash through the same workspace binding (via a test-support helper that streams the identical byte sequence the production CAS digest uses, without tripping the oversize-rejection Buffer.from spy). Preview, BOM round-trip, identity-save, and happy-path assertions all use bound hashes. The nested-workspace-alias concurrency test keeps its single-winner outcome with each session's own binding: the winner reports its binding's hash and the rejected save reports the loser's view of the winner's bytes. All 42 cases in sessions-files.test.ts pass, plus the repository (16/16), workspace-policy lifecycle regression, workspace-fs, agents-files, and claws-drafts suites.
  • Real behavior proof — the lifecycle regression in server.sessions.create.workspace-policy.test.ts runs the registered sessions.files.get/set handlers through the real session/worktree lifecycle (real worktree allocation, same-key reset, replacement with identical base\n bytes) and asserts exactly the requested behaviors: stale-save rejection before disk mutation, replacement bytes unchanged, draft preserved client-side by the existing conflict flow, and a fresh-read save succeeding into the replacement. No mocks replace the reset or file-writing owners.

Would a maintainer reopen this PR for re-review, or should I open a fresh one from the same branch? @clawsweeper re-review

@potoior

potoior commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #178336, which landed a protocol-level binding of Files edits to the original read roots with draft preservation and listing refresh — a more complete design than this hash-salt approach. Keeping this closed; no reopen needed. The test updates here remain useful evidence that the workspace-bound token contract is observable at the RPC boundary.

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

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Files saves can overwrite a replacement workspace after a same-chat reset

2 participants