Skip to content

Commit 55475b4

Browse files
committed
Detect compaction from message data instead of event
- Scan messages for assistant messages with info.summary === true - Run compaction detection on every checkSession call, not just init - Remove session.compacted event handler (no longer needed) - Remove lastCompacted from persistence (derived from messages now) - Remove unused checkForCompaction function This ensures compaction is detected even when: - Plugin was disabled during compaction - Session was loaded before plugin was installed - Any other case where the event wasn't captured
1 parent 1a2af46 commit 55475b4

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

lib/hooks.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ export function createEventHandler(
4848
return
4949
}
5050

51-
if (event.type === "session.compacted") {
52-
logger.info("Session compaction detected - updating state")
53-
state.lastCompaction = Date.now()
54-
}
55-
5651
if (event.type === "session.status" && event.properties.status.type === "idle") {
5752
if (!config.strategies.onIdle.enabled) {
5853
return

lib/shared-utils.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Logger } from "./logger"
21
import { SessionState, WithParts } from "./state"
32

43
export const isMessageCompacted = (
@@ -20,12 +19,4 @@ export const getLastUserMessage = (
2019
return null
2120
}
2221

23-
export const checkForCompaction = (
24-
state: SessionState,
25-
messages: WithParts[],
26-
logger: Logger
27-
): void => {
28-
for (const msg of messages) {
2922

30-
}
31-
}

lib/state/persistence.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export interface PersistedSessionState {
1616
prune: Prune
1717
stats: SessionStats;
1818
lastUpdated: string;
19-
lastCompacted: number
2019
}
2120

2221
const STORAGE_DIR = join(
@@ -55,8 +54,7 @@ export async function saveSessionState(
5554
sessionName: sessionName,
5655
prune: sessionState.prune,
5756
stats: sessionState.stats,
58-
lastUpdated: new Date().toISOString(),
59-
lastCompacted: sessionState.lastCompaction
57+
lastUpdated: new Date().toISOString()
6058
};
6159

6260
const filePath = getSessionFilePath(sessionState.sessionId);

lib/state/state.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ export const checkSession = async (
2121
if (state.sessionId === null || state.sessionId !== lastSessionId) {
2222
logger.info(`Session changed: ${state.sessionId} -> ${lastSessionId}`)
2323
try {
24-
await ensureSessionInitialized(client, state, lastSessionId, logger)
24+
await ensureSessionInitialized(client, state, lastSessionId, logger, messages)
2525
} catch (err: any) {
2626
logger.error("Failed to initialize session state", { error: err.message })
2727
}
2828
}
29+
30+
// Always check for compaction on every message check
31+
const lastCompactionTimestamp = findLastCompactionTimestamp(messages)
32+
if (lastCompactionTimestamp > state.lastCompaction) {
33+
state.lastCompaction = lastCompactionTimestamp
34+
logger.info("Detected compaction from messages", { timestamp: lastCompactionTimestamp })
35+
}
2936
}
3037

3138
export function createSessionState(): SessionState {
@@ -66,7 +73,8 @@ export async function ensureSessionInitialized(
6673
client: any,
6774
state: SessionState,
6875
sessionId: string,
69-
logger: Logger
76+
logger: Logger,
77+
messages: WithParts[]
7078
): Promise<void> {
7179
if (state.sessionId === sessionId) {
7280
return;
@@ -97,5 +105,17 @@ export async function ensureSessionInitialized(
97105
pruneTokenCounter: persisted.stats?.pruneTokenCounter || 0,
98106
totalPruneTokens: persisted.stats?.totalPruneTokens || 0,
99107
}
100-
state.lastCompaction = persisted.lastCompacted || 0
108+
}
109+
110+
function findLastCompactionTimestamp(messages: WithParts[]): number {
111+
let lastTimestamp = 0
112+
for (const msg of messages) {
113+
if (msg.info.role === "assistant" && msg.info.summary === true) {
114+
const created = msg.info.time.created
115+
if (created > lastTimestamp) {
116+
lastTimestamp = created
117+
}
118+
}
119+
}
120+
return lastTimestamp
101121
}

lib/strategies/prune-tool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ export function createPruneTool(
6666
return "No numeric IDs provided. Format: [reason, id1, id2, ...] where reason is 'completion', 'noise', or 'consolidation'."
6767
}
6868

69-
await ensureSessionInitialized(ctx.client, state, sessionId, logger)
70-
7169
// Fetch messages to calculate tokens and find current agent
7270
const messagesResponse = await client.session.messages({
7371
path: { id: sessionId }
7472
})
7573
const messages: WithParts[] = messagesResponse.data || messagesResponse
7674

75+
await ensureSessionInitialized(ctx.client, state, sessionId, logger, messages)
76+
7777
const currentParams = getCurrentParams(messages, logger)
7878
const toolIdList: string[] = buildToolIdList(state, messages, logger)
7979

0 commit comments

Comments
 (0)