Skip to content

Commit abd8a46

Browse files
authored
improve: reduce hot-path linear scans and redundant I/O (#95697)
Merged via squash. Prepared head SHA: 67f2678 Co-authored-by: vincentkoc <[email protected]> Co-authored-by: vincentkoc <[email protected]> Reviewed-by: @vincentkoc
1 parent ce391dc commit abd8a46

12 files changed

Lines changed: 355 additions & 52 deletions

‎extensions/memory-core/src/short-term-promotion.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import {
2020
applyShortTermPromotions,
2121
auditShortTermPromotionArtifacts,
22+
filterLiveShortTermRecallEntries,
2223
isShortTermMemoryPath,
2324
loadShortTermPromotionDreamingStats,
2425
recordGroundedShortTermCandidates,
@@ -171,6 +172,42 @@ describe("short-term promotion", () => {
171172
});
172173
});
173174

175+
it("deduplicates source-file checks within a recall batch", async () => {
176+
await withTempWorkspace(async (workspaceDir) => {
177+
const notePath = await writeDailyMemoryNote(workspaceDir, "2026-04-03", [
178+
"Deduplicated source check note.",
179+
]);
180+
const relativePath = path.relative(workspaceDir, notePath).replaceAll("\\", "/");
181+
const entry = {
182+
key: "duplicate-source",
183+
path: relativePath,
184+
startLine: 1,
185+
endLine: 1,
186+
source: "memory" as const,
187+
snippet: "Deduplicated source check note.",
188+
recallCount: 1,
189+
dailyCount: 1,
190+
groundedCount: 0,
191+
totalScore: 0.9,
192+
maxScore: 0.9,
193+
firstRecalledAt: "2026-04-03T00:00:00.000Z",
194+
lastRecalledAt: "2026-04-03T00:00:00.000Z",
195+
queryHashes: ["query"],
196+
recallDays: ["2026-04-03"],
197+
conceptTags: [],
198+
};
199+
const statSpy = vi.spyOn(fs, "stat");
200+
201+
const live = await filterLiveShortTermRecallEntries({
202+
workspaceDir,
203+
entries: [entry, { ...entry, key: "duplicate-source-2" }],
204+
});
205+
206+
expect(live).toHaveLength(2);
207+
expect(statSpy).toHaveBeenCalledTimes(1);
208+
});
209+
});
210+
174211
it("falls back when the injected recall timestamp is outside Date range", async () => {
175212
vi.spyOn(Date, "now").mockReturnValue(Date.UTC(2026, 4, 30, 12, 0, 0));
176213
await withTempWorkspace(async (workspaceDir) => {

‎extensions/memory-core/src/short-term-promotion.ts‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,39 +1302,47 @@ export async function loadShortTermPromotionDreamingStats(params: {
13021302
};
13031303
}
13041304

1305-
async function shortTermRecallSourceExists(params: {
1306-
workspaceDir: string;
1307-
entry: Pick<ShortTermRecallEntry, "path">;
1308-
}): Promise<boolean> {
1309-
const workspaceDir = params.workspaceDir.trim();
1310-
if (!workspaceDir) {
1311-
return false;
1312-
}
1313-
for (const sourcePath of resolveShortTermSourcePathCandidates(workspaceDir, params.entry.path)) {
1314-
try {
1315-
const stat = await fs.stat(sourcePath);
1316-
if (stat.isFile()) {
1317-
return true;
1318-
}
1319-
} catch (err) {
1320-
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
1321-
continue;
1322-
}
1323-
throw err;
1305+
async function shortTermRecallSourceIsFile(sourcePath: string): Promise<boolean> {
1306+
try {
1307+
const stat = await fs.stat(sourcePath);
1308+
return stat.isFile();
1309+
} catch (err) {
1310+
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
1311+
return false;
13241312
}
1313+
throw err;
13251314
}
1326-
return false;
13271315
}
13281316

13291317
export async function filterLiveShortTermRecallEntries(params: {
13301318
workspaceDir: string;
13311319
entries: ShortTermRecallEntry[];
13321320
}): Promise<ShortTermRecallEntry[]> {
1321+
const workspaceDir = params.workspaceDir.trim();
1322+
if (!workspaceDir) {
1323+
return [];
1324+
}
1325+
const sourceFileChecks = new Map<string, Promise<boolean>>();
1326+
const checkSourceFile = (sourcePath: string): Promise<boolean> => {
1327+
const existing = sourceFileChecks.get(sourcePath);
1328+
if (existing) {
1329+
return existing;
1330+
}
1331+
const check = shortTermRecallSourceIsFile(sourcePath);
1332+
sourceFileChecks.set(sourcePath, check);
1333+
return check;
1334+
};
13331335
const results = await Promise.all(
1334-
params.entries.map(async (entry) => ({
1335-
entry,
1336-
exists: await shortTermRecallSourceExists({ workspaceDir: params.workspaceDir, entry }),
1337-
})),
1336+
params.entries.map(async (entry) => {
1337+
let exists = false;
1338+
for (const sourcePath of resolveShortTermSourcePathCandidates(workspaceDir, entry.path)) {
1339+
if (await checkSourceFile(sourcePath)) {
1340+
exists = true;
1341+
break;
1342+
}
1343+
}
1344+
return { entry, exists };
1345+
}),
13381346
);
13391347
return results.filter((result) => result.exists).map((result) => result.entry);
13401348
}

‎packages/markdown-core/src/render-aware-chunking.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ describe("renderMarkdownIRChunksWithinLimit", () => {
107107
expect(chunks.map((chunk) => chunk.source.text)).toEqual(["A", "😀"]);
108108
});
109109

110+
it("keeps split order while processing the worklist as a stack", () => {
111+
const text = "abcdefghijklmnopqrstuvwx";
112+
const chunks = renderMarkdownIRChunksWithinLimit({
113+
ir: markdownToIR(text),
114+
limit: 5,
115+
renderChunk: (chunk) => chunk.text,
116+
measureRendered: (rendered) => rendered.length,
117+
});
118+
119+
expect(chunks.map((chunk) => chunk.source.text).join("")).toBe(text);
120+
expect(chunks.every((chunk) => chunk.rendered.length <= 5)).toBe(true);
121+
});
122+
110123
it("treats Infinity as no size cap and returns a single chunk", () => {
111124
const text = "one two three four five six seven eight nine ten";
112125
const ir = markdownToIR(text);

‎packages/markdown-core/src/render-aware-chunking.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ export function renderMarkdownIRChunksWithinLimit<TRendered>(
5656
}
5757

5858
const normalizedLimit = resolveIntegerOption(options.limit, 1, { min: 1 });
59-
const pending = chunkMarkdownIR(options.ir, normalizedLimit);
59+
// Treat the pending worklist as a stack so each dequeue/enqueue stays O(1).
60+
// The initial reverse keeps the final order stable while avoiding shift/unshift
61+
// moving every remaining chunk for long messages.
62+
const pending = chunkMarkdownIR(options.ir, normalizedLimit).toReversed();
6063
const finalized: MarkdownIR[] = [];
6164

6265
while (pending.length > 0) {
63-
const chunk = pending.shift();
66+
const chunk = pending.pop();
6467
if (!chunk) {
6568
continue;
6669
}
@@ -77,7 +80,12 @@ export function renderMarkdownIRChunksWithinLimit<TRendered>(
7780
finalized.push(chunk);
7881
continue;
7982
}
80-
pending.unshift(...split);
83+
for (let index = split.length - 1; index >= 0; index -= 1) {
84+
const next = split[index];
85+
if (next) {
86+
pending.push(next);
87+
}
88+
}
8189
}
8290

8391
return coalesceWhitespaceOnlyMarkdownIRChunks(finalized, normalizedLimit, options).map(

‎src/agents/openai-transport-stream.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2854,6 +2854,7 @@ async function processOpenAICompletionsStream(
28542854
const toolCallBlocksByIndex = new Map<number, ToolCallBlock>();
28552855
const toolCallBlocksById = new Map<string, ToolCallBlock>();
28562856
const toolCallBlockBytes = new WeakMap<ToolCallBlock, number>();
2857+
const toolCallBlockIndices = new WeakMap<ToolCallBlock, number>();
28572858
let sawStopFinishReason = false;
28582859
const blockIndex = () => output.content.length - 1;
28592860
const measureUtf8Bytes = (text: string) => Buffer.byteLength(text, "utf8");
@@ -2986,14 +2987,15 @@ async function processOpenAICompletionsStream(
29862987
};
29872988
currentBlock = block;
29882989
output.content.push(block);
2990+
toolCallBlockIndices.set(block, output.content.length - 1);
29892991
pushStreamEvent({
29902992
type: "toolcall_start",
2991-
contentIndex: output.content.indexOf(block),
2993+
contentIndex: toolCallBlockIndices.get(block) ?? -1,
29922994
partial: output,
29932995
});
29942996
pushStreamEvent({
29952997
type: "toolcall_delta",
2996-
contentIndex: output.content.indexOf(block),
2998+
contentIndex: toolCallBlockIndices.get(block) ?? -1,
29972999
delta: toolCall.partialArgs,
29983000
partial: output,
29993001
});
@@ -3186,9 +3188,10 @@ async function processOpenAICompletionsStream(
31863188
...(initialSig ? { thoughtSignature: initialSig } : {}),
31873189
};
31883190
output.content.push(block);
3191+
toolCallBlockIndices.set(block, output.content.length - 1);
31893192
pushStreamEvent({
31903193
type: "toolcall_start",
3191-
contentIndex: output.content.indexOf(block),
3194+
contentIndex: toolCallBlockIndices.get(block) ?? -1,
31923195
partial: output,
31933196
});
31943197
}
@@ -3218,7 +3221,7 @@ async function processOpenAICompletionsStream(
32183221
block.arguments = parseStreamingJson(block.partialArgs);
32193222
pushStreamEvent({
32203223
type: "toolcall_delta",
3221-
contentIndex: output.content.indexOf(block),
3224+
contentIndex: toolCallBlockIndices.get(block) ?? -1,
32223225
delta: toolCall.function.arguments,
32233226
partial: output,
32243227
});

‎src/config/sessions/transcript-append.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ async function resolveTranscriptLeafIdFromTrailingControls(
223223
return { appendMode: "active" };
224224
}
225225

226-
async function readTranscriptLeafInfo(transcriptPath: string): Promise<TranscriptLeafInfo> {
226+
async function readTranscriptLeafInfoForward(transcriptPath: string): Promise<TranscriptLeafInfo> {
227227
let leafId: string | undefined;
228228
let hasParentLinkedEntries = false;
229229
let nonSessionEntryCount = 0;
@@ -266,6 +266,57 @@ async function readTranscriptLeafInfo(transcriptPath: string): Promise<Transcrip
266266
};
267267
}
268268

269+
async function readTranscriptLeafInfo(transcriptPath: string): Promise<TranscriptLeafInfo> {
270+
let latestEntryId: string | undefined;
271+
for await (const line of streamSessionTranscriptLinesReverse(transcriptPath)) {
272+
const lineInfo = readTranscriptLineInfo(line);
273+
if (!lineInfo.entryId) {
274+
continue;
275+
}
276+
if (lineInfo.invalidLeafControl) {
277+
break;
278+
}
279+
if (lineInfo.leafControl) {
280+
if (latestEntryId) {
281+
const valid = await validateTranscriptLeafControlReferences({
282+
transcriptPath,
283+
leafControlId: lineInfo.entryId,
284+
leafControl: lineInfo.leafControl,
285+
});
286+
if (!valid) {
287+
break;
288+
}
289+
return {
290+
leafId: latestEntryId,
291+
appendMode: lineInfo.leafControl.appendMode === "side" ? "side" : "active",
292+
hasParentLinkedEntries: true,
293+
nonSessionEntryCount: 0,
294+
};
295+
}
296+
const resolvedLeaf = await resolveTranscriptLeafIdFromTrailingControls(transcriptPath);
297+
return {
298+
...(resolvedLeaf.leafId ? { leafId: resolvedLeaf.leafId } : {}),
299+
appendMode: resolvedLeaf.appendMode,
300+
hasParentLinkedEntries: true,
301+
nonSessionEntryCount: 0,
302+
};
303+
}
304+
latestEntryId ??= lineInfo.entryId;
305+
if (lineInfo.isCanonicalEntry && lineInfo.hasParentLinkedEntry) {
306+
return {
307+
leafId: latestEntryId,
308+
appendMode: lineInfo.appendMode === "side" ? "side" : "active",
309+
hasParentLinkedEntries: true,
310+
nonSessionEntryCount: 0,
311+
};
312+
}
313+
// A latest entry without parent linkage may be a legacy linear transcript.
314+
// Fall back to the full scan only when migration detection needs it.
315+
break;
316+
}
317+
return await readTranscriptLeafInfoForward(transcriptPath);
318+
}
319+
269320
async function migrateLinearTranscriptToParentLinked(transcriptPath: string): Promise<{
270321
leafId?: string;
271322
}> {

0 commit comments

Comments
 (0)