Skip to content

Commit becb9c7

Browse files
authored
fix(app): order Recent sessions by the last list reload and drop the rename guard (#63370)
1 parent f738b32 commit becb9c7

15 files changed

Lines changed: 567 additions & 293 deletions

File tree

‎packages/app/e2e/regression/tabs.spec.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,74 @@ test("Recent shows the last 7 days, and Show more reveals 30 more rows, fetching
814814
await expect(more).toBeHidden()
815815
})
816816

817+
test("Recent keeps the server's order from its last reload, which a run end or window focus triggers", async ({
818+
page,
819+
}) => {
820+
const t = Date.now() - 60_000
821+
822+
const workspace = await mockWorkspace(page, {
823+
name: "RecentOrder",
824+
sessions: [
825+
{ ...a, time: { created: t, updated: t, idle: t } },
826+
{ ...b, time: { created: t + 1, updated: t + 1, idle: t + 1 } },
827+
],
828+
seed: {
829+
tabs: [],
830+
settings: { appearance: { tabLayout: "vertical" }, general: { sidebarOrganize: "recent" } },
831+
},
832+
})
833+
834+
await page.goto(
835+
`/e2e/utils/windows-menu.html?${new URLSearchParams({ server: SERVER, path: sessionHref(a.id, "sidecar") })}`,
836+
)
837+
838+
const recent = page.locator('[data-slot="sidebar-recent"]').getByRole("group", { name: "Recent", exact: true })
839+
const rows = recent.locator("[data-titlebar-tab-slot]")
840+
841+
// The mock serves these records, so changing one changes what the next list reload returns.
842+
const advance = (id: string, time: { updated: number; idle?: number }, title?: string) => {
843+
const session = workspace.sessions.find((item) => item.id === id)
844+
845+
if (!session) throw new Error(`Unknown session ${id}`)
846+
847+
Object.assign(session, { title: title ?? session.title, time: { ...session.time, ...time } })
848+
}
849+
850+
await expect(rows).toHaveText([/Tab B session/, /Tab A session/])
851+
852+
// Renaming advances the server's time, but the row stays where the last reload put it.
853+
advance(a.id, { updated: t + 2 }, "Renamed A session")
854+
await workspace.push([
855+
{
856+
id: "evt_recent_order_rename",
857+
created: t + 2,
858+
type: "session.renamed",
859+
location: { directory: workspace.directory },
860+
durable: { aggregateID: a.id, seq: 1, version: 1 },
861+
data: { sessionID: a.id, title: "Renamed A session" },
862+
} satisfies Extract<OpenCodeEvent, { type: "session.renamed" }>,
863+
])
864+
await expect(rows.first()).toContainText("Tab B session")
865+
await expect(rows.last()).toContainText("Renamed A session")
866+
867+
// The window coming back reloads the list, as the iOS app does when it returns to the foreground.
868+
await page.evaluate(() => window.dispatchEvent(new Event("focus")))
869+
await expect(rows).toHaveText([/Renamed A session/, /Tab B session/])
870+
871+
advance(b.id, { updated: t + 3, idle: t + 3 })
872+
await workspace.push([
873+
{
874+
id: "evt_recent_order_run_end",
875+
created: t + 3,
876+
type: "session.execution.succeeded",
877+
location: { directory: workspace.directory },
878+
durable: { aggregateID: b.id, seq: 1, version: 1 },
879+
data: { sessionID: b.id },
880+
} satisfies Extract<OpenCodeEvent, { type: "session.execution.succeeded" }>,
881+
])
882+
await expect(rows).toHaveText([/Tab B session/, /Renamed A session/])
883+
})
884+
817885
test("an unfocused window preserves a restored manual unread reminder", async ({ page }) => {
818886
// Playwright emulates focus for each page, so model a background window explicitly.
819887
await page.addInitScript(() => {

‎packages/app/src/composer/model.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export function createComposerModel(adapter: ComposerAdapter, options?: { queue?
204204
const sessions = createServerSessionEntries({
205205
server: server.key,
206206
known: createKnownServerSessions(() => server.ctx),
207+
apply: server.ctx.data.session.apply,
207208
projects: server.ctx.projects.list,
208209
load: async (search, signal) => {
209210
const page = await serverSDK.api.session.list({ parentID: null, search, archived: false, limit: 50 }, { signal })

‎packages/app/src/home/sessions/controller.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ export function createHomeSessionsController(home: HomeController) {
390390
.update({ sessionID: session.id, title: next }, { signal })
391391
.then(() => {
392392
ctx.data.session.remember({ ...(ctx.data.session.get(session.id) ?? session), title: next })
393-
// Rename advances time.updated server-side, but the data layer's rename guard keeps the session where it was.
393+
// Rename advances time.updated server-side; like the iOS app, the session moves when the list next reloads.
394394
queryClient.setQueryData<SessionInfo[]>(["home-sessions", conn], (current) =>
395395
current?.map((item) => (item.id === session.id ? { ...item, title: next } : item)),
396396
)

‎packages/app/src/home/start/command-palette.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function HomeCommandPalette(props: {
3636
const sessions = createServerSessionEntries({
3737
server: ServerConnection.key(props.server),
3838
known: createKnownServerSessions(server),
39+
apply: (sessions) => server()?.data.session.apply(sessions) ?? sessions,
3940
projects: () => server()?.projects.list() ?? [],
4041
load: (search, signal) =>
4142
server()?.sdk.api.session.list({ parentID: null, search, archived: false, limit: 50 }, { signal }) ??

‎packages/app/src/runtime/server/data.test.ts‎

Lines changed: 83 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { afterEach, expect, setSystemTime, test } from "bun:test"
22
import { createRoot } from "solid-js"
33
import { createData, type CreateDataInput } from "@opencode/client/solid"
44
import { OpenCode, type OpenCodeEvent, type SessionInfo } from "@opencode/client/promise"
5+
import { mergeRecentSessions } from "@/shell/titlebar/recent-sessions-model"
56
import { createDesktopData, createSessionMutations, createSessionRecency, createSessionRefresh } from "./data"
7+
import { mergeRootSessionIndex } from "./session-index"
68

79
const session: SessionInfo = {
810
id: "ses_test",
@@ -167,61 +169,80 @@ test("passive session events do not promote a session", () => {
167169
}
168170
})
169171

170-
test("a rename does not promote the session, including when a refetch returns the rename time", () => {
171-
const setup = setupDesktop(async () => Response.json({ data: session }))
172+
test("after a reload, Recent shows the order a restarted window shows for the same server data", () => {
173+
const other = { ...session, id: "ses_other", time: { created: 0, updated: 20 } }
174+
const running = setupDesktop(async () => Response.json({ data: session }))
175+
const restarted = setupDesktop(async () => Response.json({ data: session }))
176+
177+
const order = (setup: typeof running, snapshot: SessionInfo[]) =>
178+
mergeRecentSessions("server", snapshot, setup.data.session.known(), (rows) =>
179+
setup.data.session.apply(rows, { reloads: true }),
180+
).map((entry) => entry.session.id)
181+
182+
try {
183+
// The running window renamed the session, which advanced its server time. It keeps its row until a reload.
184+
running.seed(10)
185+
running.emit("session.renamed", 30, { title: "Renamed" })
186+
expect(order(running, [other, { ...session, time: { created: 0, updated: 10 } }])).toEqual([
187+
"ses_other",
188+
session.id,
189+
])
190+
191+
const reloaded = [{ ...session, title: "Renamed", time: { created: 0, updated: 30 } }, other]
192+
restarted.seed(30)
193+
expect(order(running, reloaded)).toEqual([session.id, "ses_other"])
194+
expect(order(restarted, reloaded)).toEqual([session.id, "ses_other"])
195+
} finally {
196+
running.dispose()
197+
restarted.dispose()
198+
}
199+
})
200+
201+
// The live record lags the server after a rename, model or agent change, step or metadata update, while a reloaded
202+
// list shows the newer time. A send must still bump past it until a newer record arrives.
203+
test("a send after a list showed a time ahead of the live record still bumps until the server passes it", () => {
204+
const setup = setupDesktop(async () => Response.json({ data: { ...session, time: { created: 0, updated: 10 } } }))
172205

173206
try {
174207
setup.seed(10)
175-
setup.emit("session.renamed", 50, { title: "Renamed" })
208+
setup.data.session.apply([{ ...session, time: { created: 0, updated: 50 } }], { reloads: true })
209+
setSystemTime(new Date(1_000))
210+
setup.data.session.acceptSend(session.id)
176211

177-
const renamed = { ...session, title: "Renamed", time: { created: 0, updated: 50 } }
178-
expect(setup.data.session.apply([renamed])[0].time.updated).toBe(10)
212+
const at = (updated: number) =>
213+
setup.data.session.apply([{ ...session, time: { created: 0, updated } }], { reloads: true })[0].time.updated
179214

180-
// A later change promoted the session. A stale refetch at the rename time must not demote it.
181-
expect(setup.data.session.apply([{ ...renamed, time: { created: 0, updated: 60 } }])[0].time.updated).toBe(60)
182-
expect(setup.data.session.apply([renamed])[0].time.updated).toBe(60)
215+
expect(setup.data.session.get(session.id)?.time.updated).toBe(10)
216+
expect(at(50)).toBe(1_000)
217+
expect(at(51)).toBe(51)
183218
} finally {
184219
setup.dispose()
185220
}
186221
})
187222

188-
test("a rename with no live record waits for a stale copy before settling", () => {
189-
const stale = { ...session, time: { created: 0, updated: 10 } }
190-
const renamed = { ...session, title: "Renamed", time: { created: 0, updated: 50 } }
223+
test("a projected send bump merged through an index never becomes a server time", () => {
224+
const setup = setupDesktop(async () => Response.json({ data: session }))
191225

192-
const batch = (records: SessionInfo[]) => {
193-
const recency = createSessionRecency()
194-
recency.renamed(session.id, undefined, 50)
226+
try {
227+
setup.seed(10)
228+
setSystemTime(new Date(1_000))
229+
setup.data.session.acceptSend(session.id)
195230

196-
return recency.apply(records).map((item) => item.time.updated)
197-
}
231+
// A list rendered the projected row, then merged it with a fetched copy whose idle did not advance.
232+
const projected = setup.data.session.apply([{ ...session, time: { created: 0, updated: 10 } }])
233+
expect(projected[0].time.updated).toBe(1_000)
198234

199-
// Whichever order the copies appear in, the stale copy supplies the pre-rename value.
200-
expect(batch([stale, renamed])).toEqual([10, 10])
201-
expect(batch([renamed, stale])).toEqual([10, 10])
235+
const merged = mergeRootSessionIndex(projected, [
236+
{ ...session, title: "Fetched", time: { created: 0, updated: 20 } },
237+
])
202238

203-
const sequence = (records: SessionInfo[]) => {
204-
const recency = createSessionRecency()
205-
recency.renamed(session.id, undefined, 50)
239+
expect(setup.data.session.apply(merged)[0].time.updated).toBe(1_000)
206240

207-
return [...records, stale, renamed].map((record) => recency.apply([record])[0].time.updated)
241+
// The server caught up behind the local clock. A laundered 1,000 would have pinned the session there.
242+
expect(setup.data.session.apply([{ ...session, time: { created: 0, updated: 150 } }])[0].time.updated).toBe(150)
243+
} finally {
244+
setup.dispose()
208245
}
209-
210-
// A renamed-only read has nothing to compare against. It uses the creation floor instead of promoting to `at`.
211-
const renamedFirst = sequence([renamed])
212-
expect(renamedFirst[0]).toBe(0)
213-
expect(renamedFirst.slice(1)).toEqual([10, 10])
214-
215-
const staleFirst = sequence([stale])
216-
expect(staleFirst).toEqual([10, 10, 10])
217-
})
218-
219-
test("a pre-rename record observed before the rename supplies the guard value", () => {
220-
const recency = createSessionRecency()
221-
recency.apply([{ ...session, time: { created: 0, updated: 10 } }])
222-
recency.renamed(session.id, undefined, 50)
223-
224-
expect(recency.apply([{ ...session, time: { created: 0, updated: 50 } }])[0].time.updated).toBe(10)
225246
})
226247

227248
test("an accepted send promotes immediately and survives a stale fetch until the server catches up", () => {
@@ -246,23 +267,6 @@ test("an accepted send promotes immediately and survives a stale fetch until the
246267
}
247268
})
248269

249-
test("a send after a rename is not demoted by a stale refetch at the rename time", () => {
250-
const setup = setupDesktop(async () => Response.json({ data: session }))
251-
252-
try {
253-
setup.seed(10)
254-
setup.emit("session.renamed", 50, { title: "Renamed" })
255-
setSystemTime(new Date(1_000))
256-
setup.data.session.acceptSend(session.id)
257-
258-
expect(
259-
setup.data.session.apply([{ ...session, title: "Renamed", time: { created: 0, updated: 50 } }])[0].time.updated,
260-
).toBe(1_000)
261-
} finally {
262-
setup.dispose()
263-
}
264-
})
265-
266270
test("send catch-up does not depend on the order records are applied", () => {
267271
const stale = { ...session, time: { created: 0, updated: 90 } }
268272
const fresh = { ...session, time: { created: 0, updated: 150 } }
@@ -291,44 +295,6 @@ test("send catch-up does not depend on the order records are applied", () => {
291295
expect(sequence([fresh, stale])).toBe(150)
292296
})
293297

294-
test("multiple renames keep the original pre-rename value through either renamed copy", () => {
295-
const recency = createSessionRecency()
296-
const pre = { ...session, time: { created: 0, updated: 10 } }
297-
const first = { ...session, time: { created: 0, updated: 50 } }
298-
const second = { ...session, time: { created: 0, updated: 80 } }
299-
300-
recency.renamed(session.id, pre, 50)
301-
expect(recency.apply([first])[0].time.updated).toBe(10)
302-
303-
// The live store now holds the first rename time, but it is not real activity.
304-
recency.renamed(session.id, first, 80)
305-
expect(recency.apply([first])[0].time.updated).toBe(10)
306-
expect(recency.apply([second])[0].time.updated).toBe(10)
307-
expect(recency.apply([first, second]).map((item) => item.time.updated)).toEqual([10, 10])
308-
})
309-
310-
test("a refetch that lands before the rename event still supplies the pre-rename value", () => {
311-
const recency = createSessionRecency()
312-
const stale = { ...session, time: { created: 0, updated: 10 } }
313-
const renamed = { ...session, time: { created: 0, updated: 50 } }
314-
315-
recency.apply([stale])
316-
recency.apply([renamed])
317-
recency.renamed(session.id, undefined, 50)
318-
319-
expect(recency.apply([renamed])[0].time.updated).toBe(10)
320-
})
321-
322-
test("a rename during a send does not clamp the session to the local send time", () => {
323-
const recency = createSessionRecency()
324-
const before = { ...session, time: { created: 0, updated: 200 } }
325-
326-
recency.acceptSend(session.id, before, 1_000)
327-
recency.renamed(session.id, before, 300)
328-
329-
expect(recency.apply([{ ...session, time: { created: 0, updated: 210 } }])[0].time.updated).toBe(210)
330-
})
331-
332298
test("remember restores the raw server time after apply projected a send bump", () => {
333299
const setup = setupDesktop(async () => Response.json({ data: session }))
334300

@@ -357,14 +323,6 @@ test("bounds per-session recency state", () => {
357323
expect(recency.apply([{ ...session, id: "ses_0", time: { created: 0, updated: 0 } }])[0].time.updated).toBe(0)
358324
})
359325

360-
test("the newest indexed row keeps its pre-rename time across a large page batch", () => {
361-
const recency = createSessionRecency()
362-
const rows = Array.from({ length: 600 }, (_, i) => ({ ...session, id: `ses_${i}`, time: { created: 0, updated: 600 - i } }))
363-
recency.apply(rows)
364-
recency.renamed(rows[0].id, undefined, 1_000)
365-
expect(recency.apply([{ ...rows[0], time: { created: 0, updated: 1_000 } }])[0].time.updated).toBe(600)
366-
})
367-
368326
test("reading passively synced metadata leaves an accepted send pending for indexed lists", () => {
369327
const recency = createSessionRecency()
370328
const indexed = { ...session, time: { created: 0, updated: 10 } }
@@ -427,26 +385,33 @@ test.each(["loaded", "listed"] as const)("an execution end refreshes a %s root e
427385
}
428386
})
429387

430-
test.each(["child", "unknown"] as const)("an execution end skips an unloaded %s session", async (source) => {
431-
let gets = 0
388+
test.each(["child", "unknown", "self-reloading"] as const)(
389+
"an execution end skips an unloaded %s session",
390+
async (source) => {
391+
let gets = 0
432392

433-
const setup = setupDesktop(async () => {
434-
gets++
393+
const setup = setupDesktop(async () => {
394+
gets++
435395

436-
return Response.json({ data: session })
437-
})
396+
return Response.json({ data: session })
397+
})
438398

439-
try {
440-
if (source === "child") setup.data.session.apply([{ ...session, parentID: "ses_parent" }])
441-
setup.emit("session.execution.succeeded", 50)
442-
await new Promise((resolve) => setTimeout(resolve, 0))
399+
try {
400+
if (source === "child") setup.data.session.apply([{ ...session, parentID: "ses_parent" }])
443401

444-
expect(gets).toBe(0)
445-
expect(setup.data.session.get(session.id)).toBeUndefined()
446-
} finally {
447-
setup.dispose()
448-
}
449-
})
402+
// Recent reloads its snapshot at run end, so its roots need no separate refresh.
403+
if (source === "self-reloading") setup.data.session.apply([session], { reloads: true })
404+
405+
setup.emit("session.execution.succeeded", 50)
406+
await new Promise((resolve) => setTimeout(resolve, 0))
407+
408+
expect(gets).toBe(0)
409+
expect(setup.data.session.get(session.id)).toBeUndefined()
410+
} finally {
411+
setup.dispose()
412+
}
413+
},
414+
)
450415

451416
test("coalesces overlapping refreshes for one session and keeps a queued one", async () => {
452417
const calls: string[] = []

0 commit comments

Comments
 (0)