11import { z } from "zod"
22import type { Logger } from "./logger"
33import type { PruningStrategy } from "./config"
4+ import type { PluginState } from "./state"
45import { buildAnalysisPrompt } from "./prompt"
56import { selectModel , extractModelFromSession } from "./model-selector"
67import { estimateTokensBatch , formatTokenCount } from "./tokenizer"
78import { detectDuplicates } from "./deduplicator"
89import { extractParameterKey } from "./display-utils"
910import { saveSessionState } from "./state-persistence"
11+ import { ensureSessionRestored } from "./state"
1012
1113export interface SessionStats {
1214 totalToolsPruned : number
@@ -30,20 +32,28 @@ export interface PruningOptions {
3032}
3133
3234export class Janitor {
35+ private prunedIdsState : Map < string , string [ ] >
36+ private statsState : Map < string , SessionStats >
37+ private toolParametersCache : Map < string , any >
38+ private modelCache : Map < string , { providerID : string ; modelID : string } >
39+
3340 constructor (
3441 private client : any ,
35- private prunedIdsState : Map < string , string [ ] > ,
36- private statsState : Map < string , SessionStats > ,
42+ private state : PluginState ,
3743 private logger : Logger ,
38- private toolParametersCache : Map < string , any > ,
3944 private protectedTools : string [ ] ,
40- private modelCache : Map < string , { providerID : string ; modelID : string } > ,
4145 private configModel ?: string ,
4246 private showModelErrorToasts : boolean = true ,
4347 private strictModelSelection : boolean = false ,
4448 private pruningSummary : "off" | "minimal" | "detailed" = "detailed" ,
4549 private workingDirectory ?: string
46- ) { }
50+ ) {
51+ // Bind state references for convenience
52+ this . prunedIdsState = state . prunedIds
53+ this . statsState = state . stats
54+ this . toolParametersCache = state . toolParameters
55+ this . modelCache = state . model
56+ }
4757
4858 private async sendIgnoredMessage ( sessionID : string , text : string , agent ?: string ) {
4959 try {
@@ -86,6 +96,9 @@ export class Janitor {
8696 return null
8797 }
8898
99+ // Ensure persisted state is restored before processing
100+ await ensureSessionRestored ( this . state , sessionID , this . logger )
101+
89102 const [ sessionInfoResponse , messagesResponse ] = await Promise . all ( [
90103 this . client . session . get ( { path : { id : sessionID } } ) ,
91104 this . client . session . messages ( { path : { id : sessionID } , query : { limit : 100 } } )
0 commit comments