Skip to content

Commit e1dbf03

Browse files
committed
fix(usage): preserve partial workspace snapshots
1 parent 15b2fcf commit e1dbf03

3 files changed

Lines changed: 41 additions & 31 deletions

File tree

‎apps/server/src/usage/usageWorkspaceCwds.test.ts‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,40 @@ class ProjectionUnavailable extends Data.TaggedError("ProjectionUnavailable") {}
88

99
it.effect("falls back to the server cwd when workspace projections fail", () =>
1010
Effect.gen(function* () {
11-
const workspaceCwds = yield* resolveUsageWorkspaceCwds(
12-
"/server-cwd",
11+
const workspaceCwds = yield* resolveUsageWorkspaceCwds("/server-cwd", [
1312
Effect.fail(new ProjectionUnavailable()),
14-
);
13+
]);
1514

1615
assert.deepEqual(workspaceCwds, ["/server-cwd"]);
1716
}),
1817
);
1918

2019
it.effect("collects distinct project and worktree cwd values", () =>
2120
Effect.gen(function* () {
22-
const workspaceCwds = yield* resolveUsageWorkspaceCwds(
23-
"/server-cwd",
24-
Effect.succeed([
25-
{
26-
projects: [{ workspaceRoot: "/project-a" }, { workspaceRoot: "/server-cwd" }],
27-
threads: [{ worktreePath: "/worktree-a" }, { worktreePath: null }],
28-
},
29-
{
30-
projects: [{ workspaceRoot: "/project-a" }],
31-
threads: [{ worktreePath: "/worktree-a" }],
32-
},
33-
]),
34-
);
21+
const workspaceCwds = yield* resolveUsageWorkspaceCwds("/server-cwd", [
22+
Effect.succeed({
23+
projects: [{ workspaceRoot: "/project-a" }, { workspaceRoot: "/server-cwd" }],
24+
threads: [{ worktreePath: "/worktree-a" }, { worktreePath: null }],
25+
}),
26+
Effect.succeed({
27+
projects: [{ workspaceRoot: "/project-a" }],
28+
threads: [{ worktreePath: "/worktree-a" }],
29+
}),
30+
]);
31+
32+
assert.deepEqual(workspaceCwds, ["/server-cwd", "/project-a", "/worktree-a"]);
33+
}),
34+
);
35+
36+
it.effect("keeps successful workspace projections when another fails", () =>
37+
Effect.gen(function* () {
38+
const workspaceCwds = yield* resolveUsageWorkspaceCwds("/server-cwd", [
39+
Effect.succeed({
40+
projects: [{ workspaceRoot: "/project-a" }],
41+
threads: [{ worktreePath: "/worktree-a" }],
42+
}),
43+
Effect.fail(new ProjectionUnavailable()),
44+
]);
3545

3646
assert.deepEqual(workspaceCwds, ["/server-cwd", "/project-a", "/worktree-a"]);
3747
}),

‎apps/server/src/usage/usageWorkspaceCwds.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@ interface UsageWorkspaceSnapshot {
1212
*/
1313
export const resolveUsageWorkspaceCwds = Effect.fn("resolveUsageWorkspaceCwds")(function* <E, R>(
1414
serverCwd: string,
15-
loadSnapshots: Effect.Effect<readonly UsageWorkspaceSnapshot[], E, R>,
15+
loadSnapshots: readonly Effect.Effect<UsageWorkspaceSnapshot, E, R>[],
1616
) {
17-
const snapshots = yield* loadSnapshots.pipe(
18-
Effect.catch((error) =>
19-
Effect.logWarning("Failed to read project workspaces for usage", { error }).pipe(
20-
Effect.as([] as readonly UsageWorkspaceSnapshot[]),
17+
const snapshots = yield* Effect.all(
18+
loadSnapshots.map((loadSnapshot) =>
19+
loadSnapshot.pipe(
20+
Effect.catch((error) =>
21+
Effect.logWarning("Failed to read project workspaces for usage", { error }).pipe(
22+
Effect.as(null as UsageWorkspaceSnapshot | null),
23+
),
24+
),
2125
),
2226
),
27+
{ concurrency: "unbounded" },
2328
);
2429
const workspaceCwds = new Set([serverCwd]);
2530
for (const snapshot of snapshots) {
31+
if (snapshot === null) continue;
2632
for (const project of snapshot.projects) workspaceCwds.add(project.workspaceRoot);
2733
for (const thread of snapshot.threads) {
2834
if (thread.worktreePath !== null) workspaceCwds.add(thread.worktreePath);

‎apps/server/src/ws.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,16 +1568,10 @@ const makeWsRpcLayer = (
15681568
observeRpcEffect(
15691569
WS_METHODS.serverGetUsageSummary,
15701570
Effect.gen(function* () {
1571-
const workspaceCwds = yield* resolveUsageWorkspaceCwds(
1572-
config.cwd,
1573-
Effect.all(
1574-
[
1575-
projectionSnapshotQuery.getShellSnapshot(),
1576-
projectionSnapshotQuery.getArchivedShellSnapshot(),
1577-
],
1578-
{ concurrency: "unbounded" },
1579-
),
1580-
);
1571+
const workspaceCwds = yield* resolveUsageWorkspaceCwds(config.cwd, [
1572+
projectionSnapshotQuery.getShellSnapshot(),
1573+
projectionSnapshotQuery.getArchivedShellSnapshot(),
1574+
]);
15811575
return yield* usage.readSummary(input, workspaceCwds);
15821576
}),
15831577
{ "rpc.aggregate": "server" },

0 commit comments

Comments
 (0)