Fix reusable workspace recency ordering

This commit is contained in:
Dotta committed 2026-05-01 10:19:54 -05:00
1 parent 58fd1c1eca
commit 9fea47e19b
2 files changed
+58 -20

No files matched your search

@@ -6,17 +6,17 @@ function workspace(overrides: Partial<ReusableExecutionWorkspaceLike>): 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",
]);
});
});
+9 -9
View File
@@ -2,11 +2,11 @@ export interface ReusableExecutionWorkspaceLike {
id: string;
name: string;
cwd: string | null;
updatedAt: Date | string;
lastUsedAt: Date | string;
}
function workspaceUpdatedTime(workspace: Pick<ReusableExecutionWorkspaceLike, "updatedAt">) {
const time = new Date(workspace.updatedAt).getTime();
function workspaceLastUsedTime(workspace: Pick<ReusableExecutionWorkspaceLike, "lastUsedAt">) {
const time = new Date(workspace.lastUsedAt).getTime();
return Number.isFinite(time) ? time : 0;
}
@@ -27,7 +27,7 @@ export function orderReusableExecutionWorkspaces<T extends ReusableExecutionWork
for (const workspace of workspaces) {
const key = workspace.cwd ?? workspace.id;
const existing = deduplicatedByPath.get(key);
if (!existing || workspaceUpdatedTime(workspace) > workspaceUpdatedTime(existing)) {
if (!existing || workspaceLastUsedTime(workspace) > workspaceLastUsedTime(existing)) {
deduplicatedByPath.set(key, workspace);
}
}
@@ -35,15 +35,15 @@ export function orderReusableExecutionWorkspaces<T extends ReusableExecutionWork
const alphabetized = Array.from(deduplicatedByPath.values()).sort(compareWorkspaceNames);
if (alphabetized.length <= 1) return alphabetized;
let mostRecentlyUpdated = alphabetized[0]!;
let mostRecentlyUsed = alphabetized[0]!;
for (const workspace of alphabetized.slice(1)) {
if (workspaceUpdatedTime(workspace) > 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),
];
}