From 9fea47e19bce39d9734bbc7d84a41bebeaa24887 Mon Sep 17 00:00:00 2001 From: Dotta Date: Fri, 1 May 2026 10:19:54 -0500 Subject: [PATCH] Fix reusable workspace recency ordering --- .../lib/reusable-execution-workspaces.test.ts | 60 +++++++++++++++---- ui/src/lib/reusable-execution-workspaces.ts | 18 +++--- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/ui/src/lib/reusable-execution-workspaces.test.ts b/ui/src/lib/reusable-execution-workspaces.test.ts index 31e949de3d0..a1c380a216e 100644 --- a/ui/src/lib/reusable-execution-workspaces.test.ts +++ b/ui/src/lib/reusable-execution-workspaces.test.ts @@ -6,17 +6,17 @@ function workspace(overrides: Partial): Reusable id: overrides.id ?? "workspace-id", name: overrides.name ?? "Workspace", cwd: overrides.cwd ?? null, - updatedAt: overrides.updatedAt ?? "2026-01-01T00:00:00.000Z", + lastUsedAt: overrides.lastUsedAt ?? "2026-01-01T00:00:00.000Z", }; } describe("orderReusableExecutionWorkspaces", () => { - it("puts the most recently updated workspace first and sorts the rest alphabetically", () => { + it("puts the most recently used workspace first and sorts the rest alphabetically", () => { const workspaces = [ - workspace({ id: "charlie", name: "Charlie", updatedAt: "2026-01-03T00:00:00.000Z" }), - workspace({ id: "zulu", name: "Zulu", updatedAt: "2026-01-05T00:00:00.000Z" }), - workspace({ id: "alpha", name: "Alpha", updatedAt: "2026-01-01T00:00:00.000Z" }), - workspace({ id: "bravo", name: "Bravo", updatedAt: "2026-01-04T00:00:00.000Z" }), + workspace({ id: "charlie", name: "Charlie", lastUsedAt: "2026-01-03T00:00:00.000Z" }), + workspace({ id: "zulu", name: "Zulu", lastUsedAt: "2026-01-05T00:00:00.000Z" }), + workspace({ id: "alpha", name: "Alpha", lastUsedAt: "2026-01-01T00:00:00.000Z" }), + workspace({ id: "bravo", name: "Bravo", lastUsedAt: "2026-01-04T00:00:00.000Z" }), ]; expect(orderReusableExecutionWorkspaces(workspaces).map((item) => item.id)).toEqual([ @@ -27,12 +27,22 @@ describe("orderReusableExecutionWorkspaces", () => { ]); }); - it("keeps only the latest updated workspace for duplicate paths before sorting", () => { + it("keeps only the latest used workspace for duplicate paths before sorting", () => { const workspaces = [ - workspace({ id: "older-duplicate", name: "Older duplicate", cwd: "/tmp/shared", updatedAt: "2026-01-01T00:00:00.000Z" }), - workspace({ id: "beta", name: "Beta", cwd: "/tmp/beta", updatedAt: "2026-01-02T00:00:00.000Z" }), - workspace({ id: "newer-duplicate", name: "Newer duplicate", cwd: "/tmp/shared", updatedAt: "2026-01-04T00:00:00.000Z" }), - workspace({ id: "alpha", name: "Alpha", cwd: "/tmp/alpha", updatedAt: "2026-01-03T00:00:00.000Z" }), + workspace({ + id: "older-duplicate", + name: "Older duplicate", + cwd: "/tmp/shared", + lastUsedAt: "2026-01-01T00:00:00.000Z", + }), + workspace({ id: "beta", name: "Beta", cwd: "/tmp/beta", lastUsedAt: "2026-01-02T00:00:00.000Z" }), + workspace({ + id: "newer-duplicate", + name: "Newer duplicate", + cwd: "/tmp/shared", + lastUsedAt: "2026-01-04T00:00:00.000Z", + }), + workspace({ id: "alpha", name: "Alpha", cwd: "/tmp/alpha", lastUsedAt: "2026-01-03T00:00:00.000Z" }), ]; expect(orderReusableExecutionWorkspaces(workspaces).map((item) => item.id)).toEqual([ @@ -41,4 +51,32 @@ describe("orderReusableExecutionWorkspaces", () => { "beta", ]); }); + + it("does not let updatedAt churn outrank the last used workspace", () => { + type WorkspaceWithUpdatedAt = ReusableExecutionWorkspaceLike & { updatedAt: Date | string }; + const workspaces: WorkspaceWithUpdatedAt[] = [ + { + ...workspace({ + id: "recently-used", + name: "Recently used", + cwd: "/tmp/shared", + lastUsedAt: "2026-01-04T00:00:00.000Z", + }), + updatedAt: "2026-01-01T00:00:00.000Z", + }, + { + ...workspace({ + id: "recently-updated", + name: "Recently updated", + cwd: "/tmp/shared", + lastUsedAt: "2026-01-01T00:00:00.000Z", + }), + updatedAt: "2026-01-05T00:00:00.000Z", + }, + ]; + + expect(orderReusableExecutionWorkspaces(workspaces).map((item) => item.id)).toEqual([ + "recently-used", + ]); + }); }); diff --git a/ui/src/lib/reusable-execution-workspaces.ts b/ui/src/lib/reusable-execution-workspaces.ts index c2d4a1bca32..633c9122f84 100644 --- a/ui/src/lib/reusable-execution-workspaces.ts +++ b/ui/src/lib/reusable-execution-workspaces.ts @@ -2,11 +2,11 @@ export interface ReusableExecutionWorkspaceLike { id: string; name: string; cwd: string | null; - updatedAt: Date | string; + lastUsedAt: Date | string; } -function workspaceUpdatedTime(workspace: Pick) { - const time = new Date(workspace.updatedAt).getTime(); +function workspaceLastUsedTime(workspace: Pick) { + const time = new Date(workspace.lastUsedAt).getTime(); return Number.isFinite(time) ? time : 0; } @@ -27,7 +27,7 @@ export function orderReusableExecutionWorkspaces workspaceUpdatedTime(existing)) { + if (!existing || workspaceLastUsedTime(workspace) > workspaceLastUsedTime(existing)) { deduplicatedByPath.set(key, workspace); } } @@ -35,15 +35,15 @@ export function orderReusableExecutionWorkspaces workspaceUpdatedTime(mostRecentlyUpdated)) { - mostRecentlyUpdated = workspace; + if (workspaceLastUsedTime(workspace) > workspaceLastUsedTime(mostRecentlyUsed)) { + mostRecentlyUsed = workspace; } } return [ - mostRecentlyUpdated, - ...alphabetized.filter((workspace) => workspace.id !== mostRecentlyUpdated.id), + mostRecentlyUsed, + ...alphabetized.filter((workspace) => workspace.id !== mostRecentlyUsed.id), ]; }