GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(desktop): retry failed storage writes and refuse writes after close by Hona · Pull Request #61804 · anomalyco/opencode

Conversation

@Hona

@Hona Hona commented Nov 4, 2026

Copy link
Copy Markdown
Member

Summary

The desktop main process buffers renderer storage writes (StorageUpdate, DraftsSet) and writes them to SQLite in batches. Its lifecycle was a pending map, a timer handle and a closed flag. This PR moves it to one state machine in packages/desktop/src/main/storage/write-behind-machine.ts and fixes three bugs.

Bugs

Trigger Before After
A batch write fails (for example SQLITE_BUSY) Rows go back into pending, but no timer runs. They are written only on the next set() or a clean quit; a crash in between loses writes the renderer already saw as saved The failure schedules a retry with backoff (1×, 2×, 4× … up to 64× the delay) before the error is reported
set() after close() Silently dropped. state.update() still increments revision, StorageUpdate broadcasts the unsaved rows to other windows, and the sender marks its store clean set() throws, and state.update() throws before it increments revision, so the RPC fails
drop() removes the last queued row The timer keeps running and fires an empty flush The machine returns to idle and cancels the timer

State

type WriteBehindState<T> =
  | { status: "idle" }
  | { status: "scheduled"; pending: ReadonlyMap<string, T>; attempt: number; failures: number }
  | { status: "flushing"; inFlight: ReadonlyMap<string, T>; queued: ReadonlyMap<string, T>;
      attempt: number; failures: number; closing: boolean }
  | { status: "closed" }
stateDiagram-v2
  [*] --> idle
  idle --> scheduled: set / start-timer(delay)
  idle --> closed: close.requested
  scheduled --> idle: drop (all rows) / cancel-timer
  scheduled --> flushing: timer.fired(attempt) / flush.requested / close.requested
  flushing --> idle: write.succeeded (nothing queued)
  flushing --> scheduled: write.succeeded (rows queued) / start-timer(delay)
  flushing --> scheduled: write.failed / start-timer(backoff), report-error
  flushing --> closed: write settled while closing
  closed --> closed: set / reject-closed
Loading
  • attempt names the current timer and write, so a stale timer or write result changes nothing.
  • On a failed write, rows set during the write win over the failed batch, and rows dropped during it (state.clear) are not written again.
  • The driver in write-behind.ts turns commands into calls. It owns the one timer and is the only try/catch, around the store's own write callback.
  • get, has, entries and the new closed() read the selectors selectPendingRows and isWriteBehindClosed.

Deleted

  • pending, timer and closed in createWriteBehind.
  • The silent if (closed) return in set().

Deviations from the design and product calls

  • One selector instead of three. selectPendingRows() returns the merged map; get, has and entries read it.
  • A refused write throws. StorageUpdate and DraftsSet reject after close instead of acknowledging a write that never happens. This happens only during shutdown, when the database closes too.
  • A failed write while closing is reported and dropped. The database closes with the app, so there is no later write to retry. This matches dev.

Visual

<svg xmlns="http://www.w3.org/2000/svg" width="240" height="72" viewBox="0 0 240 72" fill="none" stroke="currentColor">
  <style>
    .row{animation:send 3s ease-in-out infinite}
    .x{animation:fail 3s linear infinite}
    .ok{animation:ok 3s linear infinite}
    @keyframes send{0%{transform:translateX(0)}25%{transform:translateX(60px)}35%{transform:translateX(0)}70%,100%{transform:translateX(116px);opacity:0}}
    @keyframes fail{0%,20%{opacity:0}25%,35%{opacity:.8}40%,100%{opacity:0}}
    @keyframes ok{0%,65%{opacity:0}72%,95%{opacity:.8}100%{opacity:0}}
    @media (prefers-reduced-motion:reduce){.row,.x{animation:none;opacity:0}.ok{animation:none;opacity:.8}}
  </style>
  <rect x="20.5" y="22.5" width="40" height="28" rx="3" opacity=".5"/>
  <path d="M28 32h24M28 40h16" opacity=".4" stroke-width="2"/>
  <ellipse cx="190" cy="24" rx="20" ry="6" opacity=".6"/>
  <path d="M170 24v24c0 3.3 9 6 20 6s20-2.7 20-6V24" opacity=".6"/>
  <rect class="row" x="64" y="32" width="12" height="8" rx="1" fill="currentColor" stroke="none" opacity=".6"/>
  <path class="x" d="M128 30l8 8M136 30l-8 8" stroke-width="2"/>
  <path class="ok" d="M184 38l4 4 8-8" stroke-width="2"/>
</svg>

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Hona
fix(desktop): retry failed storage writes and refuse writes after close
fb98c25
@Hona
Hona requested a review from Brendonovich as a code owner November 4, 2026 21:45
Copilot AI balanced review requested due to automatic review settings November 4, 2026 21:45
Copilot AI reviewed Nov 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
opencodejr[bot]
opencodejr Bot reviewed Nov 4, 2026
View reviewed changes

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The fix looks correct to me. On the base, a write-behind script showed that a failed batch is never retried until the next set(), and that set() after close() is silently dropped. With this PR the failed batch is retried with backoff, writes after close throw, and dropping every queued row clears the timer. The storage tests pass and tsc -b is clean for packages/desktop. I did not run the Electron app itself.

Public API: unchanged. StorageUpdate and DraftsSet are internal desktop IPC calls; they now reject after storage closes during shutdown instead of acknowledging a write that never happens.

The machine follows the state-machine pattern in packages/desktop/AGENTS.md, and the tests use fake timers, so they shouldn't be flaky. Nothing blocking.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Hona
Hona merged commit 1a25051 into dev Nov 4, 2026
14 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@Hona
Hona deleted the storage-write-machine branch November 4, 2026 22:25
@amrkmn amrkmn mentioned this pull request Nov 7, 2026
opencode 2.2.6 amrkmn/homebrew-tap#1217
Closed
1 task
This was referenced Nov 7, 2026
feat: opencode release v2.2.6 sprisa/opencode-server#84
Merged
Tool release: OpenCode 2 v2.2.6 (was v2.2.5) agent-sh/agnix#1808
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@opencodejr opencodejr[bot] opencodejr[bot] left review comments
@Brendonovich Brendonovich Awaiting requested review from Brendonovich

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Hona
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.