@nextclaw/kernel 0.19.3 → 0.19.4
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/app/worker/index.d.ts +2 -2
- package/dist/app/worker/index.js +3 -2
- package/dist/{automatic-update-check.utils-C6lQfErN.d.ts → automatic-update-check.utils-8_3eYuwY.d.ts} +1 -1
- package/dist/{automatic-update-check.utils-C6lQfErN.d.ts.map → automatic-update-check.utils-8_3eYuwY.d.ts.map} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +651 -1510
- package/dist/index.js.map +1 -1
- package/dist/ncp-agent-session-journal-entry.utils-Dh0Ck_oW.js +112 -0
- package/dist/ncp-agent-session-journal-entry.utils-Dh0Ck_oW.js.map +1 -0
- package/dist/ncp-agent-session-journal.utils-B7a_PKCq.js +1457 -0
- package/dist/ncp-agent-session-journal.utils-B7a_PKCq.js.map +1 -0
- package/dist/ncp-agent-session-message-projection-tail.store-5sjmoFxv.js +117 -0
- package/dist/ncp-agent-session-message-projection-tail.store-5sjmoFxv.js.map +1 -0
- package/dist/{nextclaw-harness.manager-GK_qgrlk.d.ts → nextclaw-harness.manager-j0JHST4g.d.ts} +348 -80
- package/dist/nextclaw-harness.manager-j0JHST4g.d.ts.map +1 -0
- package/dist/{nextclaw-harness.manager-DHzYOORP.js → nextclaw-harness.manager-mJToWeCB.js} +1871 -1657
- package/dist/nextclaw-harness.manager-mJToWeCB.js.map +1 -0
- package/dist/session-journal-delta-coalescer.utils-CI-N-Zpj.js +25 -0
- package/dist/session-journal-delta-coalescer.utils-CI-N-Zpj.js.map +1 -0
- package/dist/session-journal-maintenance.store-DrZMlaxf.js +621 -0
- package/dist/session-journal-maintenance.store-DrZMlaxf.js.map +1 -0
- package/dist/session-journal-stream.utils-CupSA3YS.js +207 -0
- package/dist/session-journal-stream.utils-CupSA3YS.js.map +1 -0
- package/dist/types/update-contract.types.d.ts +1 -1
- package/dist/types/update-contract.types.js +1 -1
- package/dist/{update-manifest.types-CBp2VTM1.js → update-manifest.types-DUocefbs.js} +1 -1
- package/dist/{update-manifest.types-CBp2VTM1.js.map → update-manifest.types-DUocefbs.js.map} +1 -1
- package/dist/{update-manifest.types-C_60CVvw.d.ts → update-manifest.types-Dijq24Om.d.ts} +1 -1
- package/dist/{update-manifest.types-C_60CVvw.d.ts.map → update-manifest.types-Dijq24Om.d.ts.map} +1 -1
- package/dist/utils/automatic-update-check.utils.d.ts +1 -1
- package/package.json +13 -10
- package/dist/nextclaw-harness.manager-DHzYOORP.js.map +0 -1
- package/dist/nextclaw-harness.manager-GK_qgrlk.d.ts.map +0 -1
|
@@ -0,0 +1,1457 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { CHAT_CONTINUATION_TARGET_MESSAGE_METADATA_KEY } from "@nextclaw/shared";
|
|
3
|
+
import { MODEL_ROUND_PART_OFFSETS, buildOpenAiFunctionTool, ncpMessageToOpenAiMessageGroups, validateToolArgs } from "@nextclaw/ncp-agent-runtime";
|
|
4
|
+
import { BUILTIN_MAIN_AGENT_ID } from "@nextclaw/core";
|
|
5
|
+
import { NcpEventType, normalizeAssistantText } from "@nextclaw/ncp";
|
|
6
|
+
import { DefaultNcpAgentConversationStateManager, insertMessageByTimeline, mergeToolExecutionTiming } from "@nextclaw/ncp-toolkit";
|
|
7
|
+
import { CONTEXT_COMPACTION_METADATA_KEY as CONTEXT_COMPACTION_METADATA_KEY$1, ContextCompactionService, ContextWindowBudgetService as ContextWindowBudgetService$1, buildCompressingCompactionCheckpoint, buildContextWindowSnapshot, readCompressedContextCompactionCheckpoint as readCompressedContextCompactionCheckpoint$1 } from "@nextclaw/core/context-compaction";
|
|
8
|
+
import { estimateInputTokens } from "@nextclaw/core/model-input-budget";
|
|
9
|
+
import { readCompressedContextCompactionCheckpoint as readCompressedContextCompactionCheckpoint$2 } from "@nextclaw/core/context-compaction-checkpoint";
|
|
10
|
+
//#region src/tools/structured-result.tools.ts
|
|
11
|
+
const STRUCTURED_RESULT_TOOL_NAME = "nextclaw_submit_result";
|
|
12
|
+
var StructuredResultSubmitTool = class {
|
|
13
|
+
name = STRUCTURED_RESULT_TOOL_NAME;
|
|
14
|
+
description = "Submit the structured object result for this request.";
|
|
15
|
+
constructor(contract) {
|
|
16
|
+
this.contract = contract;
|
|
17
|
+
}
|
|
18
|
+
get parameters() {
|
|
19
|
+
return this.contract.schema;
|
|
20
|
+
}
|
|
21
|
+
validateArgs = (args) => validateToolArgs(args, this.contract.schema);
|
|
22
|
+
execute = async (args) => args;
|
|
23
|
+
};
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/utils/agent-peer-session.utils.ts
|
|
26
|
+
const AGENT_RUN_PEER_ID_METADATA_KEY = "agent_peer_id";
|
|
27
|
+
const AGENT_RUN_PEER_SCOPE_METADATA_KEY = "agent_peer_scope";
|
|
28
|
+
function createAgentPeerSessionIdentity(params) {
|
|
29
|
+
const scope = resolveAgentPeerScope(params);
|
|
30
|
+
return {
|
|
31
|
+
metadata: {
|
|
32
|
+
[AGENT_RUN_PEER_ID_METADATA_KEY]: params.peerId,
|
|
33
|
+
[AGENT_RUN_PEER_SCOPE_METADATA_KEY]: scope
|
|
34
|
+
},
|
|
35
|
+
sessionId: `agent-peer-${createHash("sha256").update(`${scope}\0${params.peerId}`).digest("hex").slice(0, 32)}`
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function resolveAgentPeerScope(params) {
|
|
39
|
+
const metadata = params.metadata ?? {};
|
|
40
|
+
const explicitScope = readOptionalString$1(metadata["agent_peer_scope"]) ?? readOptionalString$1(metadata.agentPeerScope);
|
|
41
|
+
if (explicitScope) return explicitScope;
|
|
42
|
+
return `agent:${readOptionalString$1(params.agentId) ?? BUILTIN_MAIN_AGENT_ID}:${readOptionalString$1(params.channel) ?? readOptionalString$1(metadata.channel) ?? "agent-run"}:${readOptionalString$1(metadata.accountId) ?? readOptionalString$1(metadata.account_id) ?? "default"}`;
|
|
43
|
+
}
|
|
44
|
+
function readOptionalString$1(value) {
|
|
45
|
+
if (typeof value !== "string") return;
|
|
46
|
+
return value.trim() || void 0;
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/utils/ncp-agent-session-label.utils.ts
|
|
50
|
+
const AUTO_SESSION_LABEL_MAX_LENGTH = 64;
|
|
51
|
+
function resolveNcpAgentSessionLabel(messages) {
|
|
52
|
+
for (const message of messages) {
|
|
53
|
+
if (message.role !== "user") continue;
|
|
54
|
+
for (const part of message.parts) {
|
|
55
|
+
if (part.type !== "text" && part.type !== "rich-text") continue;
|
|
56
|
+
const text = typeof part.text === "string" ? part.text.trim() : "";
|
|
57
|
+
if (text) return truncateLabel(text);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
function truncateLabel(value) {
|
|
63
|
+
const chars = Array.from(value);
|
|
64
|
+
return chars.length <= AUTO_SESSION_LABEL_MAX_LENGTH ? value : `${chars.slice(0, AUTO_SESSION_LABEL_MAX_LENGTH).join("")}...`;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/features/context-compaction/services/context-compaction-journal-recovery.service.ts
|
|
68
|
+
var ContextCompactionJournalRecoveryService = class {
|
|
69
|
+
pendingMessageIds = /* @__PURE__ */ new Set();
|
|
70
|
+
pendingCheckpoints = /* @__PURE__ */ new Map();
|
|
71
|
+
recoveredTerminals = /* @__PURE__ */ new Map();
|
|
72
|
+
terminalMessages = /* @__PURE__ */ new Map();
|
|
73
|
+
seed = (messages) => {
|
|
74
|
+
for (const message of messages) {
|
|
75
|
+
if (message.role === "assistant" && (message.status === "final" || message.status === "error")) this.terminalMessages.set(message.id, {
|
|
76
|
+
status: "failed",
|
|
77
|
+
updatedAt: message.lifecycle?.endedAt ?? message.timestamp
|
|
78
|
+
});
|
|
79
|
+
const checkpoint = this.readCheckpoint(message);
|
|
80
|
+
if (!checkpoint) continue;
|
|
81
|
+
if (checkpoint.status === "compressing") {
|
|
82
|
+
this.pendingMessageIds.add(message.id);
|
|
83
|
+
this.pendingCheckpoints.set(message.id, structuredClone(checkpoint));
|
|
84
|
+
} else {
|
|
85
|
+
this.pendingMessageIds.delete(message.id);
|
|
86
|
+
this.pendingCheckpoints.delete(message.id);
|
|
87
|
+
this.recoveredTerminals.delete(message.id);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
track = (event) => {
|
|
92
|
+
if (event.type === NcpEventType.MessageSent) {
|
|
93
|
+
this.trackMessageSent(event);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
this.trackTerminalEvent(event);
|
|
97
|
+
};
|
|
98
|
+
trackMessageSent = (event) => {
|
|
99
|
+
const checkpoint = this.readCheckpoint(event.payload.message);
|
|
100
|
+
if (!checkpoint) return;
|
|
101
|
+
if (checkpoint.status === "compressing") {
|
|
102
|
+
this.pendingMessageIds.add(event.payload.message.id);
|
|
103
|
+
this.pendingCheckpoints.set(event.payload.message.id, structuredClone(checkpoint));
|
|
104
|
+
const continuationMessageId = this.readContinuationMessageId(checkpoint);
|
|
105
|
+
const terminal = continuationMessageId ? this.terminalMessages.get(continuationMessageId) : void 0;
|
|
106
|
+
if (terminal) this.recoveredTerminals.set(event.payload.message.id, terminal);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
this.pendingMessageIds.delete(event.payload.message.id);
|
|
110
|
+
this.pendingCheckpoints.delete(event.payload.message.id);
|
|
111
|
+
this.recoveredTerminals.delete(event.payload.message.id);
|
|
112
|
+
};
|
|
113
|
+
trackTerminalEvent = (event) => {
|
|
114
|
+
const terminalStatus = event.type === NcpEventType.MessageAbort ? "cancelled" : event.type === NcpEventType.RunError || event.type === NcpEventType.RunFinished ? "failed" : null;
|
|
115
|
+
if (!terminalStatus) return;
|
|
116
|
+
const terminal = {
|
|
117
|
+
status: terminalStatus,
|
|
118
|
+
updatedAt: event.occurredAt ?? (/* @__PURE__ */ new Date(0)).toISOString()
|
|
119
|
+
};
|
|
120
|
+
const eventMessageId = this.readEventMessageId(event);
|
|
121
|
+
if (eventMessageId) this.terminalMessages.set(eventMessageId, terminal);
|
|
122
|
+
if (this.pendingMessageIds.size === 0) return;
|
|
123
|
+
const pendingMessageIds = [...this.pendingMessageIds];
|
|
124
|
+
const targetedMessageIds = eventMessageId ? pendingMessageIds.filter((messageId) => {
|
|
125
|
+
const checkpoint = this.readCheckpointByMessageId(messageId);
|
|
126
|
+
return this.readContinuationMessageId(checkpoint) === eventMessageId;
|
|
127
|
+
}) : [];
|
|
128
|
+
const legacyMessageIds = pendingMessageIds.filter((messageId) => this.readContinuationMessageId(this.readCheckpointByMessageId(messageId)) === null);
|
|
129
|
+
const messageIds = targetedMessageIds.length > 0 ? [...targetedMessageIds, ...legacyMessageIds] : eventMessageId && legacyMessageIds.length < pendingMessageIds.length ? legacyMessageIds : pendingMessageIds;
|
|
130
|
+
for (const messageId of messageIds) {
|
|
131
|
+
this.recoveredTerminals.set(messageId, terminal);
|
|
132
|
+
this.pendingMessageIds.delete(messageId);
|
|
133
|
+
this.pendingCheckpoints.delete(messageId);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
terminalize = (message) => {
|
|
137
|
+
const terminal = this.recoveredTerminals.get(message.id);
|
|
138
|
+
if (!terminal) return structuredClone(message);
|
|
139
|
+
const checkpoint = this.readCheckpoint(message);
|
|
140
|
+
if (!checkpoint || checkpoint.status !== "compressing") return structuredClone(message);
|
|
141
|
+
return {
|
|
142
|
+
...structuredClone(message),
|
|
143
|
+
parts: [{
|
|
144
|
+
type: "text",
|
|
145
|
+
text: terminal.status === "cancelled" ? "Context compaction was cancelled" : "Context compaction failed"
|
|
146
|
+
}],
|
|
147
|
+
metadata: {
|
|
148
|
+
...structuredClone(message.metadata ?? {}),
|
|
149
|
+
checkpoint: {
|
|
150
|
+
...structuredClone(checkpoint),
|
|
151
|
+
status: terminal.status,
|
|
152
|
+
updatedAt: terminal.updatedAt
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
readCheckpoint = (message) => {
|
|
158
|
+
const metadata = message.metadata;
|
|
159
|
+
const checkpoint = metadata?.checkpoint;
|
|
160
|
+
return metadata?.nextclaw_timeline_kind === "context_compaction" && checkpoint && typeof checkpoint === "object" && !Array.isArray(checkpoint) ? checkpoint : null;
|
|
161
|
+
};
|
|
162
|
+
readCheckpointByMessageId = (messageId) => {
|
|
163
|
+
const checkpoint = this.pendingCheckpoints.get(messageId);
|
|
164
|
+
return checkpoint ? structuredClone(checkpoint) : null;
|
|
165
|
+
};
|
|
166
|
+
readContinuationMessageId = (checkpoint) => {
|
|
167
|
+
const value = checkpoint?.continuationMessageId;
|
|
168
|
+
return typeof value === "string" && value.trim() ? value.trim() : null;
|
|
169
|
+
};
|
|
170
|
+
readEventMessageId = (event) => {
|
|
171
|
+
if (event.type === NcpEventType.MessageAbort || event.type === NcpEventType.RunError || event.type === NcpEventType.RunFinished) {
|
|
172
|
+
const messageId = event.payload.messageId;
|
|
173
|
+
return typeof messageId === "string" && messageId.trim() ? messageId.trim() : null;
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/tools/tool-schema.tools.ts
|
|
180
|
+
const TOOL_SCHEMA_NAME = "tool_schema";
|
|
181
|
+
const EAGER_TOOL_NAMES = new Set([
|
|
182
|
+
TOOL_SCHEMA_NAME,
|
|
183
|
+
STRUCTURED_RESULT_TOOL_NAME,
|
|
184
|
+
"node_repl",
|
|
185
|
+
"read_file",
|
|
186
|
+
"write_file",
|
|
187
|
+
"edit_file",
|
|
188
|
+
"list_dir",
|
|
189
|
+
"exec",
|
|
190
|
+
"web_search",
|
|
191
|
+
"web_fetch",
|
|
192
|
+
"view_image",
|
|
193
|
+
"memory_search",
|
|
194
|
+
"memory_get"
|
|
195
|
+
]);
|
|
196
|
+
/** Model-facing disclosure only; execution continues to validate the original schema. */
|
|
197
|
+
function selectToolModelParameters(tool) {
|
|
198
|
+
const parameters = tool.modelParameters ?? tool.parameters;
|
|
199
|
+
return EAGER_TOOL_NAMES.has(tool.name) || !parameters || JSON.stringify(parameters).length <= 160 ? parameters : { type: "object" };
|
|
200
|
+
}
|
|
201
|
+
var ToolSchemaTool = class {
|
|
202
|
+
name = TOOL_SCHEMA_NAME;
|
|
203
|
+
description = "Read full parameters for an available tool. Before first using a tool whose object schema has no properties field, query its exact name here, then call the original tool with the returned parameter shape.";
|
|
204
|
+
parameters = {
|
|
205
|
+
type: "object",
|
|
206
|
+
properties: { name: {
|
|
207
|
+
type: "string",
|
|
208
|
+
minLength: 1
|
|
209
|
+
} },
|
|
210
|
+
required: ["name"],
|
|
211
|
+
additionalProperties: false
|
|
212
|
+
};
|
|
213
|
+
modelParameters;
|
|
214
|
+
validateArgs;
|
|
215
|
+
constructor(readTools, portableValidation = false) {
|
|
216
|
+
this.readTools = readTools;
|
|
217
|
+
if (portableValidation) {
|
|
218
|
+
this.modelParameters = this.parameters;
|
|
219
|
+
this.validateArgs = (value) => [...typeof value.name === "string" && value.name.length >= 1 ? [] : ["name must be a nonempty string"], ...Object.keys(value).filter((key) => key !== "name").map((key) => `${key} is not allowed`)];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
execute = async (args) => {
|
|
223
|
+
const name = args && typeof args === "object" && "name" in args ? args.name : null;
|
|
224
|
+
if (typeof name !== "string" || !name.trim()) throw new Error("Tool name is required.");
|
|
225
|
+
const tool = this.readTools().find((candidate) => candidate.name === name.trim());
|
|
226
|
+
if (!tool) throw new Error(`Tool is not in the current allowed catalog: ${name}`);
|
|
227
|
+
return structuredClone({
|
|
228
|
+
name: tool.name,
|
|
229
|
+
description: tool.description,
|
|
230
|
+
parameters: tool.modelParameters ?? tool.parameters
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
/** Build one run-scoped catalog from host-provided tools, with the same duplicate and schema rules on every host. */
|
|
235
|
+
function composeAgentToolCatalog(groups, options = {}) {
|
|
236
|
+
const tools = [];
|
|
237
|
+
const seen = /* @__PURE__ */ new Set();
|
|
238
|
+
const allowed = options.allowedNames ?? null;
|
|
239
|
+
const isAllowed = (name) => allowed === null || allowed.has(name);
|
|
240
|
+
if (options.includeSchemaTool !== false && isAllowed("tool_schema")) {
|
|
241
|
+
const lookup = new ToolSchemaTool(() => tools, options.portableValidation);
|
|
242
|
+
tools.push(options.wrapTool?.(lookup) ?? lookup);
|
|
243
|
+
seen.add(TOOL_SCHEMA_NAME);
|
|
244
|
+
}
|
|
245
|
+
for (const group of groups) for (const tool of group) {
|
|
246
|
+
if (!isAllowed(tool.name)) continue;
|
|
247
|
+
if (seen.has(tool.name)) {
|
|
248
|
+
if (allowed !== null) throw new Error(`Restricted tool catalog has a duplicate name: ${tool.name}`);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
seen.add(tool.name);
|
|
252
|
+
tools.push(options.wrapTool?.(tool) ?? tool);
|
|
253
|
+
}
|
|
254
|
+
return tools;
|
|
255
|
+
}
|
|
256
|
+
//#endregion
|
|
257
|
+
//#region src/utils/agent-model-input-budget.utils.ts
|
|
258
|
+
function buildContextBlockInputMessages(contextBlocks = []) {
|
|
259
|
+
const contextContent = contextBlocks.map((block) => block.trim()).filter(Boolean).join("\n\n");
|
|
260
|
+
return contextContent ? [{
|
|
261
|
+
role: "system",
|
|
262
|
+
content: contextContent
|
|
263
|
+
}] : [];
|
|
264
|
+
}
|
|
265
|
+
function buildProviderTools(tools) {
|
|
266
|
+
const hasSchemaLookup = tools.some((tool) => tool.name === TOOL_SCHEMA_NAME);
|
|
267
|
+
return tools.map((tool) => buildOpenAiFunctionTool({
|
|
268
|
+
name: tool.name,
|
|
269
|
+
description: tool.description,
|
|
270
|
+
parameters: hasSchemaLookup ? selectToolModelParameters(tool) : tool.modelParameters ?? tool.parameters
|
|
271
|
+
}));
|
|
272
|
+
}
|
|
273
|
+
function estimateToolInputTokens(tools) {
|
|
274
|
+
return estimateInputTokens(buildProviderTools(tools));
|
|
275
|
+
}
|
|
276
|
+
//#endregion
|
|
277
|
+
//#region src/utils/context-compaction-summary-source.utils.ts
|
|
278
|
+
const SOURCE_FIELDS = [
|
|
279
|
+
"role",
|
|
280
|
+
"content",
|
|
281
|
+
"reasoning_content",
|
|
282
|
+
"name",
|
|
283
|
+
"tool_calls",
|
|
284
|
+
"tool_call_id",
|
|
285
|
+
"timestamp",
|
|
286
|
+
"ncp_message_id",
|
|
287
|
+
"ncp_part_start",
|
|
288
|
+
"ncp_part_end"
|
|
289
|
+
];
|
|
290
|
+
const IDENTITY_FIELDS = new Set([
|
|
291
|
+
"role",
|
|
292
|
+
"name",
|
|
293
|
+
"id",
|
|
294
|
+
"type",
|
|
295
|
+
"tool_call_id",
|
|
296
|
+
"ncp_message_id",
|
|
297
|
+
"timestamp"
|
|
298
|
+
]);
|
|
299
|
+
const MIN_SOURCE_TEXT_CHARS = 128;
|
|
300
|
+
function truncateSummarySourceString(value, maxChars) {
|
|
301
|
+
if (typeof value !== "string" || value.length <= maxChars) return value;
|
|
302
|
+
const marker = `[${value.length - maxChars} chars omitted]`;
|
|
303
|
+
const kept = Math.max(0, maxChars - marker.length - 2);
|
|
304
|
+
const head = Math.ceil(kept / 2);
|
|
305
|
+
return `${value.slice(0, head).trimEnd()}\n${marker}\n${kept > head ? value.slice(-(kept - head)).trimStart() : ""}`;
|
|
306
|
+
}
|
|
307
|
+
function sourceMessages(messages) {
|
|
308
|
+
return messages.map((message) => Object.fromEntries(SOURCE_FIELDS.filter((field) => message[field] !== void 0).map((field) => [field, message[field]])));
|
|
309
|
+
}
|
|
310
|
+
function serializeSource(messages, textChars) {
|
|
311
|
+
const summaries = new Set(messages.filter((message) => (message.role === "system" || message.role === "service") && typeof message.content === "string").map((message) => message.content));
|
|
312
|
+
let truncated = false;
|
|
313
|
+
return {
|
|
314
|
+
text: JSON.stringify(sourceMessages(messages), (key, value) => {
|
|
315
|
+
const fitted = IDENTITY_FIELDS.has(key) || summaries.has(value) ? value : truncateSummarySourceString(value, textChars);
|
|
316
|
+
if (fitted !== value) truncated = true;
|
|
317
|
+
return fitted;
|
|
318
|
+
}),
|
|
319
|
+
truncated
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
/** Shrink verbose fields, never discard a message or break the outer JSON. */
|
|
323
|
+
function stringifyCompactionSource(messages, maxChars, report) {
|
|
324
|
+
if (messages.length === 0) return "[]";
|
|
325
|
+
const minimal = serializeSource(messages, MIN_SOURCE_TEXT_CHARS);
|
|
326
|
+
if (minimal.text.length > maxChars) throw new Error("Compaction source identities exceed the input budget; split the source before summarizing.");
|
|
327
|
+
let fitted = minimal;
|
|
328
|
+
let lower = MIN_SOURCE_TEXT_CHARS;
|
|
329
|
+
let upper = maxChars;
|
|
330
|
+
while (lower <= upper) {
|
|
331
|
+
const limit = Math.floor((lower + upper) / 2);
|
|
332
|
+
const candidate = serializeSource(messages, limit);
|
|
333
|
+
if (candidate.text.length <= maxChars) {
|
|
334
|
+
fitted = candidate;
|
|
335
|
+
lower = limit + 1;
|
|
336
|
+
} else upper = limit - 1;
|
|
337
|
+
}
|
|
338
|
+
report?.(fitted.truncated);
|
|
339
|
+
return fitted.text;
|
|
340
|
+
}
|
|
341
|
+
//#endregion
|
|
342
|
+
//#region src/utils/context-compaction-summary-input.utils.ts
|
|
343
|
+
const SUMMARY_SOURCE_MAX_CHARS = 12e4;
|
|
344
|
+
const SUMMARY_SYSTEM_PROMPT = [
|
|
345
|
+
"Summarize facts as Markdown starting '# Compressed Working Context'.",
|
|
346
|
+
"Describe Active Request, Current Work State, Safety and User Constraints, and Continuation Contract. These are content priorities, not a machine-readable protocol.",
|
|
347
|
+
"Include latest intent, decisions, constraints, changed files, evidence and next step. Distinguish Done/In Progress/Blocked/Failed; keep command/file identities and outcomes. Attempted calls alone are not completion.",
|
|
348
|
+
"Update any previous summary: carry completed work and constraints forward, replace stale next steps. Completed requests are history. Repeat reads only for freshness/verification; recover uncertain side-effect status before repeating. A greeting does not erase the prior task or restart onboarding.",
|
|
349
|
+
"Include technical context and verification evidence when relevant. State uncertainty explicitly; never invent completion or missing constraints. Finish naturally within the output budget."
|
|
350
|
+
].join("\n");
|
|
351
|
+
function buildSummaryProviderMessages(params) {
|
|
352
|
+
const { essentialOnly, messages, sourceMaxChars, targetSummaryTokens } = params;
|
|
353
|
+
let sourceTruncated = false;
|
|
354
|
+
const source = stringifyCompactionSource(messages, sourceMaxChars, (truncated) => {
|
|
355
|
+
sourceTruncated = truncated;
|
|
356
|
+
});
|
|
357
|
+
return {
|
|
358
|
+
sourceTruncated,
|
|
359
|
+
messages: [{
|
|
360
|
+
role: "system",
|
|
361
|
+
content: essentialOnly ? `${SUMMARY_SYSTEM_PROMPT}\nThis is the final recovery attempt. Stop immediately after the essential completion marker and do not output optional sections.` : SUMMARY_SYSTEM_PROMPT
|
|
362
|
+
}, {
|
|
363
|
+
role: "user",
|
|
364
|
+
content: [
|
|
365
|
+
"Compress these runtime messages into a reusable working context.",
|
|
366
|
+
`Keep the visible Markdown summary within ${targetSummaryTokens} tokens. Use the output budget for the summary, not for hidden reasoning.`,
|
|
367
|
+
essentialOnly ? "Output the four essential sections only, then stop after the essential completion marker." : "Prioritize the active task, constraints, verified outcomes and next step. Finish naturally.",
|
|
368
|
+
"",
|
|
369
|
+
"Messages JSON:",
|
|
370
|
+
source
|
|
371
|
+
].join("\n")
|
|
372
|
+
}]
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function fitContextCompactionSummaryInput(params) {
|
|
376
|
+
const { essentialOnly = false, maxInputTokens, messages, sourceMaxChars = SUMMARY_SOURCE_MAX_CHARS, targetSummaryTokens } = params;
|
|
377
|
+
const fixedTokens = estimateInputTokens(buildSummaryProviderMessages({
|
|
378
|
+
essentialOnly,
|
|
379
|
+
messages: [],
|
|
380
|
+
sourceMaxChars: 0,
|
|
381
|
+
targetSummaryTokens
|
|
382
|
+
}).messages);
|
|
383
|
+
if (fixedTokens > maxInputTokens) throw new Error(`Context compaction summary prompt needs more than ${maxInputTokens} input tokens. Increase the agent contextTokens setting.`);
|
|
384
|
+
const fitted = buildSummaryProviderMessages({
|
|
385
|
+
essentialOnly,
|
|
386
|
+
messages,
|
|
387
|
+
targetSummaryTokens,
|
|
388
|
+
sourceMaxChars: Math.min(sourceMaxChars, Math.max(0, (maxInputTokens - fixedTokens - 8) * 3))
|
|
389
|
+
});
|
|
390
|
+
if (estimateInputTokens(fitted.messages) > maxInputTokens) throw new Error("Compaction source cannot fit without dropping message identities.");
|
|
391
|
+
return fitted;
|
|
392
|
+
}
|
|
393
|
+
function normalizeContextCompactionSummary(content) {
|
|
394
|
+
return normalizeAssistantText(content, "think-tags").text.trim();
|
|
395
|
+
}
|
|
396
|
+
//#endregion
|
|
397
|
+
//#region src/features/context-compaction/services/context-compaction-summary-generation.service.ts
|
|
398
|
+
const NATURAL_FINISH_REASONS = new Set([
|
|
399
|
+
"completed",
|
|
400
|
+
"end_turn",
|
|
401
|
+
"stop"
|
|
402
|
+
]);
|
|
403
|
+
const SUMMARY_TIMEOUT_MS = 6e4;
|
|
404
|
+
var UnusableCompactionSummary = class extends Error {};
|
|
405
|
+
const COMPACTION_RECOVERY_NOTICE = [
|
|
406
|
+
"Earlier conversation is archived, not reliably summarized. Its constraints and action outcomes remain unresolved.",
|
|
407
|
+
"Read the original session history before relying on old facts. Do not repeat actions or infer completion.",
|
|
408
|
+
"Only read-only recovery is permitted. If required business conditions cannot be verified, ask a specific clarification."
|
|
409
|
+
].join("\n");
|
|
410
|
+
var ContextCompactionSummaryGenerationService = class {
|
|
411
|
+
constructor(providerManager) {
|
|
412
|
+
this.providerManager = providerManager;
|
|
413
|
+
}
|
|
414
|
+
generate = async (params) => {
|
|
415
|
+
params.signal?.throwIfAborted();
|
|
416
|
+
let inputTokens = 0;
|
|
417
|
+
let attemptCount = 0;
|
|
418
|
+
let finishReason = "not-called";
|
|
419
|
+
let rawSummaryTokens = 0;
|
|
420
|
+
let providerUsage = {};
|
|
421
|
+
let summary = "";
|
|
422
|
+
let failure;
|
|
423
|
+
let sourceTruncated = false;
|
|
424
|
+
const controller = new AbortController();
|
|
425
|
+
const abort = () => controller.abort(params.signal?.reason);
|
|
426
|
+
params.signal?.addEventListener("abort", abort, { once: true });
|
|
427
|
+
let timer;
|
|
428
|
+
try {
|
|
429
|
+
if (!this.providerManager) throw new Error("Summary provider unavailable");
|
|
430
|
+
let messages;
|
|
431
|
+
try {
|
|
432
|
+
const fitted = fitContextCompactionSummaryInput({
|
|
433
|
+
maxInputTokens: params.maxInputTokens,
|
|
434
|
+
messages: params.messages,
|
|
435
|
+
sourceMaxChars: Math.min(512 * 1024, params.maxInputTokens * 3),
|
|
436
|
+
targetSummaryTokens: params.targetSummaryTokens
|
|
437
|
+
});
|
|
438
|
+
messages = fitted.messages;
|
|
439
|
+
sourceTruncated = fitted.sourceTruncated;
|
|
440
|
+
} catch (error) {
|
|
441
|
+
throw new UnusableCompactionSummary(error instanceof Error ? error.message : String(error));
|
|
442
|
+
}
|
|
443
|
+
inputTokens = estimateInputTokens(messages);
|
|
444
|
+
attemptCount = 1;
|
|
445
|
+
const timeout = new Promise((_, reject) => {
|
|
446
|
+
timer = setTimeout(() => {
|
|
447
|
+
const error = new UnusableCompactionSummary("Compaction summary deadline exceeded");
|
|
448
|
+
controller.abort(error);
|
|
449
|
+
reject(error);
|
|
450
|
+
}, SUMMARY_TIMEOUT_MS);
|
|
451
|
+
});
|
|
452
|
+
const cancelled = new Promise((_, reject) => {
|
|
453
|
+
controller.signal.addEventListener("abort", () => reject(controller.signal.reason), { once: true });
|
|
454
|
+
});
|
|
455
|
+
const response = await Promise.race([
|
|
456
|
+
this.providerManager.chat({
|
|
457
|
+
model: params.model,
|
|
458
|
+
maxTokens: params.maxTokens,
|
|
459
|
+
messages,
|
|
460
|
+
requestId: params.requestId,
|
|
461
|
+
sessionId: params.sessionId,
|
|
462
|
+
signal: controller.signal,
|
|
463
|
+
thinkingLevel: "off"
|
|
464
|
+
}),
|
|
465
|
+
timeout,
|
|
466
|
+
cancelled
|
|
467
|
+
]);
|
|
468
|
+
providerUsage = response.usage;
|
|
469
|
+
finishReason = response.finishReason;
|
|
470
|
+
summary = response.content ? normalizeContextCompactionSummary(response.content) : "";
|
|
471
|
+
rawSummaryTokens = summary ? estimateInputTokens(summary) : 0;
|
|
472
|
+
if (response.toolCalls?.length) throw new UnusableCompactionSummary("Unexpected summary tool calls");
|
|
473
|
+
if (!NATURAL_FINISH_REASONS.has(finishReason.trim().toLowerCase())) throw new UnusableCompactionSummary(`Incomplete summary: finishReason=${finishReason}`);
|
|
474
|
+
if (!summary) throw new UnusableCompactionSummary("Empty visible summary");
|
|
475
|
+
if (rawSummaryTokens > Math.min(params.targetSummaryTokens, params.maxInstallableSummaryTokens)) throw new UnusableCompactionSummary("Summary exceeds the installable budget");
|
|
476
|
+
} catch (error) {
|
|
477
|
+
params.signal?.throwIfAborted();
|
|
478
|
+
if (!(error instanceof UnusableCompactionSummary)) throw error;
|
|
479
|
+
failure = error instanceof Error ? error.message : String(error);
|
|
480
|
+
summary = COMPACTION_RECOVERY_NOTICE;
|
|
481
|
+
if (estimateInputTokens(summary) > params.maxInstallableSummaryTokens) throw new Error(`Recovery notice cannot fit the model input budget: ${failure}`);
|
|
482
|
+
} finally {
|
|
483
|
+
if (timer) clearTimeout(timer);
|
|
484
|
+
params.signal?.removeEventListener("abort", abort);
|
|
485
|
+
}
|
|
486
|
+
params.signal?.throwIfAborted();
|
|
487
|
+
return {
|
|
488
|
+
summary,
|
|
489
|
+
diagnostics: {
|
|
490
|
+
attemptCount,
|
|
491
|
+
degraded: failure !== void 0 || sourceTruncated,
|
|
492
|
+
finishReason,
|
|
493
|
+
sourceTruncated,
|
|
494
|
+
installedSummaryTokens: estimateInputTokens(summary),
|
|
495
|
+
providerInputTokens: inputTokens,
|
|
496
|
+
providerMaxOutputTokens: params.maxTokens,
|
|
497
|
+
providerUsage,
|
|
498
|
+
rawSummaryTokens,
|
|
499
|
+
recovery: failure ? "deterministic-recent-context" : "provider-summary",
|
|
500
|
+
targetSummaryTokens: params.targetSummaryTokens,
|
|
501
|
+
sourceBatchCount: 1,
|
|
502
|
+
...failure ? { failure } : {}
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
};
|
|
506
|
+
};
|
|
507
|
+
//#endregion
|
|
508
|
+
//#region src/features/context-compaction/utils/context-compaction.utils.ts
|
|
509
|
+
const NEXTCLAW_TIMELINE_KIND_METADATA_KEY = "nextclaw_timeline_kind";
|
|
510
|
+
const CONTEXT_COMPACTION_TIMELINE_KIND = "context_compaction";
|
|
511
|
+
const CONTEXT_COMPACTION_PROJECTION_METADATA_KEY = "nextclaw_context_projection";
|
|
512
|
+
const CONTEXT_COMPACTION_PROJECTION_KIND = "compressed_context";
|
|
513
|
+
const CONTEXT_COMPACTION_PART_START = "nextclaw_compaction_part_start";
|
|
514
|
+
const CONTEXT_COMPACTION_CONTINUATION_TEXT = "Continue the active run from the compressed working context. Do not repeat completed tool calls; proceed with the next required action.";
|
|
515
|
+
const CONTEXT_COMPACTION_SYSTEM_PREAMBLE = ["Compressed working context for this session. Original messages and recorded tool outcomes remain authoritative; verify uncertain constraints and effects using session history.", "Continue from this context and any following messages. Do not restart onboarding or treat missing profile fields as a new-session trigger unless the compressed context says onboarding is the active user task."].join("\n");
|
|
516
|
+
const CONTEXT_COMPACTION_RECOVERY_PREAMBLE = "Unresolved archived history: read original session history to verify old constraints and action outcomes. Only read-only recovery is permitted; ask a specific business clarification if required facts remain unknown.";
|
|
517
|
+
function readCheckpointTimelineText(checkpoint) {
|
|
518
|
+
if (checkpoint.status === "compressing") return "Compressing earlier context";
|
|
519
|
+
return checkpoint.status === "compressed" ? "Earlier context was auto-compacted" : "Context compaction failed";
|
|
520
|
+
}
|
|
521
|
+
function buildCompressedContextSystemText(checkpoint) {
|
|
522
|
+
return [
|
|
523
|
+
CONTEXT_COMPACTION_SYSTEM_PREAMBLE,
|
|
524
|
+
"",
|
|
525
|
+
!checkpoint.recovery && checkpoint.summary === COMPACTION_RECOVERY_NOTICE ? "Earlier conversation remains archived. Follow the latest human-confirmed task conditions and read original history for any older facts needed." : checkpoint.summary,
|
|
526
|
+
...checkpoint.recovery ? [CONTEXT_COMPACTION_RECOVERY_PREAMBLE] : []
|
|
527
|
+
].join("\n");
|
|
528
|
+
}
|
|
529
|
+
function readContextRecoveryState(messages, fallback) {
|
|
530
|
+
const checkpoint = readLatestContextCompactionCheckpoint(messages) ?? fallback;
|
|
531
|
+
const unresolvedArchive = messages.some((message) => message.metadata?.nextclaw_archived_body === true && (!checkpoint?.recovery || Date.parse(message.timestamp) > Date.parse(checkpoint.recovery.archiveThrough)));
|
|
532
|
+
if (!checkpoint?.recovery) return {
|
|
533
|
+
required: unresolvedArchive,
|
|
534
|
+
unresolvedArchive
|
|
535
|
+
};
|
|
536
|
+
const checkpointId = checkpoint.recovery.checkpointId ?? checkpoint.id;
|
|
537
|
+
return {
|
|
538
|
+
required: !messages.some((message) => message.role === "user" && message.metadata?.nextclaw_context_recovery_ack === checkpointId) || unresolvedArchive,
|
|
539
|
+
checkpointId,
|
|
540
|
+
unresolvedArchive
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
function readContextCompactionCheckpoint(message) {
|
|
544
|
+
const metadata = message.metadata;
|
|
545
|
+
return metadata?.["nextclaw_timeline_kind"] === "context_compaction" ? readCompressedContextCompactionCheckpoint$2(metadata.checkpoint) : null;
|
|
546
|
+
}
|
|
547
|
+
function readLatestContextCompactionMarker(sessionMessages) {
|
|
548
|
+
return sessionMessages.reduce((marker, message) => {
|
|
549
|
+
const candidateCheckpoint = readContextCompactionCheckpoint(message);
|
|
550
|
+
return candidateCheckpoint && (!marker || Date.parse(candidateCheckpoint.updatedAt) >= Date.parse(marker.checkpoint.updatedAt)) ? { checkpoint: candidateCheckpoint } : marker;
|
|
551
|
+
}, null);
|
|
552
|
+
}
|
|
553
|
+
function buildContextCompactionSummaryMessage(params) {
|
|
554
|
+
const { checkpoint, sessionId } = params;
|
|
555
|
+
return {
|
|
556
|
+
id: `${sessionId}:context-compaction-summary:${checkpoint.id}:${checkpoint.updatedAt}`,
|
|
557
|
+
sessionId,
|
|
558
|
+
role: "service",
|
|
559
|
+
status: "final",
|
|
560
|
+
timestamp: checkpoint.updatedAt,
|
|
561
|
+
parts: [{
|
|
562
|
+
type: "text",
|
|
563
|
+
text: buildCompressedContextSystemText(checkpoint)
|
|
564
|
+
}],
|
|
565
|
+
metadata: {
|
|
566
|
+
[CONTEXT_COMPACTION_PROJECTION_METADATA_KEY]: CONTEXT_COMPACTION_PROJECTION_KIND,
|
|
567
|
+
...checkpoint.recovery ? { nextclaw_context_recovery: checkpoint.recovery } : {}
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
function buildMidRunContinuationMessage(params) {
|
|
572
|
+
const { checkpoint, sessionId } = params;
|
|
573
|
+
return {
|
|
574
|
+
id: `${sessionId}:context-compaction-continuation:${checkpoint.id}:${checkpoint.updatedAt}`,
|
|
575
|
+
sessionId,
|
|
576
|
+
role: "user",
|
|
577
|
+
status: "final",
|
|
578
|
+
timestamp: checkpoint.updatedAt,
|
|
579
|
+
parts: [{
|
|
580
|
+
type: "text",
|
|
581
|
+
text: CONTEXT_COMPACTION_CONTINUATION_TEXT
|
|
582
|
+
}]
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
function sliceMessageParts(message, start, end) {
|
|
586
|
+
const base = Number(message.metadata?.["nextclaw_compaction_part_start"] ?? 0);
|
|
587
|
+
const physicalEnd = base + message.parts.length;
|
|
588
|
+
end ??= physicalEnd;
|
|
589
|
+
if (!Number.isInteger(start) || start < 0 || start > end || !Number.isInteger(end)) throw new Error("Invalid compacted message part boundary");
|
|
590
|
+
if (!message.metadata?.nextclaw_partial_projection && (start < base || end > physicalEnd)) throw new Error("Invalid compacted message part boundary");
|
|
591
|
+
const localStart = Math.max(0, start - base);
|
|
592
|
+
const localEnd = Math.min(message.parts.length, end - base);
|
|
593
|
+
const projected = {
|
|
594
|
+
...message,
|
|
595
|
+
parts: message.parts.slice(localStart, Math.max(localStart, localEnd)).map((part) => structuredClone(part)),
|
|
596
|
+
metadata: structuredClone(message.metadata ?? {})
|
|
597
|
+
};
|
|
598
|
+
projected.metadata = {
|
|
599
|
+
...projected.metadata,
|
|
600
|
+
[CONTEXT_COMPACTION_PART_START]: start
|
|
601
|
+
};
|
|
602
|
+
const offsets = projected.metadata?.[MODEL_ROUND_PART_OFFSETS];
|
|
603
|
+
if (Array.isArray(offsets)) projected.metadata = {
|
|
604
|
+
...projected.metadata,
|
|
605
|
+
[MODEL_ROUND_PART_OFFSETS]: offsets.filter((offset) => offset > localStart && offset < localEnd).map((offset) => offset - localStart)
|
|
606
|
+
};
|
|
607
|
+
return projected;
|
|
608
|
+
}
|
|
609
|
+
function projectMessageAfterCheckpoint(message, checkpoint) {
|
|
610
|
+
const retainedEnd = checkpoint.retainedMessagePartEnds?.[message.id];
|
|
611
|
+
const physicalEnd = Number(message.metadata?.["nextclaw_compaction_part_start"] ?? 0) + message.parts.length;
|
|
612
|
+
if (typeof retainedEnd === "number") return physicalEnd > retainedEnd ? sliceMessageParts(message, retainedEnd) : null;
|
|
613
|
+
const coveredPartCount = checkpoint.continuationMessageCoveredPartCount;
|
|
614
|
+
if (message.id === checkpoint.continuationMessageId && typeof coveredPartCount === "number" && Number.isInteger(coveredPartCount) && coveredPartCount >= 0) return physicalEnd > coveredPartCount ? sliceMessageParts(message, coveredPartCount) : null;
|
|
615
|
+
return Date.parse(message.timestamp) > Date.parse(readCheckpointCoveredUntil(checkpoint)) ? structuredClone(message) : null;
|
|
616
|
+
}
|
|
617
|
+
function readCheckpointMessageIds(value) {
|
|
618
|
+
return new Set(Array.isArray(value) ? value.filter((messageId) => typeof messageId === "string" && messageId.length > 0) : []);
|
|
619
|
+
}
|
|
620
|
+
function projectPreservedUserMessage(message, checkpoint) {
|
|
621
|
+
const truncated = checkpoint.truncatedPreservedUserMessage;
|
|
622
|
+
if (truncated?.messageId === message.id && typeof truncated.text === "string" && truncated.text.length > 0) return {
|
|
623
|
+
...structuredClone(message),
|
|
624
|
+
parts: [{
|
|
625
|
+
type: "text",
|
|
626
|
+
text: truncated.text
|
|
627
|
+
}]
|
|
628
|
+
};
|
|
629
|
+
return structuredClone(message);
|
|
630
|
+
}
|
|
631
|
+
function readCheckpointCoveredUntil(checkpoint) {
|
|
632
|
+
return checkpoint.coveredUntil ?? checkpoint.updatedAt;
|
|
633
|
+
}
|
|
634
|
+
function createContextCompactionMessageId() {
|
|
635
|
+
return `context-compaction-message-${crypto.randomUUID()}`;
|
|
636
|
+
}
|
|
637
|
+
function buildContextCompactionTimelineNcpMessage(params) {
|
|
638
|
+
const { checkpoint, messageId, sessionId } = params;
|
|
639
|
+
const text = readCheckpointTimelineText(checkpoint);
|
|
640
|
+
return {
|
|
641
|
+
id: messageId,
|
|
642
|
+
sessionId,
|
|
643
|
+
role: "service",
|
|
644
|
+
status: "final",
|
|
645
|
+
timestamp: checkpoint.updatedAt,
|
|
646
|
+
parts: [{
|
|
647
|
+
type: "text",
|
|
648
|
+
text
|
|
649
|
+
}],
|
|
650
|
+
metadata: {
|
|
651
|
+
[NEXTCLAW_TIMELINE_KIND_METADATA_KEY]: CONTEXT_COMPACTION_TIMELINE_KIND,
|
|
652
|
+
checkpoint
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
function isContextCompactionTimelineMessage(message) {
|
|
657
|
+
return message?.metadata?.[NEXTCLAW_TIMELINE_KIND_METADATA_KEY] === CONTEXT_COMPACTION_TIMELINE_KIND;
|
|
658
|
+
}
|
|
659
|
+
function isContextCompactionProjectionMessage(message) {
|
|
660
|
+
return message?.metadata?.[CONTEXT_COMPACTION_PROJECTION_METADATA_KEY] === CONTEXT_COMPACTION_PROJECTION_KIND;
|
|
661
|
+
}
|
|
662
|
+
function readLatestContextCompactionCheckpoint(sessionMessages) {
|
|
663
|
+
return readLatestContextCompactionMarker(sessionMessages)?.checkpoint ?? null;
|
|
664
|
+
}
|
|
665
|
+
function buildContextCompactionModelProjection(params) {
|
|
666
|
+
const { sessionId, sessionMessages } = params;
|
|
667
|
+
const marker = readLatestContextCompactionMarker(sessionMessages);
|
|
668
|
+
const regularMessages = sessionMessages.filter((message) => !readContextCompactionCheckpoint(message));
|
|
669
|
+
if (!marker) return {
|
|
670
|
+
messages: regularMessages.map((message) => structuredClone(message)),
|
|
671
|
+
stablePrefixMessageCount: 0
|
|
672
|
+
};
|
|
673
|
+
const { checkpoint: storedCheckpoint } = marker;
|
|
674
|
+
const checkpoint = readContextRecoveryState(sessionMessages).required ? storedCheckpoint : {
|
|
675
|
+
...storedCheckpoint,
|
|
676
|
+
recovery: void 0
|
|
677
|
+
};
|
|
678
|
+
const preservedUserMessageIds = readCheckpointMessageIds(checkpoint.preservedUserMessageIds);
|
|
679
|
+
const retainedMessageIds = readCheckpointMessageIds(checkpoint.retainedMessageIds);
|
|
680
|
+
const preservedUserMessages = regularMessages.filter((message) => message.role === "user" && preservedUserMessageIds.has(message.id)).map((message) => projectPreservedUserMessage(message, checkpoint)).sort((left, right) => Date.parse(left.timestamp) - Date.parse(right.timestamp));
|
|
681
|
+
const retainedMessages = regularMessages.filter((message) => retainedMessageIds.has(message.id)).map((message) => sliceMessageParts(message, checkpoint.retainedMessagePartStarts?.[message.id] ?? Number(message.metadata?.["nextclaw_compaction_part_start"] ?? 0), checkpoint.retainedMessagePartEnds?.[message.id] ?? Number(message.metadata?.["nextclaw_compaction_part_start"] ?? 0) + message.parts.length)).sort((left, right) => Date.parse(left.timestamp) - Date.parse(right.timestamp));
|
|
682
|
+
const projectedRegularMessages = regularMessages.filter((message) => !preservedUserMessageIds.has(message.id) && (!retainedMessageIds.has(message.id) || checkpoint.retainedMessagePartEnds?.[message.id] !== void 0)).map((message) => projectMessageAfterCheckpoint(message, checkpoint)).filter((message) => Boolean(message)).sort((left, right) => Date.parse(left.timestamp) - Date.parse(right.timestamp));
|
|
683
|
+
const stablePrefixMessages = [
|
|
684
|
+
buildContextCompactionSummaryMessage({
|
|
685
|
+
checkpoint,
|
|
686
|
+
sessionId
|
|
687
|
+
}),
|
|
688
|
+
...preservedUserMessages,
|
|
689
|
+
...checkpoint.phase === "mid-run" ? [buildMidRunContinuationMessage({
|
|
690
|
+
checkpoint,
|
|
691
|
+
sessionId
|
|
692
|
+
})] : [],
|
|
693
|
+
...retainedMessages
|
|
694
|
+
].map((message) => structuredClone(message));
|
|
695
|
+
return {
|
|
696
|
+
messages: [...stablePrefixMessages, ...projectedRegularMessages.map((message) => structuredClone(message))],
|
|
697
|
+
stablePrefixMessageCount: stablePrefixMessages.length
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
function readContextWindowEventSessionId(event) {
|
|
701
|
+
const payload = "payload" in event ? event.payload : null;
|
|
702
|
+
if (!payload || typeof payload !== "object") return null;
|
|
703
|
+
return "sessionId" in payload && typeof payload.sessionId === "string" ? payload.sessionId.trim() || null : null;
|
|
704
|
+
}
|
|
705
|
+
function isContextWindowSnapshot(value) {
|
|
706
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
707
|
+
}
|
|
708
|
+
function createContextWindowSignature(value) {
|
|
709
|
+
return JSON.stringify(value);
|
|
710
|
+
}
|
|
711
|
+
function shouldRefreshContextWindowDuringStream(event) {
|
|
712
|
+
switch (event.type) {
|
|
713
|
+
case NcpEventType.MessageTextStart:
|
|
714
|
+
case NcpEventType.MessageTextDelta:
|
|
715
|
+
case NcpEventType.MessageTextEnd:
|
|
716
|
+
case NcpEventType.MessageReasoningStart:
|
|
717
|
+
case NcpEventType.MessageReasoningDelta:
|
|
718
|
+
case NcpEventType.MessageReasoningEnd:
|
|
719
|
+
case NcpEventType.MessageToolCallStart:
|
|
720
|
+
case NcpEventType.MessageToolCallArgs:
|
|
721
|
+
case NcpEventType.MessageToolCallArgsDelta:
|
|
722
|
+
case NcpEventType.MessageToolCallEnd:
|
|
723
|
+
case NcpEventType.MessageToolCallResult: return true;
|
|
724
|
+
default: return false;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
function shouldRefreshContextWindowImmediately(event) {
|
|
728
|
+
switch (event.type) {
|
|
729
|
+
case NcpEventType.MessageSent:
|
|
730
|
+
case NcpEventType.MessageCompleted:
|
|
731
|
+
case NcpEventType.MessageFailed:
|
|
732
|
+
case NcpEventType.MessageAbort:
|
|
733
|
+
case NcpEventType.RunFinished:
|
|
734
|
+
case NcpEventType.RunError: return true;
|
|
735
|
+
default: return false;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
//#endregion
|
|
739
|
+
//#region src/features/context-compaction/services/context-compaction-preflight.service.ts
|
|
740
|
+
function toCompactionModelMessages(messages, assetStore) {
|
|
741
|
+
return messages.flatMap((message) => ncpMessageToOpenAiMessageGroups(message, { assetStore }).flatMap((group, index, groups) => group.messages.map((providerMessage) => ({
|
|
742
|
+
...providerMessage,
|
|
743
|
+
ncp_message_id: message.id,
|
|
744
|
+
ncp_part_start: group.partStart + Number(message.metadata?.["nextclaw_compaction_part_start"] ?? 0),
|
|
745
|
+
ncp_part_end: (groups[index + 1]?.partStart ?? message.parts.length) + Number(message.metadata?.["nextclaw_compaction_part_start"] ?? 0),
|
|
746
|
+
timestamp: message.timestamp
|
|
747
|
+
}))));
|
|
748
|
+
}
|
|
749
|
+
function estimateCompactionProjectionOverhead(phase) {
|
|
750
|
+
const summaryPlanOverhead = estimateInputTokens({
|
|
751
|
+
role: "user",
|
|
752
|
+
content: ""
|
|
753
|
+
});
|
|
754
|
+
const summaryProjectionOverhead = estimateInputTokens({
|
|
755
|
+
role: "system",
|
|
756
|
+
content: `${CONTEXT_COMPACTION_SYSTEM_PREAMBLE}\n\n${CONTEXT_COMPACTION_RECOVERY_PREAMBLE}\n\n`
|
|
757
|
+
});
|
|
758
|
+
const continuationTokens = phase === "mid-run" ? estimateInputTokens({
|
|
759
|
+
role: "user",
|
|
760
|
+
content: CONTEXT_COMPACTION_CONTINUATION_TEXT
|
|
761
|
+
}) : 0;
|
|
762
|
+
return Math.max(0, summaryProjectionOverhead - summaryPlanOverhead) + continuationTokens;
|
|
763
|
+
}
|
|
764
|
+
function mergeInputMessages(params) {
|
|
765
|
+
const messages = params.sessionMessages.map((message) => structuredClone(message));
|
|
766
|
+
const seen = new Set(messages.map((message) => message.id));
|
|
767
|
+
for (const message of params.inputMessages) {
|
|
768
|
+
if (seen.has(message.id)) continue;
|
|
769
|
+
messages.push(structuredClone(message));
|
|
770
|
+
}
|
|
771
|
+
return messages;
|
|
772
|
+
}
|
|
773
|
+
function readContinuationMessageBoundary(messages) {
|
|
774
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
775
|
+
const message = messages[index];
|
|
776
|
+
if (message?.role === "assistant" && (message.status === "pending" || message.status === "streaming")) return {
|
|
777
|
+
messageId: message.id,
|
|
778
|
+
coveredPartCount: Number(message.metadata?.["nextclaw_compaction_part_start"] ?? 0) + message.parts.length
|
|
779
|
+
};
|
|
780
|
+
}
|
|
781
|
+
return null;
|
|
782
|
+
}
|
|
783
|
+
function readPreRunContinuationBoundary(messages) {
|
|
784
|
+
const targetMessageId = messages.at(-1)?.metadata?.[CHAT_CONTINUATION_TARGET_MESSAGE_METADATA_KEY];
|
|
785
|
+
if (typeof targetMessageId !== "string" || !targetMessageId.trim()) return null;
|
|
786
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
787
|
+
const target = messages[index];
|
|
788
|
+
if (target?.id === targetMessageId && target.role === "assistant") return {
|
|
789
|
+
messageId: target.id,
|
|
790
|
+
coveredPartCount: Number(target.metadata?.["nextclaw_compaction_part_start"] ?? 0) + target.parts.length
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
return null;
|
|
794
|
+
}
|
|
795
|
+
function buildContextWindowSnapshotFromBudget(params) {
|
|
796
|
+
const { budget, checkpoint, completeInputBudget, fixedInputTokens, reservedContextTokens, totalContextTokens } = params;
|
|
797
|
+
return buildContextWindowSnapshot({
|
|
798
|
+
usedContextTokens: budget.estimatedTokens,
|
|
799
|
+
completeInputBudget,
|
|
800
|
+
totalContextTokens,
|
|
801
|
+
fixedInputTokens,
|
|
802
|
+
reservedContextTokens,
|
|
803
|
+
triggerContextTokens: budget.triggerTokens,
|
|
804
|
+
prunedUsedContextTokens: budget.estimatedTokens,
|
|
805
|
+
droppedHistoryCount: budget.droppedHistoryCount,
|
|
806
|
+
truncatedToolResultCount: budget.truncatedToolResultCount,
|
|
807
|
+
truncatedSystemPrompt: budget.truncatedSystemPrompt,
|
|
808
|
+
truncatedUserMessage: budget.truncatedUserMessage,
|
|
809
|
+
checkpoint,
|
|
810
|
+
compactedUsedContextTokens: checkpoint ? budget.estimatedTokens : void 0
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
var ContextCompactionPreflightService = class {
|
|
814
|
+
deferredAttempts = /* @__PURE__ */ new Map();
|
|
815
|
+
compactionService = new ContextCompactionService();
|
|
816
|
+
contextWindowBudgetService = new ContextWindowBudgetService$1();
|
|
817
|
+
summaryGenerationService;
|
|
818
|
+
constructor(agentManager, providerManager, assetStore = null) {
|
|
819
|
+
this.agentManager = agentManager;
|
|
820
|
+
this.assetStore = assetStore;
|
|
821
|
+
this.summaryGenerationService = new ContextCompactionSummaryGenerationService(providerManager);
|
|
822
|
+
}
|
|
823
|
+
preview = (params) => {
|
|
824
|
+
const { completeInputBudget = false, contextBlocks = [], fixedInputTokens, requestMetadata, sessionId, sessionMessages, storedAgentId, storedMetadata, tools = [] } = params;
|
|
825
|
+
const profile = this.resolveCompactionProfile({
|
|
826
|
+
requestMetadata,
|
|
827
|
+
storedAgentId
|
|
828
|
+
});
|
|
829
|
+
const existingCheckpoint = readCompressedContextCompactionCheckpoint$1(storedMetadata[CONTEXT_COMPACTION_METADATA_KEY$1]) ?? readLatestContextCompactionCheckpoint(sessionMessages);
|
|
830
|
+
const projectedMessages = existingCheckpoint ? buildContextCompactionModelProjection({
|
|
831
|
+
sessionId,
|
|
832
|
+
sessionMessages
|
|
833
|
+
}).messages : sessionMessages.filter((message) => !isContextCompactionTimelineMessage(message));
|
|
834
|
+
const contextBlockMessages = buildContextBlockInputMessages(contextBlocks);
|
|
835
|
+
const contextBlockInputTokens = estimateInputTokens(contextBlockMessages);
|
|
836
|
+
const toolInputTokens = estimateToolInputTokens(tools);
|
|
837
|
+
const snapshotFixedInputTokens = fixedInputTokens ?? contextBlockInputTokens + toolInputTokens;
|
|
838
|
+
const messages = [...fixedInputTokens === void 0 ? contextBlockMessages : [], ...toCompactionModelMessages(projectedMessages, this.assetStore)];
|
|
839
|
+
return buildContextWindowSnapshotFromBudget({
|
|
840
|
+
budget: this.contextWindowBudgetService.evaluate({
|
|
841
|
+
messages,
|
|
842
|
+
contextTokens: profile.contextTokens,
|
|
843
|
+
fixedInputTokens: fixedInputTokens ?? toolInputTokens,
|
|
844
|
+
reservedContextTokens: profile.reservedContextTokens
|
|
845
|
+
}),
|
|
846
|
+
checkpoint: existingCheckpoint,
|
|
847
|
+
completeInputBudget,
|
|
848
|
+
fixedInputTokens: snapshotFixedInputTokens,
|
|
849
|
+
reservedContextTokens: profile.reservedContextTokens,
|
|
850
|
+
totalContextTokens: profile.contextTokens
|
|
851
|
+
});
|
|
852
|
+
};
|
|
853
|
+
begin = (params) => {
|
|
854
|
+
const { contextBlocks = [], inputMessages, model, phase = "pre-run", requestMetadata, sessionId, sessionMessages, storedAgentId, storedMetadata, tools = [], trigger = "automatic" } = params;
|
|
855
|
+
const profile = this.resolveCompactionProfile({
|
|
856
|
+
requestMetadata,
|
|
857
|
+
storedAgentId
|
|
858
|
+
});
|
|
859
|
+
const contextTokens = profile.contextTokens;
|
|
860
|
+
const reservedContextTokens = Math.max(profile.reservedContextTokens, params.maxOutputTokens ?? 0);
|
|
861
|
+
const ncpMessages = mergeInputMessages({
|
|
862
|
+
inputMessages,
|
|
863
|
+
sessionMessages
|
|
864
|
+
});
|
|
865
|
+
const continuationMessageBoundary = phase === "mid-run" ? readContinuationMessageBoundary(ncpMessages) : readPreRunContinuationBoundary(ncpMessages);
|
|
866
|
+
const existingCheckpoint = readCompressedContextCompactionCheckpoint$1(storedMetadata[CONTEXT_COMPACTION_METADATA_KEY$1]) ?? readLatestContextCompactionCheckpoint(ncpMessages);
|
|
867
|
+
const projectedMessages = existingCheckpoint ? buildContextCompactionModelProjection({
|
|
868
|
+
sessionId,
|
|
869
|
+
sessionMessages: ncpMessages
|
|
870
|
+
}).messages : ncpMessages.filter((message) => !isContextCompactionTimelineMessage(message));
|
|
871
|
+
const contextBlockMessages = buildContextBlockInputMessages(contextBlocks);
|
|
872
|
+
const messages = [...contextBlockMessages, ...toCompactionModelMessages(projectedMessages, this.assetStore)];
|
|
873
|
+
const preservableUserMessageIds = ncpMessages.flatMap((message) => message.role === "user" ? [message.id] : []);
|
|
874
|
+
const fixedInputTokens = estimateToolInputTokens(tools);
|
|
875
|
+
const snapshotFixedInputTokens = estimateInputTokens(contextBlockMessages) + fixedInputTokens;
|
|
876
|
+
const budget = this.contextWindowBudgetService.evaluate({
|
|
877
|
+
messages,
|
|
878
|
+
contextTokens,
|
|
879
|
+
fixedInputTokens,
|
|
880
|
+
reservedContextTokens
|
|
881
|
+
});
|
|
882
|
+
const hardInputLimit = contextTokens - reservedContextTokens;
|
|
883
|
+
const structuralPressure = readContextRecoveryState(ncpMessages, existingCheckpoint).unresolvedArchive || ncpMessages.length > 256 || ncpMessages.reduce((count, message) => count + message.parts.length, 0) > 1024;
|
|
884
|
+
const deferred = this.deferredAttempts.get(sessionId);
|
|
885
|
+
const retryGrowth = Math.max(1, Math.floor((hardInputLimit - budget.triggerTokens) / 2));
|
|
886
|
+
const throttled = !params.overflow && trigger === "automatic" && budget.estimatedTokens <= hardInputLimit && deferred?.model === model && budget.estimatedTokens < deferred.tokens + retryGrowth;
|
|
887
|
+
const plan = trigger === "automatic" && (!budget.shouldCompact && !params.overflow && !structuralPressure || throttled && !structuralPressure) ? null : this.compactionService.prepareForModelInput({
|
|
888
|
+
messages,
|
|
889
|
+
coverLeadingSystemMessage: contextBlockMessages.length === 0 && isContextCompactionProjectionMessage(projectedMessages[0]),
|
|
890
|
+
contextTokens,
|
|
891
|
+
compactionThresholdTokens: trigger === "manual" || params.overflow || structuralPressure ? 0 : budget.triggerTokens,
|
|
892
|
+
fixedInputTokens,
|
|
893
|
+
preservableUserMessageIds,
|
|
894
|
+
projectedTokenLimit: Math.max(1, Math.min(hardInputLimit, params.overflow ? Math.floor(budget.estimatedTokens * .7) : Math.max(budget.triggerTokens, snapshotFixedInputTokens + 1024)) - estimateCompactionProjectionOverhead(phase)),
|
|
895
|
+
retainLatestMessage: phase === "pre-run",
|
|
896
|
+
retainRecentTokens: 2e4
|
|
897
|
+
});
|
|
898
|
+
const coveredSessionMessageCount = plan ? (existingCheckpoint?.coveredSessionMessageCount ?? 0) + plan.coveredMessages.length - (existingCheckpoint ? 1 : 0) : 0;
|
|
899
|
+
const serviceMessageId = createContextCompactionMessageId();
|
|
900
|
+
const checkpoint = plan ? {
|
|
901
|
+
...buildCompressingCompactionCheckpoint(storedMetadata[CONTEXT_COMPACTION_METADATA_KEY$1]),
|
|
902
|
+
phase,
|
|
903
|
+
continuationMessageId: continuationMessageBoundary?.messageId,
|
|
904
|
+
continuationMessageCoveredPartCount: continuationMessageBoundary?.coveredPartCount,
|
|
905
|
+
coveredMessageCount: coveredSessionMessageCount,
|
|
906
|
+
coveredSessionMessageCount,
|
|
907
|
+
originalEstimatedTokens: plan.originalEstimatedTokens,
|
|
908
|
+
projectedEstimatedTokens: budget.estimatedTokens
|
|
909
|
+
} : existingCheckpoint;
|
|
910
|
+
return {
|
|
911
|
+
contextWindow: buildContextWindowSnapshotFromBudget({
|
|
912
|
+
budget,
|
|
913
|
+
checkpoint,
|
|
914
|
+
completeInputBudget: true,
|
|
915
|
+
fixedInputTokens: snapshotFixedInputTokens,
|
|
916
|
+
reservedContextTokens,
|
|
917
|
+
totalContextTokens: contextTokens
|
|
918
|
+
}),
|
|
919
|
+
sessionMessages: ncpMessages,
|
|
920
|
+
pendingCompaction: plan && checkpoint ? {
|
|
921
|
+
originalBudget: budget,
|
|
922
|
+
overflow: params.overflow === true,
|
|
923
|
+
priorCheckpoint: existingCheckpoint,
|
|
924
|
+
checkpoint,
|
|
925
|
+
contextBlockMessages,
|
|
926
|
+
continuationMessageBoundary,
|
|
927
|
+
contextTokens,
|
|
928
|
+
phase,
|
|
929
|
+
serviceMessageId,
|
|
930
|
+
model,
|
|
931
|
+
plan,
|
|
932
|
+
reservedContextTokens,
|
|
933
|
+
snapshotFixedInputTokens,
|
|
934
|
+
sessionId,
|
|
935
|
+
sessionMessages: ncpMessages
|
|
936
|
+
} : null
|
|
937
|
+
};
|
|
938
|
+
};
|
|
939
|
+
finish = async (pending, signal) => {
|
|
940
|
+
let summaryDiagnostics;
|
|
941
|
+
const generatedCheckpoint = (await this.compactionService.compactPreparedForModelInput({
|
|
942
|
+
contextTokens: pending.contextTokens,
|
|
943
|
+
plan: pending.plan,
|
|
944
|
+
generateSummary: async ({ maxInputTokens, maxInstallableSummaryTokens, maxTokens, messages, targetSummaryTokens }) => {
|
|
945
|
+
const generated = await this.summaryGenerationService.generate({
|
|
946
|
+
maxInputTokens,
|
|
947
|
+
maxInstallableSummaryTokens,
|
|
948
|
+
maxTokens,
|
|
949
|
+
messages,
|
|
950
|
+
model: pending.model,
|
|
951
|
+
requestId: pending.serviceMessageId,
|
|
952
|
+
sessionId: pending.sessionId,
|
|
953
|
+
signal,
|
|
954
|
+
targetSummaryTokens
|
|
955
|
+
});
|
|
956
|
+
summaryDiagnostics = generated.diagnostics;
|
|
957
|
+
return generated.summary;
|
|
958
|
+
}
|
|
959
|
+
})).checkpoint;
|
|
960
|
+
if (!generatedCheckpoint) throw new Error("context compaction pending work did not produce a checkpoint");
|
|
961
|
+
signal?.throwIfAborted();
|
|
962
|
+
if ((summaryDiagnostics?.recovery === "deterministic-recent-context" || summaryDiagnostics?.sourceTruncated) && !pending.overflow && !pending.sessionMessages.some((message) => message.metadata?.nextclaw_archived_body) && pending.originalBudget.estimatedTokens <= pending.contextTokens - pending.reservedContextTokens) {
|
|
963
|
+
this.deferredAttempts.delete(pending.sessionId);
|
|
964
|
+
this.deferredAttempts.set(pending.sessionId, {
|
|
965
|
+
model: pending.model,
|
|
966
|
+
tokens: pending.originalBudget.estimatedTokens
|
|
967
|
+
});
|
|
968
|
+
if (this.deferredAttempts.size > 512) this.deferredAttempts.delete(this.deferredAttempts.keys().next().value);
|
|
969
|
+
return {
|
|
970
|
+
contextWindow: buildContextWindowSnapshotFromBudget({
|
|
971
|
+
budget: pending.originalBudget,
|
|
972
|
+
checkpoint: pending.priorCheckpoint,
|
|
973
|
+
completeInputBudget: true,
|
|
974
|
+
fixedInputTokens: pending.snapshotFixedInputTokens,
|
|
975
|
+
reservedContextTokens: pending.reservedContextTokens,
|
|
976
|
+
totalContextTokens: pending.contextTokens
|
|
977
|
+
}),
|
|
978
|
+
sessionMessages: pending.sessionMessages,
|
|
979
|
+
timelineMessage: buildContextCompactionTimelineNcpMessage({
|
|
980
|
+
messageId: pending.serviceMessageId,
|
|
981
|
+
sessionId: pending.sessionId,
|
|
982
|
+
checkpoint: {
|
|
983
|
+
...pending.checkpoint,
|
|
984
|
+
status: "failed",
|
|
985
|
+
summaryDiagnostics,
|
|
986
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
987
|
+
}
|
|
988
|
+
})
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
this.deferredAttempts.delete(pending.sessionId);
|
|
992
|
+
const checkpoint = {
|
|
993
|
+
...generatedCheckpoint,
|
|
994
|
+
id: pending.checkpoint.id,
|
|
995
|
+
createdAt: pending.checkpoint.createdAt,
|
|
996
|
+
phase: pending.phase,
|
|
997
|
+
continuationMessageId: pending.continuationMessageBoundary?.messageId,
|
|
998
|
+
continuationMessageCoveredPartCount: pending.continuationMessageBoundary?.coveredPartCount,
|
|
999
|
+
coveredMessageCount: pending.checkpoint.coveredMessageCount,
|
|
1000
|
+
coveredSessionMessageCount: pending.checkpoint.coveredSessionMessageCount,
|
|
1001
|
+
summaryDiagnostics,
|
|
1002
|
+
status: "compressed",
|
|
1003
|
+
...summaryDiagnostics?.recovery === "deterministic-recent-context" || summaryDiagnostics?.sourceTruncated || readContextRecoveryState(pending.sessionMessages, pending.priorCheckpoint).required ? { recovery: {
|
|
1004
|
+
required: true,
|
|
1005
|
+
reason: summaryDiagnostics?.recovery === "deterministic-recent-context" ? "summary-unavailable" : "unresolved-archive",
|
|
1006
|
+
archiveThrough: pending.sessionMessages.reduce((latest, message) => Date.parse(message.timestamp) > Date.parse(latest) ? message.timestamp : latest, generatedCheckpoint.coveredUntil ?? generatedCheckpoint.updatedAt),
|
|
1007
|
+
checkpointId: summaryDiagnostics?.recovery === "deterministic-recent-context" || summaryDiagnostics?.sourceTruncated || readContextRecoveryState(pending.sessionMessages, pending.priorCheckpoint).unresolvedArchive ? pending.checkpoint.id : pending.priorCheckpoint?.recovery?.checkpointId ?? pending.priorCheckpoint?.id ?? pending.checkpoint.id
|
|
1008
|
+
} } : {}
|
|
1009
|
+
};
|
|
1010
|
+
const timelineMessage = buildContextCompactionTimelineNcpMessage({
|
|
1011
|
+
messageId: pending.serviceMessageId,
|
|
1012
|
+
sessionId: pending.sessionId,
|
|
1013
|
+
checkpoint
|
|
1014
|
+
});
|
|
1015
|
+
const projectedMessages = buildContextCompactionModelProjection({
|
|
1016
|
+
sessionId: pending.sessionId,
|
|
1017
|
+
sessionMessages: [...pending.sessionMessages, timelineMessage]
|
|
1018
|
+
}).messages;
|
|
1019
|
+
const budget = this.contextWindowBudgetService.evaluate({
|
|
1020
|
+
messages: [...pending.contextBlockMessages, ...toCompactionModelMessages(projectedMessages, this.assetStore)],
|
|
1021
|
+
contextTokens: pending.contextTokens,
|
|
1022
|
+
fixedInputTokens: pending.plan.fixedInputTokens,
|
|
1023
|
+
reservedContextTokens: pending.reservedContextTokens
|
|
1024
|
+
});
|
|
1025
|
+
if (budget.estimatedTokens > pending.contextTokens - pending.reservedContextTokens) throw new Error(`Context compaction checkpoint does not fit the final provider input surface: ${budget.estimatedTokens} estimated tokens exceeds ${budget.triggerTokens}. The previous checkpoint was kept unchanged.`);
|
|
1026
|
+
return {
|
|
1027
|
+
contextWindow: buildContextWindowSnapshotFromBudget({
|
|
1028
|
+
budget,
|
|
1029
|
+
checkpoint,
|
|
1030
|
+
completeInputBudget: true,
|
|
1031
|
+
fixedInputTokens: pending.snapshotFixedInputTokens,
|
|
1032
|
+
reservedContextTokens: pending.reservedContextTokens,
|
|
1033
|
+
totalContextTokens: pending.contextTokens
|
|
1034
|
+
}),
|
|
1035
|
+
sessionMessages: pending.sessionMessages,
|
|
1036
|
+
timelineMessage
|
|
1037
|
+
};
|
|
1038
|
+
};
|
|
1039
|
+
resolveCompactionProfile = (params) => {
|
|
1040
|
+
const profile = this.agentManager.resolveAgentProfileForRun({
|
|
1041
|
+
requestMetadata: params.requestMetadata,
|
|
1042
|
+
storedAgentId: params.storedAgentId
|
|
1043
|
+
});
|
|
1044
|
+
return {
|
|
1045
|
+
contextTokens: profile.contextTokens,
|
|
1046
|
+
reservedContextTokens: profile.reservedContextTokens
|
|
1047
|
+
};
|
|
1048
|
+
};
|
|
1049
|
+
};
|
|
1050
|
+
//#endregion
|
|
1051
|
+
//#region src/utils/ncp-agent-session-replay-event.utils.ts
|
|
1052
|
+
var SessionReplaySupersessionTracker = class {
|
|
1053
|
+
interrupted = [];
|
|
1054
|
+
lastContinuationByMessageId = /* @__PURE__ */ new Map();
|
|
1055
|
+
lastContinuationByRunId = /* @__PURE__ */ new Map();
|
|
1056
|
+
toolOwners = /* @__PURE__ */ new Map();
|
|
1057
|
+
observe = (event, index) => {
|
|
1058
|
+
const messageId = readEventMessageId(event);
|
|
1059
|
+
const runId = readEventRunId(event);
|
|
1060
|
+
const toolCallId = readEventToolCallId(event);
|
|
1061
|
+
if (toolCallId && messageId) this.toolOwners.set(toolCallId, messageId);
|
|
1062
|
+
if (event.type === NcpEventType.RunError && event.payload.interrupted === true) this.interrupted.push({
|
|
1063
|
+
index,
|
|
1064
|
+
messageId,
|
|
1065
|
+
runId
|
|
1066
|
+
});
|
|
1067
|
+
if (!isReplayContinuationEvent(event)) return;
|
|
1068
|
+
const ownerMessageId = messageId ?? (toolCallId ? this.toolOwners.get(toolCallId) ?? null : null);
|
|
1069
|
+
if (ownerMessageId) this.lastContinuationByMessageId.set(ownerMessageId, index);
|
|
1070
|
+
if (runId) this.lastContinuationByRunId.set(runId, index);
|
|
1071
|
+
};
|
|
1072
|
+
finish = () => new Set(this.interrupted.filter(({ index, messageId, runId }) => (messageId ? (this.lastContinuationByMessageId.get(messageId) ?? -1) > index : false) || (runId ? (this.lastContinuationByRunId.get(runId) ?? -1) > index : false)).map(({ index }) => index));
|
|
1073
|
+
};
|
|
1074
|
+
function isReplayContinuationEvent(event) {
|
|
1075
|
+
if (isJournalOnlyEvent(event)) return false;
|
|
1076
|
+
if (event.type === "session.snapshot.message") return true;
|
|
1077
|
+
return readStreamingMessageId(event) !== null || event.type === NcpEventType.MessageCompleted || event.type === NcpEventType.RunFinished;
|
|
1078
|
+
}
|
|
1079
|
+
function isStreamingMessageCreationEvent(event) {
|
|
1080
|
+
return event.type === NcpEventType.MessageTextStart || event.type === NcpEventType.MessageReasoningStart;
|
|
1081
|
+
}
|
|
1082
|
+
function readEventMessageId(event) {
|
|
1083
|
+
if (isJournalOnlyEvent(event)) return null;
|
|
1084
|
+
const message = readMessageFromSummaryEvent$1(event);
|
|
1085
|
+
if (message?.id) return message.id;
|
|
1086
|
+
if (event.type === "session.snapshot.message") return null;
|
|
1087
|
+
const streamingMessageId = readStreamingMessageId(event);
|
|
1088
|
+
if (streamingMessageId) return streamingMessageId;
|
|
1089
|
+
const messageId = ("payload" in event && isRecord$1(event.payload) ? event.payload : null)?.messageId;
|
|
1090
|
+
if (typeof messageId === "string" && messageId.trim()) return messageId.trim();
|
|
1091
|
+
return null;
|
|
1092
|
+
}
|
|
1093
|
+
function readEventRunId(event) {
|
|
1094
|
+
if (isJournalOnlyEvent(event)) return null;
|
|
1095
|
+
if (event.type === "session.snapshot.message") return null;
|
|
1096
|
+
const runId = ("payload" in event && isRecord$1(event.payload) ? event.payload : null)?.runId;
|
|
1097
|
+
if (typeof runId === "string" && runId.trim()) return runId.trim();
|
|
1098
|
+
return null;
|
|
1099
|
+
}
|
|
1100
|
+
function readEventToolCallId(event) {
|
|
1101
|
+
if (isJournalOnlyEvent(event)) return null;
|
|
1102
|
+
const toolCallId = ("payload" in event && isRecord$1(event.payload) ? event.payload : null)?.toolCallId;
|
|
1103
|
+
return typeof toolCallId === "string" && toolCallId.trim() ? toolCallId.trim() : null;
|
|
1104
|
+
}
|
|
1105
|
+
function createReplayEvent(event, toolResultsByCallId) {
|
|
1106
|
+
const replayEvent = structuredClone(event);
|
|
1107
|
+
const occurredAt = readReplayEventOccurredAt(replayEvent);
|
|
1108
|
+
const replayMessage = readMessageFromSummaryEvent$1(replayEvent);
|
|
1109
|
+
const legacyCompactionMessageId = readLegacyContextCompactionMessageId(replayMessage);
|
|
1110
|
+
if (replayMessage && legacyCompactionMessageId) replayMessage.id = legacyCompactionMessageId;
|
|
1111
|
+
if (replayMessage && replayEvent.type === NcpEventType.MessageCompleted) replayMessage.status = "final";
|
|
1112
|
+
if (replayEvent.type === "session.snapshot.message" || replayEvent.type === NcpEventType.MessageCompleted) {
|
|
1113
|
+
replayEvent.payload.message = mergeReplayCompletedToolResults(replayEvent.payload.message, toolResultsByCallId);
|
|
1114
|
+
return {
|
|
1115
|
+
occurredAt,
|
|
1116
|
+
type: NcpEventType.MessageSent,
|
|
1117
|
+
payload: replayEvent.payload
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
return replayEvent;
|
|
1121
|
+
}
|
|
1122
|
+
function readReplayEventOccurredAt(event) {
|
|
1123
|
+
if (!("occurredAt" in event) || typeof event.occurredAt !== "string") return;
|
|
1124
|
+
return event.occurredAt;
|
|
1125
|
+
}
|
|
1126
|
+
function mergeReplayCompletedToolResults(message, toolResultsByCallId) {
|
|
1127
|
+
let changed = false;
|
|
1128
|
+
const parts = message.parts.map((part) => {
|
|
1129
|
+
if (part.type !== "tool-invocation" || !part.toolCallId) return part;
|
|
1130
|
+
const result = toolResultsByCallId.get(part.toolCallId);
|
|
1131
|
+
if (!result) return part;
|
|
1132
|
+
changed = true;
|
|
1133
|
+
return {
|
|
1134
|
+
...part,
|
|
1135
|
+
state: "result",
|
|
1136
|
+
result: result.content,
|
|
1137
|
+
...result.contentItems ? { resultContentItems: result.contentItems } : {},
|
|
1138
|
+
...result.execution ? { execution: mergeToolExecutionTiming(result.execution, part.execution) ?? result.execution } : {}
|
|
1139
|
+
};
|
|
1140
|
+
});
|
|
1141
|
+
return changed ? {
|
|
1142
|
+
...message,
|
|
1143
|
+
parts
|
|
1144
|
+
} : message;
|
|
1145
|
+
}
|
|
1146
|
+
function readLegacyContextCompactionMessageId(message) {
|
|
1147
|
+
const checkpoint = isRecord$1(message?.metadata?.checkpoint) ? message.metadata.checkpoint : null;
|
|
1148
|
+
const checkpointId = typeof checkpoint?.id === "string" ? checkpoint.id : "";
|
|
1149
|
+
const coveredCount = checkpoint?.coveredSessionMessageCount;
|
|
1150
|
+
const legacyId = `${message?.sessionId}:service:context-compaction:${checkpointId}`;
|
|
1151
|
+
return typeof coveredCount === "number" && message?.id === legacyId ? `${legacyId}:${coveredCount}` : null;
|
|
1152
|
+
}
|
|
1153
|
+
function createReplayStreamingBootstrapEvent(event, knownMessageIds) {
|
|
1154
|
+
const messageId = readStreamingMessageId(event);
|
|
1155
|
+
if (!messageId || knownMessageIds.has(messageId)) return null;
|
|
1156
|
+
return {
|
|
1157
|
+
messageId,
|
|
1158
|
+
event: {
|
|
1159
|
+
occurredAt: event.occurredAt,
|
|
1160
|
+
type: NcpEventType.MessageSent,
|
|
1161
|
+
payload: {
|
|
1162
|
+
sessionId: readEventSessionId(event),
|
|
1163
|
+
message: {
|
|
1164
|
+
id: messageId,
|
|
1165
|
+
sessionId: readEventSessionId(event),
|
|
1166
|
+
role: "assistant",
|
|
1167
|
+
status: "streaming",
|
|
1168
|
+
parts: [],
|
|
1169
|
+
timestamp: readReplayPayloadTimestamp(event) ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
};
|
|
1174
|
+
}
|
|
1175
|
+
function readReplayMessageId(event) {
|
|
1176
|
+
return readMessageFromSummaryEvent$1(event)?.id ?? null;
|
|
1177
|
+
}
|
|
1178
|
+
function readEventSessionId(event) {
|
|
1179
|
+
const sessionId = ("payload" in event && isRecord$1(event.payload) ? event.payload : null)?.sessionId;
|
|
1180
|
+
return typeof sessionId === "string" ? sessionId : "";
|
|
1181
|
+
}
|
|
1182
|
+
function readReplayPayloadTimestamp(event) {
|
|
1183
|
+
const payload = "payload" in event && isRecord$1(event.payload) ? event.payload : null;
|
|
1184
|
+
const timestamp = typeof payload?.timestamp === "string" ? payload.timestamp : "";
|
|
1185
|
+
return Number.isFinite(Date.parse(timestamp)) ? new Date(timestamp).toISOString() : null;
|
|
1186
|
+
}
|
|
1187
|
+
function readStreamingMessageId(event) {
|
|
1188
|
+
switch (event.type) {
|
|
1189
|
+
case NcpEventType.MessageTextStart:
|
|
1190
|
+
case NcpEventType.MessageTextDelta:
|
|
1191
|
+
case NcpEventType.MessageTextEnd:
|
|
1192
|
+
case NcpEventType.MessageReasoningStart:
|
|
1193
|
+
case NcpEventType.MessageReasoningDelta:
|
|
1194
|
+
case NcpEventType.MessageReasoningEnd:
|
|
1195
|
+
case NcpEventType.MessageToolCallStart:
|
|
1196
|
+
case NcpEventType.MessageToolCallArgsDelta:
|
|
1197
|
+
case NcpEventType.MessageToolExecutionStarted: return event.payload.messageId?.trim() || null;
|
|
1198
|
+
default: return null;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
function isJournalOnlyEvent(event) {
|
|
1202
|
+
return event.type === "session.request.accepted" || event.type === "session.request.completed" || event.type === "session.request.failed";
|
|
1203
|
+
}
|
|
1204
|
+
function isRecord$1(value) {
|
|
1205
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
1206
|
+
}
|
|
1207
|
+
function readMessageFromSummaryEvent$1(event) {
|
|
1208
|
+
if (event.type === NcpEventType.MessageSent || event.type === NcpEventType.MessageCompleted || event.type === "session.snapshot.message") return event.payload.message;
|
|
1209
|
+
}
|
|
1210
|
+
//#endregion
|
|
1211
|
+
//#region src/utils/ncp-agent-session-replay-tool-ownership.utils.ts
|
|
1212
|
+
function seedReplayToolOwners(messages) {
|
|
1213
|
+
const owners = /* @__PURE__ */ new Map();
|
|
1214
|
+
for (const message of messages) for (const part of message.parts) if (part.type === "tool-invocation" && part.toolCallId) owners.set(part.toolCallId, message.id);
|
|
1215
|
+
return owners;
|
|
1216
|
+
}
|
|
1217
|
+
function readReplayToolOwners(event) {
|
|
1218
|
+
const message = readMessageFromSummaryEvent$1(event);
|
|
1219
|
+
const owners = seedReplayToolOwners(message ? [message] : []);
|
|
1220
|
+
if (event.type === NcpEventType.MessageToolCallStart && event.payload.messageId) owners.set(event.payload.toolCallId, event.payload.messageId);
|
|
1221
|
+
return owners;
|
|
1222
|
+
}
|
|
1223
|
+
//#endregion
|
|
1224
|
+
//#region src/utils/ncp-agent-session-replay.utils.ts
|
|
1225
|
+
function mergePartialSessionMessage(message, original) {
|
|
1226
|
+
if (message.role !== "assistant" || message.metadata?.nextclaw_partial_projection !== true) return message;
|
|
1227
|
+
if (!original) throw new Error("Partial session projection has no original message to preserve.");
|
|
1228
|
+
const source = message.metadata.nextclaw_source;
|
|
1229
|
+
const indices = source?.partIndices ?? [];
|
|
1230
|
+
const incoming = message.metadata[MODEL_ROUND_PART_OFFSETS];
|
|
1231
|
+
const old = original.metadata?.[MODEL_ROUND_PART_OFFSETS];
|
|
1232
|
+
const mapped = Array.isArray(incoming) ? incoming.filter(Number.isInteger).map((offset) => indices[offset] ?? (source?.partCount ?? original.parts.length) + offset - indices.length) : [];
|
|
1233
|
+
const metadata = {
|
|
1234
|
+
...original.metadata,
|
|
1235
|
+
...message.metadata,
|
|
1236
|
+
[MODEL_ROUND_PART_OFFSETS]: [...new Set([...Array.isArray(old) ? old.filter(Number.isInteger) : [], ...mapped])].sort((left, right) => left - right)
|
|
1237
|
+
};
|
|
1238
|
+
delete metadata.nextclaw_partial_projection;
|
|
1239
|
+
delete metadata.nextclaw_source;
|
|
1240
|
+
delete metadata.nextclaw_compaction_part_start;
|
|
1241
|
+
delete metadata.nextclaw_archived_body;
|
|
1242
|
+
return {
|
|
1243
|
+
...message,
|
|
1244
|
+
parts: original.parts,
|
|
1245
|
+
metadata
|
|
1246
|
+
};
|
|
1247
|
+
}
|
|
1248
|
+
var ReplayContext = class {
|
|
1249
|
+
knownMessageIds;
|
|
1250
|
+
terminalMessageIds;
|
|
1251
|
+
toolResultsByCallId = /* @__PURE__ */ new Map();
|
|
1252
|
+
compactionRecovery = new ContextCompactionJournalRecoveryService();
|
|
1253
|
+
activeTailRunIds = /* @__PURE__ */ new Set();
|
|
1254
|
+
toolOwners;
|
|
1255
|
+
constructor(stateManager, seeds) {
|
|
1256
|
+
this.stateManager = stateManager;
|
|
1257
|
+
this.knownMessageIds = new Set(seeds.map((message) => message.id));
|
|
1258
|
+
this.terminalMessageIds = new Set(seeds.filter((message) => message.role === "assistant" && (message.status === "final" || message.status === "error")).map((message) => message.id));
|
|
1259
|
+
this.toolOwners = seedReplayToolOwners(seeds);
|
|
1260
|
+
this.compactionRecovery.seed(seeds);
|
|
1261
|
+
}
|
|
1262
|
+
recordEvent = (event, activeMessageId) => {
|
|
1263
|
+
for (const [callId, messageId] of readReplayToolOwners(event)) this.toolOwners.set(callId, messageId);
|
|
1264
|
+
const message = readMessageFromSummaryEvent$1(event);
|
|
1265
|
+
if (message?.role === "assistant" && (message.status === "final" || message.status === "error")) this.terminalMessageIds.add(message.id);
|
|
1266
|
+
if (event.type === NcpEventType.RunError || event.type === NcpEventType.RunFinished || event.type === NcpEventType.MessageAbort) {
|
|
1267
|
+
const messageId = readEventMessageId(event) ?? activeMessageId;
|
|
1268
|
+
if (messageId) this.terminalMessageIds.add(messageId);
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
};
|
|
1272
|
+
var SessionEventReplayer = class SessionEventReplayer {
|
|
1273
|
+
constructor(context, allowUnknownStreamingBootstrap) {
|
|
1274
|
+
this.context = context;
|
|
1275
|
+
this.allowUnknownStreamingBootstrap = allowUnknownStreamingBootstrap;
|
|
1276
|
+
}
|
|
1277
|
+
static create = async (seedMessages = [], activeMessageId, allowUnknownStreamingBootstrap = true) => new SessionEventReplayer(await createReplayContext(seedMessages, activeMessageId), allowUnknownStreamingBootstrap);
|
|
1278
|
+
append = async (event, eventIndex, supersededSyntheticRecoveryIndexes) => {
|
|
1279
|
+
if (isJournalOnlyEvent(event)) return;
|
|
1280
|
+
let sourceEvent = event;
|
|
1281
|
+
const snapshot = readMessageFromSummaryEvent$1(event);
|
|
1282
|
+
if (snapshot?.metadata?.nextclaw_partial_projection === true) {
|
|
1283
|
+
const state = this.context.stateManager.getSnapshot();
|
|
1284
|
+
const original = state.streamingMessage?.id === snapshot.id ? state.streamingMessage : state.messages.find((message) => message.id === snapshot.id) ?? null;
|
|
1285
|
+
sourceEvent = {
|
|
1286
|
+
...event,
|
|
1287
|
+
payload: {
|
|
1288
|
+
...event.payload,
|
|
1289
|
+
message: mergePartialSessionMessage(snapshot, original)
|
|
1290
|
+
}
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
const replayEvent = createReplayEvent(sourceEvent, this.context.toolResultsByCallId);
|
|
1294
|
+
updateActiveTailRunIds(this.context.activeTailRunIds, replayEvent, eventIndex);
|
|
1295
|
+
if (supersededSyntheticRecoveryIndexes.has(eventIndex)) return;
|
|
1296
|
+
await replayJournalEvent(this.context, replayEvent, this.allowUnknownStreamingBootstrap);
|
|
1297
|
+
};
|
|
1298
|
+
finish = () => {
|
|
1299
|
+
const snapshot = this.context.stateManager.getSnapshot();
|
|
1300
|
+
return (snapshot.streamingMessage ? insertMessageByTimeline(snapshot.messages, snapshot.streamingMessage) : snapshot.messages).map(this.context.compactionRecovery.terminalize);
|
|
1301
|
+
};
|
|
1302
|
+
};
|
|
1303
|
+
async function createReplayContext(seedMessages, activeMessageId) {
|
|
1304
|
+
const stateManager = new DefaultNcpAgentConversationStateManager();
|
|
1305
|
+
if (seedMessages.length > 0) stateManager.hydrate({
|
|
1306
|
+
sessionId: seedMessages[0]?.sessionId ?? "",
|
|
1307
|
+
messages: seedMessages
|
|
1308
|
+
});
|
|
1309
|
+
const activeMessage = activeMessageId ? seedMessages.find((message) => message.id === activeMessageId) : void 0;
|
|
1310
|
+
if (activeMessage) await stateManager.dispatch({
|
|
1311
|
+
occurredAt: activeMessage.timestamp,
|
|
1312
|
+
type: NcpEventType.MessageSent,
|
|
1313
|
+
payload: {
|
|
1314
|
+
sessionId: activeMessage.sessionId,
|
|
1315
|
+
message: activeMessage
|
|
1316
|
+
}
|
|
1317
|
+
});
|
|
1318
|
+
return new ReplayContext(stateManager, seedMessages);
|
|
1319
|
+
}
|
|
1320
|
+
function updateActiveTailRunIds(activeTailRunIds, event, eventIndex) {
|
|
1321
|
+
if (event.type === NcpEventType.RunStarted) {
|
|
1322
|
+
activeTailRunIds.add(event.payload.runId ?? event.payload.messageId ?? `run-event-${eventIndex}`);
|
|
1323
|
+
return;
|
|
1324
|
+
}
|
|
1325
|
+
if (event.type !== NcpEventType.RunFinished && event.type !== NcpEventType.RunError && event.type !== NcpEventType.MessageAbort) return;
|
|
1326
|
+
const runId = readEventRunId(event);
|
|
1327
|
+
if (runId) activeTailRunIds.delete(runId);
|
|
1328
|
+
else activeTailRunIds.clear();
|
|
1329
|
+
}
|
|
1330
|
+
async function replayJournalEvent(context, sourceEvent, allowUnknownStreamingBootstrap) {
|
|
1331
|
+
let replayEvent = sourceEvent;
|
|
1332
|
+
const { activeTailRunIds, compactionRecovery, knownMessageIds, terminalMessageIds, stateManager, toolOwners, recordEvent } = context;
|
|
1333
|
+
const summary = readMessageFromSummaryEvent$1(replayEvent);
|
|
1334
|
+
if (summary && (summary.status === "pending" || summary.status === "streaming") && terminalMessageIds.has(summary.id)) return;
|
|
1335
|
+
if (replayEvent.type === NcpEventType.MessageToolCallStart && !replayEvent.payload.messageId) {
|
|
1336
|
+
const activeId = stateManager.getSnapshot().streamingMessage?.id;
|
|
1337
|
+
if (!activeId) {
|
|
1338
|
+
console.warn(`[ncp-agent-session-journal] ignored tool start without a message: ${replayEvent.payload.toolCallId}`);
|
|
1339
|
+
return;
|
|
1340
|
+
}
|
|
1341
|
+
replayEvent = {
|
|
1342
|
+
...replayEvent,
|
|
1343
|
+
payload: {
|
|
1344
|
+
...replayEvent.payload,
|
|
1345
|
+
messageId: activeId
|
|
1346
|
+
}
|
|
1347
|
+
};
|
|
1348
|
+
}
|
|
1349
|
+
const toolCallId = readEventToolCallId(replayEvent);
|
|
1350
|
+
const toolOwner = toolCallId ? toolOwners.get(toolCallId) : void 0;
|
|
1351
|
+
if (toolCallId && replayEvent.type !== NcpEventType.MessageToolCallStart && !toolOwner) {
|
|
1352
|
+
console.warn(`[ncp-agent-session-journal] ignored unowned ${replayEvent.type}: ${toolCallId}`);
|
|
1353
|
+
return;
|
|
1354
|
+
}
|
|
1355
|
+
if (toolOwner && terminalMessageIds.has(toolOwner) && replayEvent.type !== NcpEventType.MessageToolCallResult) return;
|
|
1356
|
+
const streamingMessageId = readStreamingMessageId(replayEvent);
|
|
1357
|
+
if (streamingMessageId && terminalMessageIds.has(streamingMessageId)) return;
|
|
1358
|
+
if (streamingMessageId && !allowUnknownStreamingBootstrap && !knownMessageIds.has(streamingMessageId) && !isStreamingMessageCreationEvent(replayEvent) && activeTailRunIds.size === 0) return;
|
|
1359
|
+
compactionRecovery.track(replayEvent);
|
|
1360
|
+
const activeMessageId = stateManager.getSnapshot().streamingMessage?.id;
|
|
1361
|
+
await dispatchReplayEvent(context, replayEvent, streamingMessageId, allowUnknownStreamingBootstrap);
|
|
1362
|
+
recordEvent(replayEvent, activeMessageId);
|
|
1363
|
+
}
|
|
1364
|
+
async function dispatchReplayEvent(context, replayEvent, streamingMessageId, allowUnknownStreamingBootstrap) {
|
|
1365
|
+
const { knownMessageIds, stateManager, toolResultsByCallId } = context;
|
|
1366
|
+
const bootstrap = allowUnknownStreamingBootstrap || isStreamingMessageCreationEvent(replayEvent) ? createReplayStreamingBootstrapEvent(replayEvent, knownMessageIds) : null;
|
|
1367
|
+
if (bootstrap) {
|
|
1368
|
+
knownMessageIds.add(bootstrap.messageId);
|
|
1369
|
+
await stateManager.dispatch(bootstrap.event);
|
|
1370
|
+
}
|
|
1371
|
+
const replayMessageId = readReplayMessageId(replayEvent);
|
|
1372
|
+
if (replayMessageId) knownMessageIds.add(replayMessageId);
|
|
1373
|
+
if (streamingMessageId && isStreamingMessageCreationEvent(replayEvent)) knownMessageIds.add(streamingMessageId);
|
|
1374
|
+
await stateManager.dispatch(replayEvent);
|
|
1375
|
+
if (replayEvent.type === NcpEventType.MessageToolCallResult && replayEvent.payload.final !== false) toolResultsByCallId.set(replayEvent.payload.toolCallId, replayEvent.payload);
|
|
1376
|
+
}
|
|
1377
|
+
const NCP_AGENT_SESSION_JOURNAL_INDEX_FILE = ".ncp-agent-session-index.json";
|
|
1378
|
+
const NCP_AGENT_SESSION_SNAPSHOT_MESSAGE_EVENT_TYPE = "session.snapshot.message";
|
|
1379
|
+
const NCP_SESSION_REQUEST_ACCEPTED_EVENT_TYPE = "session.request.accepted";
|
|
1380
|
+
const NCP_SESSION_REQUEST_COMPLETED_EVENT_TYPE = "session.request.completed";
|
|
1381
|
+
const NCP_SESSION_REQUEST_FAILED_EVENT_TYPE = "session.request.failed";
|
|
1382
|
+
function normalizeNcpSessionId(sessionId) {
|
|
1383
|
+
return sessionId.trim();
|
|
1384
|
+
}
|
|
1385
|
+
function safeNcpSessionFilename(value) {
|
|
1386
|
+
return value.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
1387
|
+
}
|
|
1388
|
+
function normalizeNcpAgentId(agentId) {
|
|
1389
|
+
return agentId?.trim().toLowerCase() || void 0;
|
|
1390
|
+
}
|
|
1391
|
+
function isRecord(value) {
|
|
1392
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
1393
|
+
}
|
|
1394
|
+
function toIsoString(value, fallback) {
|
|
1395
|
+
const parsed = typeof value === "string" ? Date.parse(value) : NaN;
|
|
1396
|
+
return Number.isFinite(parsed) ? new Date(parsed).toISOString() : fallback;
|
|
1397
|
+
}
|
|
1398
|
+
function createNcpAgentSessionSummary(record) {
|
|
1399
|
+
const metadata = structuredClone(record.metadata ?? {});
|
|
1400
|
+
const label = readOptionalText(metadata.label) ?? resolveNcpAgentSessionLabel(record.messages);
|
|
1401
|
+
const peerId = readNcpAgentSessionPeerId(metadata);
|
|
1402
|
+
if (label) metadata.label = label;
|
|
1403
|
+
const lastMessageAt = record.messages.reduceRight((timestamp, message) => timestamp ?? readMessageTimestamp(message), void 0);
|
|
1404
|
+
return {
|
|
1405
|
+
sessionId: record.sessionId,
|
|
1406
|
+
peerId: peerId ?? void 0,
|
|
1407
|
+
...normalizeNcpAgentId(record.agentId) ? { agentId: normalizeNcpAgentId(record.agentId) } : {},
|
|
1408
|
+
messageCount: record.messages.length,
|
|
1409
|
+
...record.createdAt ? { createdAt: record.createdAt } : {},
|
|
1410
|
+
updatedAt: record.updatedAt,
|
|
1411
|
+
...lastMessageAt ? { lastMessageAt } : {},
|
|
1412
|
+
status: "idle",
|
|
1413
|
+
...Object.keys(metadata).length > 0 ? { metadata } : {}
|
|
1414
|
+
};
|
|
1415
|
+
}
|
|
1416
|
+
function readNcpAgentSessionPeerId(metadata) {
|
|
1417
|
+
return readOptionalText(metadata[AGENT_RUN_PEER_ID_METADATA_KEY]);
|
|
1418
|
+
}
|
|
1419
|
+
function createNcpAgentSessionJournalMetadataEntry(record) {
|
|
1420
|
+
return {
|
|
1421
|
+
_type: "metadata",
|
|
1422
|
+
version: 1,
|
|
1423
|
+
created_at: record.createdAt ?? record.updatedAt,
|
|
1424
|
+
updated_at: record.updatedAt,
|
|
1425
|
+
...normalizeNcpAgentId(record.agentId) ? { agent_id: normalizeNcpAgentId(record.agentId) } : {},
|
|
1426
|
+
metadata: structuredClone(record.metadata ?? {})
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1429
|
+
function upsertNcpAgentSessionSummaryEvent(params) {
|
|
1430
|
+
const { current, event, sessionId, updatedAt } = params;
|
|
1431
|
+
const eventMessage = readMessageFromSummaryEvent(event);
|
|
1432
|
+
const messageCount = current ? current.messageCount + (eventMessage ? 1 : 0) : eventMessage ? 1 : 0;
|
|
1433
|
+
const lastMessageAt = readMessageTimestamp(eventMessage) ?? current?.lastMessageAt;
|
|
1434
|
+
return {
|
|
1435
|
+
sessionId,
|
|
1436
|
+
peerId: current?.peerId,
|
|
1437
|
+
...normalizeNcpAgentId(current?.agentId) ? { agentId: normalizeNcpAgentId(current?.agentId) } : {},
|
|
1438
|
+
messageCount,
|
|
1439
|
+
createdAt: current?.createdAt ?? updatedAt,
|
|
1440
|
+
updatedAt,
|
|
1441
|
+
...lastMessageAt ? { lastMessageAt } : {},
|
|
1442
|
+
status: "idle"
|
|
1443
|
+
};
|
|
1444
|
+
}
|
|
1445
|
+
function readMessageTimestamp(message) {
|
|
1446
|
+
return message?.status === "final" ? toIsoString(message.timestamp, "") || void 0 : void 0;
|
|
1447
|
+
}
|
|
1448
|
+
function readOptionalText(value) {
|
|
1449
|
+
return (typeof value === "string" ? value.trim() : "") || null;
|
|
1450
|
+
}
|
|
1451
|
+
function readMessageFromSummaryEvent(event) {
|
|
1452
|
+
if (event.type === NcpEventType.MessageSent || event.type === NcpEventType.MessageCompleted || event.type === "session.snapshot.message") return event.payload.message;
|
|
1453
|
+
}
|
|
1454
|
+
//#endregion
|
|
1455
|
+
export { resolveNcpAgentSessionLabel as $, CONTEXT_COMPACTION_RECOVERY_PREAMBLE as A, isContextWindowSnapshot as B, readEventToolCallId as C, CONTEXT_COMPACTION_PART_START as D, CONTEXT_COMPACTION_CONTINUATION_TEXT as E, buildContextCompactionTimelineNcpMessage as F, shouldRefreshContextWindowDuringStream as G, readContextRecoveryState as H, createContextCompactionMessageId as I, buildContextBlockInputMessages as J, shouldRefreshContextWindowImmediately as K, createContextWindowSignature as L, CONTEXT_COMPACTION_TIMELINE_KIND as M, NEXTCLAW_TIMELINE_KIND_METADATA_KEY as N, CONTEXT_COMPACTION_PROJECTION_KIND as O, buildContextCompactionModelProjection as P, ContextCompactionJournalRecoveryService as Q, isContextCompactionProjectionMessage as R, readEventRunId as S, ContextCompactionPreflightService as T, readContextWindowEventSessionId as U, readContextCompactionCheckpoint as V, readLatestContextCompactionCheckpoint as W, estimateToolInputTokens as X, buildProviderTools as Y, composeAgentToolCatalog as Z, seedReplayToolOwners as _, NCP_SESSION_REQUEST_FAILED_EVENT_TYPE as a, isStreamingMessageCreationEvent as b, isRecord as c, safeNcpSessionFilename as d, createAgentPeerSessionIdentity as et, toIsoString as f, readReplayToolOwners as g, mergePartialSessionMessage as h, NCP_SESSION_REQUEST_COMPLETED_EVENT_TYPE as i, CONTEXT_COMPACTION_SYSTEM_PREAMBLE as j, CONTEXT_COMPACTION_PROJECTION_METADATA_KEY as k, normalizeNcpAgentId as l, SessionEventReplayer as m, NCP_AGENT_SESSION_SNAPSHOT_MESSAGE_EVENT_TYPE as n, StructuredResultSubmitTool as nt, createNcpAgentSessionJournalMetadataEntry as o, upsertNcpAgentSessionSummaryEvent as p, COMPACTION_RECOVERY_NOTICE as q, NCP_SESSION_REQUEST_ACCEPTED_EVENT_TYPE as r, createNcpAgentSessionSummary as s, NCP_AGENT_SESSION_JOURNAL_INDEX_FILE as t, STRUCTURED_RESULT_TOOL_NAME as tt, normalizeNcpSessionId as u, SessionReplaySupersessionTracker as v, readMessageFromSummaryEvent$1 as w, readEventMessageId as x, isReplayContinuationEvent as y, isContextCompactionTimelineMessage as z };
|
|
1456
|
+
|
|
1457
|
+
//# sourceMappingURL=ncp-agent-session-journal.utils-B7a_PKCq.js.map
|