dsh-lcx-codex 0.5.2 → 0.6.0-pre.2
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/README.md +16 -9
- package/THIRD_PARTY_NOTICES.md +3 -2
- package/icon.svg +1 -0
- package/lib/abort-error.js +12 -1
- package/lib/client.js +137 -39
- package/lib/compact-v2.js +16 -56
- package/lib/defaults.js +15 -0
- package/lib/dsh-compat.js +15 -76
- package/lib/dsh-responses.js +172 -71
- package/lib/grok-native-search.js +1 -3
- package/lib/index.js +228 -78
- package/lib/model-catalog.js +5 -7
- package/lib/native-checkpoint.js +57 -182
- package/lib/pi-responses-runtime.js +140 -50
- package/lib/provider-headers.js +31 -0
- package/lib/responses-request.js +4 -12
- package/lib/responses-stream.js +116 -218
- package/lib/route.js +169 -75
- package/lib/search-accounting.js +19 -21
- package/lib/token-budget.js +6 -41
- package/lib/tool-surface.js +106 -0
- package/lib/transport.js +89 -107
- package/lib/types/client/search-media.d.ts +0 -2
- package/lib/types/index.d.ts +4 -0
- package/lib/util.js +21 -0
- package/lib/web-run-output.js +0 -3
- package/lib/web-search-alpha.js +22 -27
- package/lib/web-search-capability.js +1 -3
- package/lib/web-search-hosted.js +2 -2
- package/lib/web-search-ref-store.js +1 -3
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +37 -32
- package/lib/pi086-responses-allowlist.js +0 -131
package/lib/native-checkpoint.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
// @ts-check
|
|
2
1
|
import { isCompactCheckpointSource } from "@deepseek-ai/dsh-compaction";
|
|
3
2
|
import { projectToolUpdates, } from "@deepseek-ai/dsh-llm";
|
|
4
3
|
import { baseURLFingerprint, routeCompatible } from "./route.js";
|
|
5
4
|
import { persistNativeImageReferences, responseInputItems } from "./dsh-responses.js";
|
|
6
5
|
import { estimateBudgetItem, modelVisibleBudgetView, portableBudgetError, portableTokenCeiling, } from "./token-budget.js";
|
|
6
|
+
import { isRecord } from "./util.js";
|
|
7
|
+
import { DEFAULTS } from "./defaults.js";
|
|
7
8
|
function sessionQueryUnavailable(cause) {
|
|
8
9
|
return Object.assign(new Error("LCX checkpoint history requires the DSH session query service", { cause }), { code: "LCX_SESSION_QUERY_UNAVAILABLE" });
|
|
9
10
|
}
|
|
@@ -18,127 +19,65 @@ async function observeEvents(ctx, session) {
|
|
|
18
19
|
throw sessionQueryUnavailable(cause);
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
|
-
/**
|
|
22
|
-
* @typedef {object} NativeCheckpointBase
|
|
23
|
-
* @property {string} compactionId
|
|
24
|
-
* @property {string} provider
|
|
25
|
-
* @property {string} model
|
|
26
|
-
* @property {string} baseURLFingerprint
|
|
27
|
-
* @property {string} sourceSessionId
|
|
28
|
-
* @property {NativeOutputItem[]} nativeOutput
|
|
29
|
-
* @property {CompactionItem} [nativeCompaction]
|
|
30
|
-
* @property {number} [retainedInputCount]
|
|
31
|
-
* @property {number} [retainedClientCount]
|
|
32
|
-
* @property {number} [retainedAssistantCount]
|
|
33
|
-
* @property {number} [retainedEstimatedTokens]
|
|
34
|
-
* @property {number} [createdAt]
|
|
35
|
-
*/
|
|
36
|
-
/** @typedef {NativeCheckpointBase & { type: 'lcx-native-compaction-v5', version: 5, retentionPolicy?: 'conversation-fidelity-v1' }} NativeCheckpointV5 */
|
|
37
|
-
/** @typedef {NativeCheckpointV5} NativeCheckpointBlock */
|
|
38
22
|
/**
|
|
39
23
|
* Minimal validated candidate shape. DSH rawOutput is unknown until the existing
|
|
40
24
|
* version, field, count, and compaction validation below has completed.
|
|
41
|
-
* @typedef {object} NativeCheckpointCandidate
|
|
42
|
-
* @property {typeof NATIVE_BLOCK_TYPE} type
|
|
43
|
-
* @property {typeof NATIVE_BLOCK_VERSION} version
|
|
44
|
-
* @property {unknown} [compactionId]
|
|
45
|
-
* @property {unknown} [nativeOutput]
|
|
46
|
-
* @property {unknown} [provider]
|
|
47
|
-
* @property {unknown} [model]
|
|
48
|
-
* @property {unknown} [baseURLFingerprint]
|
|
49
|
-
* @property {unknown} [sourceSessionId]
|
|
50
|
-
* @property {unknown} [retainedInputCount]
|
|
51
|
-
* @property {unknown} [retainedClientCount]
|
|
52
|
-
* @property {unknown} [retainedAssistantCount]
|
|
53
25
|
*/
|
|
54
|
-
/** @typedef {{ compaction: CompactionItem }} NativeCompactionResult */
|
|
55
|
-
/** @typedef {{ session: Session, route: RouteIdentity, result: NativeCompactionResult, input?: unknown[], ephemeralPreludeItemCount?: number, imageMap?: unknown, retentionOptions?: RetentionOptions }} CreateCheckpointOptions */
|
|
56
|
-
/** @typedef {Error & { code?: string }} LcxError */
|
|
57
26
|
export const NATIVE_BLOCK_TYPE = "lcx-native-compaction-v5";
|
|
58
27
|
const MIGRATED_NATIVE_BLOCK_PREFIX = "plugin:lcx-native-compaction-";
|
|
59
28
|
export const NATIVE_BLOCK_VERSION = 5;
|
|
60
|
-
export const RETAINED_MESSAGE_TOKEN_BUDGET =
|
|
61
|
-
export const ASSISTANT_RETENTION_TOKEN_RESERVE =
|
|
62
|
-
export const ASSISTANT_RETENTION_PER_MESSAGE_TOKEN_CAP =
|
|
63
|
-
/**
|
|
64
|
-
* @param {unknown} value
|
|
65
|
-
* @returns {value is UnknownRecord}
|
|
66
|
-
*/
|
|
67
|
-
function isObject(value) {
|
|
68
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
69
|
-
}
|
|
70
|
-
/** @param {Message | null | undefined} message */
|
|
71
|
-
function textOf(message) {
|
|
72
|
-
return (message?.content ?? [])
|
|
73
|
-
.filter((block) => block.type === "text")
|
|
74
|
-
.map((block) => block.text)
|
|
75
|
-
.join("");
|
|
76
|
-
}
|
|
77
|
-
/** @param {unknown} item */
|
|
29
|
+
export const RETAINED_MESSAGE_TOKEN_BUDGET = DEFAULTS.nativeRetentionTokenBudget;
|
|
30
|
+
export const ASSISTANT_RETENTION_TOKEN_RESERVE = DEFAULTS.assistantRetentionTokenReserve;
|
|
31
|
+
export const ASSISTANT_RETENTION_PER_MESSAGE_TOKEN_CAP = DEFAULTS.assistantRetentionPerMessageTokenCap;
|
|
78
32
|
function estimatedItemTokens(item) {
|
|
79
33
|
return estimateBudgetItem(item);
|
|
80
34
|
}
|
|
81
|
-
/**
|
|
82
|
-
* @param {unknown} item
|
|
83
|
-
* @returns {item is UnknownRecord}
|
|
84
|
-
*/
|
|
85
35
|
function isRetainedClientItem(item) {
|
|
86
|
-
if (!
|
|
36
|
+
if (!isRecord(item) || (item.type !== undefined && item.type !== "message"))
|
|
87
37
|
return false;
|
|
88
38
|
return (item.role === "user" || item.role === "developer" || item.role === "system");
|
|
89
39
|
}
|
|
90
40
|
function assistantTextParts(item) {
|
|
91
|
-
if (!
|
|
41
|
+
if (!isRecord(item) ||
|
|
92
42
|
item.type !== "message" ||
|
|
93
43
|
item.role !== "assistant" ||
|
|
94
44
|
!Array.isArray(item.content))
|
|
95
45
|
return [];
|
|
96
|
-
return item.content.filter((part) =>
|
|
46
|
+
return item.content.filter((part) => isRecord(part) &&
|
|
97
47
|
part.type === "output_text" &&
|
|
98
48
|
typeof part.text === "string" &&
|
|
99
49
|
part.text.length > 0);
|
|
100
50
|
}
|
|
101
|
-
/** @param {unknown} item */
|
|
102
51
|
function isRetainedAssistantItem(item) {
|
|
103
52
|
return assistantTextParts(item).length > 0;
|
|
104
53
|
}
|
|
105
|
-
/** @param {unknown} item */
|
|
106
54
|
function assistantText(item) {
|
|
107
55
|
return assistantTextParts(item)
|
|
108
56
|
.map((part) => part.text)
|
|
109
57
|
.join("");
|
|
110
58
|
}
|
|
111
|
-
/** @param {unknown} item */
|
|
112
59
|
function retainedAssistantPhase(item) {
|
|
113
|
-
const phase =
|
|
60
|
+
const phase = isRecord(item) ? item.phase : undefined;
|
|
114
61
|
return phase === "commentary" || phase === "final_answer" ? phase : undefined;
|
|
115
62
|
}
|
|
116
|
-
/** @param {unknown} value */
|
|
117
63
|
function containsBudgetImage(value) {
|
|
118
64
|
if (Array.isArray(value))
|
|
119
65
|
return value.some(containsBudgetImage);
|
|
120
|
-
if (!
|
|
66
|
+
if (!isRecord(value))
|
|
121
67
|
return false;
|
|
122
68
|
return value.image === true || Object.values(value).some(containsBudgetImage);
|
|
123
69
|
}
|
|
124
|
-
/** @param {unknown} item */
|
|
125
70
|
function isImageBudgetItem(item) {
|
|
126
71
|
return containsBudgetImage(modelVisibleBudgetView(item));
|
|
127
72
|
}
|
|
128
|
-
/**
|
|
129
|
-
* @param {unknown} item
|
|
130
|
-
* @param {number} [maxTokens]
|
|
131
|
-
* @returns {UnknownRecord | undefined}
|
|
132
|
-
*/
|
|
133
73
|
function truncateVisibleAssistantItem(item, maxTokens = ASSISTANT_RETENTION_PER_MESSAGE_TOKEN_CAP) {
|
|
134
74
|
const text = assistantText(item);
|
|
135
75
|
if (!text)
|
|
136
76
|
return undefined;
|
|
137
77
|
const phase = retainedAssistantPhase(item);
|
|
138
|
-
const providerId =
|
|
78
|
+
const providerId = isRecord(item) && typeof item.id === "string" && item.id
|
|
139
79
|
? item.id
|
|
140
80
|
: undefined;
|
|
141
|
-
/** @param {string} retained @param {boolean} preserveProviderId */
|
|
142
81
|
const candidate = (retained, preserveProviderId) => ({
|
|
143
82
|
type: "message",
|
|
144
83
|
role: "assistant",
|
|
@@ -150,7 +89,6 @@ function truncateVisibleAssistantItem(item, maxTokens = ASSISTANT_RETENTION_PER_
|
|
|
150
89
|
if ((estimatedItemTokens(full) ?? Infinity) <= maxTokens)
|
|
151
90
|
return full;
|
|
152
91
|
const marker = "\n…[LCX retained answer truncated]…\n";
|
|
153
|
-
/** @param {number} count */
|
|
154
92
|
const shortened = (count) => {
|
|
155
93
|
const available = Math.max(0, count - marker.length);
|
|
156
94
|
const head = Math.floor(available * 0.72);
|
|
@@ -172,15 +110,7 @@ function truncateVisibleAssistantItem(item, maxTokens = ASSISTANT_RETENTION_PER_
|
|
|
172
110
|
}
|
|
173
111
|
return best;
|
|
174
112
|
}
|
|
175
|
-
/**
|
|
176
|
-
* @param {IndexedItem[]} candidates
|
|
177
|
-
* @param {number} budget
|
|
178
|
-
* @param {(value: IndexedItem) => unknown} [transform]
|
|
179
|
-
* @param {boolean} [stopOnImageOverflow]
|
|
180
|
-
* @returns {Selection}
|
|
181
|
-
*/
|
|
182
113
|
function selectNewest(candidates, budget, transform = (value) => structuredClone(value.item), stopOnImageOverflow = false) {
|
|
183
|
-
/** @type {SelectedItem[]} */
|
|
184
114
|
const selected = [];
|
|
185
115
|
let used = 0;
|
|
186
116
|
for (let index = candidates.length - 1; index >= 0; index -= 1) {
|
|
@@ -201,30 +131,6 @@ function selectNewest(candidates, budget, transform = (value) => structuredClone
|
|
|
201
131
|
}
|
|
202
132
|
return { selected, used };
|
|
203
133
|
}
|
|
204
|
-
/**
|
|
205
|
-
* @param {unknown[] | null | undefined} input
|
|
206
|
-
* @param {RetentionOptions} [options]
|
|
207
|
-
* @returns {unknown[]}
|
|
208
|
-
*/
|
|
209
|
-
export function retainedCompactionInput(input, options = {}) {
|
|
210
|
-
const configuredBudget = options.tokenBudget;
|
|
211
|
-
const budget = typeof configuredBudget === "number" &&
|
|
212
|
-
Number.isSafeInteger(configuredBudget) &&
|
|
213
|
-
configuredBudget > 0
|
|
214
|
-
? configuredBudget
|
|
215
|
-
: RETAINED_MESSAGE_TOKEN_BUDGET;
|
|
216
|
-
const candidates = (input ?? [])
|
|
217
|
-
.map((item, index) => ({ item, index }))
|
|
218
|
-
.filter(({ item }) => isRetainedClientItem(item));
|
|
219
|
-
return selectNewest(candidates, budget)
|
|
220
|
-
.selected.sort((a, b) => a.index - b.index)
|
|
221
|
-
.map(({ item }) => item);
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* @param {unknown[] | null | undefined} input
|
|
225
|
-
* @param {RetentionOptions} [options]
|
|
226
|
-
* @returns {RetentionPlan}
|
|
227
|
-
*/
|
|
228
134
|
export function retainedConversationPlan(input, options = {}) {
|
|
229
135
|
const configuredBudget = options.tokenBudget;
|
|
230
136
|
const totalBudget = typeof configuredBudget === "number" &&
|
|
@@ -258,18 +164,6 @@ export function retainedConversationPlan(input, options = {}) {
|
|
|
258
164
|
assistantEstimatedTokens: assistant.used,
|
|
259
165
|
};
|
|
260
166
|
}
|
|
261
|
-
/**
|
|
262
|
-
* @param {unknown[] | null | undefined} input
|
|
263
|
-
* @param {RetentionOptions} [options]
|
|
264
|
-
*/
|
|
265
|
-
export function retainedConversationInput(input, options = {}) {
|
|
266
|
-
return retainedConversationPlan(input, options).items;
|
|
267
|
-
}
|
|
268
|
-
/** @param {unknown[] | null | undefined} items */
|
|
269
|
-
export function hasRetainedCompactionInput(items) {
|
|
270
|
-
return (items ?? []).some((item) => isRetainedClientItem(item) || isRetainedAssistantItem(item));
|
|
271
|
-
}
|
|
272
|
-
/** @param {Message | null | undefined} message */
|
|
273
167
|
function hasCompactionId(source) {
|
|
274
168
|
return (isCompactCheckpointSource(source) &&
|
|
275
169
|
"compactionId" in source &&
|
|
@@ -301,7 +195,7 @@ export function assertSupportedCheckpointMessage(message) {
|
|
|
301
195
|
export async function activeCompactionId(ctx, session) {
|
|
302
196
|
if (!session)
|
|
303
197
|
return undefined;
|
|
304
|
-
|
|
198
|
+
const ended = new Set();
|
|
305
199
|
const lease = await observeEvents(ctx, session);
|
|
306
200
|
try {
|
|
307
201
|
const events = lease.events;
|
|
@@ -322,10 +216,27 @@ export async function activeCompactionId(ctx, session) {
|
|
|
322
216
|
}
|
|
323
217
|
}
|
|
324
218
|
/**
|
|
325
|
-
*
|
|
326
|
-
*
|
|
219
|
+
* On "in-history" routes DSH records every system prompt change as a full later `system` message
|
|
220
|
+
* (source `system-prompt`). A checkpoint begins a new DSH series whose head is re-rendered with the
|
|
221
|
+
* CURRENT prompt, so those snapshots in the retained window are superseded by that head. Retaining
|
|
222
|
+
* one, as developer/system or even as user text, would keep a stale full prompt (for example plan-mode
|
|
223
|
+
* policy that has since been switched off) in front of the model after the compaction.
|
|
224
|
+
*
|
|
225
|
+
* Only items the serializer traced back to such snapshots (`supersededItemIndices`, absolute positions
|
|
226
|
+
* in `input`) are removed. Any other retained developer/system item is genuine client context and
|
|
227
|
+
* stays. The ephemeral prelude is removed by the caller's slice, and the retention budget is computed
|
|
228
|
+
* over what is actually kept.
|
|
327
229
|
*/
|
|
328
|
-
|
|
230
|
+
function dropSupersededItems(input, preludeCount, superseded) {
|
|
231
|
+
const drop = new Set();
|
|
232
|
+
for (const position of superseded) {
|
|
233
|
+
if (!Number.isSafeInteger(position) || position < preludeCount || position >= input.length)
|
|
234
|
+
throw Object.assign(new Error("Native compaction superseded-item provenance is invalid"), { code: "LCX_COMPACTION_PRELUDE_PROVENANCE_INVALID" });
|
|
235
|
+
drop.add(position);
|
|
236
|
+
}
|
|
237
|
+
return input.slice(preludeCount).filter((_, offset) => !drop.has(preludeCount + offset));
|
|
238
|
+
}
|
|
239
|
+
export async function createNativeCheckpointBlock({ ctx, session, route, result, input = [], ephemeralPreludeItemCount = 0, supersededItemIndices = [], imageMap, retentionOptions = {}, }) {
|
|
329
240
|
const compactionId = await activeCompactionId(ctx, session);
|
|
330
241
|
if (!compactionId) {
|
|
331
242
|
const error = new Error("Native compaction could not correlate the active DSH compaction transaction");
|
|
@@ -337,8 +248,7 @@ export async function createNativeCheckpointBlock({ ctx, session, route, result,
|
|
|
337
248
|
ephemeralPreludeItemCount > input.length) {
|
|
338
249
|
throw Object.assign(new Error("Native compaction prelude provenance is invalid"), { code: "LCX_COMPACTION_PRELUDE_PROVENANCE_INVALID" });
|
|
339
250
|
}
|
|
340
|
-
const retention = retainedConversationPlan(input
|
|
341
|
-
/** @type {NativeOutputItem[]} */
|
|
251
|
+
const retention = retainedConversationPlan(dropSupersededItems(input, ephemeralPreludeItemCount, supersededItemIndices), retentionOptions);
|
|
342
252
|
const nativeOutput = persistNativeImageReferences([...retention.items, structuredClone(result.compaction)], imageMap ?? new Map());
|
|
343
253
|
return {
|
|
344
254
|
type: NATIVE_BLOCK_TYPE,
|
|
@@ -358,11 +268,6 @@ export async function createNativeCheckpointBlock({ ctx, session, route, result,
|
|
|
358
268
|
createdAt: Date.now(),
|
|
359
269
|
};
|
|
360
270
|
}
|
|
361
|
-
/**
|
|
362
|
-
* @param {NativeCheckpointBlock} block
|
|
363
|
-
* @param {unknown} usage
|
|
364
|
-
* @returns {UnknownRecord[]}
|
|
365
|
-
*/
|
|
366
271
|
export function nativeCheckpointChunks(block, usage) {
|
|
367
272
|
const text = "LCX Native V2 checkpoint saved in the DSH session log.";
|
|
368
273
|
return [
|
|
@@ -375,11 +280,6 @@ export function nativeCheckpointChunks(block, usage) {
|
|
|
375
280
|
{ type: "finish", reason: { kind: "stop" } },
|
|
376
281
|
];
|
|
377
282
|
}
|
|
378
|
-
/**
|
|
379
|
-
* @param {Session | null | undefined} session
|
|
380
|
-
* @param {string | null | undefined} compactionId
|
|
381
|
-
* @returns {Promise<CompactionSummaryEvent | undefined>}
|
|
382
|
-
*/
|
|
383
283
|
export async function compactionSummaryEvent(ctx, session, compactionId) {
|
|
384
284
|
if (!session || !compactionId)
|
|
385
285
|
return undefined;
|
|
@@ -400,11 +300,9 @@ export async function compactionSummaryEvent(ctx, session, compactionId) {
|
|
|
400
300
|
}
|
|
401
301
|
/**
|
|
402
302
|
* rawOutput elements remain untrusted until stateFromSummaryEvent validates them.
|
|
403
|
-
* @param {CompactionSummaryEvent | null | undefined} event
|
|
404
|
-
* @returns {NativeCheckpointCandidate | undefined}
|
|
405
303
|
*/
|
|
406
304
|
function isNativeCheckpointCandidate(value) {
|
|
407
|
-
if (!
|
|
305
|
+
if (!isRecord(value))
|
|
408
306
|
return false;
|
|
409
307
|
return (value.type === NATIVE_BLOCK_TYPE && value.version === NATIVE_BLOCK_VERSION);
|
|
410
308
|
}
|
|
@@ -420,21 +318,16 @@ function nativeBlockFromSummary(event) {
|
|
|
420
318
|
return undefined;
|
|
421
319
|
}
|
|
422
320
|
function assertNoMigratedNativeBlock(raw) {
|
|
423
|
-
if (raw?.some(value =>
|
|
321
|
+
if (raw?.some(value => isRecord(value) && typeof value.type === "string" &&
|
|
424
322
|
value.type.startsWith(MIGRATED_NATIVE_BLOCK_PREFIX)))
|
|
425
323
|
throw Object.assign(new Error("Unsupported LCX checkpoint format; start a new session"), {
|
|
426
324
|
code: "LCX_CHECKPOINT_UNSUPPORTED",
|
|
427
325
|
});
|
|
428
326
|
}
|
|
429
|
-
/** @param {unknown} value */
|
|
430
327
|
function validateCount(value) {
|
|
431
328
|
return (value === undefined ||
|
|
432
329
|
(typeof value === "number" && Number.isSafeInteger(value) && value >= 0));
|
|
433
330
|
}
|
|
434
|
-
/**
|
|
435
|
-
* @param {CompactionSummaryEvent | null | undefined} event
|
|
436
|
-
* @returns {NativeCheckpointBlock | undefined}
|
|
437
|
-
*/
|
|
438
331
|
export function stateFromSummaryEvent(event) {
|
|
439
332
|
if (!event)
|
|
440
333
|
return undefined;
|
|
@@ -460,12 +353,12 @@ export function stateFromSummaryEvent(event) {
|
|
|
460
353
|
catch {
|
|
461
354
|
return undefined;
|
|
462
355
|
}
|
|
463
|
-
const compactions = block.nativeOutput.filter((item) =>
|
|
356
|
+
const compactions = block.nativeOutput.filter((item) => isRecord(item) &&
|
|
464
357
|
item.type === "compaction" &&
|
|
465
358
|
typeof item.encrypted_content === "string");
|
|
466
359
|
if (compactions.length !== 1 || compactions[0].encrypted_content.trim().length === 0)
|
|
467
360
|
return undefined;
|
|
468
|
-
const compactionIndex = block.nativeOutput.findIndex((item) =>
|
|
361
|
+
const compactionIndex = block.nativeOutput.findIndex((item) => isRecord(item) && item.type === "compaction");
|
|
469
362
|
const prefix = compactionIndex >= 0 ? block.nativeOutput.slice(0, compactionIndex) : [];
|
|
470
363
|
{
|
|
471
364
|
const clients = prefix.filter(isRetainedClientItem).length;
|
|
@@ -504,10 +397,6 @@ export function stateFromSummaryEvent(event) {
|
|
|
504
397
|
};
|
|
505
398
|
return { ...base, type: NATIVE_BLOCK_TYPE, version: NATIVE_BLOCK_VERSION };
|
|
506
399
|
}
|
|
507
|
-
/**
|
|
508
|
-
* @param {Session} session
|
|
509
|
-
* @param {Message} message
|
|
510
|
-
*/
|
|
511
400
|
export function checkpointStateForMessage(ctx, session, message) {
|
|
512
401
|
assertSupportedCheckpointMessage(message);
|
|
513
402
|
const id = compactCheckpointId(message);
|
|
@@ -518,28 +407,22 @@ export function checkpointStateForMessage(ctx, session, message) {
|
|
|
518
407
|
async function checkpointStateForMessageAsync(ctx, session, id) {
|
|
519
408
|
const event = await compactionSummaryEvent(ctx, session, id);
|
|
520
409
|
const state = stateFromSummaryEvent(event);
|
|
521
|
-
const hasNativeBlock = event?.data.rawOutput?.some(value =>
|
|
410
|
+
const hasNativeBlock = event?.data.rawOutput?.some(value => isRecord(value) && typeof value.type === "string" &&
|
|
522
411
|
(value.type.startsWith("lcx-native-compaction-") || value.type.startsWith(MIGRATED_NATIVE_BLOCK_PREFIX)));
|
|
523
412
|
if (hasNativeBlock && !state)
|
|
524
413
|
throw Object.assign(new Error("Unsupported or invalid LCX checkpoint; start a new session"), { code: "LCX_CHECKPOINT_UNSUPPORTED" });
|
|
525
414
|
return state;
|
|
526
415
|
}
|
|
527
|
-
/**
|
|
528
|
-
* @param {NativeCheckpointBlock} state
|
|
529
|
-
* @param {RouteIdentity} route
|
|
530
|
-
* @param {unknown} ctx
|
|
531
|
-
*/
|
|
532
416
|
export function stateRouteCompatible(state, route, ctx) {
|
|
533
417
|
return routeCompatible(state, route, ctx);
|
|
534
418
|
}
|
|
535
419
|
const DSH_PROJECTION_UNAVAILABLE_PREFIX = 'session message projection "';
|
|
536
420
|
const DSH_PROJECTION_UNAVAILABLE_SUFFIX = '" was removed or replaced; restore the session with its owning plugin';
|
|
537
421
|
/**
|
|
538
|
-
* DSH
|
|
422
|
+
* DSH attaches no error code or subclass to this failure and exposes no
|
|
539
423
|
* public inventory of required or active projections, so message shape is the
|
|
540
424
|
* only available signal. Both classes fail closed, so a misclassification
|
|
541
425
|
* costs precision in the error code, not correctness.
|
|
542
|
-
* @param {unknown} cause
|
|
543
426
|
*/
|
|
544
427
|
function isMissingOrReplacedProjectionError(cause) {
|
|
545
428
|
if (!(cause instanceof Error))
|
|
@@ -557,15 +440,12 @@ function checkpointHistoryReadError(cause) {
|
|
|
557
440
|
}), { code: "LCX_CHECKPOINT_UNSUPPORTED" });
|
|
558
441
|
}
|
|
559
442
|
/**
|
|
560
|
-
* DSH
|
|
443
|
+
* DSH derives a message only while every `SessionMessageProjection` the
|
|
561
444
|
* durable log committed is still registered; an absent or replaced owner throws
|
|
562
445
|
* instead of yielding pre-projection content. Reconstructing the shadowed
|
|
563
446
|
* transcript without that owner would resurrect content a durable decision
|
|
564
447
|
* (for example `image/offload`) removed, so this is a checkpoint
|
|
565
448
|
* incompatibility rather than a recoverable read.
|
|
566
|
-
* @param {Session} session
|
|
567
|
-
* @param {number} seq
|
|
568
|
-
* @returns {Message | undefined}
|
|
569
449
|
*/
|
|
570
450
|
function eventMessage(session, events, seq) {
|
|
571
451
|
if (!Number.isSafeInteger(seq) || seq < 0)
|
|
@@ -582,7 +462,6 @@ function eventMessage(session, events, seq) {
|
|
|
582
462
|
throw checkpointHistoryReadError(cause);
|
|
583
463
|
}
|
|
584
464
|
}
|
|
585
|
-
/** @param {Message} message */
|
|
586
465
|
function estimateChars(message) {
|
|
587
466
|
try {
|
|
588
467
|
const encoded = JSON.stringify(message);
|
|
@@ -592,11 +471,6 @@ function estimateChars(message) {
|
|
|
592
471
|
return undefined;
|
|
593
472
|
}
|
|
594
473
|
}
|
|
595
|
-
/**
|
|
596
|
-
* @param {Session} session
|
|
597
|
-
* @param {string} compactionId
|
|
598
|
-
* @returns {Promise<Message[]>}
|
|
599
|
-
*/
|
|
600
474
|
async function expandedCheckpointMessages(ctx, session, compactionId) {
|
|
601
475
|
const lease = await observeEvents(ctx, session);
|
|
602
476
|
try {
|
|
@@ -642,20 +516,10 @@ async function expandedCheckpointMessages(ctx, session, compactionId) {
|
|
|
642
516
|
lease[Symbol.dispose]();
|
|
643
517
|
}
|
|
644
518
|
}
|
|
645
|
-
/**
|
|
646
|
-
* @param {Session} session
|
|
647
|
-
* @param {string} compactionId
|
|
648
|
-
* @returns {Promise<Message[]>}
|
|
649
|
-
*/
|
|
650
519
|
export async function shadowedMessagesForCheckpoint(ctx, session, compactionId) {
|
|
651
520
|
return expandedCheckpointMessages(ctx, session, compactionId);
|
|
652
521
|
}
|
|
653
|
-
/**
|
|
654
|
-
* @param {readonly Message[]} messages
|
|
655
|
-
* @returns {Message[][]}
|
|
656
|
-
*/
|
|
657
522
|
function groupMessages(messages) {
|
|
658
|
-
/** @type {Message[][]} */
|
|
659
523
|
const groups = [];
|
|
660
524
|
for (let index = 0; index < messages.length; index += 1) {
|
|
661
525
|
const message = messages[index];
|
|
@@ -695,20 +559,31 @@ function groupMessages(messages) {
|
|
|
695
559
|
return groups;
|
|
696
560
|
}
|
|
697
561
|
/**
|
|
698
|
-
*
|
|
699
|
-
*
|
|
700
|
-
*
|
|
701
|
-
*
|
|
562
|
+
* A shadowed `system/message` (source `system-prompt`) is a full system prompt snapshot DSH appended
|
|
563
|
+
* when the prompt changed on an "in-history" route. Portable replay is only used when the checkpoint
|
|
564
|
+
* cannot be replayed natively (fork, route or model migration), and the request that follows always
|
|
565
|
+
* carries a head rendered with the current prompt, so every snapshot in the shadowed range is
|
|
566
|
+
* superseded: replaying it, even as user text, would put stale instructions (for example plan-mode
|
|
567
|
+
* policy that was switched off) back in front of the model.
|
|
568
|
+
*
|
|
569
|
+
* Only messages DSH itself marks as system-prompt snapshots are dropped. The head is never inside a
|
|
570
|
+
* compaction range, and a Session V3 log (where later system messages were ordinary context) cannot
|
|
571
|
+
* reach this path: LCX refuses V3 checkpoints before transport. Other system-role messages without that
|
|
572
|
+
* source keep the previous ordinary-context mapping.
|
|
702
573
|
*/
|
|
574
|
+
export function withoutSupersededPromptSnapshots(messages) {
|
|
575
|
+
return messages.filter((message) => !(message.role === "system" && message.source?.kind === "system-prompt"));
|
|
576
|
+
}
|
|
703
577
|
export async function portableMessagesForCheckpoint(ctx, session, compactionId, options = {}) {
|
|
704
578
|
const configuredMaxChars = options.maxChars;
|
|
705
579
|
const maxChars = typeof configuredMaxChars === "number" &&
|
|
706
580
|
Number.isSafeInteger(configuredMaxChars) &&
|
|
707
581
|
configuredMaxChars > 0
|
|
708
582
|
? configuredMaxChars
|
|
709
|
-
:
|
|
583
|
+
: DEFAULTS.portableReplayMaxChars;
|
|
710
584
|
const maxTokens = portableTokenCeiling(maxChars) ?? 1;
|
|
711
|
-
|
|
585
|
+
// Superseded prompt snapshots are removed before grouping so they never consume the replay budget.
|
|
586
|
+
const expanded = withoutSupersededPromptSnapshots(await shadowedMessagesForCheckpoint(ctx, session, compactionId));
|
|
712
587
|
if (expanded.length === 0)
|
|
713
588
|
return [];
|
|
714
589
|
const groups = groupMessages(expanded);
|