

























@@ -1361,6 +1361,79 @@ describe("runMemoryFlushIfNeeded", () => {
13611361expect(compactCall.authProfileId).toBe("anthropic:claude@martian.engineering");
13621362expect(compactCall.contextTokenBudget).toBe(258_000);
13631363});
1364+it("preflight compacts a fresh session when the current prompt estimate pushes the next request over budget", async () => {
1365+registerMemoryFlushPlanResolverForTest(() => ({
1366+softThresholdTokens: 0,
1367+forceFlushTranscriptBytes: 1_000_000_000,
1368+reserveTokensFloor: 10,
1369+prompt: "Pre-compaction memory flush.\nNO_REPLY",
1370+systemPrompt: "Write memory to memory/YYYY-MM-DD.md.",
1371+relativePath: "memory/2023-11-14.md",
1372+}));
1373+const sessionEntry: SessionEntry = {
1374+sessionId: "session",
1375+updatedAt: Date.now(),
1376+totalTokens: 985,
1377+totalTokensFresh: true,
1378+compactionCount: 0,
1379+};
1380+1381+await runPreflightCompactionIfNeeded({
1382+cfg: { agents: { defaults: { compaction: { memoryFlush: {} } } } },
1383+followupRun: createTestFollowupRun({
1384+provider: "anthropic",
1385+model: "claude",
1386+sessionKey: "agent:main:main",
1387+}),
1388+promptForEstimate: "Please summarize the entire design discussion above. ".repeat(8),
1389+defaultModel: "anthropic/claude",
1390+agentCfgContextTokens: 1000,
1391+ sessionEntry,
1392+sessionStore: { "agent:main:main": sessionEntry },
1393+sessionKey: "agent:main:main",
1394+isHeartbeat: false,
1395+replyOperation: createReplyOperation(),
1396+});
1397+1398+expect(compactEmbeddedAgentSessionMock).toHaveBeenCalledTimes(1);
1399+});
1400+it("does not preflight compact a fresh session when only accumulated output tokens are large and the latest output keeps the request under budget", async () => {
1401+registerMemoryFlushPlanResolverForTest(() => ({
1402+softThresholdTokens: 0,
1403+forceFlushTranscriptBytes: 1_000_000_000,
1404+reserveTokensFloor: 10,
1405+prompt: "Pre-compaction memory flush.\nNO_REPLY",
1406+systemPrompt: "Write memory to memory/YYYY-MM-DD.md.",
1407+relativePath: "memory/2023-11-14.md",
1408+}));
1409+const sessionEntry: SessionEntry = {
1410+sessionId: "session",
1411+updatedAt: Date.now(),
1412+totalTokens: 985,
1413+outputTokens: 50_000,
1414+totalTokensFresh: true,
1415+compactionCount: 0,
1416+};
1417+1418+await runPreflightCompactionIfNeeded({
1419+cfg: { agents: { defaults: { compaction: { memoryFlush: {} } } } },
1420+followupRun: createTestFollowupRun({
1421+provider: "anthropic",
1422+model: "claude",
1423+sessionKey: "agent:main:main",
1424+}),
1425+promptForEstimate: "",
1426+defaultModel: "anthropic/claude",
1427+agentCfgContextTokens: 1000,
1428+ sessionEntry,
1429+sessionStore: { "agent:main:main": sessionEntry },
1430+sessionKey: "agent:main:main",
1431+isHeartbeat: false,
1432+replyOperation: createReplyOperation(),
1433+});
1434+1435+expect(compactEmbeddedAgentSessionMock).not.toHaveBeenCalled();
1436+});
13641437it("updates the active preflight run after transcript rotation", async () => {
13651438const sessionFile = path.join(rootDir, "session.jsonl");
13661439const successorFile = path.join(rootDir, "session-rotated.jsonl");
@@ -2113,7 +2186,7 @@ describe("runMemoryFlushIfNeeded", () => {
21132186const compactCall = requireCompactEmbeddedAgentSessionCall();
21142187expect(compactCall.sessionId).toBe("session");
21152188expect(compactCall.trigger).toBe("budget");
2116-expect(compactCall.currentTokenCount).toBe(10);
2189+expect(compactCall.currentTokenCount).toBe(12);
21172190expect(compactCall.sessionFile).toContain("large-session.jsonl");
21182191});
21192192此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。