@@ -15,6 +15,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
1515import { mergeUsage } from "@t3tools/shared/usageMerge" ;
1616import {
1717 EnvironmentId ,
18+ ProjectId ,
1819 ProviderDriverKind ,
1920 ProviderInstanceId ,
2021 UsageDay ,
@@ -42,6 +43,7 @@ import * as ServerConfig from "../config.ts";
4243import * as ServerSettings from "../serverSettings.ts" ;
4344import * as Sqlite from "../persistence/Sqlite.ts" ;
4445import * as CodexUsageHistory from "./CodexUsageHistory.ts" ;
46+ import * as UsageProjectAttribution from "./UsageProjectAttribution.ts" ;
4547import * as CursorAccountReader from "@t3tools/provider-cursor/server/CursorAccountReader" ;
4648import * as CursorKeychain from "@t3tools/provider-cursor/server/CursorKeychain" ;
4749import * as CursorUsageAccounts from "@t3tools/provider-cursor/server/CursorUsageAccounts" ;
@@ -159,7 +161,11 @@ const layerService = (input: {
159161 Layer . provideMerge ( AntigravityUsage . layer ) ,
160162 Layer . provideMerge ( MuseUsageHome . layer ) ,
161163 Layer . provideMerge ( ProviderHostLive . layer ) ,
162- Layer . provideMerge ( CodexUsageHistory . layer . pipe ( Layer . provideMerge ( Sqlite . layerMemory ) ) ) ,
164+ Layer . provideMerge (
165+ Layer . merge ( CodexUsageHistory . layer , UsageProjectAttribution . layer ) . pipe (
166+ Layer . provideMerge ( Sqlite . layerMemory ) ,
167+ ) ,
168+ ) ,
163169 Layer . provideMerge ( Layer . mock ( BackgroundPolicy . BackgroundPolicy ) ( { } ) ) ,
164170 Layer . provideMerge (
165171 Layer . mock ( ServerSecretStore . ServerSecretStore ) ( {
@@ -2402,6 +2408,80 @@ describe("UsageService", () => {
24022408 } ) . pipe ( Effect . scoped ) ,
24032409 ) ;
24042410
2411+ it . live ( "totals usage per T3 project from provider thread bindings" , ( ) =>
2412+ Effect . gen ( function * ( ) {
2413+ const { transcript, settings, home } = yield * setup ;
2414+ yield * Effect . promise ( ( ) =>
2415+ NodeFSP . writeFile (
2416+ transcript ,
2417+ claudeLine ( 1 , 3 , "claude-fable-5" , "bound" ) +
2418+ claudeLine ( 2 , 5 , "claude-fable-5" , "worktree" ) +
2419+ claudeLine ( 3 , 7 , "claude-fable-5" , "ambiguous" ) +
2420+ claudeLine ( 4 , 11 , "claude-fable-5" , "terminal" ) +
2421+ claudeLine ( 5 , 13 , "claude-fable-5" , "deleted-project" ) ,
2422+ ) ,
2423+ ) ;
2424+ const projectRows = ( summary : UsageSummary ) =>
2425+ ( summary . projectTotals ?? [ ] )
2426+ . map ( ( row ) => [ String ( row . projectId ?? null ) , row . totals . outputTokens , row . sessions ] )
2427+ . toSorted ( ( [ a ] , [ b ] ) => String ( a ) . localeCompare ( String ( b ) ) ) ;
2428+
2429+ yield * Effect . gen ( function * ( ) {
2430+ const sql = yield * SqlClient . SqlClient ;
2431+ const project = ( id : string , title : string , deletedAt : string | null = null ) =>
2432+ sql `INSERT INTO projection_projects (project_id, title, workspace_root, scripts_json, created_at, updated_at, deleted_at)
2433+ VALUES (${ id } , ${ title } , ${ `/work/${ id } ` } , '[]', '2026-08-01', '2026-08-01', ${ deletedAt } )` ;
2434+ const thread = ( id : string , projectId : string ) =>
2435+ sql `INSERT INTO orchestration_v2_projection_threads (thread_id, project_id, title, default_provider, runtime_mode, interaction_mode, created_at, updated_at, payload_json)
2436+ VALUES (${ id } , ${ projectId } , ${ id } , 'claudeAgent', 'full-access', 'default', '2026-08-01', '2026-08-01', '{}')` ;
2437+ const binding = ( id : string , threadId : string , nativeId : string , driver = "claudeAgent" ) =>
2438+ sql `INSERT INTO orchestration_v2_projection_provider_threads (provider_thread_id, thread_id, provider, driver, status, updated_at, payload_json)
2439+ VALUES (${ id } , ${ threadId } , ${ driver } , ${ driver } , 'idle', '2026-08-01', ${ encodeUnknownJsonString (
2440+ { nativeThreadRef : { driver, nativeId } } ,
2441+ ) } )`;
2442+ yield * project ( "p-app" , "App" ) ;
2443+ yield * project ( "p-other" , "Other" ) ;
2444+ yield * project ( "p-gone" , "Gone" , "2026-08-02" ) ;
2445+ yield * thread ( "t-main" , "p-app" ) ;
2446+ yield * thread ( "t-worktree" , "p-app" ) ;
2447+ yield * thread ( "t-other" , "p-other" ) ;
2448+ yield * thread ( "t-gone" , "p-gone" ) ;
2449+ yield * binding ( "pt-1" , "t-main" , "bound" ) ;
2450+ yield * binding ( "pt-2" , "t-worktree" , "worktree" ) ;
2451+ yield * binding ( "pt-3" , "t-main" , "ambiguous" ) ;
2452+ yield * binding ( "pt-4" , "t-other" , "ambiguous" ) ;
2453+ yield * binding ( "pt-5" , "t-gone" , "deleted-project" ) ;
2454+ // Another driver's identical native id must not claim a Claude session.
2455+ yield * binding ( "pt-6" , "t-other" , "terminal" , "codex" ) ;
2456+
2457+ const service = yield * UsageService . make ;
2458+ const summary = yield * service . readSummary ( WINDOW ) ;
2459+ assert . deepStrictEqual ( projectRows ( summary ) , [
2460+ [ "null" , 18 , 2 ] ,
2461+ [ "p-app" , 8 , 2 ] ,
2462+ [ "p-gone" , 13 , 1 ] ,
2463+ ] ) ;
2464+ assert . deepStrictEqual (
2465+ summary . projects ?. toSorted ( ( a , b ) => a . projectId . localeCompare ( b . projectId ) ) ,
2466+ [
2467+ { projectId : ProjectId . make ( "p-app" ) , title : "App" } ,
2468+ { projectId : ProjectId . make ( "p-gone" ) , title : "Gone" } ,
2469+ ] ,
2470+ ) ;
2471+ assert . strictEqual ( totalOutputTokens ( summary ) , 39 ) ;
2472+
2473+ yield * sql `DROP TABLE orchestration_v2_projection_provider_threads` ;
2474+ const unattributed = yield * service . readSummary ( { ...WINDOW , awaitRefresh : true } ) ;
2475+ assert . strictEqual ( unattributed . projectTotals , undefined ) ;
2476+ assert . strictEqual ( unattributed . projects , undefined ) ;
2477+ assert . strictEqual ( totalOutputTokens ( unattributed ) , 39 ) ;
2478+ } ) . pipe (
2479+ Effect . scoped ,
2480+ Effect . provide ( layerService ( { prefix : "usage-service-project-test" , home, settings } ) ) ,
2481+ ) ;
2482+ } ) . pipe ( Effect . scoped ) ,
2483+ ) ;
2484+
24052485 it . live ( "reprices unchanged transcripts when custom prices are added, edited, or removed" , ( ) =>
24062486 Effect . gen ( function * ( ) {
24072487 const { transcript, settings, home } = yield * setup ;
0 commit comments