@@ -23,6 +23,7 @@ vi.mock("../session-utils.js", async () => {
2323 const actual = await vi . importActual < typeof import ( "../session-utils.js" ) > ( "../session-utils.js" ) ;
2424 return {
2525 ...actual ,
26+ loadSessionEntryReadOnly : vi . fn ( actual . loadSessionEntryReadOnly ) ,
2627 loadCombinedSessionStoreForGateway : vi . fn ( ( ) => ( { storePath : "(multiple)" , store : { } } ) ) ,
2728 } ;
2829} ) ;
@@ -33,6 +34,7 @@ vi.mock("../../infra/session-cost-usage.js", async () => {
3334 ) ;
3435 return {
3536 ...actual ,
37+ resolveExistingUsageSessionFile : vi . fn ( actual . resolveExistingUsageSessionFile ) ,
3638 discoverAllSessions : vi . fn ( async ( params ?: { agentId ?: string } ) => {
3739 if ( params ?. agentId === "main" ) {
3840 return [
@@ -100,8 +102,9 @@ import {
100102 loadSessionCostSummariesFromCache ,
101103 loadSessionLogs ,
102104 loadSessionUsageTimeSeries ,
105+ resolveExistingUsageSessionFile ,
103106} from "../../infra/session-cost-usage.js" ;
104- import { loadCombinedSessionStoreForGateway } from "../session-utils.js" ;
107+ import { loadCombinedSessionStoreForGateway , loadSessionEntryReadOnly } from "../session-utils.js" ;
105108import { testApi , usageHandlers } from "./usage.js" ;
106109
107110const TEST_RUNTIME_CONFIG = {
@@ -182,6 +185,20 @@ function expectSuccessfulSessionsUsage(
182185 return result . sessions ;
183186}
184187
188+ function mockStoredSession ( key : string , sessionId : string ) {
189+ const entry = { sessionId, updatedAt : 1_000 } ;
190+ vi . mocked ( loadSessionEntryReadOnly ) . mockReturnValueOnce ( {
191+ cfg : TEST_RUNTIME_CONFIG ,
192+ canonicalKey : key ,
193+ entry,
194+ legacyKey : undefined ,
195+ store : { [ key ] : entry } ,
196+ storeKeys : [ key ] ,
197+ storePath : "/tmp/agents/opus/sessions/sessions.json" ,
198+ } ) ;
199+ return entry ;
200+ }
201+
185202async function withUsageState (
186203 run : ( writeSessionFile : ( fileName : string ) => string ) => Promise < void > ,
187204) {
@@ -504,7 +521,8 @@ describe("sessions.usage", () => {
504521
505522 it ( "uses the requested agent for legacy specific session keys" , async ( ) => {
506523 await withUsageState ( async ( writeSessionFile ) => {
507- const sessionFile = writeSessionFile ( "main.jsonl" ) ;
524+ writeSessionFile ( "main.jsonl" ) ;
525+ mockStoredSession ( "agent:opus:main" , "main" ) ;
508526
509527 vi . mocked ( loadCombinedSessionStoreForGateway ) . mockReturnValue ( {
510528 storePath : "(multiple)" ,
@@ -532,7 +550,7 @@ describe("sessions.usage", () => {
532550 agentId : "opus" ,
533551 sessions : expect . arrayContaining ( [
534552 expect . objectContaining ( {
535- sessionFile : fs . realpathSync ( sessionFile ) ,
553+ sessionFile : expect . stringMatching ( / ^ s q l i t e : / ) ,
536554 sessionId : "main" ,
537555 } ) ,
538556 ] ) ,
@@ -550,7 +568,8 @@ describe("sessions.usage", () => {
550568 } ;
551569
552570 await withUsageState ( async ( writeSessionFile ) => {
553- const sessionFile = writeSessionFile ( "current.jsonl" ) ;
571+ writeSessionFile ( "current.jsonl" ) ;
572+ mockStoredSession ( "global" , "current" ) ;
554573
555574 const sessionEntry = {
556575 sessionId : "current" ,
@@ -583,7 +602,7 @@ describe("sessions.usage", () => {
583602 agentId : "opus" ,
584603 sessions : expect . arrayContaining ( [
585604 expect . objectContaining ( {
586- sessionFile : fs . realpathSync ( sessionFile ) ,
605+ sessionFile : expect . stringMatching ( / ^ s q l i t e : / ) ,
587606 sessionId : "current" ,
588607 } ) ,
589608 ] ) ,
@@ -637,6 +656,7 @@ describe("sessions.usage", () => {
637656
638657 await withUsageState ( async ( writeSessionFile ) => {
639658 writeSessionFile ( "s-opus.jsonl" ) ;
659+ mockStoredSession ( storeKey , "s-opus" ) ;
640660
641661 // Swap the store mock for this test: the canonical key differs from the discovered key
642662 // but points at the same sessionId.
@@ -672,6 +692,7 @@ describe("sessions.usage", () => {
672692 await withUsageState ( async ( writeSessionFile ) => {
673693 writeSessionFile ( "current.jsonl" ) ;
674694 writeSessionFile ( "old.jsonl.reset.2026-02-01T00-00-00.000Z" ) ;
695+ mockStoredSession ( storeKey , "current" ) ;
675696
676697 vi . mocked ( loadCombinedSessionStoreForGateway ) . mockReturnValue ( {
677698 storePath : "(multiple)" ,
@@ -750,6 +771,7 @@ describe("sessions.usage", () => {
750771
751772 await withUsageState ( async ( writeSessionFile ) => {
752773 writeSessionFile ( "run-dup.jsonl" ) ;
774+ mockStoredSession ( preferredKey , "run-dup" ) ;
753775
754776 vi . mocked ( loadCombinedSessionStoreForGateway ) . mockReturnValue ( {
755777 storePath : "(multiple)" ,
@@ -774,6 +796,13 @@ describe("sessions.usage", () => {
774796 const sessions = expectSuccessfulSessionsUsage ( respond ) ;
775797 expect ( sessions ) . toHaveLength ( 1 ) ;
776798 expect ( sessions [ 0 ] ?. key ) . toBe ( preferredKey ) ;
799+ expect ( vi . mocked ( loadSessionCostSummariesFromCache ) ) . toHaveBeenCalledWith (
800+ expect . objectContaining ( {
801+ sessions : expect . arrayContaining ( [
802+ expect . objectContaining ( { sessionFile : expect . stringMatching ( / ^ s q l i t e : / ) } ) ,
803+ ] ) ,
804+ } ) ,
805+ ) ;
777806 } ) ;
778807 } ) ;
779808
@@ -789,28 +818,43 @@ describe("sessions.usage", () => {
789818 expect ( error ?. message ) . toContain ( "Invalid session reference" ) ;
790819 } ) ;
791820
792- it ( "passes parsed agentId into sessions.usage.timeseries" , async ( ) => {
793- await runSessionsUsageTimeseries ( {
794- key : "agent:opus:s-opus" ,
795- } ) ;
821+ it ( "passes a canonical SQLite target into sessions.usage.timeseries" , async ( ) => {
822+ mockStoredSession ( "agent:opus:s-opus" , "s-opus" ) ;
823+ await runSessionsUsageTimeseries ( { key : "agent:opus:s-opus" } ) ;
796824
797- expect ( vi . mocked ( loadSessionUsageTimeSeries ) ) . toHaveBeenCalled ( ) ;
798- expect (
799- ( mockArg ( vi . mocked ( loadSessionUsageTimeSeries ) , 0 , 0 ) as { agentId ?: string } ) . agentId ,
800- ) . toBe ( "opus" ) ;
825+ expect ( vi . mocked ( loadSessionUsageTimeSeries ) ) . toHaveBeenCalledWith (
826+ expect . objectContaining ( { agentId : "opus" , sessionFile : expect . stringMatching ( / ^ s q l i t e : / ) } ) ,
827+ ) ;
801828 } ) ;
802829
803- it ( "passes parsed agentId into sessions.usage.logs" , async ( ) => {
804- await runSessionsUsageLogs ( {
805- key : "agent:opus:s-opus" ,
806- } ) ;
830+ it ( "passes a canonical SQLite target into sessions.usage.logs" , async ( ) => {
831+ mockStoredSession ( "agent:opus:s-opus" , "s-opus" ) ;
832+ await runSessionsUsageLogs ( { key : "agent:opus:s-opus" } ) ;
807833
808- expect ( vi . mocked ( loadSessionLogs ) ) . toHaveBeenCalled ( ) ;
809- expect ( ( mockArg ( vi . mocked ( loadSessionLogs ) , 0 , 0 ) as { agentId ?: string } ) . agentId ) . toBe (
810- "opus" ,
834+ expect ( vi . mocked ( loadSessionLogs ) ) . toHaveBeenCalledWith (
835+ expect . objectContaining ( { agentId : "opus" , sessionFile : expect . stringMatching ( / ^ s q l i t e : / ) } ) ,
811836 ) ;
812837 } ) ;
813838
839+ it ( "preserves JSONL detail lookup for storeless sessions" , async ( ) => {
840+ await withUsageState ( async ( writeSessionFile ) => {
841+ const sessionFile = writeSessionFile ( "storeless.jsonl" ) ;
842+ await runSessionsUsageTimeseries ( { key : "agent:opus:storeless" } ) ;
843+ expect ( vi . mocked ( loadSessionUsageTimeSeries ) ) . toHaveBeenCalledWith (
844+ expect . objectContaining ( { sessionFile, sessionEntry : undefined } ) ,
845+ ) ;
846+ } ) ;
847+ } ) ;
848+
849+ it ( "fails closed when a canonical stored target no longer matches" , async ( ) => {
850+ const key = "agent:opus:stale" ;
851+ mockStoredSession ( key , "stale" ) ;
852+ vi . mocked ( resolveExistingUsageSessionFile ) . mockReturnValueOnce ( undefined ) ;
853+ const respond = await runSessionsUsageTimeseries ( { key } ) ;
854+ expect ( mockArg ( respond , 0 , 0 ) ) . toBe ( false ) ;
855+ expect ( vi . mocked ( loadSessionUsageTimeSeries ) ) . not . toHaveBeenCalled ( ) ;
856+ } ) ;
857+
814858 it ( "rejects traversal-style keys in timeseries/log lookups" , async ( ) => {
815859 const timeseriesRespond = await runSessionsUsageTimeseries ( {
816860 key : "agent:opus:../../etc/passwd" ,
0 commit comments