@cuylabs/agent-core 4.10.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/chat-loop/commit-batch.d.ts +1 -1
- package/dist/agent/chat-loop/commit-batch.d.ts.map +1 -1
- package/dist/agent/chat-loop/context-recovery.d.ts +7 -3
- package/dist/agent/chat-loop/context-recovery.d.ts.map +1 -1
- package/dist/agent/chat-loop/finalize-turn.d.ts.map +1 -1
- package/dist/agent/chat-loop/loop.d.ts.map +1 -1
- package/dist/agent/chat-loop/model-step-snapshot.d.ts +1 -1
- package/dist/agent/chat-loop/model-step-snapshot.d.ts.map +1 -1
- package/dist/agent/chat-loop/types.d.ts +1 -1
- package/dist/agent/chat-loop/types.d.ts.map +1 -1
- package/dist/agent/event-printer.d.ts.map +1 -1
- package/dist/agent/fork.d.ts +1 -1
- package/dist/agent/fork.d.ts.map +1 -1
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.d.ts.map +1 -1
- package/dist/agent/instance/context-management.d.ts +14 -1
- package/dist/agent/instance/context-management.d.ts.map +1 -1
- package/dist/agent/instance/forking.d.ts +1 -1
- package/dist/agent/instance/forking.d.ts.map +1 -1
- package/dist/agent/instance/index.d.ts +17 -5
- package/dist/agent/instance/index.d.ts.map +1 -1
- package/dist/agent/instance/sessions.d.ts +1 -1
- package/dist/agent/instance/sessions.d.ts.map +1 -1
- package/dist/agent/instance/turn-lifecycle.d.ts +1 -1
- package/dist/agent/instance/turn-lifecycle.d.ts.map +1 -1
- package/dist/agent/session.d.ts +1 -1
- package/dist/agent/session.d.ts.map +1 -1
- package/dist/agent/setup/config.d.ts +1 -1
- package/dist/agent/setup/config.d.ts.map +1 -1
- package/dist/agent/setup/context-window.d.ts.map +1 -1
- package/dist/agent/setup.d.ts +1 -1
- package/dist/agent/setup.d.ts.map +1 -1
- package/dist/agent/turn-context/compaction/agent-context.d.ts +26 -3
- package/dist/agent/turn-context/compaction/agent-context.d.ts.map +1 -1
- package/dist/agent/turn-context/compaction/budget.d.ts +2 -2
- package/dist/agent/turn-context/compaction/budget.d.ts.map +1 -1
- package/dist/agent/turn-context/compaction/check.d.ts +9 -3
- package/dist/agent/turn-context/compaction/check.d.ts.map +1 -1
- package/dist/agent/turn-context/compaction/index.d.ts +1 -1
- package/dist/agent/turn-context/compaction/index.d.ts.map +1 -1
- package/dist/agent/turn-context/compaction/memory.d.ts +5 -4
- package/dist/agent/turn-context/compaction/memory.d.ts.map +1 -1
- package/dist/agent/turn-context/compaction/results.d.ts +7 -1
- package/dist/agent/turn-context/compaction/results.d.ts.map +1 -1
- package/dist/agent/turn-context/compaction/types.d.ts +7 -1
- package/dist/agent/turn-context/compaction/types.d.ts.map +1 -1
- package/dist/agent/turn-context/fit-model-context.d.ts +1 -1
- package/dist/agent/turn-context/fit-model-context.d.ts.map +1 -1
- package/dist/agent/turn-context/index.d.ts +1 -1
- package/dist/agent/turn-context/index.d.ts.map +1 -1
- package/dist/agent/types/config.d.ts +7 -0
- package/dist/agent/types/config.d.ts.map +1 -1
- package/dist/{chunk-LX4AHGI3.js → chunk-346FIYKT.js} +1 -1
- package/dist/{chunk-EBVSPHXA.js → chunk-556CPZ3J.js} +1 -1
- package/dist/{chunk-V4YQ6MBK.js → chunk-BKHWKKSG.js} +1 -1
- package/dist/{chunk-AAGKWUXR.js → chunk-CGP6UNCQ.js} +33 -18
- package/dist/{chunk-EEAGM5MS.js → chunk-DD7S7ZG4.js} +32 -15
- package/dist/{chunk-NMJNN6LS.js → chunk-DYZGHHDB.js} +424 -121
- package/dist/{chunk-VMGZKIFT.js → chunk-EDKZOPUV.js} +34 -298
- package/dist/{chunk-TU5KDFWI.js → chunk-GHVW7L4P.js} +41 -0
- package/dist/{chunk-IQA64CAO.js → chunk-TYQWH6XH.js} +6 -2
- package/dist/context/assembly/prepare.d.ts.map +1 -1
- package/dist/context/assembly/types.d.ts +6 -2
- package/dist/context/assembly/types.d.ts.map +1 -1
- package/dist/context/config.d.ts +10 -1
- package/dist/context/config.d.ts.map +1 -1
- package/dist/context/fragments/messages.d.ts +2 -0
- package/dist/context/fragments/messages.d.ts.map +1 -1
- package/dist/context/index.js +9 -3
- package/dist/context/window/budget.d.ts +28 -1
- package/dist/context/window/budget.d.ts.map +1 -1
- package/dist/context/window/compactor.d.ts +17 -2
- package/dist/context/window/compactor.d.ts.map +1 -1
- package/dist/context/window/cut-planner.d.ts +3 -0
- package/dist/context/window/cut-planner.d.ts.map +1 -1
- package/dist/context/window/decision.d.ts +11 -2
- package/dist/context/window/decision.d.ts.map +1 -1
- package/dist/context/window/estimation.d.ts +19 -4
- package/dist/context/window/estimation.d.ts.map +1 -1
- package/dist/context/window/index.d.ts +5 -3
- package/dist/context/window/index.d.ts.map +1 -1
- package/dist/context/window/manager.d.ts +33 -0
- package/dist/context/window/manager.d.ts.map +1 -1
- package/dist/context/window/summary.d.ts +9 -0
- package/dist/context/window/summary.d.ts.map +1 -1
- package/dist/context/window/tool-pruning.d.ts +11 -0
- package/dist/context/window/tool-pruning.d.ts.map +1 -1
- package/dist/dispatch/index.js +3 -3
- package/dist/execution/index.js +3 -3
- package/dist/execution/turn/index.js +3 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +311 -125
- package/dist/memory/config.d.ts +2 -2
- package/dist/memory/config.d.ts.map +1 -1
- package/dist/memory/index.d.ts +2 -2
- package/dist/memory/index.d.ts.map +1 -1
- package/dist/memory/index.js +5 -5
- package/dist/memory/middleware.d.ts +2 -2
- package/dist/memory/middleware.d.ts.map +1 -1
- package/dist/memory/types.d.ts +34 -15
- package/dist/memory/types.d.ts.map +1 -1
- package/dist/middleware/index.d.ts +2 -1
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/middleware/index.js +1 -1
- package/dist/middleware/runner.d.ts +16 -1
- package/dist/middleware/runner.d.ts.map +1 -1
- package/dist/middleware/types.d.ts +57 -2
- package/dist/middleware/types.d.ts.map +1 -1
- package/dist/{storage → sessions}/index.d.ts +7 -8
- package/dist/sessions/index.d.ts.map +1 -0
- package/dist/{storage → sessions}/index.js +6 -18
- package/dist/{storage → sessions}/manager/default.d.ts +3 -3
- package/dist/sessions/manager/default.d.ts.map +1 -0
- package/dist/sessions/manager/index.d.ts.map +1 -0
- package/dist/{storage → sessions}/manager/session-manager.d.ts +14 -4
- package/dist/sessions/manager/session-manager.d.ts.map +1 -0
- package/dist/sessions/manager/types.d.ts.map +1 -0
- package/dist/sessions/store/lock.d.ts.map +1 -0
- package/dist/{storage → sessions/store}/memory.d.ts +5 -5
- package/dist/sessions/store/memory.d.ts.map +1 -0
- package/dist/{storage → sessions}/types.d.ts +16 -6
- package/dist/sessions/types.d.ts.map +1 -0
- package/dist/{storage → sessions}/utils.d.ts +3 -3
- package/dist/sessions/utils.d.ts.map +1 -0
- package/dist/subagents/index.js +4 -4
- package/dist/types/compaction.d.ts +49 -0
- package/dist/types/compaction.d.ts.map +1 -1
- package/dist/types/events.d.ts +4 -2
- package/dist/types/events.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/storage/file/helpers.d.ts +0 -16
- package/dist/storage/file/helpers.d.ts.map +0 -1
- package/dist/storage/file/index.d.ts +0 -6
- package/dist/storage/file/index.d.ts.map +0 -1
- package/dist/storage/file/storage.d.ts +0 -29
- package/dist/storage/file/storage.d.ts.map +0 -1
- package/dist/storage/file/types.d.ts +0 -6
- package/dist/storage/file/types.d.ts.map +0 -1
- package/dist/storage/index.d.ts.map +0 -1
- package/dist/storage/lock.d.ts.map +0 -1
- package/dist/storage/manager/default.d.ts.map +0 -1
- package/dist/storage/manager/index.d.ts.map +0 -1
- package/dist/storage/manager/session-manager.d.ts.map +0 -1
- package/dist/storage/manager/types.d.ts.map +0 -1
- package/dist/storage/memory.d.ts.map +0 -1
- package/dist/storage/paths.d.ts +0 -37
- package/dist/storage/paths.d.ts.map +0 -1
- package/dist/storage/types.d.ts.map +0 -1
- package/dist/storage/utils.d.ts.map +0 -1
- /package/dist/{storage → sessions}/manager/index.d.ts +0 -0
- /package/dist/{storage → sessions}/manager/types.d.ts +0 -0
- /package/dist/{storage → sessions/store}/lock.d.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -39,7 +39,7 @@ import {
|
|
|
39
39
|
parseSubAgentRoleFrontmatter,
|
|
40
40
|
parseSubAgentToolSpec,
|
|
41
41
|
toSubAgentRole
|
|
42
|
-
} from "./chunk-
|
|
42
|
+
} from "./chunk-BKHWKKSG.js";
|
|
43
43
|
import {
|
|
44
44
|
InMemoryMailboxStore,
|
|
45
45
|
InMemoryTaskBoardStore,
|
|
@@ -73,9 +73,9 @@ import {
|
|
|
73
73
|
createMemoryMiddleware,
|
|
74
74
|
formatMemoryContextFragment,
|
|
75
75
|
normalizeMemoryRecords,
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
} from "./chunk-
|
|
76
|
+
resolveAgentMemoryCaptureConfig,
|
|
77
|
+
resolveAgentMemoryConfig
|
|
78
|
+
} from "./chunk-DD7S7ZG4.js";
|
|
79
79
|
import {
|
|
80
80
|
MiddlewareRunner,
|
|
81
81
|
approvalMiddleware,
|
|
@@ -83,7 +83,7 @@ import {
|
|
|
83
83
|
isApprovalMiddleware,
|
|
84
84
|
otelMiddleware,
|
|
85
85
|
promptCacheMiddleware
|
|
86
|
-
} from "./chunk-
|
|
86
|
+
} from "./chunk-GHVW7L4P.js";
|
|
87
87
|
import {
|
|
88
88
|
LayeredSettings,
|
|
89
89
|
NullSettings,
|
|
@@ -157,7 +157,7 @@ import {
|
|
|
157
157
|
import "./chunk-VOUEJSW6.js";
|
|
158
158
|
import {
|
|
159
159
|
assembleModelContext
|
|
160
|
-
} from "./chunk-
|
|
160
|
+
} from "./chunk-TYQWH6XH.js";
|
|
161
161
|
import {
|
|
162
162
|
createCompositeDispatchTaskExecutor,
|
|
163
163
|
createDispatchExternalTaskControl,
|
|
@@ -179,12 +179,11 @@ import {
|
|
|
179
179
|
ensureSessionLoaded,
|
|
180
180
|
getVisibleSessionMessages,
|
|
181
181
|
repairOrphanedToolCalls
|
|
182
|
-
} from "./chunk-
|
|
182
|
+
} from "./chunk-556CPZ3J.js";
|
|
183
183
|
import {
|
|
184
|
-
|
|
184
|
+
InMemorySessionStore,
|
|
185
185
|
LocalSessionTurnLock,
|
|
186
|
-
|
|
187
|
-
STORAGE_VERSION,
|
|
186
|
+
SESSION_FORMAT_VERSION,
|
|
188
187
|
SessionManager,
|
|
189
188
|
buildEntryPath,
|
|
190
189
|
buildMessagesFromEntries,
|
|
@@ -196,18 +195,13 @@ import {
|
|
|
196
195
|
deserializeMessage,
|
|
197
196
|
extractSessionInfo,
|
|
198
197
|
generateEntryId,
|
|
199
|
-
getDataDir,
|
|
200
198
|
getDefaultSessionManager,
|
|
201
|
-
getGitRootHash,
|
|
202
199
|
getLeafId,
|
|
203
|
-
getProjectId,
|
|
204
|
-
getProjectSessionsDir,
|
|
205
|
-
getSessionsDir,
|
|
206
200
|
parseJSONL,
|
|
207
201
|
serializeMessage,
|
|
208
202
|
toJSONL,
|
|
209
203
|
toJSONLBatch
|
|
210
|
-
} from "./chunk-
|
|
204
|
+
} from "./chunk-EDKZOPUV.js";
|
|
211
205
|
import {
|
|
212
206
|
sleep
|
|
213
207
|
} from "./chunk-SZ2XBPTW.js";
|
|
@@ -241,7 +235,7 @@ import {
|
|
|
241
235
|
processStepStream,
|
|
242
236
|
runModelStep,
|
|
243
237
|
runToolBatch
|
|
244
|
-
} from "./chunk-
|
|
238
|
+
} from "./chunk-346FIYKT.js";
|
|
245
239
|
import {
|
|
246
240
|
applyAgentWorkflowCommitResult,
|
|
247
241
|
applyAgentWorkflowContextCompactionResult,
|
|
@@ -277,6 +271,7 @@ import {
|
|
|
277
271
|
DEFAULT_CONTEXT_LIMITS,
|
|
278
272
|
DEFAULT_SUMMARY_MAX_INPUT_TOKENS,
|
|
279
273
|
DEFAULT_SUMMARY_MAX_OUTPUT_TOKENS,
|
|
274
|
+
DEFAULT_TOOL_CONTEXT_PRUNE_POLICY,
|
|
280
275
|
PRUNE_PROTECTED_TOOLS,
|
|
281
276
|
canGenerateCompactionSummary,
|
|
282
277
|
createCompactionEffectiveness,
|
|
@@ -285,6 +280,8 @@ import {
|
|
|
285
280
|
extractCompactionSummaryText,
|
|
286
281
|
generateSplitTurnSummary,
|
|
287
282
|
generateSummary,
|
|
283
|
+
getAutoCompactTokenLimit,
|
|
284
|
+
getEffectiveContextWindow,
|
|
288
285
|
getUsableTokenLimit,
|
|
289
286
|
isContextOverflowing,
|
|
290
287
|
planCompactionCut,
|
|
@@ -297,7 +294,7 @@ import {
|
|
|
297
294
|
serializeMessagesForSummary,
|
|
298
295
|
shouldFallbackOnSummaryFailure,
|
|
299
296
|
shouldPruneContext
|
|
300
|
-
} from "./chunk-
|
|
297
|
+
} from "./chunk-DYZGHHDB.js";
|
|
301
298
|
import {
|
|
302
299
|
AGENT_CONTEXT_FRAGMENT_CLOSE,
|
|
303
300
|
AGENT_CONTEXT_FRAGMENT_OPEN,
|
|
@@ -314,7 +311,7 @@ import {
|
|
|
314
311
|
normalizeAgentContextFragment,
|
|
315
312
|
parseRenderedAgentContextFragment,
|
|
316
313
|
renderAgentContextFragment
|
|
317
|
-
} from "./chunk-
|
|
314
|
+
} from "./chunk-CGP6UNCQ.js";
|
|
318
315
|
import {
|
|
319
316
|
DEFAULT_MAX_OUTPUT_TOKENS,
|
|
320
317
|
DEFAULT_MAX_STEPS,
|
|
@@ -1046,6 +1043,7 @@ function createEffectiveAgentConfig(config) {
|
|
|
1046
1043
|
};
|
|
1047
1044
|
}
|
|
1048
1045
|
function resolveAgentContextLimits(contextWindow, compactionConfig = {}) {
|
|
1046
|
+
const effectiveContextWindowPercent = compactionConfig.effectiveContextWindowPercent ?? DEFAULT_CONTEXT_LIMITS.effectiveContextWindowPercent;
|
|
1049
1047
|
const proportionalReserve = Math.round(contextWindow * 0.12);
|
|
1050
1048
|
const reserveTokens = contextWindow >= 64e3 ? Math.min(32e3, Math.max(8e3, proportionalReserve)) : Math.min(
|
|
1051
1049
|
Math.max(1024, Math.round(contextWindow * 0.25)),
|
|
@@ -1053,11 +1051,17 @@ function resolveAgentContextLimits(contextWindow, compactionConfig = {}) {
|
|
|
1053
1051
|
);
|
|
1054
1052
|
const usableWindow = Math.max(
|
|
1055
1053
|
512,
|
|
1056
|
-
getUsableTokenLimit({
|
|
1054
|
+
getUsableTokenLimit({
|
|
1055
|
+
contextWindow,
|
|
1056
|
+
reserveTokens,
|
|
1057
|
+
effectiveContextWindowPercent
|
|
1058
|
+
})
|
|
1057
1059
|
);
|
|
1058
1060
|
const requestedProtected = compactionConfig.protectedTokens ?? DEFAULT_CONTEXT_LIMITS.protectedTokens;
|
|
1059
1061
|
const requestedPruneMinimum = compactionConfig.pruneMinimum ?? DEFAULT_CONTEXT_LIMITS.pruneMinimum;
|
|
1060
1062
|
return {
|
|
1063
|
+
...effectiveContextWindowPercent !== void 0 ? { effectiveContextWindowPercent } : {},
|
|
1064
|
+
...compactionConfig.autoCompactTokenLimit !== void 0 ? { autoCompactTokenLimit: compactionConfig.autoCompactTokenLimit } : {},
|
|
1061
1065
|
reserveTokens,
|
|
1062
1066
|
protectedTokens: Math.min(
|
|
1063
1067
|
requestedProtected,
|
|
@@ -1089,7 +1093,12 @@ function createAgentContextWindowManager(config) {
|
|
|
1089
1093
|
maxInputTokens: compactionConfig.summaryMaxInputTokens,
|
|
1090
1094
|
maxOutputTokens: compactionConfig.summaryMaxOutputTokens,
|
|
1091
1095
|
targetRatio: compactionConfig.summaryTargetRatio
|
|
1092
|
-
}
|
|
1096
|
+
},
|
|
1097
|
+
toolPruning: {
|
|
1098
|
+
enabled: compactionConfig.prune ?? true,
|
|
1099
|
+
policy: compactionConfig.toolPruning
|
|
1100
|
+
},
|
|
1101
|
+
tokenEstimator: config.context?.tokenEstimator
|
|
1093
1102
|
});
|
|
1094
1103
|
}
|
|
1095
1104
|
|
|
@@ -1491,7 +1500,7 @@ function createMiddlewareSetup(input, config) {
|
|
|
1491
1500
|
createMemoryMiddleware({
|
|
1492
1501
|
provider: memory.provider,
|
|
1493
1502
|
recall: memory.recall,
|
|
1494
|
-
|
|
1503
|
+
capture: memory.capture,
|
|
1495
1504
|
cwd: config.cwd
|
|
1496
1505
|
})
|
|
1497
1506
|
);
|
|
@@ -1569,7 +1578,7 @@ function createAgentSetup(input) {
|
|
|
1569
1578
|
return {
|
|
1570
1579
|
config,
|
|
1571
1580
|
tools: toolSetup.tools,
|
|
1572
|
-
sessions: input.sessionManager ?? getDefaultSessionManager(),
|
|
1581
|
+
sessions: input.sessionManager ?? (input.sessionStore ? new SessionManager(input.sessionStore) : getDefaultSessionManager()),
|
|
1573
1582
|
sessionTurnLock: input.sessionTurnLock ?? new LocalSessionTurnLock(),
|
|
1574
1583
|
state: createAgentState(config),
|
|
1575
1584
|
contextManager: createAgentContextWindowManager(config),
|
|
@@ -1589,27 +1598,27 @@ function createAgentSetup(input) {
|
|
|
1589
1598
|
}
|
|
1590
1599
|
|
|
1591
1600
|
// src/agent/turn-context/compaction/budget.ts
|
|
1592
|
-
function
|
|
1601
|
+
function normalizeContextFragmentTokens(value) {
|
|
1593
1602
|
if (!Number.isFinite(value) || value === void 0 || value <= 0) return 0;
|
|
1594
1603
|
return Math.floor(value);
|
|
1595
1604
|
}
|
|
1596
|
-
function
|
|
1597
|
-
if (
|
|
1605
|
+
function reserveContextFragmentBudget(limits, contextFragmentTokens) {
|
|
1606
|
+
if (contextFragmentTokens <= 0) return limits;
|
|
1598
1607
|
const usableLimit = getUsableTokenLimit(limits);
|
|
1599
|
-
const
|
|
1608
|
+
const fragmentAdjustedUsableLimit = Math.max(
|
|
1600
1609
|
1,
|
|
1601
|
-
usableLimit -
|
|
1610
|
+
usableLimit - contextFragmentTokens
|
|
1602
1611
|
);
|
|
1603
1612
|
return {
|
|
1604
1613
|
...limits,
|
|
1605
|
-
contextWindow:
|
|
1614
|
+
contextWindow: fragmentAdjustedUsableLimit + limits.reserveTokens,
|
|
1606
1615
|
protectedTokens: Math.min(
|
|
1607
1616
|
limits.protectedTokens,
|
|
1608
|
-
Math.max(1, Math.round(
|
|
1617
|
+
Math.max(1, Math.round(fragmentAdjustedUsableLimit * 0.6))
|
|
1609
1618
|
),
|
|
1610
1619
|
pruneMinimum: Math.min(
|
|
1611
1620
|
limits.pruneMinimum,
|
|
1612
|
-
Math.max(1, Math.round(
|
|
1621
|
+
Math.max(1, Math.round(fragmentAdjustedUsableLimit * 0.5))
|
|
1613
1622
|
)
|
|
1614
1623
|
};
|
|
1615
1624
|
}
|
|
@@ -1650,18 +1659,19 @@ function collectDurableContextFragments(removedMessages, keptMessages) {
|
|
|
1650
1659
|
}
|
|
1651
1660
|
|
|
1652
1661
|
// src/agent/turn-context/compaction/memory.ts
|
|
1653
|
-
async function
|
|
1654
|
-
if (!options.provider?.
|
|
1662
|
+
async function captureMemoryBeforeCompactionCommit(options) {
|
|
1663
|
+
if (!options.provider?.captureBeforeCompactionCommit) return;
|
|
1655
1664
|
if (options.removedMessages.length === 0) return;
|
|
1656
1665
|
try {
|
|
1657
|
-
await options.provider.
|
|
1666
|
+
await options.provider.captureBeforeCompactionCommit({
|
|
1658
1667
|
sessionId: options.sessionId ?? "unknown",
|
|
1659
1668
|
...options.turnId ? { turnId: options.turnId } : {},
|
|
1660
1669
|
cwd: options.cwd ?? process.cwd(),
|
|
1661
1670
|
...options.phase ? { phase: options.phase } : {},
|
|
1662
1671
|
removedMessages: options.removedMessages,
|
|
1663
|
-
|
|
1664
|
-
...options.
|
|
1672
|
+
recentKeptMessages: options.recentKeptMessages,
|
|
1673
|
+
...options.previousSummary !== void 0 ? { previousSummary: options.previousSummary } : {},
|
|
1674
|
+
...options.nextSummary !== void 0 ? { nextSummary: options.nextSummary } : {},
|
|
1665
1675
|
...options.abort ? { abort: options.abort } : {}
|
|
1666
1676
|
});
|
|
1667
1677
|
} catch (error) {
|
|
@@ -1679,6 +1689,8 @@ function createUnchangedCompactionResult(options) {
|
|
|
1679
1689
|
messages: [...options.messages],
|
|
1680
1690
|
persistedMessages: [...options.messages],
|
|
1681
1691
|
decision: options.decision,
|
|
1692
|
+
trigger: options.trigger,
|
|
1693
|
+
reason: options.reason,
|
|
1682
1694
|
effectiveness: createCompactionEffectiveness(
|
|
1683
1695
|
options.tokensBefore,
|
|
1684
1696
|
options.tokensBefore
|
|
@@ -1698,12 +1710,17 @@ function createUnchangedCompactionResult(options) {
|
|
|
1698
1710
|
}
|
|
1699
1711
|
function createToolContextCompactionResult(options) {
|
|
1700
1712
|
const { messages, tokensBefore } = options;
|
|
1701
|
-
const tokensAfter = estimateConversationTokens(
|
|
1713
|
+
const tokensAfter = estimateConversationTokens(
|
|
1714
|
+
messages,
|
|
1715
|
+
options.tokenEstimator
|
|
1716
|
+
);
|
|
1702
1717
|
return {
|
|
1703
1718
|
compacted: true,
|
|
1704
1719
|
messages,
|
|
1705
1720
|
persistedMessages: messages,
|
|
1706
1721
|
decision: options.decision,
|
|
1722
|
+
trigger: options.trigger,
|
|
1723
|
+
reason: options.reason,
|
|
1707
1724
|
effectiveness: createCompactionEffectiveness(tokensBefore, tokensAfter),
|
|
1708
1725
|
displaySummary: false,
|
|
1709
1726
|
storageSummary: "Large historical tool context was compacted.",
|
|
@@ -1731,32 +1748,41 @@ function buildFallbackCompactionSummary(messages) {
|
|
|
1731
1748
|
// src/agent/turn-context/compaction/agent-context.ts
|
|
1732
1749
|
async function compactAgentMessages(options) {
|
|
1733
1750
|
const { contextManager, messages, logger, force = false } = options;
|
|
1751
|
+
const trigger = options.trigger ?? (options.phase === "manual" ? "manual" : force ? "recovery" : "auto");
|
|
1752
|
+
const reason = options.reason ?? (trigger === "manual" ? "manual-request" : force ? "provider-overflow" : "context-limit");
|
|
1734
1753
|
const log = logger ?? silentLogger;
|
|
1735
1754
|
const visibleMessages = [...messages];
|
|
1755
|
+
let previousSummary = findLatestCompactionSummary(visibleMessages);
|
|
1736
1756
|
const baseLimits = contextManager.getLimits();
|
|
1737
|
-
const
|
|
1738
|
-
options.
|
|
1757
|
+
const contextFragmentTokens = normalizeContextFragmentTokens(
|
|
1758
|
+
options.contextFragmentTokens
|
|
1739
1759
|
);
|
|
1740
|
-
const
|
|
1760
|
+
const tokenEstimator = contextManager.getTokenEstimator();
|
|
1761
|
+
const compactionLimits = reserveContextFragmentBudget(
|
|
1741
1762
|
baseLimits,
|
|
1742
|
-
|
|
1763
|
+
contextFragmentTokens
|
|
1743
1764
|
);
|
|
1744
|
-
const limit =
|
|
1745
|
-
const tokensBefore =
|
|
1765
|
+
const limit = getAutoCompactTokenLimit(compactionLimits);
|
|
1766
|
+
const tokensBefore = contextManager.estimateTokens(visibleMessages);
|
|
1746
1767
|
const summaryPolicy = contextManager.getSummaryPolicy();
|
|
1747
1768
|
const canSummarize = canGenerateCompactionSummary(summaryPolicy);
|
|
1748
1769
|
const initialDecision = decideContextCompaction(visibleMessages, {
|
|
1749
1770
|
limits: baseLimits,
|
|
1750
1771
|
force,
|
|
1751
1772
|
canSummarize,
|
|
1752
|
-
inputTokens: tokensBefore +
|
|
1773
|
+
inputTokens: tokensBefore + contextFragmentTokens,
|
|
1774
|
+
trigger,
|
|
1775
|
+
reason,
|
|
1776
|
+
tokenEstimator
|
|
1753
1777
|
});
|
|
1754
1778
|
if (!force && !initialDecision.needed) {
|
|
1755
1779
|
return createUnchangedCompactionResult({
|
|
1756
1780
|
messages: visibleMessages,
|
|
1757
1781
|
decision: initialDecision,
|
|
1782
|
+
trigger,
|
|
1783
|
+
reason,
|
|
1758
1784
|
tokensBefore,
|
|
1759
|
-
limit:
|
|
1785
|
+
limit: getAutoCompactTokenLimit(baseLimits)
|
|
1760
1786
|
});
|
|
1761
1787
|
}
|
|
1762
1788
|
let removedCount = 0;
|
|
@@ -1771,25 +1797,30 @@ async function compactAgentMessages(options) {
|
|
|
1771
1797
|
let fallbackRemovedMessages;
|
|
1772
1798
|
let fallbackKeptMessages;
|
|
1773
1799
|
try {
|
|
1774
|
-
const
|
|
1800
|
+
const result2 = await pruneContext(visibleMessages, {
|
|
1775
1801
|
limits: compactionLimits,
|
|
1776
1802
|
summary: summaryPolicy,
|
|
1777
|
-
force
|
|
1803
|
+
force,
|
|
1804
|
+
toolPruning: contextManager.getToolPruning(),
|
|
1805
|
+
trigger,
|
|
1806
|
+
reason,
|
|
1807
|
+
tokenEstimator
|
|
1778
1808
|
});
|
|
1779
|
-
removedCount =
|
|
1780
|
-
pruneTokensRemoved =
|
|
1781
|
-
summarized =
|
|
1782
|
-
summary =
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1809
|
+
removedCount = result2.removedCount;
|
|
1810
|
+
pruneTokensRemoved = result2.effectiveness.tokensRemoved;
|
|
1811
|
+
summarized = result2.summarized;
|
|
1812
|
+
summary = result2.summary;
|
|
1813
|
+
previousSummary ??= result2.previousSummary;
|
|
1814
|
+
cutIndex = result2.cutIndex;
|
|
1815
|
+
cutReason = result2.cutReason;
|
|
1816
|
+
prunedMessages = result2.messages;
|
|
1817
|
+
fallbackRemovedMessages = result2.removedMessages;
|
|
1787
1818
|
if (removedCount > 0) {
|
|
1788
1819
|
keptMessagesFromPrune = stripSummaryPreviewMessage(
|
|
1789
|
-
|
|
1820
|
+
result2.messages,
|
|
1790
1821
|
summary
|
|
1791
1822
|
);
|
|
1792
|
-
fallbackKeptMessages =
|
|
1823
|
+
fallbackKeptMessages = result2.keptMessages;
|
|
1793
1824
|
}
|
|
1794
1825
|
} catch (pruneError) {
|
|
1795
1826
|
if (!shouldFallbackOnSummaryFailure({ policy: summaryPolicy, force })) {
|
|
@@ -1801,13 +1832,17 @@ async function compactAgentMessages(options) {
|
|
|
1801
1832
|
}
|
|
1802
1833
|
if (removedCount === 0 && prunedMessages && pruneTokensRemoved > 0 && !decideContextCompaction(prunedMessages, {
|
|
1803
1834
|
limits: compactionLimits,
|
|
1804
|
-
canSummarize
|
|
1835
|
+
canSummarize,
|
|
1836
|
+
tokenEstimator
|
|
1805
1837
|
}).needed) {
|
|
1806
1838
|
return createToolContextCompactionResult({
|
|
1807
1839
|
messages: prunedMessages,
|
|
1808
1840
|
decision,
|
|
1841
|
+
trigger,
|
|
1842
|
+
reason,
|
|
1809
1843
|
tokensBefore,
|
|
1810
|
-
limit
|
|
1844
|
+
limit,
|
|
1845
|
+
tokenEstimator
|
|
1811
1846
|
});
|
|
1812
1847
|
}
|
|
1813
1848
|
if (removedCount === 0) {
|
|
@@ -1817,7 +1852,8 @@ async function compactAgentMessages(options) {
|
|
|
1817
1852
|
compactionLimits.protectedTokens,
|
|
1818
1853
|
{
|
|
1819
1854
|
allowSplitTurn: summaryPolicy.mode !== "disabled",
|
|
1820
|
-
maxKeptTokens: getUsableTokenLimit(compactionLimits)
|
|
1855
|
+
maxKeptTokens: getUsableTokenLimit(compactionLimits),
|
|
1856
|
+
tokenEstimator
|
|
1821
1857
|
}
|
|
1822
1858
|
);
|
|
1823
1859
|
removedCount = fallbackPlan.cutIndex;
|
|
@@ -1831,13 +1867,18 @@ async function compactAgentMessages(options) {
|
|
|
1831
1867
|
return createToolContextCompactionResult({
|
|
1832
1868
|
messages: prunedMessages,
|
|
1833
1869
|
decision,
|
|
1870
|
+
trigger,
|
|
1871
|
+
reason,
|
|
1834
1872
|
tokensBefore,
|
|
1835
|
-
limit
|
|
1873
|
+
limit,
|
|
1874
|
+
tokenEstimator
|
|
1836
1875
|
});
|
|
1837
1876
|
}
|
|
1838
1877
|
return createUnchangedCompactionResult({
|
|
1839
1878
|
messages: visibleMessages,
|
|
1840
1879
|
decision,
|
|
1880
|
+
trigger,
|
|
1881
|
+
reason,
|
|
1841
1882
|
tokensBefore,
|
|
1842
1883
|
limit,
|
|
1843
1884
|
cutIndex,
|
|
@@ -1852,22 +1893,15 @@ async function compactAgentMessages(options) {
|
|
|
1852
1893
|
);
|
|
1853
1894
|
if (summaryPolicy.mode === "disabled" && !summary) {
|
|
1854
1895
|
const persistedMessages2 = [...preservedContextFragments, ...keptMessages];
|
|
1855
|
-
const tokensAfter2 =
|
|
1856
|
-
|
|
1857
|
-
provider: options.memoryProvider,
|
|
1858
|
-
sessionId: options.sessionId,
|
|
1859
|
-
turnId: options.turnId,
|
|
1860
|
-
cwd: options.cwd,
|
|
1861
|
-
abort: options.abort,
|
|
1862
|
-
phase: options.phase,
|
|
1863
|
-
removedMessages,
|
|
1864
|
-
keptMessages,
|
|
1865
|
-
logger: log
|
|
1866
|
-
});
|
|
1867
|
-
return {
|
|
1896
|
+
const tokensAfter2 = contextManager.estimateTokens(persistedMessages2);
|
|
1897
|
+
const result2 = {
|
|
1868
1898
|
compacted: true,
|
|
1899
|
+
trigger,
|
|
1900
|
+
reason,
|
|
1869
1901
|
messages: persistedMessages2,
|
|
1870
1902
|
persistedMessages: persistedMessages2,
|
|
1903
|
+
removedMessages,
|
|
1904
|
+
keptMessages,
|
|
1871
1905
|
decision,
|
|
1872
1906
|
effectiveness: createCompactionEffectiveness(tokensBefore, tokensAfter2),
|
|
1873
1907
|
displaySummary: false,
|
|
@@ -1876,12 +1910,22 @@ async function compactAgentMessages(options) {
|
|
|
1876
1910
|
tokensRemoved: Math.max(0, tokensBefore - tokensAfter2),
|
|
1877
1911
|
summarized: false,
|
|
1878
1912
|
summary: void 0,
|
|
1913
|
+
...previousSummary !== void 0 ? { previousSummary } : {},
|
|
1879
1914
|
cutIndex,
|
|
1880
1915
|
cutReason,
|
|
1881
1916
|
tokensBefore,
|
|
1882
1917
|
tokensAfter: tokensAfter2,
|
|
1883
1918
|
limit
|
|
1884
1919
|
};
|
|
1920
|
+
await runBeforeContextCompactionCommitHooks({
|
|
1921
|
+
result: result2,
|
|
1922
|
+
removedMessages,
|
|
1923
|
+
keptMessages,
|
|
1924
|
+
options,
|
|
1925
|
+
inputTokens: tokensBefore + contextFragmentTokens,
|
|
1926
|
+
log
|
|
1927
|
+
});
|
|
1928
|
+
return result2;
|
|
1885
1929
|
}
|
|
1886
1930
|
const summaryText = summary ?? buildFallbackCompactionSummary(removedMessages);
|
|
1887
1931
|
const persistedMessages = [...preservedContextFragments, ...keptMessages];
|
|
@@ -1896,23 +1940,15 @@ async function compactAgentMessages(options) {
|
|
|
1896
1940
|
...preservedContextFragments,
|
|
1897
1941
|
...keptMessages
|
|
1898
1942
|
];
|
|
1899
|
-
const tokensAfter =
|
|
1900
|
-
|
|
1901
|
-
provider: options.memoryProvider,
|
|
1902
|
-
sessionId: options.sessionId,
|
|
1903
|
-
turnId: options.turnId,
|
|
1904
|
-
cwd: options.cwd,
|
|
1905
|
-
abort: options.abort,
|
|
1906
|
-
phase: options.phase,
|
|
1907
|
-
removedMessages,
|
|
1908
|
-
keptMessages,
|
|
1909
|
-
summary: summaryText,
|
|
1910
|
-
logger: log
|
|
1911
|
-
});
|
|
1912
|
-
return {
|
|
1943
|
+
const tokensAfter = contextManager.estimateTokens(messagesWithSummary);
|
|
1944
|
+
const result = {
|
|
1913
1945
|
compacted: true,
|
|
1946
|
+
trigger,
|
|
1947
|
+
reason,
|
|
1914
1948
|
messages: messagesWithSummary,
|
|
1915
1949
|
persistedMessages,
|
|
1950
|
+
removedMessages,
|
|
1951
|
+
keptMessages,
|
|
1916
1952
|
decision,
|
|
1917
1953
|
effectiveness: createCompactionEffectiveness(tokensBefore, tokensAfter),
|
|
1918
1954
|
displaySummary: true,
|
|
@@ -1921,12 +1957,23 @@ async function compactAgentMessages(options) {
|
|
|
1921
1957
|
tokensRemoved: Math.max(0, tokensBefore - tokensAfter),
|
|
1922
1958
|
summarized,
|
|
1923
1959
|
summary: summaryText,
|
|
1960
|
+
...previousSummary !== void 0 ? { previousSummary } : {},
|
|
1961
|
+
nextSummary: summaryText,
|
|
1924
1962
|
cutIndex,
|
|
1925
1963
|
cutReason,
|
|
1926
1964
|
tokensBefore,
|
|
1927
1965
|
tokensAfter,
|
|
1928
1966
|
limit
|
|
1929
1967
|
};
|
|
1968
|
+
await runBeforeContextCompactionCommitHooks({
|
|
1969
|
+
result,
|
|
1970
|
+
removedMessages,
|
|
1971
|
+
keptMessages,
|
|
1972
|
+
options,
|
|
1973
|
+
inputTokens: tokensBefore + contextFragmentTokens,
|
|
1974
|
+
log
|
|
1975
|
+
});
|
|
1976
|
+
return result;
|
|
1930
1977
|
}
|
|
1931
1978
|
async function compactAgentContext(options) {
|
|
1932
1979
|
const result = await compactAgentMessages({
|
|
@@ -1935,33 +1982,142 @@ async function compactAgentContext(options) {
|
|
|
1935
1982
|
logger: options.logger,
|
|
1936
1983
|
force: options.force,
|
|
1937
1984
|
memoryProvider: options.memoryProvider,
|
|
1985
|
+
middlewareRunner: options.middlewareRunner,
|
|
1938
1986
|
sessionId: options.sessionId ?? options.sessions.getSessionId() ?? void 0,
|
|
1939
1987
|
turnId: options.turnId,
|
|
1940
1988
|
cwd: options.cwd ?? options.sessions.getHeader()?.cwd,
|
|
1941
1989
|
abort: options.abort,
|
|
1942
1990
|
phase: options.phase,
|
|
1943
|
-
|
|
1991
|
+
trigger: options.trigger,
|
|
1992
|
+
reason: options.reason,
|
|
1993
|
+
contextFragmentTokens: options.contextFragmentTokens
|
|
1994
|
+
});
|
|
1995
|
+
await commitAgentContextCompaction({
|
|
1996
|
+
sessions: options.sessions,
|
|
1997
|
+
result,
|
|
1998
|
+
middlewareRunner: options.middlewareRunner,
|
|
1999
|
+
sessionId: options.sessionId,
|
|
2000
|
+
turnId: options.turnId,
|
|
2001
|
+
cwd: options.cwd,
|
|
2002
|
+
abort: options.abort,
|
|
2003
|
+
phase: options.phase,
|
|
2004
|
+
contextFragmentTokens: options.contextFragmentTokens
|
|
1944
2005
|
});
|
|
1945
|
-
if (result.compacted) {
|
|
1946
|
-
await options.sessions.replaceWithCompaction({
|
|
1947
|
-
summary: result.storageSummary,
|
|
1948
|
-
displaySummary: result.displaySummary,
|
|
1949
|
-
messages: result.persistedMessages,
|
|
1950
|
-
tokensBefore: result.tokensBefore,
|
|
1951
|
-
tokensAfter: result.tokensAfter
|
|
1952
|
-
});
|
|
1953
|
-
}
|
|
1954
2006
|
return {
|
|
1955
2007
|
removedCount: result.removedCount,
|
|
1956
2008
|
decision: result.decision,
|
|
2009
|
+
trigger: result.trigger,
|
|
2010
|
+
reason: result.reason,
|
|
1957
2011
|
effectiveness: result.effectiveness,
|
|
1958
2012
|
tokensRemoved: result.tokensRemoved,
|
|
1959
2013
|
summarized: result.summarized,
|
|
1960
2014
|
summary: result.summary,
|
|
2015
|
+
...result.previousSummary !== void 0 ? { previousSummary: result.previousSummary } : {},
|
|
2016
|
+
...result.nextSummary !== void 0 ? { nextSummary: result.nextSummary } : {},
|
|
1961
2017
|
cutIndex: result.cutIndex,
|
|
1962
2018
|
cutReason: result.cutReason
|
|
1963
2019
|
};
|
|
1964
2020
|
}
|
|
2021
|
+
async function commitAgentContextCompaction(options) {
|
|
2022
|
+
const { result } = options;
|
|
2023
|
+
if (!result.compacted) {
|
|
2024
|
+
return void 0;
|
|
2025
|
+
}
|
|
2026
|
+
const compactionId = await options.sessions.replaceWithCompaction({
|
|
2027
|
+
summary: result.storageSummary,
|
|
2028
|
+
displaySummary: result.displaySummary,
|
|
2029
|
+
messages: result.persistedMessages,
|
|
2030
|
+
tokensBefore: result.tokensBefore,
|
|
2031
|
+
tokensAfter: result.tokensAfter,
|
|
2032
|
+
...options.phase ? { phase: options.phase } : {},
|
|
2033
|
+
...result.trigger ? { trigger: result.trigger } : {},
|
|
2034
|
+
...result.reason ? { reason: result.reason } : {},
|
|
2035
|
+
...result.cutReason ? { cutReason: result.cutReason } : {}
|
|
2036
|
+
});
|
|
2037
|
+
await options.middlewareRunner?.runAfterContextCompactionCommit(
|
|
2038
|
+
buildContextCompactionHookContext({
|
|
2039
|
+
result,
|
|
2040
|
+
removedMessages: result.removedMessages ?? [],
|
|
2041
|
+
keptMessages: result.keptMessages ?? result.persistedMessages,
|
|
2042
|
+
sessionId: options.sessionId ?? options.sessions.getSessionId() ?? "unknown",
|
|
2043
|
+
turnId: options.turnId,
|
|
2044
|
+
cwd: options.cwd ?? options.sessions.getHeader()?.cwd ?? process.cwd(),
|
|
2045
|
+
abort: options.abort,
|
|
2046
|
+
phase: options.phase,
|
|
2047
|
+
inputTokens: result.tokensBefore + normalizeContextFragmentTokens(options.contextFragmentTokens)
|
|
2048
|
+
})
|
|
2049
|
+
);
|
|
2050
|
+
return compactionId;
|
|
2051
|
+
}
|
|
2052
|
+
async function runBeforeContextCompactionCommitHooks(options) {
|
|
2053
|
+
const context = buildContextCompactionHookContext({
|
|
2054
|
+
result: options.result,
|
|
2055
|
+
removedMessages: options.removedMessages,
|
|
2056
|
+
keptMessages: options.keptMessages,
|
|
2057
|
+
sessionId: options.options.sessionId ?? "unknown",
|
|
2058
|
+
turnId: options.options.turnId,
|
|
2059
|
+
cwd: options.options.cwd ?? process.cwd(),
|
|
2060
|
+
abort: options.options.abort,
|
|
2061
|
+
phase: options.options.phase,
|
|
2062
|
+
inputTokens: options.inputTokens
|
|
2063
|
+
});
|
|
2064
|
+
if (options.options.middlewareRunner) {
|
|
2065
|
+
await options.options.middlewareRunner.runBeforeContextCompactionCommit(
|
|
2066
|
+
context
|
|
2067
|
+
);
|
|
2068
|
+
return;
|
|
2069
|
+
}
|
|
2070
|
+
await captureMemoryBeforeCompactionCommit({
|
|
2071
|
+
provider: options.options.memoryProvider,
|
|
2072
|
+
sessionId: context.sessionId,
|
|
2073
|
+
...context.turnId ? { turnId: context.turnId } : {},
|
|
2074
|
+
cwd: context.cwd,
|
|
2075
|
+
...context.abort ? { abort: context.abort } : {},
|
|
2076
|
+
...context.phase ? { phase: context.phase } : {},
|
|
2077
|
+
removedMessages: context.removedMessages,
|
|
2078
|
+
recentKeptMessages: context.keptMessages.slice(-20),
|
|
2079
|
+
...context.previousSummary !== void 0 ? { previousSummary: context.previousSummary } : {},
|
|
2080
|
+
...context.nextSummary !== void 0 ? { nextSummary: context.nextSummary } : {},
|
|
2081
|
+
logger: options.log
|
|
2082
|
+
});
|
|
2083
|
+
}
|
|
2084
|
+
function buildContextCompactionHookContext(options) {
|
|
2085
|
+
return {
|
|
2086
|
+
sessionId: options.sessionId,
|
|
2087
|
+
...options.turnId ? { turnId: options.turnId } : {},
|
|
2088
|
+
cwd: options.cwd,
|
|
2089
|
+
...options.phase ? { phase: options.phase } : {},
|
|
2090
|
+
...options.result.trigger ? { trigger: options.result.trigger } : {},
|
|
2091
|
+
...options.result.reason ? { reason: options.result.reason } : {},
|
|
2092
|
+
inputTokens: options.inputTokens,
|
|
2093
|
+
limit: options.result.limit,
|
|
2094
|
+
decision: options.result.decision,
|
|
2095
|
+
effectiveness: options.result.effectiveness,
|
|
2096
|
+
removedMessages: options.removedMessages,
|
|
2097
|
+
keptMessages: options.keptMessages,
|
|
2098
|
+
messagesAfter: options.result.messages,
|
|
2099
|
+
persistedMessages: options.result.persistedMessages,
|
|
2100
|
+
removedCount: options.result.removedCount,
|
|
2101
|
+
tokensRemoved: options.result.tokensRemoved,
|
|
2102
|
+
summarized: options.result.summarized,
|
|
2103
|
+
...options.result.previousSummary !== void 0 ? { previousSummary: options.result.previousSummary } : {},
|
|
2104
|
+
...options.result.nextSummary !== void 0 ? { nextSummary: options.result.nextSummary } : {},
|
|
2105
|
+
...options.result.cutIndex !== void 0 ? { cutIndex: options.result.cutIndex } : {},
|
|
2106
|
+
...options.result.cutReason ? { cutReason: options.result.cutReason } : {},
|
|
2107
|
+
tokensBefore: options.result.tokensBefore,
|
|
2108
|
+
tokensAfter: options.result.tokensAfter,
|
|
2109
|
+
...options.abort ? { abort: options.abort } : {}
|
|
2110
|
+
};
|
|
2111
|
+
}
|
|
2112
|
+
function findLatestCompactionSummary(messages) {
|
|
2113
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
2114
|
+
const summary = extractCompactionSummaryText(messages[index]);
|
|
2115
|
+
if (summary !== void 0) {
|
|
2116
|
+
return summary;
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
return void 0;
|
|
2120
|
+
}
|
|
1965
2121
|
|
|
1966
2122
|
// src/agent/turn-context/compaction/check.ts
|
|
1967
2123
|
async function runCompactionCheck(params) {
|
|
@@ -1976,17 +2132,22 @@ async function runCompactionCheck(params) {
|
|
|
1976
2132
|
cwd,
|
|
1977
2133
|
abort,
|
|
1978
2134
|
memoryProvider,
|
|
2135
|
+
middlewareRunner,
|
|
1979
2136
|
retryAttempt,
|
|
1980
|
-
|
|
2137
|
+
contextFragmentTokens,
|
|
2138
|
+
trigger = phase === "manual" ? "manual" : phase === "recovery" ? "recovery" : "auto",
|
|
2139
|
+
reason = phase === "manual" ? "manual-request" : force ? "provider-overflow" : "context-limit"
|
|
1981
2140
|
} = params;
|
|
1982
2141
|
const messages = sessions.getMessages();
|
|
1983
2142
|
const limits = contextManager.getLimits();
|
|
1984
|
-
const
|
|
1985
|
-
const inputTokens =
|
|
1986
|
-
const limit =
|
|
2143
|
+
const fragmentTokens = Number.isFinite(contextFragmentTokens) && contextFragmentTokens ? Math.max(0, Math.floor(contextFragmentTokens)) : 0;
|
|
2144
|
+
const inputTokens = contextManager.estimateTokens(messages) + fragmentTokens;
|
|
2145
|
+
const limit = getAutoCompactTokenLimit(limits);
|
|
1987
2146
|
const decision = contextManager.decide(messages, {
|
|
1988
2147
|
force,
|
|
1989
|
-
inputTokens
|
|
2148
|
+
inputTokens,
|
|
2149
|
+
trigger,
|
|
2150
|
+
reason
|
|
1990
2151
|
});
|
|
1991
2152
|
if (!force && !decision.needed) {
|
|
1992
2153
|
return {
|
|
@@ -1997,6 +2158,8 @@ async function runCompactionCheck(params) {
|
|
|
1997
2158
|
report: {
|
|
1998
2159
|
applied: false,
|
|
1999
2160
|
phase,
|
|
2161
|
+
trigger,
|
|
2162
|
+
reason,
|
|
2000
2163
|
inputTokens,
|
|
2001
2164
|
limit,
|
|
2002
2165
|
decision,
|
|
@@ -2019,13 +2182,18 @@ async function runCompactionCheck(params) {
|
|
|
2019
2182
|
cwd,
|
|
2020
2183
|
abort,
|
|
2021
2184
|
memoryProvider,
|
|
2185
|
+
middlewareRunner,
|
|
2022
2186
|
phase,
|
|
2023
|
-
|
|
2187
|
+
trigger,
|
|
2188
|
+
reason,
|
|
2189
|
+
contextFragmentTokens: fragmentTokens
|
|
2024
2190
|
});
|
|
2025
2191
|
if (pruneResult.removedCount > 0 || pruneResult.summarized || pruneResult.tokensRemoved > 0) {
|
|
2026
2192
|
events.push({
|
|
2027
2193
|
type: "context-compaction",
|
|
2028
2194
|
phase,
|
|
2195
|
+
trigger,
|
|
2196
|
+
reason,
|
|
2029
2197
|
inputTokens,
|
|
2030
2198
|
limit,
|
|
2031
2199
|
decision: pruneResult.decision,
|
|
@@ -2049,6 +2217,8 @@ async function runCompactionCheck(params) {
|
|
|
2049
2217
|
report: {
|
|
2050
2218
|
applied: true,
|
|
2051
2219
|
phase,
|
|
2220
|
+
trigger,
|
|
2221
|
+
reason,
|
|
2052
2222
|
inputTokens,
|
|
2053
2223
|
limit,
|
|
2054
2224
|
decision: pruneResult.decision,
|
|
@@ -2075,6 +2245,8 @@ async function runCompactionCheck(params) {
|
|
|
2075
2245
|
report: {
|
|
2076
2246
|
applied: false,
|
|
2077
2247
|
phase,
|
|
2248
|
+
trigger,
|
|
2249
|
+
reason,
|
|
2078
2250
|
inputTokens,
|
|
2079
2251
|
limit,
|
|
2080
2252
|
decision,
|
|
@@ -2095,7 +2267,7 @@ async function runAutoCompaction(params) {
|
|
|
2095
2267
|
|
|
2096
2268
|
// src/agent/turn-context/fit-model-context.ts
|
|
2097
2269
|
function resolveConfiguredFragmentBudget(context) {
|
|
2098
|
-
const maxEstimatedTokens = context?.
|
|
2270
|
+
const maxEstimatedTokens = context?.maxFragmentTokens;
|
|
2099
2271
|
if (maxEstimatedTokens === void 0 || !Number.isFinite(maxEstimatedTokens)) {
|
|
2100
2272
|
return void 0;
|
|
2101
2273
|
}
|
|
@@ -2128,7 +2300,7 @@ async function assembleChatModelContextToFit(params) {
|
|
|
2128
2300
|
if (!params.fitAssembledContext || attempt > 0 || !windowAfter?.shouldPrune) {
|
|
2129
2301
|
break;
|
|
2130
2302
|
}
|
|
2131
|
-
const
|
|
2303
|
+
const contextFragmentTokens = Math.max(
|
|
2132
2304
|
0,
|
|
2133
2305
|
windowAfter.tokens - (windowBefore?.tokens ?? 0)
|
|
2134
2306
|
);
|
|
@@ -2143,7 +2315,8 @@ async function assembleChatModelContextToFit(params) {
|
|
|
2143
2315
|
cwd: params.cwd,
|
|
2144
2316
|
...params.abort ? { abort: params.abort } : {},
|
|
2145
2317
|
memoryProvider: params.memoryProvider,
|
|
2146
|
-
|
|
2318
|
+
middlewareRunner: params.middlewareRunner,
|
|
2319
|
+
contextFragmentTokens
|
|
2147
2320
|
});
|
|
2148
2321
|
events.push(...compaction.events);
|
|
2149
2322
|
if (!compaction.compacted) {
|
|
@@ -2206,7 +2379,7 @@ function createAgentTurnRuntimeConfig(options) {
|
|
|
2206
2379
|
|
|
2207
2380
|
// src/agent/instance/sessions.ts
|
|
2208
2381
|
function createAgentSessionManager(sessions) {
|
|
2209
|
-
return new SessionManager(sessions.
|
|
2382
|
+
return new SessionManager(sessions.getStore());
|
|
2210
2383
|
}
|
|
2211
2384
|
async function acquireAgentSessionLock(lock, sessionId, options) {
|
|
2212
2385
|
return await lock.acquire(sessionId, options);
|
|
@@ -2421,7 +2594,7 @@ function createForkedAgentConfig(options) {
|
|
|
2421
2594
|
topP: parentConfig.topP,
|
|
2422
2595
|
reasoningLevel: forkOptions.reasoningLevel ?? reasoningLevel,
|
|
2423
2596
|
...childPrompt,
|
|
2424
|
-
sessionManager: new SessionManager(sessions.
|
|
2597
|
+
sessionManager: new SessionManager(sessions.getStore()),
|
|
2425
2598
|
tools,
|
|
2426
2599
|
onDoomLoop: parentConfig.onDoomLoop,
|
|
2427
2600
|
enforceDoomLoop: parentConfig.enforceDoomLoop,
|
|
@@ -2772,6 +2945,7 @@ async function maybeRunRecoveryCompaction(options) {
|
|
|
2772
2945
|
cwd: deps.config.cwd,
|
|
2773
2946
|
abort: deps.abort,
|
|
2774
2947
|
memoryProvider: deps.memoryProvider,
|
|
2948
|
+
middlewareRunner: deps.middlewareRunner,
|
|
2775
2949
|
retryAttempt: contextRecoveryAttempts + 1
|
|
2776
2950
|
});
|
|
2777
2951
|
for (const event of recoveryCompaction.events) {
|
|
@@ -2811,7 +2985,8 @@ async function* finalizeChatLoopTurn(options) {
|
|
|
2811
2985
|
turnId,
|
|
2812
2986
|
cwd: config.cwd,
|
|
2813
2987
|
abort,
|
|
2814
|
-
memoryProvider: deps.memoryProvider
|
|
2988
|
+
memoryProvider: deps.memoryProvider,
|
|
2989
|
+
middlewareRunner
|
|
2815
2990
|
});
|
|
2816
2991
|
for (const event of compactionEvents) {
|
|
2817
2992
|
middlewareRunner.emitEvent(event);
|
|
@@ -2967,7 +3142,8 @@ async function* runChatLoop(deps) {
|
|
|
2967
3142
|
turnId,
|
|
2968
3143
|
cwd: config.cwd,
|
|
2969
3144
|
abort,
|
|
2970
|
-
memoryProvider: deps.memoryProvider
|
|
3145
|
+
memoryProvider: deps.memoryProvider,
|
|
3146
|
+
middlewareRunner
|
|
2971
3147
|
});
|
|
2972
3148
|
for (const event of preStepCompaction.events) {
|
|
2973
3149
|
middlewareRunner.emitEvent(event);
|
|
@@ -3491,8 +3667,12 @@ var Agent = class _Agent {
|
|
|
3491
3667
|
sessions: this.sessions,
|
|
3492
3668
|
logger: this._logger,
|
|
3493
3669
|
memoryProvider: this.memoryProvider,
|
|
3670
|
+
middlewareRunner: this.middlewareRunner,
|
|
3494
3671
|
sessionId: this.sessions.getSessionId() ?? void 0,
|
|
3495
|
-
cwd: this.config.cwd
|
|
3672
|
+
cwd: this.config.cwd,
|
|
3673
|
+
phase: "manual",
|
|
3674
|
+
trigger: "manual",
|
|
3675
|
+
reason: "manual-request"
|
|
3496
3676
|
});
|
|
3497
3677
|
}
|
|
3498
3678
|
async compactMessages(messages, options = {}) {
|
|
@@ -3501,9 +3681,15 @@ var Agent = class _Agent {
|
|
|
3501
3681
|
messages,
|
|
3502
3682
|
logger: this._logger,
|
|
3503
3683
|
force: options.force,
|
|
3684
|
+
trigger: options.trigger ?? (options.force ? "recovery" : "manual"),
|
|
3685
|
+
reason: options.reason ?? (options.force ? "provider-overflow" : "manual-request"),
|
|
3504
3686
|
memoryProvider: this.memoryProvider,
|
|
3505
|
-
|
|
3506
|
-
|
|
3687
|
+
middlewareRunner: this.middlewareRunner,
|
|
3688
|
+
sessionId: options.sessionId ?? this.sessions.getSessionId() ?? void 0,
|
|
3689
|
+
...options.turnId ? { turnId: options.turnId } : {},
|
|
3690
|
+
cwd: options.cwd ?? this.config.cwd,
|
|
3691
|
+
...options.phase ? { phase: options.phase } : {},
|
|
3692
|
+
...options.contextFragmentTokens !== void 0 ? { contextFragmentTokens: options.contextFragmentTokens } : {}
|
|
3507
3693
|
});
|
|
3508
3694
|
}
|
|
3509
3695
|
clearRememberedDoomLoopTools() {
|
|
@@ -3707,12 +3893,14 @@ var Agent = class _Agent {
|
|
|
3707
3893
|
|
|
3708
3894
|
// src/agent/event-printer.ts
|
|
3709
3895
|
function formatContextCompactionEvent(event) {
|
|
3896
|
+
const trigger = event.trigger ? `, trigger=${event.trigger}` : "";
|
|
3897
|
+
const runReason = event.reason ? `, runReason=${event.reason}` : "";
|
|
3710
3898
|
if (event.removedCount === 0 && event.tokensRemoved > 0 && !event.cutReason) {
|
|
3711
|
-
return `\u{1F4E6} Context compaction (${event.phase}): compacted tool context, ${event.tokensRemoved} tokens
|
|
3899
|
+
return `\u{1F4E6} Context compaction (${event.phase}): compacted tool context, ${event.tokensRemoved} tokens${trigger}${runReason}
|
|
3712
3900
|
`;
|
|
3713
3901
|
}
|
|
3714
3902
|
const cutReason = event.cutReason ? `, reason=${event.cutReason}` : "";
|
|
3715
|
-
return `\u{1F4E6} Context compaction (${event.phase}): removed ${event.removedCount} message(s), ${event.tokensRemoved} tokens${cutReason}
|
|
3903
|
+
return `\u{1F4E6} Context compaction (${event.phase}): removed ${event.removedCount} message(s), ${event.tokensRemoved} tokens${cutReason}${trigger}${runReason}
|
|
3716
3904
|
`;
|
|
3717
3905
|
}
|
|
3718
3906
|
function createEventPrinter(options = {}) {
|
|
@@ -3978,14 +4166,15 @@ export {
|
|
|
3978
4166
|
DEFAULT_SUMMARY_MAX_INPUT_TOKENS,
|
|
3979
4167
|
DEFAULT_SUMMARY_MAX_OUTPUT_TOKENS,
|
|
3980
4168
|
DEFAULT_SYSTEM_PROMPT,
|
|
4169
|
+
DEFAULT_TOOL_CONTEXT_PRUNE_POLICY,
|
|
3981
4170
|
DISPATCH_STATES,
|
|
3982
4171
|
DoomLoopError,
|
|
3983
4172
|
EXTENDED_LEVELS,
|
|
3984
4173
|
FIXED_LEVELS,
|
|
3985
|
-
FileStorage,
|
|
3986
4174
|
HumanInputTimeoutError,
|
|
3987
4175
|
HumanInputUnavailableError,
|
|
3988
4176
|
InMemoryMailboxStore,
|
|
4177
|
+
InMemorySessionStore,
|
|
3989
4178
|
InMemoryTaskBoardStore,
|
|
3990
4179
|
Inference,
|
|
3991
4180
|
InterventionController,
|
|
@@ -3997,7 +4186,6 @@ export {
|
|
|
3997
4186
|
MAX_BYTES,
|
|
3998
4187
|
MAX_LINES,
|
|
3999
4188
|
Mailbox,
|
|
4000
|
-
MemoryStorage,
|
|
4001
4189
|
MiddlewareRunner,
|
|
4002
4190
|
ModelCapabilityResolver,
|
|
4003
4191
|
NullSettings,
|
|
@@ -4015,9 +4203,9 @@ export {
|
|
|
4015
4203
|
PromptBuilder,
|
|
4016
4204
|
RemoteCapabilityFetcher,
|
|
4017
4205
|
RemoteCapabilitySource,
|
|
4206
|
+
SESSION_FORMAT_VERSION,
|
|
4018
4207
|
SKILL_FILENAME,
|
|
4019
4208
|
STANDARD_LEVELS,
|
|
4020
|
-
STORAGE_VERSION,
|
|
4021
4209
|
SUBAGENT_TOOL_IDS,
|
|
4022
4210
|
SessionManager,
|
|
4023
4211
|
SkillRegistry,
|
|
@@ -4076,6 +4264,7 @@ export {
|
|
|
4076
4264
|
clearInstalledSubAgents,
|
|
4077
4265
|
cloneAgentWorkflowTurnState,
|
|
4078
4266
|
code,
|
|
4267
|
+
commitAgentContextCompaction,
|
|
4079
4268
|
commitOutput,
|
|
4080
4269
|
commitStep,
|
|
4081
4270
|
compactAgentMessages,
|
|
@@ -4200,21 +4389,19 @@ export {
|
|
|
4200
4389
|
generateSplitTurnSummary,
|
|
4201
4390
|
generateSummary,
|
|
4202
4391
|
getAgentContextFragmentMetadata,
|
|
4392
|
+
getAutoCompactTokenLimit,
|
|
4203
4393
|
getAvailableFamilies,
|
|
4204
4394
|
getConfiguredSubAgents,
|
|
4205
4395
|
getCurrentAgentTurnStepRecord,
|
|
4206
|
-
getDataDir,
|
|
4207
4396
|
getDefaultResolver,
|
|
4208
4397
|
getDefaultSessionManager,
|
|
4398
|
+
getEffectiveContextWindow,
|
|
4209
4399
|
getErrorCategory,
|
|
4210
|
-
getGitRootHash,
|
|
4211
4400
|
getInstalledSubAgentBackend,
|
|
4212
4401
|
getLeafId,
|
|
4213
4402
|
getModelId,
|
|
4214
4403
|
getNetworkStatus,
|
|
4215
4404
|
getPluginLoader,
|
|
4216
|
-
getProjectId,
|
|
4217
|
-
getProjectSessionsDir,
|
|
4218
4405
|
getProjectSubAgentRolesDir,
|
|
4219
4406
|
getProviderCompatibility,
|
|
4220
4407
|
getProviderId,
|
|
@@ -4223,7 +4410,6 @@ export {
|
|
|
4223
4410
|
getReasoningConfigSync,
|
|
4224
4411
|
getRequiredToolHost,
|
|
4225
4412
|
getRetryDelay,
|
|
4226
|
-
getSessionsDir,
|
|
4227
4413
|
getTemplate,
|
|
4228
4414
|
getToolRisk,
|
|
4229
4415
|
getUsableTokenLimit,
|
|
@@ -4293,8 +4479,8 @@ export {
|
|
|
4293
4479
|
resetFrameworkAliases,
|
|
4294
4480
|
resetPluginLoader,
|
|
4295
4481
|
resolveAgentDefaults,
|
|
4482
|
+
resolveAgentMemoryCaptureConfig,
|
|
4296
4483
|
resolveAgentMemoryConfig,
|
|
4297
|
-
resolveAgentMemoryTurnConfig,
|
|
4298
4484
|
resolveCapability,
|
|
4299
4485
|
resolveContextSummaryPolicy,
|
|
4300
4486
|
resolveFrameworkAliases,
|