Skip to content

Commit 6b72d65

Browse files
authored
feat(ui): preserve composer drafts across restarts (#125332)
* feat(ui): preserve composer drafts across restarts Persist draft text and attachment Blobs in browser-local IndexedDB, scoped to Gateway credentials and session or New Session targets. Clean up durable data after successful send or creation, attachment removal, and session deletion. * fix(ui): preserve text when draft attachments exceed cap preserve text-only durable fallback for oversized attachments; serialize visible Incognito transition with its storage fence; document retention and disposal. * test(ui): make draft retirement proof isolate-safe The shared isolate:false UI suite exposed an order-dependent module mock; exercise and settle the real IndexedDB boundary instead. * style(ui): format session mutation imports * fix(ui): keep incognito drafts memory-only * perf(ui): lazy-load durable draft storage * fix(ui): sequence durable draft transitions * fix(ui): restore text-only new session drafts Programmatic draft restoration must not advance the user-mutation generation; real user input continues through setMessage. Move navigation transition ownership into the existing handoff module as a behavior-neutral extraction that keeps the submission owner within max-lines. * fix(ui): restore text-only drafts without import cycles Complete the text-only restore fix by moving started-session route transitions into a leaf module. This keeps max-lines ownership clean without reintroducing the Madge cycle. * fix(ui): reconcile attachment handoffs with durable drafts Restore programmatic attachment handoffs without recording a user mutation. Cover stale navigation state losing to newer durable drafts across two pages and a fresh page. * fix(ui): start durable attachment writes before teardown Start each CAS IndexedDB write and retirement immediately so text and attachment transactions register before page teardown. IndexedDB readwrite ordering and draft revisions keep snapshots serialized. * fix(ui): persist New Session drafts before teardown Accept the committed predecessor or a known in-flight local write ID atomically so New Session writes can start before teardown without spurious local-lineage conflicts. Reset cached lineage when authoritative storage is missing and re-snapshot the still-current edit.
1 parent 1c91f1d commit 6b72d65

30 files changed

Lines changed: 2734 additions & 132 deletions

‎docs/web/control-ui.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ Choose **Set icon** from a single session's context menu to give its sidebar row
236236

237237
## New session page
238238

239-
The **+** in the sidebar session-list header opens a full-page draft at `/new`: nothing is created until you send the first message. A unified **Place** picker chooses the working folder and, for admin operators, the execution destination: **Gateway · local**, a paired node that exposes `system.run`, or an available cloud profile. The folder defaults to the agent workspace. Connections with `operator.write` can browse, restore recent folders, and start sessions anywhere inside a configured agent workspace; another absolute Gateway path requires `operator.admin` but can run directly without being a Git checkout. When the selected Gateway folder is a Git checkout, the same picker offers optional **Worktree** isolation with a base-branch picker backed by `worktrees.branches` (no fetch) and an optional worktree name (the branch becomes `openclaw/<name>`). Cloud workers require that managed-worktree path; paired nodes and their filesystem browsing remain admin-only. The composer footer chooses the new session's model and reasoning level. The **Incognito** toggle in the new-session page's top-right control rail creates a web-only thread whose session entry, transcript, and compaction state stay in memory until the Gateway restarts; OpenClaw also skips its automatic memory flush. The agent keeps its normal tools, so an explicit save request or tool-driven file write can still persist data. The model provider still processes messages, and content-free audit metadata is still recorded. Cloud starts persist their model and reasoning choices before dispatching the session to its worker.
239+
The **+** in the sidebar session-list header opens a full-page draft at `/new`: nothing is created until you send the first message. A unified **Place** picker chooses the working folder and, for admin operators, the execution destination: **Gateway · local**, a paired node that exposes `system.run`, or an available cloud profile. The folder defaults to the agent workspace. Connections with `operator.write` can browse, restore recent folders, and start sessions anywhere inside a configured agent workspace; another absolute Gateway path requires `operator.admin` but can run directly without being a Git checkout. When the selected Gateway folder is a Git checkout, the same picker offers optional **Worktree** isolation with a base-branch picker backed by `worktrees.branches` (no fetch) and an optional worktree name (the branch becomes `openclaw/<name>`). Cloud workers require that managed-worktree path; paired nodes and their filesystem browsing remain admin-only. The composer footer chooses the new session's model and reasoning level.
240+
241+
Unsent text and staged attachments can be recovered only in the same browser profile and Gateway credential scope; they are never stored on the Gateway or synced across devices. The browser keeps the 20 most recently edited draft scopes per Gateway credential scope for up to seven days, with at most 25 MiB of attachment data per draft. A successful send or New Session creation, explicit attachment removal, confirmed session deletion, or clearing browser site data removes the corresponding durable data. If a draft's attachments exceed the cap, the current tab keeps them and shows the existing storage warning, but only the text is restart-recoverable. Incognito and private-browser drafts are excluded from durable restart recovery. The **Incognito** toggle in the new-session page's top-right control rail retires that browser draft and creates a web-only thread whose session entry, transcript, and compaction state stay in memory until the Gateway restarts; OpenClaw also skips its automatic memory flush. The agent keeps its normal tools, so an explicit save request or tool-driven file write can still persist data. The model provider still processes messages, and content-free audit metadata is still recorded. Cloud starts persist their model and reasoning choices before dispatching the session to its worker.
240242

241243
**Projects.** The Place picker lists configured agent workspaces and repositories recorded with `projects.register`. Read-only connections receive project names and IDs; checkout paths and origin URLs are included only at `operator.write`. An admin can browse to a Git checkout and choose **Register as project**; write-only operators see a hint directing them to that flow. Choosing a project sends its ID through `sessions.create`, so it can run directly or supply the source for optional Worktree isolation without submitting a raw path. If its checkout was moved or removed, re-register it or run `openclaw doctor --fix` before starting another session there.
242244

@@ -454,7 +456,7 @@ Two capture modes package page context for the agent:
454456

455457
One composer accepts up to four browser annotation cards and 8,000 total characters of generated annotation context. When it reaches either limit, the browser panel keeps the current capture so you can remove a card and retry; Undo also preserves the limit instead of evicting another card.
456458

457-
Within the same Control UI tab, staged images, files, pasted images, large pasted text, browser annotations, and mixed attachment packages stay with their pane and session across ordinary route changes and narrow split-pane remounts. Switching sessions keeps them scoped to the original session and restores them only when that session is revisited. A hard reload, pane close, Gateway-owner change, or application shutdown discards the staged package.
459+
Staged images, files, pasted images, large pasted text, browser annotations, and mixed attachment packages stay with their composer and session across route changes, split-pane remounts, hard reloads, and application restarts. The browser-local retention, scope, and disposal rules described under [New session page](#new-session-page) also apply to existing-session composers. If attachments exceed the durable cap, the current tab keeps them and shows the storage warning; the text remains restart-recoverable, but those attachments do not. If the browser refuses storage entirely, the current tab keeps the live composer and shows the same warning, but that draft cannot be recovered after restart.
458460

459461
When this preference is off, the macOS app keeps its native link-browser sidebar for links clicked in the dashboard. The browser panel works there too, and is the way to annotate pages on every other platform.
460462

‎ui/src/e2e/chat-attachment-read-lifecycle.e2e.test.ts‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,121 @@ async function pastePng(composer: Locator): Promise<void> {
5656
}
5757

5858
suite.define(() => {
59+
it("restores isolated session drafts across fresh pages and retires sent or removed attachments", async () => {
60+
const firstSession = "agent:main:restart-session-a";
61+
const secondSession = "agent:main:restart-session-b";
62+
const sessionsList = {
63+
count: 2,
64+
defaults: { contextTokens: null, model: "gpt-5.5", modelProvider: "openai" },
65+
path: "",
66+
sessions: [
67+
{ key: firstSession, kind: "direct", updatedAt: 2 },
68+
{ key: secondSession, kind: "direct", updatedAt: 1 },
69+
],
70+
ts: Date.now(),
71+
};
72+
const artifactDir = process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim();
73+
if (artifactDir) {
74+
await mkdir(artifactDir, { recursive: true });
75+
}
76+
const context = await suite.browser.newContext({
77+
locale: "en-US",
78+
...(artifactDir
79+
? { recordVideo: { dir: artifactDir, size: { height: 900, width: 1280 } } }
80+
: {}),
81+
serviceWorkers: "block",
82+
viewport: { height: 900, width: 1280 },
83+
});
84+
const activeComposer = (page: Page) =>
85+
page.locator(
86+
'openclaw-chat-pane[aria-hidden="false"] .agent-chat__composer-combobox textarea',
87+
);
88+
const activeAttachments = (page: Page) =>
89+
page.locator('openclaw-chat-pane[aria-hidden="false"] .chat-attachment-thumb');
90+
try {
91+
const firstPage = await context.newPage();
92+
await installMockGateway(firstPage, {
93+
methodResponses: { "sessions.list": sessionsList },
94+
sessionKey: firstSession,
95+
});
96+
await firstPage.goto(controlUiSessionUrl(suite.server.baseUrl, firstSession));
97+
await activeComposer(firstPage).fill("restart draft A with image");
98+
await pastePng(activeComposer(firstPage));
99+
await expect.poll(() => activeAttachments(firstPage).count()).toBe(1);
100+
101+
await navigateToControlUiSession(firstPage, secondSession);
102+
await activeComposer(firstPage).fill("restart draft B with removable file");
103+
await firstPage
104+
.locator('openclaw-chat-pane[aria-hidden="false"] .agent-chat__file-input')
105+
.setInputFiles({
106+
name: "remove-me.txt",
107+
mimeType: "text/plain",
108+
buffer: Buffer.from("remove this attachment"),
109+
});
110+
await expect.poll(() => activeAttachments(firstPage).count()).toBe(1);
111+
await firstPage.close();
112+
113+
const restoredPage = await context.newPage();
114+
const restoredGateway = await installMockGateway(restoredPage, {
115+
methodResponses: { "sessions.list": sessionsList },
116+
sessionKey: firstSession,
117+
});
118+
await restoredPage.goto(controlUiSessionUrl(suite.server.baseUrl, firstSession));
119+
await expect
120+
.poll(() => activeComposer(restoredPage).inputValue())
121+
.toBe("restart draft A with image");
122+
await expect.poll(() => activeAttachments(restoredPage).count()).toBe(1);
123+
if (artifactDir) {
124+
await restoredPage.screenshot({
125+
path: path.join(artifactDir, "existing-session-restart-draft-restored.png"),
126+
});
127+
}
128+
129+
await navigateToControlUiSession(restoredPage, secondSession);
130+
await expect
131+
.poll(() => activeComposer(restoredPage).inputValue())
132+
.toBe("restart draft B with removable file");
133+
await expect.poll(() => activeAttachments(restoredPage).count()).toBe(1);
134+
await restoredPage
135+
.locator('openclaw-chat-pane[aria-hidden="false"] .chat-attachment-remove')
136+
.click();
137+
await expect.poll(() => activeAttachments(restoredPage).count()).toBe(0);
138+
139+
await navigateToControlUiSession(restoredPage, firstSession);
140+
await activeComposer(restoredPage).press("Enter");
141+
const send = await restoredGateway.waitForRequest("chat.send");
142+
expect(send.params).toMatchObject({
143+
sessionKey: firstSession,
144+
message: "restart draft A with image",
145+
attachments: [{ content: ONE_PIXEL_PNG_B64, fileName: "pixel.png", mimeType: "image/png" }],
146+
});
147+
await expect.poll(() => activeComposer(restoredPage).inputValue()).toBe("");
148+
await expect.poll(() => activeAttachments(restoredPage).count()).toBe(0);
149+
await restoredPage.close();
150+
151+
const clearedPage = await context.newPage();
152+
await installMockGateway(clearedPage, {
153+
methodResponses: { "sessions.list": sessionsList },
154+
sessionKey: firstSession,
155+
});
156+
await clearedPage.goto(controlUiSessionUrl(suite.server.baseUrl, firstSession));
157+
await expect.poll(() => activeComposer(clearedPage).inputValue()).toBe("");
158+
await expect.poll(() => activeAttachments(clearedPage).count()).toBe(0);
159+
await navigateToControlUiSession(clearedPage, secondSession);
160+
await expect
161+
.poll(() => activeComposer(clearedPage).inputValue())
162+
.toBe("restart draft B with removable file");
163+
await expect.poll(() => activeAttachments(clearedPage).count()).toBe(0);
164+
if (artifactDir) {
165+
await clearedPage.screenshot({
166+
path: path.join(artifactDir, "existing-session-restart-drafts-cleaned.png"),
167+
});
168+
}
169+
} finally {
170+
await context.close();
171+
}
172+
});
173+
59174
it("rejects a combined attachment frame before the Gateway connection is lost", async () => {
60175
await suite.withPage(
61176
{

0 commit comments

Comments
 (0)