Skip to content

fix(chat): prevent stale attachment metadata writes - #1259

Merged
shanselman merged 1 commit into
mainfrom
shanselman-fix-attachment-metadata-race
Aug 28, 2026
Merged

shanselman merged 1 commit into
mainfrom
shanselman-fix-attachment-metadata-race

Conversation

@shanselman

Copy link
Copy Markdown
Collaborator

Summary

  • Version attachment-cache mutations and reject stale snapshots before serialized disk writes.
  • Preserve the existing lock boundary so the general metadata state lock is not held during serialization or disk IO.
  • Make AttachmentMetadata_ConcurrentSavesPreserveEveryCompletedSend deterministic by pausing the older snapshot until the newer save completes, then verifying both completed sends remain on disk.

Validation

  • ./build.ps1: passed.
  • dotnet test ./tests/OpenClaw.Shared.Tests/OpenClaw.Shared.Tests.csproj --no-restore: 3,819 passed, 32 skipped.
  • dotnet test ./tests/OpenClaw.SetupEngine.Tests/OpenClaw.SetupEngine.Tests.csproj --no-restore: 1,022 passed.
  • Focused AttachmentMetadata_ConcurrentSavesPreserveEveryCompletedSend: 25/25 passed before the final rebase and 10/10 passed on the current head.
  • Full OpenClaw.Tray.Tests was attempted twice. The runner hung without a test failure summary and the second diagnostic run was externally stopped. Hosted CI remains the full-suite proof.
  • Rubber-duck review: no blocking issues.
  • python .agents\skills\autoreview\scripts\autoreview --mode local --engine codex --model gpt-5.6-sol --thinking high --stream-engine-output: clean, no accepted or actionable findings, overall confidence 0.94.

Real behavior proof

The deterministic regression test captures attachment save version 1, blocks it after snapshot capture, completes version 2 with both sends, and then releases version 1. The stale version 1 writer is rejected and the persisted JSON retains both first.png and second.png.

PR #1205 dependency

PR #1205 must merge this fix from main after this PR lands, then rerun its GitHub CI. This PR does not merge #1205 or itself.

@clawsweeper

clawsweeper Bot commented Aug 28, 2026

Copy link
Copy Markdown

🦞👀
ClawSweeper picked this up.

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

@shanselman shanselman added the status: 🚢 actively landing A maintainer or agent is actively driving this item through implementation, validation, or merge. label Aug 28, 2026
@clawsweeper clawsweeper Bot added P2 Normal priority bug or improvement with limited blast radius. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: needs maintainer proof decision A ClawSweeper-authored PR needs a maintainer proof capture or override decision. labels Aug 28, 2026
@clawsweeper

clawsweeper Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codex review: needs real behavior proof before merge. Reviewed August 28, 2026, 4:38 AM ET / 08:38 UTC.

ClawSweeper review

What this changes

The PR versions local chat attachment-metadata saves so an older snapshot cannot overwrite newer completed-send metadata on disk.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep open. Current main still permits an older attachment snapshot to write after a newer one, while this focused patch versions mutations and rejects that stale final write; the collaborator-authored work is actively landing and has a deterministic regression test.

Priority: P2
Reviewed head: 6af10270ecbb326eec974ed24f1627252a148d07

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) PR readiness rating was derived from proof quality, review findings, security review, and reviewer confidence.
Proof confidence 🌊 off-meta tidepool Not applicable: The author is a collaborator, so the external-contributor real-behavior-proof gate does not apply; the PR body also describes a deterministic current-head regression scenario.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The author is a collaborator, so the external-contributor real-behavior-proof gate does not apply; the PR body also describes a deterministic current-head regression scenario.
Evidence reviewed 5 items Current-main gap: The verified PR base, which is the fetched current main revision, snapshots attachment metadata and writes it under a serialization gate without a version or dirty-state recheck, allowing a later-finishing older snapshot to replace newer data.
Introduced stale-write protection: Each attachment mutation now increments a save version, and the writer checks that version both before snapshotting and immediately before the atomic write while holding the attachment writer gate.
Deterministic regression coverage: The changed test pauses version 1 after its snapshot, completes version 2, releases version 1, and verifies that the persisted cache contains both attachment records.
Findings None None.
Security None None.

Live Verification

Command: dotnet test ./tests/OpenClaw.Tray.Tests/OpenClaw.Tray.Tests.csproj --filter FullyQualifiedName~AttachmentMetadata_ConcurrentSavesPreserveEveryCompletedSend

Result: FAIL (failed) — execution before step 1 expect_output: sh -lc pnpm install --ignore-scripts --frozen-lockfile failed: ! Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-11.24.0.tgz | [ERR_PNPM_NO_LOCKFILE] Cannot install with "frozen-lockfile" because pnpm-lock.yaml is absent Note that in CI environments this setting is true by default. If you still need to run install in such cases, use "pnpm install --no-frozen-lockfile"

sh -lc pnpm install --ignore-scripts --frozen-lockfile failed: ! Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-11.24.0.tgz | [ERR_PNPM_NO_LOCKFILE] Cannot install with "frozen-lockfile" because pnpm-lock.yaml is absent Note that in CI environments this setting is true by default. If you still need to run install in such cases, use "pnpm install --no-frozen-lockfile"

Assertions:

  • FAIL expect_output: Passed!

How this fits together

The native Tray chat provider records attachment metadata after a completed send and persists it to a local JSON cache. Later chat history restoration reads that cache to associate local attachment details with messages.

flowchart LR
A[Completed chat send] --> B[Attachment metadata cache]
B --> C[Versioned snapshot]
C --> D[Stale-write check]
D --> E[Atomic JSON cache]
E --> F[Chat history restore]
Loading

Before merge

  • Add real behavior proof - Not applicable: The author is a collaborator, so the external-contributor real-behavior-proof gate does not apply; the PR body also describes a deterministic current-head regression scenario.
  • Resolve merge risk (P1) - The supplied context has no completed exact-head full Tray-suite result: the PR reports local suite hangs and the hosted test job was still running.
  • Complete next step (P2) - No discrete repair was found in review; the remaining action is normal exact-head validation and merge review.
Agent review details

Security

None.

Review metrics

Metric Value Why it matters
Focused change scope 2 files affected: production +41/-5, tests +51/-37 The implementation and deterministic regression coverage remain tightly scoped to attachment-cache persistence.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Preserve the versioned attachment writer and land it once the exact-head full Tray validation confirms the focused race fix integrates cleanly.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Preserve the versioned attachment writer and land it once the exact-head full Tray validation confirms the focused race fix integrates cleanly.

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

Yes, from source. On the base revision, two concurrent saves can snapshot different cache states, and the older snapshot can acquire the writer gate last and overwrite the newer cache file.

Is this the best way to solve the issue?

Yes. A monotonic per-mutation version with a final gated recheck directly prevents the source-proven last-stale-writer race without holding the metadata lock during serialization or disk I/O.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 4c078f9ee5f9.

Labels

Label changes:

  • add P2: This fixes a bounded local chat metadata persistence race without evidence of an urgent outage or security impact.
  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🌊 off-meta tidepool and patch quality is 🦞 diamond lobster.
  • add status: needs maintainer proof decision: A ClawSweeper-authored PR needs a maintainer proof capture or override decision. Not applicable: The author is a collaborator, so the external-contributor real-behavior-proof gate does not apply; the PR body also describes a deterministic current-head regression scenario.

Label justifications:

  • P2: This fixes a bounded local chat metadata persistence race without evidence of an urgent outage or security impact.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🌊 off-meta tidepool and patch quality is 🦞 diamond lobster.
  • status: needs maintainer proof decision: A ClawSweeper-authored PR needs a maintainer proof capture or override decision. Not applicable: The author is a collaborator, so the external-contributor real-behavior-proof gate does not apply; the PR body also describes a deterministic current-head regression scenario.

Evidence

What I checked:

Likely related people:

  • Caleb Eden: The feature-history log associates the native chat media introduction with the metadata-store path. (role: attachment metadata feature introducer; confidence: high; commits: a76c85218c7d; files: src/OpenClaw.Tray.WinUI/Chat/ChatMetadataStore.cs)
  • Scott Hanselman: Recent current-history entries cover the chat provider and its tests, in addition to the reviewed fix. (role: recent chat-area contributor; confidence: medium; commits: a1d7cb05de95; files: src/OpenClaw.Tray.WinUI/Chat/OpenClawChatDataProvider.cs, tests/OpenClaw.Tray.Tests/OpenClawChatDataProviderTests.cs)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@shanselman shanselman removed the status: needs maintainer proof decision A ClawSweeper-authored PR needs a maintainer proof capture or override decision. label Aug 28, 2026
@shanselman
shanselman merged commit 309299b into main Aug 28, 2026
24 checks passed
@shanselman
shanselman deleted the shanselman-fix-attachment-metadata-race branch August 28, 2026 09:00
@shanselman shanselman removed the status: 🚢 actively landing A maintainer or agent is actively driving this item through implementation, validation, or merge. label Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal priority bug or improvement with limited blast radius. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant