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