Skip to content

Commit df502b9

Browse files
committed
fix: ensure persisted state is restored before janitor runs
1 parent 687394c commit df502b9

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ const plugin: Plugin = (async (ctx) => {
2828

2929
const janitor = new Janitor(
3030
ctx.client,
31-
state.prunedIds,
32-
state.stats,
31+
state,
3332
logger,
34-
state.toolParameters,
3533
config.protectedTools,
36-
state.model,
3734
config.model,
3835
config.showModelErrorToasts,
3936
config.strictModelSelection,

lib/janitor.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { z } from "zod"
22
import type { Logger } from "./logger"
33
import type { PruningStrategy } from "./config"
4+
import type { PluginState } from "./state"
45
import { buildAnalysisPrompt } from "./prompt"
56
import { selectModel, extractModelFromSession } from "./model-selector"
67
import { estimateTokensBatch, formatTokenCount } from "./tokenizer"
78
import { detectDuplicates } from "./deduplicator"
89
import { extractParameterKey } from "./display-utils"
910
import { saveSessionState } from "./state-persistence"
11+
import { ensureSessionRestored } from "./state"
1012

1113
export interface SessionStats {
1214
totalToolsPruned: number
@@ -30,20 +32,28 @@ export interface PruningOptions {
3032
}
3133

3234
export 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

Comments
 (0)