@illuma-ai/agents 1.1.1 → 1.1.3
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/cjs/common/constants.cjs +12 -0
- package/dist/cjs/common/constants.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +156 -82
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/main.cjs +1 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/esm/common/constants.mjs +12 -1
- package/dist/esm/common/constants.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +158 -84
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -1
- package/dist/types/common/constants.d.ts +11 -0
- package/package.json +1 -1
- package/src/common/constants.ts +12 -0
- package/src/graphs/Graph.ts +203 -102
- package/src/graphs/gapFeatures.test.ts +345 -0
package/dist/cjs/main.cjs
CHANGED
|
@@ -249,6 +249,7 @@ exports.CONTEXT_SAFETY_BUFFER = constants.CONTEXT_SAFETY_BUFFER;
|
|
|
249
249
|
exports.DEDUP_MAX_CONTENT_LENGTH = constants.DEDUP_MAX_CONTENT_LENGTH;
|
|
250
250
|
exports.MIN_THINKING_BUDGET = constants.MIN_THINKING_BUDGET;
|
|
251
251
|
exports.MULTI_DOCUMENT_THRESHOLD = constants.MULTI_DOCUMENT_THRESHOLD;
|
|
252
|
+
exports.PROACTIVE_SUMMARY_THRESHOLD = constants.PROACTIVE_SUMMARY_THRESHOLD;
|
|
252
253
|
exports.PRUNING_EMA_ALPHA = constants.PRUNING_EMA_ALPHA;
|
|
253
254
|
exports.PRUNING_INITIAL_CALIBRATION = constants.PRUNING_INITIAL_CALIBRATION;
|
|
254
255
|
exports.SUMMARIZATION_CONTEXT_THRESHOLD = constants.SUMMARIZATION_CONTEXT_THRESHOLD;
|
package/dist/cjs/main.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -51,6 +51,17 @@ const CONTEXT_SAFETY_BUFFER = 0.9;
|
|
|
51
51
|
* When the context window is ≥80% full, pruning + summarization activates.
|
|
52
52
|
*/
|
|
53
53
|
const SUMMARIZATION_CONTEXT_THRESHOLD = 80;
|
|
54
|
+
/**
|
|
55
|
+
* Proactive summarization threshold (0-1 fraction of context window).
|
|
56
|
+
* At this utilization level, background summarization fires BEFORE pruning is needed.
|
|
57
|
+
* This gives the summary time to complete so it's ready when context actually fills up.
|
|
58
|
+
*
|
|
59
|
+
* Inspired by VS Code Copilot Chat's 3-tier strategy:
|
|
60
|
+
* 80% → proactive background summary
|
|
61
|
+
* 90% → pruning kicks in (with summary already cached)
|
|
62
|
+
* 100% → graceful: use existing summary + recent messages, never block
|
|
63
|
+
*/
|
|
64
|
+
const PROACTIVE_SUMMARY_THRESHOLD = 0.8;
|
|
54
65
|
/**
|
|
55
66
|
* Default reserve ratio (0-1) — fraction of context window to preserve as recent messages.
|
|
56
67
|
* 0.3 means 30% of the context budget is reserved for the most recent messages,
|
|
@@ -88,5 +99,5 @@ const TOOL_DISCOVERY_CACHE_MAX_SIZE = 200;
|
|
|
88
99
|
*/
|
|
89
100
|
const DEDUP_MAX_CONTENT_LENGTH = 10000;
|
|
90
101
|
|
|
91
|
-
export { CONTEXT_SAFETY_BUFFER, DEDUP_MAX_CONTENT_LENGTH, MIN_THINKING_BUDGET, MULTI_DOCUMENT_THRESHOLD, PRUNING_EMA_ALPHA, PRUNING_INITIAL_CALIBRATION, SUMMARIZATION_CONTEXT_THRESHOLD, SUMMARIZATION_RESERVE_RATIO, TOOL_DISCOVERY_CACHE_MAX_SIZE, TOOL_TURN_THINKING_BUDGET };
|
|
102
|
+
export { CONTEXT_SAFETY_BUFFER, DEDUP_MAX_CONTENT_LENGTH, MIN_THINKING_BUDGET, MULTI_DOCUMENT_THRESHOLD, PROACTIVE_SUMMARY_THRESHOLD, PRUNING_EMA_ALPHA, PRUNING_INITIAL_CALIBRATION, SUMMARIZATION_CONTEXT_THRESHOLD, SUMMARIZATION_RESERVE_RATIO, TOOL_DISCOVERY_CACHE_MAX_SIZE, TOOL_TURN_THINKING_BUDGET };
|
|
92
103
|
//# sourceMappingURL=constants.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.mjs","sources":["../../../src/common/constants.ts"],"sourcesContent":["// src/common/constants.ts\n\n/**\n * Minimum thinking budget allowed by the Anthropic API.\n * Extended thinking requires at least 1024 budget_tokens.\n * @see https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking\n */\nexport const MIN_THINKING_BUDGET = 1024;\n\n/**\n * Reduced thinking budget for subsequent ReAct iterations (tool-result turns).\n *\n * In a ReAct agent loop, the first LLM call processes the user's query and\n * may need deep reasoning. Subsequent iterations (after tool results return)\n * typically only need to decide \"call next tool\" or \"generate final response\"\n * — 1024 tokens is sufficient for this routing logic.\n *\n * This reduces wall-clock time per iteration from ~20-30s to ~5-10s,\n * compounding across multi-tool conversations (e.g., 10 tool calls).\n */\nexport const TOOL_TURN_THINKING_BUDGET = 1024;\n\n// ============================================================================\n// CONTEXT OVERFLOW MANAGEMENT\n//\n// Context overflow is handled mechanically — no token budget numbers are\n// exposed to the LLM. The system uses: pruning (Graph), summarization\n// (summarizeCallback), and auto-continuation (client.js max_tokens detection).\n//\n// See: docs/context-overflow-architecture.md\n// ============================================================================\n\n/**\n * Minimum number of attached documents before the multi-document delegation\n * hint is injected. Below this threshold, the agent processes documents\n * directly within its own context.\n */\nexport const MULTI_DOCUMENT_THRESHOLD = 3;\n\n/**\n * Context utilization safety buffer multiplier (0-1).\n * Applied as: effectiveMax = (maxContextTokens - maxOutputTokens) * CONTEXT_SAFETY_BUFFER\n *\n * Reserves headroom so the LLM doesn't hit hard token limits mid-generation.\n * 0.9 = 10% reserved for safety.\n */\nexport const CONTEXT_SAFETY_BUFFER = 0.9;\n\n// ============================================================================\n// SUMMARIZATION CONFIGURATION DEFAULTS\n//\n// These constants provide sensible defaults for the SummarizationConfig.\n// They can be overridden per-agent via AgentInputs.summarizationConfig.\n// ============================================================================\n\n/**\n * Default context utilization percentage (0-100) at which summarization triggers.\n * When the context window is ≥80% full, pruning + summarization activates.\n */\nexport const SUMMARIZATION_CONTEXT_THRESHOLD = 80;\n\n/**\n * Default reserve ratio (0-1) — fraction of context window to preserve as recent messages.\n * 0.3 means 30% of the context budget is reserved for the most recent messages,\n * ensuring the model always has immediate conversation history even after aggressive pruning.\n */\nexport const SUMMARIZATION_RESERVE_RATIO = 0.3;\n\n/**\n * Default EMA (Exponential Moving Average) alpha for pruning calibration.\n * Controls how quickly the calibration adapts to new token counts.\n * Higher α = faster adaptation (more responsive to recent changes).\n * Lower α = smoother adaptation (more stable across iterations).\n * 0.3 provides a balance between responsiveness and stability.\n */\nexport const PRUNING_EMA_ALPHA = 0.3;\n\n/**\n * Default initial calibration ratio for EMA pruning.\n * 1.0 means no adjustment on the first iteration (trust the raw token counts).\n * Subsequent iterations will adjust based on actual vs. estimated token usage.\n */\nexport const PRUNING_INITIAL_CALIBRATION = 1.0;\n\n// ============================================================================\n// TOOL DISCOVERY CACHING\n// ============================================================================\n\n/**\n * Maximum number of tool discovery entries to cache per conversation.\n * Prevents unbounded memory growth in very long conversations.\n */\nexport const TOOL_DISCOVERY_CACHE_MAX_SIZE = 200;\n\n// ============================================================================\n// MESSAGE DEDUPLICATION\n// ============================================================================\n\n/**\n * Maximum length of system message content to hash for deduplication.\n * Messages longer than this are always considered unique (hashing would be expensive).\n */\nexport const DEDUP_MAX_CONTENT_LENGTH = 10000;\n"],"names":[],"mappings":"AAAA;AAEA;;;;AAIG;AACI,MAAM,mBAAmB,GAAG;AAEnC;;;;;;;;;;AAUG;AACI,MAAM,yBAAyB,GAAG;AAEzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;;;;AAIG;AACI,MAAM,wBAAwB,GAAG;AAExC;;;;;;AAMG;AACI,MAAM,qBAAqB,GAAG;AAErC;AACA;AACA;AACA;AACA;AACA;AAEA;;;AAGG;AACI,MAAM,+BAA+B,GAAG;AAE/C;;;;AAIG;AACI,MAAM,2BAA2B,GAAG;AAE3C;;;;;;AAMG;AACI,MAAM,iBAAiB,GAAG;AAEjC;;;;AAIG;AACI,MAAM,2BAA2B,GAAG;AAE3C;AACA;AACA;AAEA;;;AAGG;AACI,MAAM,6BAA6B,GAAG;AAE7C;AACA;AACA;AAEA;;;AAGG;AACI,MAAM,wBAAwB,GAAG;;;;"}
|
|
1
|
+
{"version":3,"file":"constants.mjs","sources":["../../../src/common/constants.ts"],"sourcesContent":["// src/common/constants.ts\n\n/**\n * Minimum thinking budget allowed by the Anthropic API.\n * Extended thinking requires at least 1024 budget_tokens.\n * @see https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking\n */\nexport const MIN_THINKING_BUDGET = 1024;\n\n/**\n * Reduced thinking budget for subsequent ReAct iterations (tool-result turns).\n *\n * In a ReAct agent loop, the first LLM call processes the user's query and\n * may need deep reasoning. Subsequent iterations (after tool results return)\n * typically only need to decide \"call next tool\" or \"generate final response\"\n * — 1024 tokens is sufficient for this routing logic.\n *\n * This reduces wall-clock time per iteration from ~20-30s to ~5-10s,\n * compounding across multi-tool conversations (e.g., 10 tool calls).\n */\nexport const TOOL_TURN_THINKING_BUDGET = 1024;\n\n// ============================================================================\n// CONTEXT OVERFLOW MANAGEMENT\n//\n// Context overflow is handled mechanically — no token budget numbers are\n// exposed to the LLM. The system uses: pruning (Graph), summarization\n// (summarizeCallback), and auto-continuation (client.js max_tokens detection).\n//\n// See: docs/context-overflow-architecture.md\n// ============================================================================\n\n/**\n * Minimum number of attached documents before the multi-document delegation\n * hint is injected. Below this threshold, the agent processes documents\n * directly within its own context.\n */\nexport const MULTI_DOCUMENT_THRESHOLD = 3;\n\n/**\n * Context utilization safety buffer multiplier (0-1).\n * Applied as: effectiveMax = (maxContextTokens - maxOutputTokens) * CONTEXT_SAFETY_BUFFER\n *\n * Reserves headroom so the LLM doesn't hit hard token limits mid-generation.\n * 0.9 = 10% reserved for safety.\n */\nexport const CONTEXT_SAFETY_BUFFER = 0.9;\n\n// ============================================================================\n// SUMMARIZATION CONFIGURATION DEFAULTS\n//\n// These constants provide sensible defaults for the SummarizationConfig.\n// They can be overridden per-agent via AgentInputs.summarizationConfig.\n// ============================================================================\n\n/**\n * Default context utilization percentage (0-100) at which summarization triggers.\n * When the context window is ≥80% full, pruning + summarization activates.\n */\nexport const SUMMARIZATION_CONTEXT_THRESHOLD = 80;\n\n/**\n * Proactive summarization threshold (0-1 fraction of context window).\n * At this utilization level, background summarization fires BEFORE pruning is needed.\n * This gives the summary time to complete so it's ready when context actually fills up.\n *\n * Inspired by VS Code Copilot Chat's 3-tier strategy:\n * 80% → proactive background summary\n * 90% → pruning kicks in (with summary already cached)\n * 100% → graceful: use existing summary + recent messages, never block\n */\nexport const PROACTIVE_SUMMARY_THRESHOLD = 0.8;\n\n/**\n * Default reserve ratio (0-1) — fraction of context window to preserve as recent messages.\n * 0.3 means 30% of the context budget is reserved for the most recent messages,\n * ensuring the model always has immediate conversation history even after aggressive pruning.\n */\nexport const SUMMARIZATION_RESERVE_RATIO = 0.3;\n\n/**\n * Default EMA (Exponential Moving Average) alpha for pruning calibration.\n * Controls how quickly the calibration adapts to new token counts.\n * Higher α = faster adaptation (more responsive to recent changes).\n * Lower α = smoother adaptation (more stable across iterations).\n * 0.3 provides a balance between responsiveness and stability.\n */\nexport const PRUNING_EMA_ALPHA = 0.3;\n\n/**\n * Default initial calibration ratio for EMA pruning.\n * 1.0 means no adjustment on the first iteration (trust the raw token counts).\n * Subsequent iterations will adjust based on actual vs. estimated token usage.\n */\nexport const PRUNING_INITIAL_CALIBRATION = 1.0;\n\n// ============================================================================\n// TOOL DISCOVERY CACHING\n// ============================================================================\n\n/**\n * Maximum number of tool discovery entries to cache per conversation.\n * Prevents unbounded memory growth in very long conversations.\n */\nexport const TOOL_DISCOVERY_CACHE_MAX_SIZE = 200;\n\n// ============================================================================\n// MESSAGE DEDUPLICATION\n// ============================================================================\n\n/**\n * Maximum length of system message content to hash for deduplication.\n * Messages longer than this are always considered unique (hashing would be expensive).\n */\nexport const DEDUP_MAX_CONTENT_LENGTH = 10000;\n"],"names":[],"mappings":"AAAA;AAEA;;;;AAIG;AACI,MAAM,mBAAmB,GAAG;AAEnC;;;;;;;;;;AAUG;AACI,MAAM,yBAAyB,GAAG;AAEzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;;;;AAIG;AACI,MAAM,wBAAwB,GAAG;AAExC;;;;;;AAMG;AACI,MAAM,qBAAqB,GAAG;AAErC;AACA;AACA;AACA;AACA;AACA;AAEA;;;AAGG;AACI,MAAM,+BAA+B,GAAG;AAE/C;;;;;;;;;AASG;AACI,MAAM,2BAA2B,GAAG;AAE3C;;;;AAIG;AACI,MAAM,2BAA2B,GAAG;AAE3C;;;;;;AAMG;AACI,MAAM,iBAAiB,GAAG;AAEjC;;;;AAIG;AACI,MAAM,2BAA2B,GAAG;AAE3C;AACA;AACA;AAEA;;;AAGG;AACI,MAAM,6BAA6B,GAAG;AAE7C;AACA;AACA;AAEA;;;AAGG;AACI,MAAM,wBAAwB,GAAG;;;;"}
|
|
@@ -6,12 +6,12 @@ import { RunnableLambda } from '@langchain/core/runnables';
|
|
|
6
6
|
import { SystemMessage, HumanMessage, AIMessageChunk } from '@langchain/core/messages';
|
|
7
7
|
import { convertMessagesToContent, modifyDeltaProperties, formatAnthropicArtifactContent, formatArtifactPayload } from '../messages/core.mjs';
|
|
8
8
|
import { getMessageId } from '../messages/ids.mjs';
|
|
9
|
-
import { createPruneMessages } from '../messages/prune.mjs';
|
|
9
|
+
import { createPruneMessages, getContextUtilization } from '../messages/prune.mjs';
|
|
10
10
|
import { ensureThinkingBlockInMessages } from '../messages/format.mjs';
|
|
11
11
|
import { addCacheControl, addBedrockCacheControl } from '../messages/cache.mjs';
|
|
12
12
|
import { formatContentStrings } from '../messages/content.mjs';
|
|
13
13
|
import { GraphNodeKeys, Providers, ContentTypes, GraphEvents, MessageTypes, StepTypes, Constants } from '../common/enum.mjs';
|
|
14
|
-
import { TOOL_TURN_THINKING_BUDGET, SUMMARIZATION_CONTEXT_THRESHOLD } from '../common/constants.mjs';
|
|
14
|
+
import { TOOL_TURN_THINKING_BUDGET, PROACTIVE_SUMMARY_THRESHOLD, SUMMARIZATION_CONTEXT_THRESHOLD } from '../common/constants.mjs';
|
|
15
15
|
import { deduplicateSystemMessages } from '../messages/dedup.mjs';
|
|
16
16
|
import { resetIfNotEmpty, joinKeys } from '../utils/graph.mjs';
|
|
17
17
|
import { isOpenAILike, isGoogleLike } from '../utils/llm.mjs';
|
|
@@ -1080,69 +1080,158 @@ class StandardGraph extends Graph {
|
|
|
1080
1080
|
this._pruneCalibration = updatePruneCalibration(this._pruneCalibration, agentContext.currentUsage.input_tokens, estimatedTokens);
|
|
1081
1081
|
}
|
|
1082
1082
|
}
|
|
1083
|
+
// ── Proactive summarization at context pressure ───────────────────
|
|
1084
|
+
// Inspired by VS Code Copilot Chat's 3-tier strategy:
|
|
1085
|
+
// 80% → fire proactive background summary (BEFORE pruning needed)
|
|
1086
|
+
// 90% → pruning kicks in (summary already cached from 80% trigger)
|
|
1087
|
+
// 100% → graceful: use existing summary + recent messages, NEVER block
|
|
1088
|
+
//
|
|
1089
|
+
// This ensures the summary is READY by the time pruning actually occurs,
|
|
1090
|
+
// so the user never waits and never sees a context cliff.
|
|
1091
|
+
if (agentContext.maxContextTokens != null &&
|
|
1092
|
+
agentContext.maxContextTokens > 0 &&
|
|
1093
|
+
agentContext.summarizeCallback &&
|
|
1094
|
+
!this._summaryInFlight &&
|
|
1095
|
+
!this._cachedRunSummary) {
|
|
1096
|
+
const utilization = getContextUtilization(agentContext.indexTokenCountMap, agentContext.instructionTokens, agentContext.maxContextTokens);
|
|
1097
|
+
const threshold = (agentContext.summarizationConfig?.triggerThreshold ?? PROACTIVE_SUMMARY_THRESHOLD * 100);
|
|
1098
|
+
if (utilization >= threshold) {
|
|
1099
|
+
// Identify older messages to summarize proactively.
|
|
1100
|
+
// Keep the last N messages (recent turns) intact — only summarize older history.
|
|
1101
|
+
// This is incremental: the callback checks for existing summary and updates it.
|
|
1102
|
+
const recentTurnCount = Math.max(4, Math.floor(messages.length * 0.3));
|
|
1103
|
+
const oldMessages = messages.slice(messages[0]?.getType() === 'system' ? 1 : 0, Math.max(1, messages.length - recentTurnCount));
|
|
1104
|
+
if (oldMessages.length > 0) {
|
|
1105
|
+
this._summaryInFlight = true;
|
|
1106
|
+
console.debug(`[Graph:ProactiveSummary] Context at ${utilization.toFixed(1)}% (threshold ${threshold}%) — summarizing ${oldMessages.length} older msgs in background`);
|
|
1107
|
+
agentContext
|
|
1108
|
+
.summarizeCallback(oldMessages)
|
|
1109
|
+
.then((updated) => {
|
|
1110
|
+
if (updated != null && updated !== '') {
|
|
1111
|
+
this._cachedRunSummary = updated;
|
|
1112
|
+
console.debug(`[Graph:ProactiveSummary] Background summary ready (len=${updated.length})`);
|
|
1113
|
+
}
|
|
1114
|
+
})
|
|
1115
|
+
.catch((err) => {
|
|
1116
|
+
console.error('[Graph:ProactiveSummary] Background summary failed (non-fatal):', err);
|
|
1117
|
+
})
|
|
1118
|
+
.finally(() => {
|
|
1119
|
+
this._summaryInFlight = false;
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1083
1124
|
if (agentContext.pruneMessages) {
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
messagesToUse = context;
|
|
1090
|
-
// ── Non-blocking summarization ──────────────────────────────────
|
|
1091
|
-
// NEVER block the LLM call waiting for summarization. Instead:
|
|
1092
|
-
// 1. If _cachedRunSummary exists → use it, fire async update
|
|
1093
|
-
// 2. If persistedSummary exists → use it as fallback, fire async update
|
|
1094
|
-
// 3. If NOTHING exists (first-ever prune) → skip summary, fire async generation
|
|
1095
|
-
// The summary catches up asynchronously and is available for subsequent
|
|
1096
|
-
// iterations (tool calls) and the next conversation turn.
|
|
1125
|
+
// ── Context Compaction (Copilot-style: never delete messages) ─────
|
|
1126
|
+
//
|
|
1127
|
+
// DESIGN: Original messages are NEVER removed from the array.
|
|
1128
|
+
// Instead, we build a "windowed view" for the LLM:
|
|
1129
|
+
// [system prompt] + [summary of older turns] + [recent turns that fit]
|
|
1097
1130
|
//
|
|
1098
|
-
//
|
|
1099
|
-
// -
|
|
1100
|
-
// -
|
|
1101
|
-
// -
|
|
1102
|
-
|
|
1131
|
+
// This ensures:
|
|
1132
|
+
// - No context is ever lost (summary covers older turns)
|
|
1133
|
+
// - We can always re-summarize from originals if summary is stale
|
|
1134
|
+
// - Conversation chaining works naturally across turns
|
|
1135
|
+
//
|
|
1136
|
+
// Flow:
|
|
1137
|
+
// 1. Resolve best available summary (cached > persisted > seed)
|
|
1138
|
+
// 2. Calculate token budget available for recent messages
|
|
1139
|
+
// 3. Walk newest→oldest, build view of messages that fit
|
|
1140
|
+
// 4. Assemble: [system] + [summary] + [recent window]
|
|
1141
|
+
// 5. Fire background summary update for messages outside the window
|
|
1103
1142
|
const sumConfig = agentContext.summarizationConfig;
|
|
1104
|
-
const
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1143
|
+
const tokenCounter = agentContext.tokenCounter;
|
|
1144
|
+
const maxTokens = agentContext.maxContextTokens ?? 0;
|
|
1145
|
+
// Step 1: Resolve best available summary
|
|
1146
|
+
let summary;
|
|
1147
|
+
let summarySource;
|
|
1148
|
+
if (this._cachedRunSummary != null) {
|
|
1149
|
+
summary = this._cachedRunSummary;
|
|
1150
|
+
summarySource = 'cached';
|
|
1151
|
+
}
|
|
1152
|
+
else if (agentContext.persistedSummary != null &&
|
|
1153
|
+
agentContext.persistedSummary !== '') {
|
|
1154
|
+
summary = agentContext.persistedSummary;
|
|
1155
|
+
this._cachedRunSummary = summary;
|
|
1156
|
+
summarySource = 'persisted';
|
|
1157
|
+
}
|
|
1158
|
+
else if (sumConfig?.initialSummary != null &&
|
|
1159
|
+
sumConfig.initialSummary !== '') {
|
|
1160
|
+
summary = sumConfig.initialSummary;
|
|
1161
|
+
this._cachedRunSummary = summary;
|
|
1162
|
+
summarySource = 'initial-seed';
|
|
1163
|
+
}
|
|
1164
|
+
else {
|
|
1165
|
+
summarySource = 'none';
|
|
1166
|
+
}
|
|
1167
|
+
// Step 2: Calculate token budget
|
|
1168
|
+
// Apply EMA calibration for accuracy across iterations
|
|
1169
|
+
const calibratedMax = applyCalibration(maxTokens, this._pruneCalibration);
|
|
1170
|
+
const systemMsg = messages[0]?.getType() === 'system' ? messages[0] : null;
|
|
1171
|
+
const systemTokens = systemMsg != null
|
|
1172
|
+
? (agentContext.indexTokenCountMap[0] ?? 0)
|
|
1173
|
+
: 0;
|
|
1174
|
+
const summaryMsg = summary != null && summary !== ''
|
|
1175
|
+
? new SystemMessage(`[Conversation Summary]\n${summary}`)
|
|
1176
|
+
: null;
|
|
1177
|
+
const summaryTokens = summaryMsg != null && tokenCounter != null
|
|
1178
|
+
? tokenCounter(summaryMsg)
|
|
1179
|
+
: 0;
|
|
1180
|
+
// Budget for recent messages = total - system - summary - 3 (assistant priming)
|
|
1181
|
+
const recentBudget = calibratedMax - systemTokens - summaryTokens - 3;
|
|
1182
|
+
// Step 3: Walk newest→oldest, collect messages that fit in the budget
|
|
1183
|
+
const contentStart = systemMsg != null ? 1 : 0;
|
|
1184
|
+
let usedTokens = 0;
|
|
1185
|
+
let windowStart = messages.length; // index where the recent window begins
|
|
1186
|
+
for (let i = messages.length - 1; i >= contentStart; i--) {
|
|
1187
|
+
const msgTokens = agentContext.indexTokenCountMap[i] ?? 0;
|
|
1188
|
+
if (usedTokens + msgTokens > recentBudget) {
|
|
1189
|
+
break;
|
|
1190
|
+
}
|
|
1191
|
+
usedTokens += msgTokens;
|
|
1192
|
+
windowStart = i;
|
|
1193
|
+
}
|
|
1194
|
+
// Ensure we don't split tool-call / tool-result pairs.
|
|
1195
|
+
// If windowStart lands on a ToolMessage, walk back to include its AI message.
|
|
1196
|
+
while (windowStart > contentStart &&
|
|
1197
|
+
messages[windowStart]?.getType() === 'tool') {
|
|
1198
|
+
windowStart--;
|
|
1199
|
+
usedTokens += agentContext.indexTokenCountMap[windowStart] ?? 0;
|
|
1200
|
+
}
|
|
1201
|
+
const recentMessages = messages.slice(windowStart);
|
|
1202
|
+
const compactedMessages = messages.slice(contentStart, windowStart);
|
|
1203
|
+
const hasSummary = summaryMsg != null;
|
|
1204
|
+
// Step 4: Assemble the windowed view
|
|
1205
|
+
// [system] + [summary (covers compacted messages)] + [recent window]
|
|
1206
|
+
const viewParts = [];
|
|
1207
|
+
if (systemMsg != null) {
|
|
1208
|
+
viewParts.push(systemMsg);
|
|
1209
|
+
}
|
|
1210
|
+
if (summaryMsg != null) {
|
|
1211
|
+
viewParts.push(summaryMsg);
|
|
1212
|
+
}
|
|
1213
|
+
viewParts.push(...recentMessages);
|
|
1214
|
+
messagesToUse = viewParts;
|
|
1215
|
+
console.debug(`[Graph:Compaction] View: ${messages.length}→${viewParts.length} msgs ` +
|
|
1216
|
+
`(${compactedMessages.length} behind summary, ${recentMessages.length} in window) | ` +
|
|
1217
|
+
`summary=${summarySource}${summary ? ` (len=${summary.length})` : ''} | ` +
|
|
1218
|
+
`budget=${recentBudget}/${calibratedMax} used=${usedTokens}`);
|
|
1219
|
+
// Step 5: Fire background summary update (non-blocking)
|
|
1220
|
+
// Summarize messages outside the window so next iteration has a fresh summary.
|
|
1221
|
+
// Only trigger if there are compacted messages worth summarizing.
|
|
1222
|
+
if (compactedMessages.length > 0 &&
|
|
1223
|
+
agentContext.summarizeCallback) {
|
|
1224
|
+
const shouldSummarize = this.shouldTriggerSummarization(compactedMessages.length, maxTokens, agentContext.indexTokenCountMap, agentContext.instructionTokens, sumConfig);
|
|
1225
|
+
if (shouldSummarize) {
|
|
1137
1226
|
if (this._summaryInFlight) {
|
|
1138
|
-
this._pendingMessagesToRefine.push(...
|
|
1139
|
-
console.debug(`[Graph:
|
|
1227
|
+
this._pendingMessagesToRefine.push(...compactedMessages);
|
|
1228
|
+
console.debug(`[Graph:Compaction] Summary in-flight, queued ${compactedMessages.length} msgs (pending=${this._pendingMessagesToRefine.length})`);
|
|
1140
1229
|
}
|
|
1141
1230
|
else {
|
|
1142
1231
|
this._summaryInFlight = true;
|
|
1143
1232
|
const allMessages = this._pendingMessagesToRefine.length > 0
|
|
1144
|
-
? [...this._pendingMessagesToRefine, ...
|
|
1145
|
-
:
|
|
1233
|
+
? [...this._pendingMessagesToRefine, ...compactedMessages]
|
|
1234
|
+
: compactedMessages;
|
|
1146
1235
|
this._pendingMessagesToRefine = [];
|
|
1147
1236
|
agentContext
|
|
1148
1237
|
.summarizeCallback(allMessages)
|
|
@@ -1152,40 +1241,17 @@ class StandardGraph extends Graph {
|
|
|
1152
1241
|
}
|
|
1153
1242
|
})
|
|
1154
1243
|
.catch((err) => {
|
|
1155
|
-
console.error('[Graph] Background summary failed (non-fatal):', err);
|
|
1244
|
+
console.error('[Graph:Compaction] Background summary update failed (non-fatal):', err);
|
|
1156
1245
|
})
|
|
1157
1246
|
.finally(() => {
|
|
1158
1247
|
this._summaryInFlight = false;
|
|
1159
1248
|
});
|
|
1160
1249
|
}
|
|
1161
|
-
if (summary != null && summary !== '') {
|
|
1162
|
-
hasSummary = true;
|
|
1163
|
-
const summaryMsg = new SystemMessage(`[Conversation Summary]\n${summary}`);
|
|
1164
|
-
const systemIdx = messagesToUse[0]?.getType() === 'system' ? 1 : 0;
|
|
1165
|
-
messagesToUse = [
|
|
1166
|
-
...messagesToUse.slice(0, systemIdx),
|
|
1167
|
-
summaryMsg,
|
|
1168
|
-
...messagesToUse.slice(systemIdx),
|
|
1169
|
-
];
|
|
1170
|
-
}
|
|
1171
|
-
}
|
|
1172
|
-
catch (err) {
|
|
1173
|
-
console.error('[Graph] Summarization failed:', err);
|
|
1174
1250
|
}
|
|
1175
1251
|
}
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
}
|
|
1180
|
-
// Deduplicate system messages that accumulate from repeated tool iterations
|
|
1181
|
-
const { messages: dedupedMessages, removedCount } = deduplicateSystemMessages(messagesToUse);
|
|
1182
|
-
if (removedCount > 0) {
|
|
1183
|
-
messagesToUse = dedupedMessages;
|
|
1184
|
-
console.debug(`[Graph:Dedup] Removed ${removedCount} duplicate system message(s)`);
|
|
1185
|
-
}
|
|
1186
|
-
// Post-prune context note for task-tool-enabled agents
|
|
1187
|
-
if (messagesToRefine.length > 0 && hasTaskTool(agentContext.tools)) {
|
|
1188
|
-
const postPruneNote = buildPostPruneNote(messagesToRefine.length, hasSummary);
|
|
1252
|
+
// Post-compaction context note for task-tool-enabled agents
|
|
1253
|
+
if (compactedMessages.length > 0 && hasTaskTool(agentContext.tools)) {
|
|
1254
|
+
const postPruneNote = buildPostPruneNote(compactedMessages.length, hasSummary);
|
|
1189
1255
|
if (postPruneNote) {
|
|
1190
1256
|
messagesToUse = [
|
|
1191
1257
|
...messagesToUse,
|
|
@@ -1194,6 +1260,14 @@ class StandardGraph extends Graph {
|
|
|
1194
1260
|
}
|
|
1195
1261
|
}
|
|
1196
1262
|
}
|
|
1263
|
+
// Deduplicate system messages — ALWAYS runs, not just during compaction.
|
|
1264
|
+
// Duplicate system messages accumulate from repeated tool iterations,
|
|
1265
|
+
// summary injections, and context notes across turns.
|
|
1266
|
+
const { messages: dedupedMessages, removedCount } = deduplicateSystemMessages(messagesToUse);
|
|
1267
|
+
if (removedCount > 0) {
|
|
1268
|
+
messagesToUse = dedupedMessages;
|
|
1269
|
+
console.debug(`[Graph:Dedup] Removed ${removedCount} duplicate system message(s)`);
|
|
1270
|
+
}
|
|
1197
1271
|
let finalMessages = messagesToUse;
|
|
1198
1272
|
if (agentContext.useLegacyContent) {
|
|
1199
1273
|
finalMessages = formatContentStrings(finalMessages);
|