|
1 | 1 | import { |
2 | 2 | type ApplicationProjectEvent, |
3 | 3 | IsoDateTime, |
4 | | - type UsageProject, |
5 | 4 | ModelSelection, |
6 | 5 | type OrchestrationProjectShell, |
7 | 6 | StoredProjectIcon, |
@@ -56,16 +55,14 @@ const ProjectDbRow = Schema.Struct({ |
56 | 55 | scripts: Schema.fromJsonString(Schema.Array(ProjectScript)), |
57 | 56 | }); |
58 | 57 |
|
59 | | -const UsageProjectAttributionRow = Schema.Struct({ |
| 58 | +const ProviderSessionProjectRow = Schema.Struct({ |
60 | 59 | driver: Schema.String, |
61 | 60 | nativeId: Schema.String, |
62 | 61 | projectId: ProjectId, |
63 | 62 | title: Schema.String, |
64 | 63 | workspaceRoot: Schema.String, |
65 | 64 | }); |
66 | | - |
67 | | -export const usageProjectSessionKey = (driver: string, nativeId: string) => |
68 | | - `${driver}\u0000${nativeId}`; |
| 65 | +export type ProviderSessionProjectRow = typeof ProviderSessionProjectRow.Type; |
69 | 66 |
|
70 | 67 | function toShell(row: ProjectRow): OrchestrationProjectShell { |
71 | 68 | return { |
@@ -99,9 +96,9 @@ export class ProjectStoreV2 extends Context.Service< |
99 | 96 | readonly projectIds?: ReadonlyArray<ProjectId>; |
100 | 97 | readonly includeDeleted?: boolean; |
101 | 98 | }) => Effect.Effect<ReadonlyArray<ProjectRow>, ProjectStoreV2Error>; |
102 | | - /** Keyed by `usageProjectSessionKey`; deleted projects keep their past usage. */ |
103 | | - readonly listUsageProjectAttributions: () => Effect.Effect< |
104 | | - ReadonlyMap<string, UsageProject>, |
| 99 | + /** One row per provider session a thread ran, with that thread's project, deleted or not. */ |
| 100 | + readonly listProviderSessionProjects: () => Effect.Effect< |
| 101 | + ReadonlyArray<ProviderSessionProjectRow>, |
105 | 102 | ProjectStoreV2Error |
106 | 103 | >; |
107 | 104 | /** Workspace roots match by exact string; callers normalize before asking. */ |
@@ -159,9 +156,10 @@ export const make = Effect.gen(function* () { |
159 | 156 | `, |
160 | 157 | }); |
161 | 158 |
|
162 | | - const selectUsageProjectAttributionRows = SqlSchema.findAll({ |
| 159 | + // Worktree threads keep their project's id, so their sessions land on the project. |
| 160 | + const selectProviderSessionProjects = SqlSchema.findAll({ |
163 | 161 | Request: Schema.Void, |
164 | | - Result: UsageProjectAttributionRow, |
| 162 | + Result: ProviderSessionProjectRow, |
165 | 163 | execute: () => sql` |
166 | 164 | SELECT |
167 | 165 | provider_thread.driver AS "driver", |
@@ -252,32 +250,11 @@ export const make = Effect.gen(function* () { |
252 | 250 | includeDeleted: options?.includeDeleted === true, |
253 | 251 | }).pipe(NodeSqliteClient.readOnly, mapError("list")); |
254 | 252 |
|
255 | | - const listUsageProjectAttributions: ProjectStoreV2["Service"]["listUsageProjectAttributions"] = |
| 253 | + const listProviderSessionProjects: ProjectStoreV2["Service"]["listProviderSessionProjects"] = |
256 | 254 | () => |
257 | | - selectUsageProjectAttributionRows(undefined).pipe( |
| 255 | + selectProviderSessionProjects(undefined).pipe( |
258 | 256 | NodeSqliteClient.readOnly, |
259 | | - Effect.map((rows) => { |
260 | | - const projects = new Map<string, UsageProject | null>(); |
261 | | - for (const row of rows) { |
262 | | - const key = usageProjectSessionKey(row.driver, row.nativeId); |
263 | | - const existing = projects.get(key); |
264 | | - if (existing !== undefined) { |
265 | | - if (existing?.projectId !== row.projectId) projects.set(key, null); |
266 | | - continue; |
267 | | - } |
268 | | - const title = |
269 | | - row.title.trim() || |
270 | | - row.workspaceRoot.split(/[\\/]/).findLast((part) => part.length > 0) || |
271 | | - "Untitled project"; |
272 | | - projects.set(key, { projectId: row.projectId, title }); |
273 | | - } |
274 | | - const resolved = new Map<string, UsageProject>(); |
275 | | - for (const [key, project] of projects) { |
276 | | - if (project !== null) resolved.set(key, project); |
277 | | - } |
278 | | - return resolved; |
279 | | - }), |
280 | | - mapError("listUsageProjectAttributions"), |
| 257 | + mapError("listProviderSessionProjects"), |
281 | 258 | ); |
282 | 259 |
|
283 | 260 | const findActiveByWorkspaceRoot: ProjectStoreV2["Service"]["findActiveByWorkspaceRoot"] = ( |
@@ -346,7 +323,7 @@ export const make = Effect.gen(function* () { |
346 | 323 | apply, |
347 | 324 | get, |
348 | 325 | list, |
349 | | - listUsageProjectAttributions, |
| 326 | + listProviderSessionProjects, |
350 | 327 | findActiveByWorkspaceRoot, |
351 | 328 | getShell: Effect.fn("ProjectStoreV2.getShell")(function (projectId) { |
352 | 329 | return get(projectId).pipe(Effect.map(Option.map(toShell))); |
|
0 commit comments