@@ -799,6 +799,235 @@ describe("UsageService", () => {
799799 } ) . pipe ( Effect . scoped ) ,
800800 ) ;
801801
802+ it . live (
803+ "keeps a persisted predecessor fallback predecessor, then cold re-classifies on recovery" ,
804+ ( ) =>
805+ Effect . gen ( function * ( ) {
806+ const { transcript, settings, home } = yield * setup ;
807+ const secondPath = NodePath . join ( home , "claude" , "projects" , "proj" , "second.jsonl" ) ;
808+ // The target parses under the current parser as `invalid` (a recognised
809+ // field with an invalid value). The predecessor cache stored it as an
810+ // observed/partial zero because numeric quality metadata did not exist
811+ // then; a warm or resumed read would keep that stale classification.
812+ const invalidContent =
813+ encodeUnknownJsonString ( {
814+ type : "assistant" ,
815+ timestamp : "2026-08-01T10:00:00Z" ,
816+ requestId : "req_live" ,
817+ sessionId : "session-1" ,
818+ message : { id : "msg_live" , model : "claude-fable-5" , usage : { input_tokens : null } } ,
819+ } ) + "\n" ;
820+ yield * Effect . promise ( ( ) => NodeFSP . writeFile ( transcript , invalidContent ) ) ;
821+ yield * Effect . promise ( ( ) => NodeFSP . writeFile ( secondPath , claudeLine ( 2 , 7 ) ) ) ;
822+
823+ yield * Effect . gen ( function * ( ) {
824+ const config = yield * ServerConfig . ServerConfig ;
825+ const livePath = yield * Effect . promise ( ( ) => NodeFSP . realpath ( transcript ) ) ;
826+ const liveStat = yield * Effect . promise ( ( ) => NodeFSP . stat ( livePath ) ) ;
827+ const scanCachePath = NodePath . join ( config . stateDir , "usage-scan-cache.json" ) ;
828+ const TS = Date . parse ( "2026-08-01T10:00:00Z" ) ;
829+ // Seed a predecessor v4 entry for the target, matching the file exactly
830+ // so only the stale quality metadata (not size/mtime) forces the cold
831+ // re-parse.
832+ yield * Effect . promise ( ( ) =>
833+ NodeFSP . writeFile (
834+ scanCachePath ,
835+ encodeUnknownJsonString ( {
836+ version : 4 ,
837+ models : [ "claude-fable-5" ] ,
838+ sessions : [ "session-1" ] ,
839+ files : {
840+ [ livePath ] : {
841+ s : liveStat . size ,
842+ m : liveStat . mtimeMs ,
843+ p : "claude" ,
844+ r : [
845+ [
846+ TS ,
847+ 0 ,
848+ 0 ,
849+ 0 ,
850+ 0 ,
851+ 0 ,
852+ 0 ,
853+ 0 ,
854+ "msg_live:" ,
855+ null ,
856+ "req_live" ,
857+ "msg_live" ,
858+ null ,
859+ 0 ,
860+ 0 ,
861+ ] ,
862+ ] ,
863+ t : [ ] ,
864+ o : liveStat . size ,
865+ gl : 0 ,
866+ gh : 0 ,
867+ cs : null ,
868+ } ,
869+ } ,
870+ } ) ,
871+ ) ,
872+ ) ;
873+ // Deterministic failure seam: make the target unreadable, then let a
874+ // second readable file change so the cache is persisted while the
875+ // target's retained fallback row is still the predecessor one.
876+ yield * Effect . promise ( ( ) => NodeFSP . chmod ( livePath , 0o000 ) ) ;
877+
878+ const service = yield * UsageService . make ;
879+ const failed = yield * service . readSummary ( WINDOW ) ;
880+ assert . strictEqual ( totalOutputTokens ( failed ) , 7 ) ;
881+
882+ const afterFail = decodeUnknownJsonString (
883+ yield * Effect . promise ( ( ) => NodeFSP . readFile ( scanCachePath , "utf8" ) ) ,
884+ ) as { files : Record < string , { lq ?: number ; r : unknown [ ] [ ] } > } ;
885+ // The persisted fallback keeps the predecessor provenance, not just the
886+ // 15-field shape the encoder rewrites.
887+ assert . strictEqual ( afterFail . files [ livePath ] ?. lq , 1 ) ;
888+ assert . strictEqual ( afterFail . files [ livePath ] ?. r [ 0 ] ?. [ 13 ] , 0 ) ;
889+
890+ // Restore access without changing size or mtime, then restart.
891+ yield * Effect . promise ( ( ) => NodeFSP . chmod ( livePath , 0o644 ) ) ;
892+ const restarted = yield * UsageService . make ;
893+ const recovered = yield * restarted . readSummary ( WINDOW ) ;
894+ // One full cold parse recovers the corrected all-invalid
895+ // classification and counts it as malformed, not a measured zero.
896+ assert . strictEqual (
897+ recovered . sources . find ( ( source ) => source . fingerprint . provider === "claude" )
898+ ?. malformedRecords ,
899+ 1 ,
900+ ) ;
901+ assert . strictEqual ( totalOutputTokens ( recovered ) , 7 ) ;
902+
903+ const afterRecover = decodeUnknownJsonString (
904+ yield * Effect . promise ( ( ) => NodeFSP . readFile ( scanCachePath , "utf8" ) ) ,
905+ ) as { files : Record < string , { lq ?: number ; r : unknown [ ] [ ] } > } ;
906+ // Freshness is cleared only after the successful re-parse; the row now
907+ // carries the current invalid measurement code (3).
908+ assert . strictEqual ( afterRecover . files [ livePath ] ?. lq , undefined ) ;
909+ assert . strictEqual ( afterRecover . files [ livePath ] ?. r [ 0 ] ?. [ 13 ] , 3 ) ;
910+
911+ // The next unchanged scan is warm: byte-identical cache, stable totals.
912+ const beforeWarm = yield * Effect . promise ( ( ) => NodeFSP . readFile ( scanCachePath , "utf8" ) ) ;
913+ const warm = yield * restarted . readSummary ( WINDOW ) ;
914+ assert . strictEqual ( totalOutputTokens ( warm ) , 7 ) ;
915+ const afterWarm = yield * Effect . promise ( ( ) => NodeFSP . readFile ( scanCachePath , "utf8" ) ) ;
916+ assert . strictEqual ( afterWarm , beforeWarm ) ;
917+ } ) . pipe (
918+ Effect . provide (
919+ serviceLayers ( { prefix : "usage-service-freshness-persist-test" , home, settings } ) ,
920+ ) ,
921+ ) ;
922+ } ) . pipe ( Effect . scoped ) ,
923+ ) ;
924+
925+ it . live ( "does not resume a stale predecessor prefix after a restarted persist" , ( ) =>
926+ Effect . gen ( function * ( ) {
927+ const { transcript, settings, home } = yield * setup ;
928+ const secondPath = NodePath . join ( home , "claude" , "projects" , "proj" , "second.jsonl" ) ;
929+ // The target parses as `invalid` under the current parser; the predecessor
930+ // entry only has a stale zero-total partial row for it.
931+ const invalidContent =
932+ encodeUnknownJsonString ( {
933+ type : "assistant" ,
934+ timestamp : "2026-08-01T10:00:00Z" ,
935+ requestId : "req_live" ,
936+ sessionId : "session-1" ,
937+ message : { id : "msg_live" , model : "claude-fable-5" , usage : { input_tokens : null } } ,
938+ } ) + "\n" ;
939+ yield * Effect . promise ( ( ) => NodeFSP . writeFile ( transcript , invalidContent ) ) ;
940+ yield * Effect . promise ( ( ) => NodeFSP . writeFile ( secondPath , claudeLine ( 2 , 7 ) ) ) ;
941+
942+ yield * Effect . gen ( function * ( ) {
943+ const config = yield * ServerConfig . ServerConfig ;
944+ const livePath = yield * Effect . promise ( ( ) => NodeFSP . realpath ( transcript ) ) ;
945+ const liveStat = yield * Effect . promise ( ( ) => NodeFSP . stat ( livePath ) ) ;
946+ const scanCachePath = NodePath . join ( config . stateDir , "usage-scan-cache.json" ) ;
947+ const TS = Date . parse ( "2026-08-01T10:00:00Z" ) ;
948+ // A predecessor entry whose parse position already sits at the current
949+ // end of the file, matching size/mtime exactly.
950+ yield * Effect . promise ( ( ) =>
951+ NodeFSP . writeFile (
952+ scanCachePath ,
953+ encodeUnknownJsonString ( {
954+ version : 4 ,
955+ models : [ "claude-fable-5" ] ,
956+ sessions : [ "session-1" ] ,
957+ files : {
958+ [ livePath ] : {
959+ s : liveStat . size ,
960+ m : liveStat . mtimeMs ,
961+ p : "claude" ,
962+ r : [
963+ [
964+ TS ,
965+ 0 ,
966+ 0 ,
967+ 0 ,
968+ 0 ,
969+ 0 ,
970+ 0 ,
971+ 0 ,
972+ "msg_live:" ,
973+ null ,
974+ "req_live" ,
975+ "msg_live" ,
976+ null ,
977+ 0 ,
978+ 0 ,
979+ ] ,
980+ ] ,
981+ t : [ ] ,
982+ o : liveStat . size ,
983+ gl : 0 ,
984+ gh : 0 ,
985+ cs : null ,
986+ } ,
987+ } ,
988+ } ) ,
989+ ) ,
990+ ) ;
991+ // Unreadable target plus a second changed file forces a persist of the
992+ // retained fallback while the target entry is still predecessor.
993+ yield * Effect . promise ( ( ) => NodeFSP . chmod ( livePath , 0o000 ) ) ;
994+ const service = yield * UsageService . make ;
995+ const failed = yield * service . readSummary ( WINDOW ) ;
996+ assert . strictEqual ( totalOutputTokens ( failed ) , 7 ) ;
997+ const fallback = decodeUnknownJsonString (
998+ yield * Effect . promise ( ( ) => NodeFSP . readFile ( scanCachePath , "utf8" ) ) ,
999+ ) as { files : Record < string , { lq ?: number } > } ;
1000+ assert . strictEqual ( fallback . files [ livePath ] ?. lq , 1 ) ;
1001+
1002+ // Restore access and append a readable line so the file is strictly
1003+ // larger. A stale-but-declared entry would resume from the cached end
1004+ // offset and never re-see the invalid prefix; the persisted
1005+ // predecessor marker must force a full re-parse instead.
1006+ yield * Effect . promise ( ( ) => NodeFSP . chmod ( livePath , 0o644 ) ) ;
1007+ yield * Effect . promise ( ( ) => NodeFSP . appendFile ( livePath , claudeLine ( 3 , 5 ) ) ) ;
1008+ const restarted = yield * UsageService . make ;
1009+ const recovered = yield * restarted . readSummary ( WINDOW ) ;
1010+ // The invalid prefix is reclassified (not retained as a measured zero)
1011+ // and the appended line is counted: 7 + 5 = 12.
1012+ assert . strictEqual (
1013+ recovered . sources . find ( ( source ) => source . fingerprint . provider === "claude" )
1014+ ?. malformedRecords ,
1015+ 1 ,
1016+ ) ;
1017+ assert . strictEqual ( totalOutputTokens ( recovered ) , 12 ) ;
1018+ const after = decodeUnknownJsonString (
1019+ yield * Effect . promise ( ( ) => NodeFSP . readFile ( scanCachePath , "utf8" ) ) ,
1020+ ) as { files : Record < string , { lq ?: number ; r : unknown [ ] [ ] } > } ;
1021+ assert . strictEqual ( after . files [ livePath ] ?. lq , undefined ) ;
1022+ assert . strictEqual ( after . files [ livePath ] ?. r . length , 2 ) ;
1023+ } ) . pipe (
1024+ Effect . provide (
1025+ serviceLayers ( { prefix : "usage-service-stale-prefix-test" , home, settings } ) ,
1026+ ) ,
1027+ ) ;
1028+ } ) . pipe ( Effect . scoped ) ,
1029+ ) ;
1030+
8021031 it . live ( "counts Codex and Grok invalid events as malformed while keeping valid tokens" , ( ) =>
8031032 Effect . gen ( function * ( ) {
8041033 const { settings, home } = yield * setup ;
0 commit comments