Skip to content

fix(app): debounce prompt draft persistence - #40155

Open
DGGua wants to merge 2 commits into
anomalyco:devfrom
DGGua:debounce-prompt-persist
Open

fix(app): debounce prompt draft persistence#40155
DGGua wants to merge 2 commits into
anomalyco:devfrom
DGGua:debounce-prompt-persist

Conversation

@DGGua

@DGGua DGGua commented Aug 2, 2026

Copy link
Copy Markdown

Issue for this PR

Fixes #36486

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Desktop chat typing lags because every keystroke persists the prompt draft through IPC to electron-store (full-file rewrite), with no debounce. Other inputs do not hit this path.

This adds optional debounceMs to persisted(), enables 300ms debounce for prompt drafts, and updates prompt+cursor in one store write so we do not serialize/persist twice per keystroke. Drafts still flush on idle, blur/unload, and dispose.

How did you verify your code works?

  • bun test src/utils/persist.test.ts src/context/prompt-state.test.ts in packages/app
  • bun test src/v2/components/prompt-input/store.test.ts in packages/session-ui
  • Built and ran Windows desktop package locally; chat input typing felt responsive, and draft restore after quit still worked

Screenshots / recordings

N/A (performance fix, no visual UI change).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Typing in the desktop chat input was lagging because every keystroke
JSON-serialized the prompt store and wrote it through IPC to
electron-store. Debounce those writes and update prompt+cursor in one
store set so desktop input stays responsive.
@DGGua
DGGua requested review from Brendonovich and Hona as code owners August 2, 2026 12:14
Copilot AI review requested due to automatic review settings August 2, 2026 12:14
@github-actions github-actions Bot added needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Aug 2, 2026
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@DGGua

DGGua commented Aug 2, 2026

Copy link
Copy Markdown
Author

Updated the PR description to the required template and linked Fixes #36486.

@github-actions github-actions Bot removed needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Aug 2, 2026
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR reduces prompt draft persistence overhead during typing (especially on desktop) by debouncing storage writes and minimizing redundant persisted-store updates, with a unit test intended to validate write coalescing.

Changes:

  • Add an optional debounceMs to persisted targets and wrap storage with a debounced write layer.
  • Update prompt + cursor in a single store write to avoid duplicate persistence serialization.
  • Add a unit test to validate debounced write coalescing behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/session-ui/src/v2/components/prompt-input/store.ts Consolidates prompt+cursor updates into single store writes for fewer persisted-store updates.
packages/app/src/utils/persist.ts Introduces debounceMs and debounced storage wrapper for persisted writes.
packages/app/src/utils/persist.test.ts Adds coverage for debounced storage write coalescing.
packages/app/src/context/prompt-state.ts Applies debounced persistence to prompt drafts and consolidates prompt+cursor writes.
Suppressed comments (1)

packages/app/src/utils/persist.test.ts:234

  • The debounce test currently relies on wall-clock timing and a Promise wrapper. Using vi.useFakeTimers() makes this deterministic and also lets the test cover the important cleanup-flush behavior (dispose before the timer fires) that is relied on when quitting the desktop app soon after typing.
  test("debounceWrites coalesces rapid setItem calls into one flush", async () => {
    await new Promise<void>((resolve, reject) => {
      createRoot((dispose) => {
        try {
          const api = persistTesting.debounceWrites(persistTesting.localStorageDirect(), 20)
          api.setItem("draft", '{"v":1}')
          api.setItem("draft", '{"v":2}')
          api.setItem("draft", '{"v":3}')
          expect(storage.calls.set).toBe(0)

          setTimeout(() => {
            try {
              expect(storage.calls.set).toBe(1)
              expect(storage.getItem("draft")).toBe('{"v":3}')
              dispose()
              resolve()
            } catch (error) {
              dispose()
              reject(error)
            }
          }, 40)
        } catch (error) {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/app/src/utils/persist.ts Outdated
Comment thread packages/app/src/utils/persist.test.ts Outdated
Preserve live AsyncStorage getters with a Proxy wrapper, and drive
debounce tests with fake timers plus dispose-flush coverage.
@DGGua

DGGua commented Aug 2, 2026

Copy link
Copy Markdown
Author

Addressed Copilot review feedback:

  • debounceWrites now wraps storage with a Proxy so getter-based fields like desktop AsyncStorage.length stay live
  • debounce tests use Bun fake timers (no wall-clock sleeps) and cover dispose flush + length getter preservation

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat input box has severe typing delay, other input boxes work normally

2 participants