Skip to content

Commit 544a1b5

Browse files
fix: stabilize compression timing correlation
1 parent effb538 commit 544a1b5

7 files changed

Lines changed: 442 additions & 187 deletions

File tree

lib/compress/message.ts

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { finalizeSession, prepareSession, type NotificationEntry } from "./pipel
77
import { appendProtectedTools } from "./protected-content"
88
import {
99
allocateBlockId,
10+
consumeCompressionDuration,
1011
allocateRunId,
1112
applyCompressionState,
1213
wrapCompressedSummary,
@@ -46,92 +47,93 @@ export function createCompressMessageTool(ctx: ToolContext): ReturnType<typeof t
4647
description: runtimePrompts.compressMessage + MESSAGE_FORMAT_EXTENSION,
4748
args: buildSchema(),
4849
async execute(args, toolCtx) {
49-
try {
50-
const input = args as CompressMessageToolArgs
51-
validateArgs(input)
52-
const { rawMessages, searchContext } = await prepareSession(
53-
ctx,
54-
toolCtx,
55-
`Compress Message: ${input.topic}`,
56-
)
57-
const { plans, skippedIssues } = resolveMessages(
58-
input,
59-
searchContext,
60-
ctx.state,
61-
ctx.config,
62-
)
50+
const input = args as CompressMessageToolArgs
51+
validateArgs(input)
52+
const callId =
53+
typeof (toolCtx as unknown as { callID?: unknown }).callID === "string"
54+
? (toolCtx as unknown as { callID: string }).callID
55+
: undefined
56+
const durationMs = consumeCompressionDuration(ctx.state, toolCtx.messageID, callId)
6357

64-
if (plans.length === 0 && skippedIssues.length > 0) {
65-
throw new Error(formatIssues(skippedIssues))
66-
}
58+
const { rawMessages, searchContext } = await prepareSession(
59+
ctx,
60+
toolCtx,
61+
`Compress Message: ${input.topic}`,
62+
)
63+
const { plans, skippedIssues } = resolveMessages(
64+
input,
65+
searchContext,
66+
ctx.state,
67+
ctx.config,
68+
)
6769

68-
const notifications: NotificationEntry[] = []
70+
if (plans.length === 0 && skippedIssues.length > 0) {
71+
throw new Error(formatIssues(skippedIssues))
72+
}
6973

70-
const preparedPlans: Array<{
71-
plan: (typeof plans)[number]
72-
summaryWithTools: string
73-
}> = []
74+
const notifications: NotificationEntry[] = []
7475

75-
for (const plan of plans) {
76-
const summaryWithTools = await appendProtectedTools(
77-
ctx.client,
78-
ctx.state,
79-
ctx.config.experimental.allowSubAgents,
80-
plan.entry.summary,
81-
plan.selection,
82-
searchContext,
83-
ctx.config.compress.protectedTools,
84-
ctx.config.protectedFilePatterns,
85-
)
76+
const preparedPlans: Array<{
77+
plan: (typeof plans)[number]
78+
summaryWithTools: string
79+
}> = []
8680

87-
preparedPlans.push({
88-
plan,
89-
summaryWithTools,
90-
})
91-
}
81+
for (const plan of plans) {
82+
const summaryWithTools = await appendProtectedTools(
83+
ctx.client,
84+
ctx.state,
85+
ctx.config.experimental.allowSubAgents,
86+
plan.entry.summary,
87+
plan.selection,
88+
searchContext,
89+
ctx.config.compress.protectedTools,
90+
ctx.config.protectedFilePatterns,
91+
)
9292

93-
const runId = allocateRunId(ctx.state)
94-
const durationMs = ctx.state.compressionDurations.get(toolCtx.messageID) || 0
93+
preparedPlans.push({
94+
plan,
95+
summaryWithTools,
96+
})
97+
}
9598

96-
for (const { plan, summaryWithTools } of preparedPlans) {
97-
const blockId = allocateBlockId(ctx.state)
98-
const storedSummary = wrapCompressedSummary(blockId, summaryWithTools)
99-
const summaryTokens = countTokens(storedSummary)
99+
const runId = allocateRunId(ctx.state)
100100

101-
applyCompressionState(
102-
ctx.state,
103-
{
104-
topic: plan.entry.topic,
105-
batchTopic: input.topic,
106-
startId: plan.entry.messageId,
107-
endId: plan.entry.messageId,
108-
mode: "message",
109-
runId,
110-
compressMessageId: toolCtx.messageID,
111-
summaryTokens,
112-
durationMs,
113-
},
114-
plan.selection,
115-
plan.anchorMessageId,
116-
blockId,
117-
storedSummary,
118-
[],
119-
)
101+
for (const { plan, summaryWithTools } of preparedPlans) {
102+
const blockId = allocateBlockId(ctx.state)
103+
const storedSummary = wrapCompressedSummary(blockId, summaryWithTools)
104+
const summaryTokens = countTokens(storedSummary)
120105

121-
notifications.push({
122-
blockId,
106+
applyCompressionState(
107+
ctx.state,
108+
{
109+
topic: plan.entry.topic,
110+
batchTopic: input.topic,
111+
startId: plan.entry.messageId,
112+
endId: plan.entry.messageId,
113+
mode: "message",
123114
runId,
124-
summary: summaryWithTools,
115+
compressMessageId: toolCtx.messageID,
125116
summaryTokens,
126-
})
127-
}
128-
129-
await finalizeSession(ctx, toolCtx, rawMessages, notifications, input.topic)
117+
durationMs,
118+
},
119+
plan.selection,
120+
plan.anchorMessageId,
121+
blockId,
122+
storedSummary,
123+
[],
124+
)
130125

131-
return formatResult(plans.length, skippedIssues)
132-
} finally {
133-
ctx.state.compressionDurations.delete(toolCtx.messageID)
126+
notifications.push({
127+
blockId,
128+
runId,
129+
summary: summaryWithTools,
130+
summaryTokens,
131+
})
134132
}
133+
134+
await finalizeSession(ctx, toolCtx, rawMessages, notifications, input.topic)
135+
136+
return formatResult(plans.length, skippedIssues)
135137
},
136138
})
137139
}

0 commit comments

Comments
 (0)