Skip to content

feat(chat): keep composer drafts across app restarts - #197846

Merged
obviyus merged 3 commits into
mainfrom
feat/chat-durable-drafts
Nov 12, 2026
Merged

obviyus merged 3 commits into
mainfrom
feat/chat-durable-drafts

Conversation

@obviyus

@obviyus obviyus commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Native chat (iOS and macOS) kept composer drafts only in memory (draftsBySession). If the app is killed, crashes, or iOS terminates it in the background, whatever you were typing is lost, and so are any picked attachments. On iOS a pending attachment also blocked switching sessions, because nothing else would keep it.

Solution

Composer drafts are stored per session in client-state.sqlite, the client-owned store that already holds the chat outbox. The schema was approved by the iOS owner.

  • New tables (migration client-state-composer-drafts-v11): composer_drafts (text, revision, updated time per gateway and session key) and composer_draft_attachments (same columns as outbox_attachments).
  • Forget: the existing Forget deletes a gateway's drafts. A trigger on the irreversible forget phase (same pattern as watch_message_forget) does the same when an older build performs the Forget.
  • One owner: when a store exists it owns the draft. The view model keeps only the visible session's text and attachments.
    • Text saves are debounced 300 ms through one chained write task. Switch, send, detach and app background wait for it.
    • Attachments are written and deleted one by one as they change.
    • Recall mode saves the draft underneath the recalled message, never the recalled message itself.
    • A draft is not loaded or saved until the session key's agent (and, for main/global, its routing) is known, so nothing is stored under a key that later changes. Text typed before the load arrives is appended to the loaded draft.
  • Clearing after send: a queued send clears the draft in the same transaction as the outbox insert; a live send clears it right away. Only what was actually sent is cleared, so text typed after the send stays.
  • Limits: a draft holds at most 40 MB of attachments, the same as the outbox per-message limit; going over shows the existing "Too large to send" notice. Drafts on one gateway share 50 MB. When a new attachment would pass that, attachments (never text) are dropped from the least recently used other drafts first.
  • Attachments and switching: pending attachments no longer block session switches once they are stored. An attachment whose save failed keeps the chat pinned, shows "Could not save … with this draft. Send or remove it before switching chats." and is retried on every flush.
  • Failed saves: a text save that SQLite rejects does not advance the saved revision, so the next flush, background or quit save retries it. Text switched away from before its save lands stays in memory and replaces the stored row on return.
  • Docs: docs/reference/database-schemas/personal-data.md describes the new tables, retention (until sent, cleared, or the Gateway is forgotten), and Forget.

Impact

What you type in a chat, including picked attachments, is still there after an app restart, a crash, or iOS ending the app in the background. Switching sessions with a pending attachment works. Older app versions ignore the new tables.

Evidence

Simulator (iOS 27, isolated local Gateway): type a message into a chat, terminate the app, relaunch.

Build Composer after relaunch
main (5d6a026) empty ("Message main…")
this PR the typed text is back

Screenshots are in the PR comments.

Upgrade from v2026.11.3 (simulator, local Gateway): queue a message offline on v2026.11.3 → install this PR over it → relaunch offline: no onboarding, message still "Queued" → Gateway back: reconnects and delivers it → type a draft, kill, relaunch: draft is back. Screenshots in the PR comments.

Tests: each new test fails with its fix removed and passes with it.

  • Survives a rebuilt view model (text and attachment).
  • Text typed before the draft loads is appended, and survives a switch away and back.
  • Cold launch waits for the agent and routing before keying the draft.
  • A draft bound through the agent catalog keeps saving after the catalog resets.
  • A send captured before its draft loads clears the composer.
  • A send clears its saved draft without waiting for the save pause: offline (enqueue clear) and online (live clear) each fail without their clear.
  • A replacement view model reads the draft its predecessor was still writing.
  • Recall mode saves the draft underneath, never the recalled message.
  • Attachments stay with their session across switches.
  • Failed writes (real SQLite failure via a temporary trigger): a failed text save is retried by the next flush; it survives a switch away and back; a failed attachment save keeps the attachment and blocks the switch.
  • macOS: the web conversation's pending check waits for the draft load.
  • Store: a v10 upgrade adds the draft tables and preserves the queue; Forget and the trigger path delete drafts; eviction drops only attachments of the oldest other drafts; an enqueue clears only the revision and attachments it carried.

Suites: OpenClawKit chat suites pass. Two ChatMediaVideoPlayerTests timing failures under parallel load pass when run alone, and that file is unrelated. iOS: 560 Swift Testing tests in 12 suites and 9 XCTest tests pass. macOS: swift build --product OpenClaw succeeds.

Reviews: a second-model design review and code review. All findings are applied: unbound text survives a switch, one draft key for revisions, a send captured before load, a test rewrite, macOS pending checks, a shared write chain across view-model rebuilds, and saving on macOS quit.

Accepted edge cases, named in code:

  • Two macOS windows on the same session: the last writer wins.
  • Text typed before the key resolves stays in memory until it does.
  • Only image previews are rebuilt on reload.

Size: production +710/−31 (two new files), tests +391/−7, docs +39/−1.

@clawsweeper

clawsweeper Bot commented Nov 12, 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 docs Improvements or additions to documentation app: ios App: ios app: macos App: macos size: XL labels Nov 12, 2026
@obviyus

obviyus commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Simulator proof: same steps on both builds (type a draft, terminate the app, relaunch).

Before (main): composer empty after relaunch

After (this PR): typed draft is back after relaunch

@clawsweeper clawsweeper Bot added the clawsweeper:intake:feature Preliminary submission classification; full source review pending. label Nov 12, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. 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:feature Preliminary submission classification; full source review pending. labels Nov 12, 2026
@clawsweeper

clawsweeper Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR preserves native Apple chat drafts and attachments across app restarts using the existing client-state database.

Example: Type “Draft survives restart 1794505244” in iOS chat, terminate the app, and relaunch.

  • Before: The composer is empty.
  • After: The typed draft returns.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A useful persistence feature with inspected app evidence, stable-release upgrade coverage, focused regressions, and both earlier defects resolved; exact-head CI is still pending.
Proof confidence 🐚 platinum hermit (4/6) This maintainer-authored PR is outside the contributor proof gate; inspected iOS screenshots show restart recovery and the reported v2026.11.3 upgrade sequence, while attachment recovery and failed-write behavior additionally have source-reviewed SQLite regressions.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Feature · Worth it: Yes · Fix scope: Complete
User problem: Unsent native chat text and picked attachments disappear when the app process ends.
Reason: Protecting user-authored content has clear value, the maintainer-authored direction is accepted, and the change reuses the established client-state database.

Merge readiness

✅ Ready for maintainer review

Keep open: this useful maintainer-authored feature remains absent on main, and both earlier failed-write findings are resolved without a remaining actionable finding.

Priority: P2
Reviewed head: 36cfb789f8f748d028f6d5276db3f0ad2b137bed

Before merge

None.

Findings

None.

Agent review details

How this fits together

The shared Apple chat view model receives composer edits, persists them by Gateway and session, and transfers submitted content to the existing outbox or live send pipeline.

flowchart LR
  A[Apple chat composer] --> B[Shared chat view model]
  B --> C[Draft persistence]
  C --> D[Client state SQLite]
  D --> B
  B --> E[Durable outbox]
  B --> F[Live Gateway send]
Loading

Technical review

Best possible solution:

Retain the shared SQLite draft owner, successful-write custody tracking, and transactional transfer to the existing outbox.

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

Current-main source establishes memory-only native drafts, and the inspected simulator screenshots support the reported restart loss and restored-text behavior; no target code was executed.

Is this the best way to solve the issue?

Extending the existing durable client-state owner is appropriate, and the revised implementation retains failed writes without introducing a competing persistence system.

AGENTS.md: found and applied where relevant.

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

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit bdf71d7 adds apps/shared/OpenClawKit/Sources/OpenClawChatUI/ClientDatabases.swift:17 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: bdf71d7d98b6; files: apps/shared/OpenClawKit/Sources/OpenClawChatUI/ClientDatabases.swift)
  • Vincent Koc: Raw commit 2c316f5 adds apps/shared/OpenClawKit/Sources/OpenClawChatUI/ClientDatabases.swift:17 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 2c316f51f741; files: apps/shared/OpenClawKit/Sources/OpenClawChatUI/ClientDatabases.swift)

PR surface

Source +752, Tests +462, Docs +41. Total +1255 across 25 files. Added test files: 2.

View PR surface stats
Area Files Added Removed Net
Source 18 783 31 +752
Tests 6 469 7 +462
Docs 1 42 1 +41
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 25 1294 39 +1255

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Review metrics

Metric Value Why it matters
Persisted model additions 2 tables and 1 forward migration Composer text and attachment bytes gain durable storage in client-state.sqlite, with inspected upgrade and cleanup coverage.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: Native draft recovery is a useful, scoped improvement that protects unsent user content.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: 📸 screenshot: Contributor real behavior proof includes screenshot evidence.

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-11-12T18:17:11.084Z sha bc217a4 :: blocked before merge. :: [P2] Retain text until its draft write succeeds | [P2] Keep attachments pinned when their persistence fails

Reviewed November 12, 2026, 2:17 PM ET / 19:17 UTC (Revision 2).

@obviyus

obviyus commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed the ClawSweeper findings on bc217a41ae4 in 36cfb789f8f:

  • Failed text save: saveComposerDraftText now reports whether the row was written. The saved revision advances only on success, so the next flush, background save or quit save retries a failed write. A switch away keeps unsaved text in memory, and coming back restores it over the stored row.
  • Failed attachment save: any attachment without a stored position now pins the chat. The user sees "Could not save … with this draft. Send or remove it before switching chats.", and every flush retries the save.
  • Tests: a real SQLite failure, injected with a temporary trigger on the draft table, covers retry on flush, a switch away and back, and an attachment that blocks the switch. Each test fails with its fix removed and passes with it.

Upgrade from v2026.11.3 (iPhone simulator, local Gateway):

  1. On the v2026.11.3 build: pair, stop the Gateway, send a message. It stays queued.
  2. Install this PR's build over it, keeping the app data, and relaunch while offline. There is no onboarding, the settings are kept, and the message still shows as queued.
  3. Start the Gateway again. The app reconnects and delivers the queued message.
  4. Type a draft, terminate and relaunch. The draft comes back.

After the failed-write fix, the kill/relaunch draft check passed again on this head.

v2026.11.3: message queued offline

After upgrade, offline: still queued

Gateway back: connected, delivered

Draft back after kill and relaunch on the upgraded app

@obviyus

obviyus commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:feature Preliminary submission classification; full source review pending. proof: 📸 screenshot Contributor real behavior proof includes screenshot evidence. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Nov 12, 2026
@obviyus
obviyus merged commit 8ef78ec into main Nov 12, 2026
146 of 148 checks passed
@obviyus
obviyus deleted the feat/chat-durable-drafts branch November 12, 2026 20:03
@obviyus

obviyus commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Merged via squash.

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

Labels

app: ios App: ios app: macos App: macos docs Improvements or additions to documentation P2 Normal backlog priority with limited blast radius. proof: 📸 screenshot Contributor real behavior proof includes screenshot evidence. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XL status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant