From 2c2fbd638d2b3a048d51e61202f4bde3b022ec5b Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 18 Nov 2026 17:20:32 -0500 Subject: [PATCH] fix: serialize compaction and preserve messages written mid-summarization --- coverage.txt | 14 +++++------ src/agent/deepAgents.js | 45 +++++++++++++++++++++++++++++++++++ tests/unit/deepAgents.test.js | 43 +++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 7 deletions(-) diff --git a/coverage.txt b/coverage.txt index 1c1f7909..484ff37a 100644 --- a/coverage.txt +++ b/coverage.txt @@ -8,7 +8,7 @@ ℹ compaction.js | 98.84 | 78.43 | 100.00 | 89-90 ℹ contextBackend.js | 100.00 | 100.00 | 100.00 | ℹ coreBackend.js | 100.00 | 100.00 | 100.00 | -ℹ deepAgents.js | 94.80 | 69.70 | 77.78 | 155-156 180 196-198 308-315 344-345 350-351 458-460 531-535 543-545 +ℹ deepAgents.js | 94.03 | 72.32 | 79.17 | 155-156 180 196-198 340-341 353-360 389-390 503-505 519-525 576-580 588-590 ℹ evictionCeiling.js | 100.00 | 100.00 | 100.00 | ℹ skillsMiddleware.js | 100.00 | 87.50 | 100.00 | ℹ config | | | | @@ -60,8 +60,8 @@ ℹ copilotAuth.js | 96.80 | 78.87 | 90.00 | 12-13 131-132 196-197 237-239 352-353 378-379 391-393 484 565-566 575-576 ℹ copilotRetryMiddleware.js | 88.52 | 79.49 | 83.33 | 75-88 ℹ index.js | 100.00 | 100.00 | 100.00 | -ℹ modelInfo.js | 98.17 | 96.39 | 100.00 | 128-129 179-180 -ℹ openai.js | 92.72 | 74.65 | 78.57 | 65 90-92 138-139 145-146 233 236-238 257-259 267-271 279-280 292-295 +ℹ modelInfo.js | 97.25 | 93.98 | 100.00 | 128-129 131-132 179-180 +ℹ openai.js | 92.72 | 75.71 | 78.57 | 65 90-92 138-139 145-146 233 236-238 257-259 267-271 279-280 292-295 ℹ tokenBudget.js | 97.14 | 85.71 | 82.35 | 50-51 83-84 ℹ tokenBudgetMiddleware.js | 98.10 | 78.67 | 87.50 | 94-95 97-98 ℹ sandbox | | | | @@ -186,9 +186,9 @@ ℹ xlsxParser.js | 82.21 | 83.33 | 88.89 | 47-48 93-112 130-137 150-156 ℹ zipExtractor.js | 82.32 | 77.78 | 66.67 | 62-63 104-107 124-142 149-152 ℹ git | | | | -ℹ index.js | 87.41 | 64.13 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458 +ℹ index.js | 87.41 | 64.37 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458 ℹ graphql | | | | -ℹ index.js | 97.85 | 98.13 | 93.75 | 57-68 +ℹ index.js | 97.50 | 97.17 | 93.75 | 47-48 57-68 ℹ image | | | | ℹ index.js | 100.00 | 100.00 | 50.00 | ℹ readImage.js | 94.16 | 72.22 | 100.00 | 79-80 88-89 95-96 103-104 @@ -261,7 +261,7 @@ ℹ openUrl.js | 100.00 | 100.00 | 100.00 | ℹ panels.js | 100.00 | 100.00 | 100.00 | ℹ projectsPanel.js | 26.83 | 100.00 | 80.00 | 34-123 -ℹ quotes.js | 96.97 | 87.50 | 100.00 | 62-63 +ℹ quotes.js | 100.00 | 100.00 | 100.00 | ℹ sanitize.js | 100.00 | 100.00 | 100.00 | ℹ scrollView.js | 98.83 | 97.73 | 73.91 | 197-199 ℹ selectComponents.js | 100.00 | 100.00 | 100.00 | @@ -285,6 +285,6 @@ ℹ loadAgents.js | 74.29 | 78.57 | 50.00 | 25-26 41 56-70 ℹ project.js | 100.00 | 100.00 | 100.00 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -ℹ all files | 93.39 | 86.69 | 88.51 | +ℹ all files | 93.38 | 86.70 | 88.50 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ℹ end of coverage report diff --git a/src/agent/deepAgents.js b/src/agent/deepAgents.js index 74bee03d..fcfaca09 100644 --- a/src/agent/deepAgents.js +++ b/src/agent/deepAgents.js @@ -245,6 +245,32 @@ export function toConversationExchange(message) { * Result describing what was removed and trimmed */ export async function compactAgentContext(agent, config, sessionState, options = {}) { + // Serialize compactions per thread so overlapping triggers (auto, manual, + // loop escalation, context-window handler) cannot interleave. + const key = config?.configurable?.thread_id ?? "__default__"; + const prev = compactionLocks.get(key) ?? Promise.resolve(); + const run = prev.then(() => runCompaction(agent, config, sessionState, options)); + const tail = run.catch(() => {}); + compactionLocks.set(key, tail); + try { + return await run; + } finally { + if (compactionLocks.get(key) === tail) compactionLocks.delete(key); + } +} + +/** Per-thread compaction queue tails. */ +const compactionLocks = new Map(); + +/** + * Perform one compaction. See {@link compactAgentContext}. + * @param {Object} agent - The orchestrator + * @param {Object} config - Runnable config + * @param {Object} [sessionState] - Session state to sync + * @param {Object} [options] - Compaction options + * @returns {Promise} Compaction result + */ +async function runCompaction(agent, config, sessionState, options = {}) { const keepRecent = options.keepRecent ?? DEFAULT_KEEP_MESSAGES; const backend = options.backend; const model = options.model; @@ -295,6 +321,25 @@ export async function compactAgentContext(agent, config, sessionState, options = finalMessages = filtered.slice(-keepRecent); } + // Summarization is slow; messages may have been written to the thread since + // the snapshot was read. Re-read and carry over anything new so it is not + // wiped by the replace-all update. + try { + const latest = await agent.getState(config); + const latestMessages = latest?.values?.messages || []; + const seen = new Set(messages.map((m) => m.id).filter(Boolean)); + const added = latestMessages.filter((m) => !m.id || !seen.has(m.id)); + const oldLen = messages.length; + // Messages without ids can't be matched; fall back to positional tail. + const fresh = + added.length > 0 && added.every((m) => m.id) ? added : latestMessages.slice(oldLen); + if (fresh.length > 0) { + finalMessages = [...finalMessages, ...fresh]; + } + } catch (err) { + logger.warn({ error: err.message }, "[compact] could not re-read state before write"); + } + // Replace the checkpointer state with the compacted message set. // The `model_request` node is the graph node that owns the `messages` // channel — targeting it lets updateState replace the message history. diff --git a/tests/unit/deepAgents.test.js b/tests/unit/deepAgents.test.js index dfa3de61..84ac4244 100644 --- a/tests/unit/deepAgents.test.js +++ b/tests/unit/deepAgents.test.js @@ -334,6 +334,49 @@ describe("compactAgentContext", () => { }; } + it("preserves messages written to the thread while compaction runs", async () => { + const { compactAgentContext } = await import("../../src/agent/deepAgents.js"); + const original = [ + makeMessage("human", "one", { id: "a" }), + makeMessage("ai", "two", { id: "b" }), + ]; + const arrived = makeMessage("human", "new question", { id: "c" }); + let calls = 0; + const updated = { messages: null }; + const agent = { + getState: async () => ({ + values: { messages: calls++ === 0 ? original : [...original, arrived] }, + }), + updateState: async (_config, values) => { + updated.messages = values.messages; + }, + }; + const result = await compactAgentContext(agent, { configurable: { thread_id: "race" } }); + + assert.strictEqual(result.ok, true); + assert.strictEqual(updated.messages.at(-1).content, "new question"); + }); + + it("serializes concurrent compactions on the same thread", async () => { + const { compactAgentContext } = await import("../../src/agent/deepAgents.js"); + const events = []; + let reads = 0; + const agent = { + getState: async () => { + // Odd reads are the initial snapshot of a compaction. + if (reads++ % 2 === 0) events.push("start"); + await new Promise((r) => setTimeout(r, 10)); + return { values: { messages: [makeMessage("human", "x", { id: "a" })] } }; + }, + updateState: async () => { + events.push("end"); + }, + }; + const config = { configurable: { thread_id: "serial" } }; + await Promise.all([compactAgentContext(agent, config), compactAgentContext(agent, config)]); + assert.deepStrictEqual(events, ["start", "end", "start", "end"]); + }); + it("removes readImage ToolMessages with non-empty data", async () => { const { compactAgentContext } = await import("../../src/agent/deepAgents.js"); const messages = [