@nextclaw/kernel 0.19.1 → 0.19.2-beta.1

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.
Files changed (72) hide show
  1. package/dist/app/worker/index.d.ts +4 -0
  2. package/dist/app/worker/index.js +3 -0
  3. package/dist/automatic-update-check.utils-C6lQfErN.d.ts +11 -0
  4. package/dist/automatic-update-check.utils-C6lQfErN.d.ts.map +1 -0
  5. package/dist/index.d.ts +4 -4305
  6. package/dist/index.js +12558 -18415
  7. package/dist/index.js.map +1 -1
  8. package/dist/nextclaw-harness.manager-7URpTz3z.js +9030 -0
  9. package/dist/nextclaw-harness.manager-7URpTz3z.js.map +1 -0
  10. package/dist/nextclaw-harness.manager-Df5Zagn_.d.ts +6001 -0
  11. package/dist/nextclaw-harness.manager-Df5Zagn_.d.ts.map +1 -0
  12. package/dist/types/update-contract.types.d.ts +1 -1
  13. package/dist/types/update-contract.types.js +1 -1
  14. package/dist/{update-manifest.types-eSil_QH0.js → update-manifest.types-CBp2VTM1.js} +1 -1
  15. package/dist/{update-manifest.types-eSil_QH0.js.map → update-manifest.types-CBp2VTM1.js.map} +1 -1
  16. package/dist/{update-manifest.types-Dl4B87Pa.d.ts → update-manifest.types-C_60CVvw.d.ts} +1 -1
  17. package/dist/{update-manifest.types-Dl4B87Pa.d.ts.map → update-manifest.types-C_60CVvw.d.ts.map} +1 -1
  18. package/dist/utils/automatic-update-check.utils.d.ts +2 -11
  19. package/package.json +17 -57
  20. package/dist/agent-model-input-budget.utils-tw9zs9WM.js +0 -26
  21. package/dist/agent-model-input-budget.utils-tw9zs9WM.js.map +0 -1
  22. package/dist/agent-run-context-compaction.manager-BV6tB0wL.d.ts +0 -28
  23. package/dist/agent-run-context-compaction.manager-BV6tB0wL.d.ts.map +0 -1
  24. package/dist/agent-run-model-input-builder.service-C3ydUx3d.js +0 -320
  25. package/dist/agent-run-model-input-builder.service-C3ydUx3d.js.map +0 -1
  26. package/dist/agent-run.types-CNVBBJnd.d.ts +0 -71
  27. package/dist/agent-run.types-CNVBBJnd.d.ts.map +0 -1
  28. package/dist/context-compaction-preflight.service-ClJEGWKS.d.ts +0 -97
  29. package/dist/context-compaction-preflight.service-ClJEGWKS.d.ts.map +0 -1
  30. package/dist/context-compaction-preflight.service-K0tE34Oz.js +0 -689
  31. package/dist/context-compaction-preflight.service-K0tE34Oz.js.map +0 -1
  32. package/dist/context-compaction.utils-Csf80mdq.d.ts +0 -42
  33. package/dist/context-compaction.utils-Csf80mdq.d.ts.map +0 -1
  34. package/dist/features/context-compaction/services/context-compaction-preflight.service.d.ts +0 -2
  35. package/dist/features/context-compaction/services/context-compaction-preflight.service.js +0 -2
  36. package/dist/features/context-compaction/utils/context-compaction.utils.d.ts +0 -2
  37. package/dist/features/context-compaction/utils/context-compaction.utils.js +0 -202
  38. package/dist/features/context-compaction/utils/context-compaction.utils.js.map +0 -1
  39. package/dist/index.d.ts.map +0 -1
  40. package/dist/managers/agent-run-context-compaction.manager.d.ts +0 -2
  41. package/dist/managers/agent-run-context-compaction.manager.js +0 -68
  42. package/dist/managers/agent-run-context-compaction.manager.js.map +0 -1
  43. package/dist/request-context-tail.manager-DXC4rdAP.d.ts +0 -22
  44. package/dist/request-context-tail.manager-DXC4rdAP.d.ts.map +0 -1
  45. package/dist/services/agent-run-model-input-builder.service.d.ts +0 -58
  46. package/dist/services/agent-run-model-input-builder.service.d.ts.map +0 -1
  47. package/dist/services/agent-run-model-input-builder.service.js +0 -2
  48. package/dist/tool-schema.tools-CtZEKWeY.js +0 -75
  49. package/dist/tool-schema.tools-CtZEKWeY.js.map +0 -1
  50. package/dist/tools/show-content.tools.d.ts +0 -10
  51. package/dist/tools/show-content.tools.d.ts.map +0 -1
  52. package/dist/tools/show-content.tools.js +0 -294
  53. package/dist/tools/show-content.tools.js.map +0 -1
  54. package/dist/tools/tool-schema.tools.d.ts +0 -29
  55. package/dist/tools/tool-schema.tools.d.ts.map +0 -1
  56. package/dist/tools/tool-schema.tools.js +0 -2
  57. package/dist/tools/user-question.tools.d.ts +0 -63
  58. package/dist/tools/user-question.tools.d.ts.map +0 -1
  59. package/dist/tools/user-question.tools.js +0 -77
  60. package/dist/tools/user-question.tools.js.map +0 -1
  61. package/dist/user-question.manager-BSpxlV5l.d.ts +0 -1037
  62. package/dist/user-question.manager-BSpxlV5l.d.ts.map +0 -1
  63. package/dist/user-question.utils-DWVtPv4S.d.ts +0 -27
  64. package/dist/user-question.utils-DWVtPv4S.d.ts.map +0 -1
  65. package/dist/utils/agent-onboarding-context.utils.d.ts +0 -6
  66. package/dist/utils/agent-onboarding-context.utils.d.ts.map +0 -1
  67. package/dist/utils/agent-onboarding-context.utils.js +0 -39
  68. package/dist/utils/agent-onboarding-context.utils.js.map +0 -1
  69. package/dist/utils/automatic-update-check.utils.d.ts.map +0 -1
  70. package/dist/utils/user-question.utils.d.ts +0 -2
  71. package/dist/utils/user-question.utils.js +0 -105
  72. package/dist/utils/user-question.utils.js.map +0 -1
@@ -1,689 +0,0 @@
1
- import { r as estimateToolInputTokens, t as buildContextBlockInputMessages } from "./agent-model-input-budget.utils-tw9zs9WM.js";
2
- import { CONTEXT_COMPACTION_CONTINUATION_TEXT, CONTEXT_COMPACTION_SYSTEM_PREAMBLE, buildContextCompactionModelProjection, buildContextCompactionTimelineNcpMessage, createContextCompactionMessageId, isContextCompactionProjectionMessage, isContextCompactionTimelineMessage, readLatestContextCompactionCheckpoint } from "./features/context-compaction/utils/context-compaction.utils.js";
3
- import { CHAT_CONTINUATION_TARGET_MESSAGE_METADATA_KEY } from "@nextclaw/shared";
4
- import { ncpMessageToOpenAiMessageGroups } from "@nextclaw/ncp-agent-runtime";
5
- import { normalizeAssistantText } from "@nextclaw/ncp";
6
- import { CONTEXT_COMPACTION_METADATA_KEY, ContextCompactionService, ContextWindowBudgetService, buildCompressingCompactionCheckpoint, buildContextWindowSnapshot, readCompressedContextCompactionCheckpoint } from "@nextclaw/core/context-compaction";
7
- import { estimateInputTokens } from "@nextclaw/core/model-input-budget";
8
- //#region src/utils/context-compaction-summary-input.utils.ts
9
- const SUMMARY_SOURCE_MAX_CHARS = 12e4;
10
- const SUMMARY_SOURCE_HEAD_MESSAGES = 2;
11
- const SUMMARY_SOURCE_TAIL_MESSAGES = 8;
12
- const SUMMARY_HEADING = "# Compressed Working Context";
13
- const SUMMARY_ESSENTIAL_COMPLETE_MARKER = "<!-- nextclaw-essential-context-complete -->";
14
- const SUMMARY_ESSENTIAL_SECTIONS = [
15
- "Active Request",
16
- "Current Work State",
17
- "Safety and User Constraints",
18
- "Continuation Contract"
19
- ];
20
- const SUMMARY_OPTIONAL_SECTIONS = [
21
- ["Critical Technical Context", "critical-technical-context"],
22
- ["Evidence and Verification", "evidence-and-verification"],
23
- ["Recent High-Fidelity Context", "recent-high-fidelity-context"],
24
- ["Older Relevant Context", "older-relevant-context"]
25
- ];
26
- const SUMMARY_SYSTEM_PROMPT = [
27
- "Compress a coding-agent session into standalone Markdown. Do not invent facts.",
28
- "Start with '# Compressed Working Context'. Then use these sections in exact order: Active Request; Current Work State; Safety and User Constraints; Continuation Contract; Critical Technical Context; Evidence and Verification; Recent High-Fidelity Context; Older Relevant Context.",
29
- "The first four sections are essential and must have non-empty bodies. End the Continuation Contract body with exactly '<!-- nextclaw-essential-context-complete -->'. Put no text after that marker before the next heading.",
30
- "Optional sections follow in the listed order. End each body with '<!-- nextclaw-section-complete:SLUG -->', using slugs critical-technical-context, evidence-and-verification, recent-high-fidelity-context, and older-relevant-context. Omit an optional section unless you can close it.",
31
- "Keep all essential facts in the first four sections: latest user intent, active task, current work, constraints, decisions, files and changes, tool/test evidence, failures, blockers, and exact next step.",
32
- "Do not restart onboarding for missing profile fields unless onboarding is the active task. A greeting does not erase the prior task."
33
- ].join("\n");
34
- function toCompactionSourceMessage(message) {
35
- return {
36
- role: message.role,
37
- content: message.content,
38
- timestamp: message.timestamp,
39
- ncp_message_id: message.ncp_message_id
40
- };
41
- }
42
- function truncateSummarySourceString(value, maxChars) {
43
- if (typeof value !== "string" || value.length <= maxChars) return value;
44
- const marker = `[${value.length - maxChars} chars omitted]`;
45
- const headChars = Math.floor((maxChars - marker.length) / 2);
46
- const tailChars = maxChars - marker.length - headChars;
47
- return [
48
- value.slice(0, headChars).trimEnd(),
49
- marker,
50
- value.slice(-tailChars).trimStart()
51
- ].join("\n");
52
- }
53
- function stringifyCompactionSource(messages, maxChars) {
54
- const sourceMessages = messages.map(toCompactionSourceMessage);
55
- const json = JSON.stringify(sourceMessages, null, 2);
56
- if (json.length <= maxChars) return json;
57
- const tailStart = Math.max(SUMMARY_SOURCE_HEAD_MESSAGES, sourceMessages.length - SUMMARY_SOURCE_TAIL_MESSAGES);
58
- const compactedMessages = [
59
- ...sourceMessages.slice(0, SUMMARY_SOURCE_HEAD_MESSAGES),
60
- ...tailStart > SUMMARY_SOURCE_HEAD_MESSAGES ? [{
61
- role: "system",
62
- content: `[${tailStart - SUMMARY_SOURCE_HEAD_MESSAGES} middle messages omitted from compaction source]`
63
- }] : [],
64
- ...sourceMessages.slice(tailStart)
65
- ];
66
- const maxStringChars = Math.max(256, Math.floor(maxChars / Math.max(1, compactedMessages.length) * .7));
67
- const compactedJson = JSON.stringify(compactedMessages, (_key, value) => truncateSummarySourceString(value, maxStringChars), 2);
68
- if (compactedJson.length <= maxChars) return compactedJson;
69
- const marker = "\n[truncated_compaction_source_middle]\n";
70
- if (maxChars <= 38) return compactedJson.slice(0, maxChars);
71
- const headChars = Math.floor((maxChars - 38) / 2);
72
- const tailChars = maxChars - 38 - headChars;
73
- return `${compactedJson.slice(0, headChars).trimEnd()}${marker}${compactedJson.slice(-tailChars).trimStart()}`;
74
- }
75
- function buildSummaryProviderMessages(params) {
76
- const { essentialOnly, messages, sourceMaxChars, targetSummaryTokens } = params;
77
- return [{
78
- role: "system",
79
- 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
80
- }, {
81
- role: "user",
82
- content: [
83
- "Compress these runtime messages into a reusable working context.",
84
- `Keep the visible Markdown summary within ${targetSummaryTokens} tokens. Use the output budget for the summary, not for hidden reasoning.`,
85
- essentialOnly ? "Output the four essential sections only, then stop after the essential completion marker." : "Complete the essential prefix first; add only optional sections you can close.",
86
- "",
87
- "Messages JSON:",
88
- stringifyCompactionSource(messages, sourceMaxChars)
89
- ].join("\n")
90
- }];
91
- }
92
- function fitContextCompactionSummaryInput(params) {
93
- const { essentialOnly = false, maxInputTokens, messages, sourceMaxChars = SUMMARY_SOURCE_MAX_CHARS, targetSummaryTokens } = params;
94
- let lower = 0;
95
- let upper = Math.max(0, sourceMaxChars);
96
- let fitted = buildSummaryProviderMessages({
97
- essentialOnly,
98
- messages: [],
99
- sourceMaxChars: 0,
100
- targetSummaryTokens
101
- });
102
- if (estimateInputTokens(fitted) > maxInputTokens) throw new Error(`Context compaction summary prompt needs more than ${maxInputTokens} input tokens. Increase the agent contextTokens setting.`);
103
- while (lower <= upper) {
104
- const sourceMaxChars = Math.floor((lower + upper) / 2);
105
- const candidate = buildSummaryProviderMessages({
106
- essentialOnly,
107
- messages,
108
- sourceMaxChars,
109
- targetSummaryTokens
110
- });
111
- if (estimateInputTokens(candidate) <= maxInputTokens) {
112
- fitted = candidate;
113
- lower = sourceMaxChars + 1;
114
- } else upper = sourceMaxChars - 1;
115
- }
116
- return fitted;
117
- }
118
- function normalizeContextCompactionSummary(content) {
119
- return normalizeAssistantText(content, "think-tags").text.trim();
120
- }
121
- function parseSummarySections(summary) {
122
- if (!summary.startsWith(SUMMARY_HEADING)) return null;
123
- const headings = [...summary.matchAll(/^## ([^\n]+)$/gm)];
124
- if (headings.length === 0) return null;
125
- return headings.map((headingMatch, index) => {
126
- const heading = headingMatch[1]?.trim() ?? "";
127
- const start = (headingMatch.index ?? 0) + headingMatch[0].length;
128
- const end = headings[index + 1]?.index ?? summary.length;
129
- const body = summary.slice(start, end).trim();
130
- const optional = SUMMARY_OPTIONAL_SECTIONS.find(([name]) => name === heading);
131
- const marker = heading === "## Continuation Contract".slice(3) ? SUMMARY_ESSENTIAL_COMPLETE_MARKER : optional ? `<!-- nextclaw-section-complete:${optional[1]} -->` : "";
132
- return {
133
- heading,
134
- body,
135
- complete: optional ? body.endsWith(marker) : heading === "## Continuation Contract".slice(3) ? body.endsWith(marker) : Boolean(body),
136
- marker
137
- };
138
- });
139
- }
140
- /**
141
- * Validate the priority-prefix protocol and return only complete sections.
142
- * A truncated optional tail is deliberately discarded as a unit.
143
- */
144
- function validateContextCompactionSummary(params) {
145
- if (params.summary.split("<!-- nextclaw-essential-context-complete -->").length - 1 !== 1) return {
146
- essentialComplete: false,
147
- missingEssentialSections: ["Continuation Contract"],
148
- summary: null
149
- };
150
- const sections = parseSummarySections(params.summary);
151
- if (!sections) return {
152
- essentialComplete: false,
153
- missingEssentialSections: [...SUMMARY_ESSENTIAL_SECTIONS],
154
- summary: null
155
- };
156
- const required = sections.slice(0, SUMMARY_ESSENTIAL_SECTIONS.length);
157
- const missingEssentialSections = SUMMARY_ESSENTIAL_SECTIONS.filter((heading, index) => {
158
- const section = required[index];
159
- return section?.heading !== heading || !section.body || !section.complete;
160
- });
161
- if (missingEssentialSections.length > 0) return {
162
- essentialComplete: false,
163
- missingEssentialSections,
164
- summary: null
165
- };
166
- const retained = [SUMMARY_HEADING, ...required.map((section) => `## ${section.heading}\n\n${section.body}`)];
167
- const optionalStart = SUMMARY_ESSENTIAL_SECTIONS.length;
168
- for (let index = optionalStart; index < sections.length; index += 1) {
169
- const section = sections[index];
170
- const expectedOptional = SUMMARY_OPTIONAL_SECTIONS[index - optionalStart];
171
- if (!section || !section.marker || !section.complete || !expectedOptional || section.heading !== expectedOptional[0]) break;
172
- retained.push(`## ${section.heading}\n\n${section.body}`);
173
- }
174
- return {
175
- essentialComplete: true,
176
- missingEssentialSections: [],
177
- summary: retained.join("\n\n")
178
- };
179
- }
180
- function fitContextCompactionSummaryOutput(params) {
181
- const { maxInstallableSummaryTokens, summary } = params;
182
- const validation = validateContextCompactionSummary({ summary });
183
- if (!validation.summary) return null;
184
- const fittedSummary = validation.summary;
185
- if (estimateInputTokens(fittedSummary) <= maxInstallableSummaryTokens) return fittedSummary;
186
- const sections = parseSummarySections(fittedSummary);
187
- if (!sections) return null;
188
- const essentialSections = sections.slice(0, SUMMARY_ESSENTIAL_SECTIONS.length);
189
- const composeEssential = () => [SUMMARY_HEADING, ...essentialSections.map((section) => `## ${section.heading}\n\n${section.body}`)].join("\n\n");
190
- const essentialOnly = composeEssential();
191
- if (estimateInputTokens(essentialOnly) <= maxInstallableSummaryTokens) return essentialOnly;
192
- return null;
193
- }
194
- /**
195
- * Last-resort checkpoint used after bounded semantic generation failures.
196
- * It deliberately preserves exact recent source instead of inventing a
197
- * natural-language summary, while older history is dropped Codex-style.
198
- */
199
- function buildContextCompactionEmergencySummary(params) {
200
- const { maxInstallableSummaryTokens, messages } = params;
201
- const build = (sourceMaxChars) => {
202
- const source = stringifyCompactionSource(messages, sourceMaxChars);
203
- return [
204
- SUMMARY_HEADING,
205
- "## Active Request\n\nContinue the latest raw user request retained after this checkpoint. If no raw user request follows, continue the active run from the exact recent source below.",
206
- `## Current Work State\n\nModel-based compaction did not produce a safe summary. Exact retained recent source:\n\n${source ? source.split("\n").map((line) => `> ${line}`).join("\n") : "> No recent source text fit the emergency checkpoint budget."}`,
207
- "## Safety and User Constraints\n\nHonor the retained system, service, and user constraints verbatim. Treat dropped older context as unknown and do not invent it.",
208
- `## Continuation Contract\n\nContinue from the exact recent source and following raw messages. Do not repeat work unless the retained evidence says it is incomplete.\n${SUMMARY_ESSENTIAL_COMPLETE_MARKER}`
209
- ].join("\n\n");
210
- };
211
- let lower = 0;
212
- let upper = SUMMARY_SOURCE_MAX_CHARS;
213
- let fitted = build(0);
214
- if (estimateInputTokens(fitted) > maxInstallableSummaryTokens) return null;
215
- while (lower <= upper) {
216
- const sourceMaxChars = Math.floor((lower + upper) / 2);
217
- const candidate = build(sourceMaxChars);
218
- if (estimateInputTokens(candidate) <= maxInstallableSummaryTokens) {
219
- fitted = candidate;
220
- lower = sourceMaxChars + 1;
221
- } else upper = sourceMaxChars - 1;
222
- }
223
- return fitted;
224
- }
225
- /** Build a deterministic, smaller source for semantic compaction retries. */
226
- function selectContextCompactionAttemptMessages(messages, attempt) {
227
- if (attempt <= 1) return messages.map((message) => structuredClone(message));
228
- const protectedHead = messages.slice(0, 2).filter((message) => message.role === "system" || message.role === "service");
229
- const tailCount = attempt >= 3 ? 2 : 6;
230
- const tail = messages.slice(-tailCount);
231
- const seen = new Set(protectedHead);
232
- const selectedMessages = [...protectedHead, ...tail].filter((message, index, selected) => selected.indexOf(message) === index && (index < protectedHead.length || !seen.has(message)));
233
- const contentRatio = attempt >= 3 ? .25 : .5;
234
- return selectedMessages.map((message) => {
235
- const copy = structuredClone(message);
236
- if (typeof copy.content === "string" && copy.content.length > 32) {
237
- const maxChars = Math.max(16, Math.floor(copy.content.length * contentRatio));
238
- copy.content = truncateSummarySourceString(copy.content, maxChars);
239
- }
240
- return copy;
241
- });
242
- }
243
- //#endregion
244
- //#region src/features/context-compaction/services/context-compaction-summary-generation.service.ts
245
- const TRUNCATED_SUMMARY_FINISH_REASONS = new Set([
246
- "incomplete",
247
- "length",
248
- "max_output_tokens",
249
- "max_tokens"
250
- ]);
251
- const NATURAL_SUMMARY_FINISH_REASONS = new Set([
252
- "completed",
253
- "end_turn",
254
- "stop"
255
- ]);
256
- const SUMMARY_ATTEMPT_SOURCE_MAX_CHARS = [
257
- 12e4,
258
- 6e4,
259
- 3e4
260
- ];
261
- function mergeProviderUsage(total, usage) {
262
- for (const [key, value] of Object.entries(usage)) if (Number.isFinite(value)) total[key] = (total[key] ?? 0) + value;
263
- }
264
- var ContextCompactionSummaryGenerationService = class {
265
- constructor(providerManager) {
266
- this.providerManager = providerManager;
267
- }
268
- generate = async (params) => {
269
- if (!this.providerManager) throw new Error("context compaction summary generation requires a provider manager");
270
- let lastFailure = "unknown summary failure";
271
- let lastFinishReason = "unknown";
272
- let lastInputTokens = 0;
273
- let lastRawSummaryTokens = 0;
274
- let previousInputTokens = Number.POSITIVE_INFINITY;
275
- let providerCallCount = 0;
276
- const providerUsage = {};
277
- for (let attempt = 1; attempt <= SUMMARY_ATTEMPT_SOURCE_MAX_CHARS.length; attempt += 1) {
278
- const result = await this.runAttempt({
279
- attempt,
280
- params,
281
- previousInputTokens
282
- });
283
- if (!result.calledProvider) {
284
- lastFailure = result.failure;
285
- break;
286
- }
287
- previousInputTokens = result.inputTokens;
288
- lastInputTokens = result.inputTokens;
289
- lastFinishReason = result.finishReason;
290
- lastRawSummaryTokens = result.rawSummaryTokens;
291
- providerCallCount += 1;
292
- mergeProviderUsage(providerUsage, result.providerUsage);
293
- if (result.summary) return this.buildGeneratedSummary({
294
- attemptCount: providerCallCount,
295
- degraded: result.degraded,
296
- finishReason: result.finishReason,
297
- inputTokens: result.inputTokens,
298
- providerUsage,
299
- rawSummaryTokens: result.rawSummaryTokens,
300
- recovery: "provider-summary",
301
- request: params,
302
- summary: result.summary
303
- });
304
- lastFailure = result.failure;
305
- }
306
- const emergencySummary = buildContextCompactionEmergencySummary({
307
- maxInstallableSummaryTokens: params.maxInstallableSummaryTokens,
308
- messages: selectContextCompactionAttemptMessages(params.messages, SUMMARY_ATTEMPT_SOURCE_MAX_CHARS.length)
309
- });
310
- if (!emergencySummary) throw new Error(`Context compaction summary failed after ${providerCallCount} provider calls: ${lastFailure}. The previous checkpoint was kept unchanged.`);
311
- return this.buildGeneratedSummary({
312
- attemptCount: providerCallCount,
313
- degraded: true,
314
- finishReason: lastFinishReason,
315
- inputTokens: lastInputTokens,
316
- providerUsage,
317
- rawSummaryTokens: lastRawSummaryTokens,
318
- recovery: "deterministic-recent-context",
319
- request: params,
320
- summary: emergencySummary
321
- });
322
- };
323
- runAttempt = async (input) => {
324
- const { attempt, params, previousInputTokens } = input;
325
- if (params.signal?.aborted) throw new DOMException("Context compaction summary generation was cancelled", "AbortError");
326
- const providerMessages = fitContextCompactionSummaryInput({
327
- essentialOnly: attempt === SUMMARY_ATTEMPT_SOURCE_MAX_CHARS.length,
328
- maxInputTokens: params.maxInputTokens,
329
- messages: selectContextCompactionAttemptMessages(params.messages, attempt),
330
- sourceMaxChars: SUMMARY_ATTEMPT_SOURCE_MAX_CHARS[attempt - 1],
331
- targetSummaryTokens: params.targetSummaryTokens
332
- });
333
- const inputTokens = estimateInputTokens(providerMessages);
334
- if (inputTokens >= previousInputTokens) return this.failedAttempt({
335
- calledProvider: false,
336
- failure: `semantic retry input did not shrink: ${inputTokens} tokens is not below ${previousInputTokens}`,
337
- inputTokens
338
- });
339
- const response = await this.requestProvider({
340
- attempt,
341
- messages: providerMessages,
342
- params
343
- });
344
- const finishReason = response.finishReason.trim().toLowerCase();
345
- const summary = response.content ? normalizeContextCompactionSummary(response.content) : "";
346
- const base = {
347
- calledProvider: true,
348
- finishReason: response.finishReason,
349
- inputTokens,
350
- providerUsage: response.usage,
351
- rawSummaryTokens: summary ? estimateInputTokens(summary) : 0
352
- };
353
- if (!NATURAL_SUMMARY_FINISH_REASONS.has(finishReason) && !TRUNCATED_SUMMARY_FINISH_REASONS.has(finishReason)) return this.failedAttempt({
354
- ...base,
355
- failure: `unsupported finishReason=${response.finishReason}`
356
- });
357
- if (!summary) return this.failedAttempt({
358
- ...base,
359
- failure: `empty summary: finishReason=${response.finishReason}`
360
- });
361
- const validation = validateContextCompactionSummary({ summary });
362
- if (!validation.summary) return this.failedAttempt({
363
- ...base,
364
- failure: `${TRUNCATED_SUMMARY_FINISH_REASONS.has(finishReason) ? "truncated" : "incomplete"} summary; missing essential sections: ${validation.missingEssentialSections.join(", ")}`
365
- });
366
- const installableSummary = fitContextCompactionSummaryOutput({
367
- maxInstallableSummaryTokens: params.maxInstallableSummaryTokens,
368
- summary: validation.summary
369
- });
370
- if (!installableSummary) return this.failedAttempt({
371
- ...base,
372
- failure: `complete essential prefix exceeds hard installable budget ${params.maxInstallableSummaryTokens}`
373
- });
374
- return {
375
- ...base,
376
- degraded: TRUNCATED_SUMMARY_FINISH_REASONS.has(finishReason) || validation.summary !== summary || installableSummary !== validation.summary,
377
- failure: "",
378
- summary: installableSummary
379
- };
380
- };
381
- requestProvider = async (input) => {
382
- try {
383
- return await this.providerManager.chat({
384
- model: input.params.model,
385
- maxTokens: input.params.maxTokens,
386
- messages: input.messages,
387
- requestId: input.params.requestId,
388
- sessionId: input.params.sessionId,
389
- signal: input.params.signal,
390
- thinkingLevel: "off"
391
- });
392
- } catch (error) {
393
- if (input.params.signal?.aborted || error instanceof DOMException && error.name === "AbortError") throw error;
394
- const detail = error instanceof Error ? error.message : String(error);
395
- throw new Error(`Context compaction summary provider failed on call ${input.attempt}: ${detail}. The previous checkpoint was kept unchanged.`, { cause: error });
396
- }
397
- };
398
- failedAttempt = (input) => ({
399
- calledProvider: input.calledProvider,
400
- degraded: false,
401
- failure: input.failure,
402
- finishReason: input.finishReason ?? "unknown",
403
- inputTokens: input.inputTokens,
404
- providerUsage: input.providerUsage ?? {},
405
- rawSummaryTokens: input.rawSummaryTokens ?? 0,
406
- summary: null
407
- });
408
- buildGeneratedSummary = (input) => ({
409
- diagnostics: {
410
- attemptCount: input.attemptCount,
411
- degraded: input.degraded,
412
- finishReason: input.finishReason,
413
- installedSummaryTokens: estimateInputTokens(input.summary),
414
- providerInputTokens: input.inputTokens,
415
- providerMaxOutputTokens: input.request.maxTokens,
416
- providerUsage: structuredClone(input.providerUsage),
417
- rawSummaryTokens: input.rawSummaryTokens,
418
- recovery: input.recovery,
419
- targetSummaryTokens: input.request.targetSummaryTokens
420
- },
421
- summary: input.summary
422
- });
423
- };
424
- //#endregion
425
- //#region src/features/context-compaction/services/context-compaction-preflight.service.ts
426
- function toCompactionModelMessages(messages, assetStore) {
427
- return messages.flatMap((message) => ncpMessageToOpenAiMessageGroups(message, { assetStore }).flatMap((group) => group.messages.map((providerMessage) => ({
428
- ...providerMessage,
429
- ncp_message_id: message.id,
430
- ncp_part_start: group.partStart + Number(message.metadata?.["nextclaw_compaction_part_start"] ?? 0),
431
- timestamp: message.timestamp
432
- }))));
433
- }
434
- function estimateCompactionProjectionOverhead(phase) {
435
- const summaryPlanOverhead = estimateInputTokens({
436
- role: "user",
437
- content: ""
438
- });
439
- const summaryProjectionOverhead = estimateInputTokens({
440
- role: "system",
441
- content: `${CONTEXT_COMPACTION_SYSTEM_PREAMBLE}\n\n`
442
- });
443
- const continuationTokens = phase === "mid-run" ? estimateInputTokens({
444
- role: "user",
445
- content: CONTEXT_COMPACTION_CONTINUATION_TEXT
446
- }) : 0;
447
- return Math.max(0, summaryProjectionOverhead - summaryPlanOverhead) + continuationTokens;
448
- }
449
- function mergeInputMessages(params) {
450
- const messages = params.sessionMessages.map((message) => structuredClone(message));
451
- const seen = new Set(messages.map((message) => message.id));
452
- for (const message of params.inputMessages) {
453
- if (seen.has(message.id)) continue;
454
- messages.push(structuredClone(message));
455
- }
456
- return messages;
457
- }
458
- function readContinuationMessageBoundary(messages) {
459
- for (let index = messages.length - 1; index >= 0; index -= 1) {
460
- const message = messages[index];
461
- if (message?.role === "assistant" && (message.status === "pending" || message.status === "streaming")) return {
462
- messageId: message.id,
463
- coveredPartCount: message.parts.length
464
- };
465
- }
466
- return null;
467
- }
468
- function readPreRunContinuationBoundary(messages) {
469
- const targetMessageId = messages.at(-1)?.metadata?.[CHAT_CONTINUATION_TARGET_MESSAGE_METADATA_KEY];
470
- if (typeof targetMessageId !== "string" || !targetMessageId.trim()) return null;
471
- for (let index = messages.length - 1; index >= 0; index -= 1) {
472
- const target = messages[index];
473
- if (target?.id === targetMessageId && target.role === "assistant") return {
474
- messageId: target.id,
475
- coveredPartCount: target.parts.length
476
- };
477
- }
478
- return null;
479
- }
480
- function buildContextWindowSnapshotFromBudget(params) {
481
- const { budget, checkpoint, completeInputBudget, fixedInputTokens, reservedContextTokens, totalContextTokens } = params;
482
- return buildContextWindowSnapshot({
483
- usedContextTokens: budget.estimatedTokens,
484
- completeInputBudget,
485
- totalContextTokens,
486
- fixedInputTokens,
487
- reservedContextTokens,
488
- triggerContextTokens: budget.triggerTokens,
489
- prunedUsedContextTokens: budget.estimatedTokens,
490
- droppedHistoryCount: budget.droppedHistoryCount,
491
- truncatedToolResultCount: budget.truncatedToolResultCount,
492
- truncatedSystemPrompt: budget.truncatedSystemPrompt,
493
- truncatedUserMessage: budget.truncatedUserMessage,
494
- checkpoint,
495
- compactedUsedContextTokens: checkpoint ? budget.estimatedTokens : void 0
496
- });
497
- }
498
- var ContextCompactionPreflightService = class {
499
- compactionService = new ContextCompactionService();
500
- contextWindowBudgetService = new ContextWindowBudgetService();
501
- summaryGenerationService;
502
- constructor(agentManager, providerManager, assetStore = null) {
503
- this.agentManager = agentManager;
504
- this.assetStore = assetStore;
505
- this.summaryGenerationService = new ContextCompactionSummaryGenerationService(providerManager);
506
- }
507
- preview = (params) => {
508
- const { completeInputBudget = false, contextBlocks = [], fixedInputTokens, requestMetadata, sessionId, sessionMessages, storedAgentId, storedMetadata, tools = [] } = params;
509
- const profile = this.resolveCompactionProfile({
510
- requestMetadata,
511
- storedAgentId
512
- });
513
- const existingCheckpoint = readCompressedContextCompactionCheckpoint(storedMetadata[CONTEXT_COMPACTION_METADATA_KEY]) ?? readLatestContextCompactionCheckpoint(sessionMessages);
514
- const projectedMessages = existingCheckpoint ? buildContextCompactionModelProjection({
515
- sessionId,
516
- sessionMessages
517
- }).messages : sessionMessages.filter((message) => !isContextCompactionTimelineMessage(message));
518
- const contextBlockMessages = buildContextBlockInputMessages(contextBlocks);
519
- const contextBlockInputTokens = estimateInputTokens(contextBlockMessages);
520
- const toolInputTokens = estimateToolInputTokens(tools);
521
- const snapshotFixedInputTokens = fixedInputTokens ?? contextBlockInputTokens + toolInputTokens;
522
- const messages = [...fixedInputTokens === void 0 ? contextBlockMessages : [], ...toCompactionModelMessages(projectedMessages, this.assetStore)];
523
- return buildContextWindowSnapshotFromBudget({
524
- budget: this.contextWindowBudgetService.evaluate({
525
- messages,
526
- contextTokens: profile.contextTokens,
527
- fixedInputTokens: fixedInputTokens ?? toolInputTokens,
528
- reservedContextTokens: profile.reservedContextTokens
529
- }),
530
- checkpoint: existingCheckpoint,
531
- completeInputBudget,
532
- fixedInputTokens: snapshotFixedInputTokens,
533
- reservedContextTokens: profile.reservedContextTokens,
534
- totalContextTokens: profile.contextTokens
535
- });
536
- };
537
- begin = (params) => {
538
- const { contextBlocks = [], inputMessages, model, phase = "pre-run", requestMetadata, sessionId, sessionMessages, storedAgentId, storedMetadata, tools = [], trigger = "automatic" } = params;
539
- const { contextTokens, reservedContextTokens } = this.resolveCompactionProfile({
540
- requestMetadata,
541
- storedAgentId
542
- });
543
- const ncpMessages = mergeInputMessages({
544
- inputMessages,
545
- sessionMessages
546
- });
547
- const continuationMessageBoundary = phase === "mid-run" ? readContinuationMessageBoundary(ncpMessages) : readPreRunContinuationBoundary(ncpMessages);
548
- const existingCheckpoint = readCompressedContextCompactionCheckpoint(storedMetadata[CONTEXT_COMPACTION_METADATA_KEY]) ?? readLatestContextCompactionCheckpoint(ncpMessages);
549
- const projectedMessages = existingCheckpoint ? buildContextCompactionModelProjection({
550
- sessionId,
551
- sessionMessages: ncpMessages
552
- }).messages : ncpMessages.filter((message) => !isContextCompactionTimelineMessage(message));
553
- const contextBlockMessages = buildContextBlockInputMessages(contextBlocks);
554
- const messages = [...contextBlockMessages, ...toCompactionModelMessages(projectedMessages, this.assetStore)];
555
- const preservableUserMessageIds = ncpMessages.flatMap((message) => message.role === "user" ? [message.id] : []);
556
- const fixedInputTokens = estimateToolInputTokens(tools);
557
- const snapshotFixedInputTokens = estimateInputTokens(contextBlockMessages) + fixedInputTokens;
558
- const budget = this.contextWindowBudgetService.evaluate({
559
- messages,
560
- contextTokens,
561
- fixedInputTokens,
562
- reservedContextTokens
563
- });
564
- const plan = trigger === "automatic" && !budget.shouldCompact ? null : this.compactionService.prepareForModelInput({
565
- messages,
566
- coverLeadingSystemMessage: contextBlockMessages.length === 0 && isContextCompactionProjectionMessage(projectedMessages[0]),
567
- contextTokens,
568
- compactionThresholdTokens: trigger === "manual" ? 0 : budget.triggerTokens,
569
- fixedInputTokens,
570
- preservableUserMessageIds,
571
- projectedTokenLimit: Math.max(1, budget.triggerTokens - estimateCompactionProjectionOverhead(phase)),
572
- retainLatestMessage: phase === "pre-run"
573
- });
574
- const coveredSessionMessageCount = plan ? (existingCheckpoint?.coveredSessionMessageCount ?? 0) + plan.coveredMessages.length - (existingCheckpoint ? 1 : 0) : 0;
575
- const serviceMessageId = createContextCompactionMessageId();
576
- const checkpoint = plan ? {
577
- ...buildCompressingCompactionCheckpoint(storedMetadata[CONTEXT_COMPACTION_METADATA_KEY]),
578
- phase,
579
- continuationMessageId: continuationMessageBoundary?.messageId,
580
- continuationMessageCoveredPartCount: continuationMessageBoundary?.coveredPartCount,
581
- coveredMessageCount: coveredSessionMessageCount,
582
- coveredSessionMessageCount,
583
- originalEstimatedTokens: plan.originalEstimatedTokens,
584
- projectedEstimatedTokens: budget.estimatedTokens
585
- } : existingCheckpoint;
586
- return {
587
- contextWindow: buildContextWindowSnapshotFromBudget({
588
- budget,
589
- checkpoint,
590
- completeInputBudget: true,
591
- fixedInputTokens: snapshotFixedInputTokens,
592
- reservedContextTokens,
593
- totalContextTokens: contextTokens
594
- }),
595
- sessionMessages: ncpMessages,
596
- pendingCompaction: plan && checkpoint ? {
597
- checkpoint,
598
- contextBlockMessages,
599
- continuationMessageBoundary,
600
- contextTokens,
601
- phase,
602
- serviceMessageId,
603
- model,
604
- plan,
605
- reservedContextTokens,
606
- snapshotFixedInputTokens,
607
- sessionId,
608
- sessionMessages: ncpMessages
609
- } : null
610
- };
611
- };
612
- finish = async (pending, signal) => {
613
- let summaryDiagnostics;
614
- const generatedCheckpoint = (await this.compactionService.compactPreparedForModelInput({
615
- contextTokens: pending.contextTokens,
616
- plan: pending.plan,
617
- generateSummary: async ({ maxInputTokens, maxInstallableSummaryTokens, maxTokens, messages, targetSummaryTokens }) => {
618
- const generated = await this.summaryGenerationService.generate({
619
- maxInputTokens,
620
- maxInstallableSummaryTokens,
621
- maxTokens,
622
- messages,
623
- model: pending.model,
624
- requestId: pending.serviceMessageId,
625
- sessionId: pending.sessionId,
626
- signal,
627
- targetSummaryTokens
628
- });
629
- summaryDiagnostics = generated.diagnostics;
630
- return generated.summary;
631
- }
632
- })).checkpoint;
633
- if (!generatedCheckpoint) throw new Error("context compaction pending work did not produce a checkpoint");
634
- const checkpoint = {
635
- ...generatedCheckpoint,
636
- id: pending.checkpoint.id,
637
- createdAt: pending.checkpoint.createdAt,
638
- phase: pending.phase,
639
- continuationMessageId: pending.continuationMessageBoundary?.messageId,
640
- continuationMessageCoveredPartCount: pending.continuationMessageBoundary?.coveredPartCount,
641
- coveredMessageCount: pending.checkpoint.coveredMessageCount,
642
- coveredSessionMessageCount: pending.checkpoint.coveredSessionMessageCount,
643
- summaryDiagnostics,
644
- status: "compressed"
645
- };
646
- const timelineMessage = buildContextCompactionTimelineNcpMessage({
647
- messageId: pending.serviceMessageId,
648
- sessionId: pending.sessionId,
649
- checkpoint
650
- });
651
- const projectedMessages = buildContextCompactionModelProjection({
652
- sessionId: pending.sessionId,
653
- sessionMessages: [...pending.sessionMessages, timelineMessage]
654
- }).messages;
655
- const budget = this.contextWindowBudgetService.evaluate({
656
- messages: [...pending.contextBlockMessages, ...toCompactionModelMessages(projectedMessages, this.assetStore)],
657
- contextTokens: pending.contextTokens,
658
- fixedInputTokens: pending.plan.fixedInputTokens,
659
- reservedContextTokens: pending.reservedContextTokens
660
- });
661
- if (budget.estimatedTokens > budget.triggerTokens) 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.`);
662
- return {
663
- contextWindow: buildContextWindowSnapshotFromBudget({
664
- budget,
665
- checkpoint,
666
- completeInputBudget: true,
667
- fixedInputTokens: pending.snapshotFixedInputTokens,
668
- reservedContextTokens: pending.reservedContextTokens,
669
- totalContextTokens: pending.contextTokens
670
- }),
671
- sessionMessages: pending.sessionMessages,
672
- timelineMessage
673
- };
674
- };
675
- resolveCompactionProfile = (params) => {
676
- const profile = this.agentManager.resolveAgentProfileForRun({
677
- requestMetadata: params.requestMetadata,
678
- storedAgentId: params.storedAgentId
679
- });
680
- return {
681
- contextTokens: profile.contextTokens,
682
- reservedContextTokens: profile.reservedContextTokens
683
- };
684
- };
685
- };
686
- //#endregion
687
- export { ContextCompactionPreflightService as t };
688
-
689
- //# sourceMappingURL=context-compaction-preflight.service-K0tE34Oz.js.map