dsh-lcx-codex 0.5.2 → 0.6.0-pre.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.
package/lib/index.js CHANGED
@@ -12,11 +12,11 @@ import { join } from "node:path";
12
12
  import { AsyncLocalStorage } from "node:async_hooks";
13
13
  import { getSupportedThinkingLevels } from "./pi-responses-runtime.js";
14
14
  import { fetchJsonWithRetry } from "./transport.js";
15
+ import { DEFAULT_TOOL_UPDATES, planToolSurface, withoutDeferLoading } from "./tool-surface.js";
15
16
  import { agentSessionId, agentUsesSession, compactionConfigState, compactionPatchCandidate, installCompactionPatch, patchCompactionConfig, patchToolResultPruner, readAgentRouteState, readSelectedAgentRouteState, recordAgentRouteEvent, resolveAgentService, resolveContextService, resolveScopedService, restoreCompactionConfig, restoreCompactionPatches, restoreToolResultPruner, sessionFor, sessionFromAgent, scopedToolRuntime, onScopedToolExecute, tokenMeterTotal, toolResultPrunerState, } from "./dsh-compat.js";
16
17
  import { InvocationPolicyScope } from "./invocation-policy-scope.js";
17
- import { resolveCatalogModel } from "./model-catalog.js";
18
- import { authenticatedGrokHeaders, authenticatedHeaders, currentRoute, generationControlsFromSession, grokPromptCacheSessionId, promptCacheKey, promptCacheRetention, promptCacheSessionId, diagnoseResponsesRoute, resolveGrokResponsesRouteConfig, resolveResponsesRouteConfig, routeFingerprint, } from "./route.js";
19
- import { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, assertAggregateImageBudget, hydrateNativeImageReferences, inputImageBase64Lengths, resolveRouteModelFacts, responseInputItems, serializeDshMessages, } from "./dsh-responses.js";
18
+ import { authenticatedGrokHeaders, authenticatedHeaders, currentRoute, generationControlsFromSession, grokPromptCacheSessionId, promptCacheKey, promptCacheRetention, promptCacheSessionId, resolveGrokResponsesRouteConfig, resolveResponsesRouteConfig, routeFingerprint, } from "./route.js";
19
+ import { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, assertAggregateImageBudget, hydrateNativeImageReferences, inputImageBase64Lengths, resolveRouteModelFacts, hasMidHistoryPromptSnapshot, responseInputItems, serializeDshMessages, } from "./dsh-responses.js";
20
20
  import { mergeFeatureHeader, requestNativeCompaction } from "./compact-v2.js";
21
21
  import { buildResponsesBody } from "./responses-request.js";
22
22
  import { managedFailureChunk, streamResponsesRequest, } from "./responses-stream.js";
@@ -58,6 +58,7 @@ function routeWithPolicies(route, config) {
58
58
  portableReplayMaxChars: config.portableReplayMaxChars,
59
59
  requestImageMaxBytes: route.requestImageMaxBytes ?? config.requestImageMaxBytes,
60
60
  requestImagePixelBudget: route.requestImagePixelBudget ?? config.requestImagePixelBudget,
61
+ toolUpdates: config.toolUpdates,
61
62
  webMaxResults: config.webMaxResults,
62
63
  };
63
64
  }
@@ -101,6 +102,7 @@ export const Config = z.object({
101
102
  grokNativeWebSearch: z.boolean().default(false).volatile(),
102
103
  grokNativeXSearch: z.boolean().default(false).volatile(),
103
104
  searchMediaPreview: z.boolean().default(false).volatile(),
105
+ incrementalToolUpdates: z.boolean().default(true).volatile(),
104
106
  supportsLongCacheRetention: z.boolean().default(false),
105
107
  supportsExplicitPromptCacheMode: z.boolean().default(false),
106
108
  alphaCapabilityPath: z.string().default(""),
@@ -148,6 +150,7 @@ function normalizeConfig(input = {}) {
148
150
  ? input.assistantRetentionTokenReserve
149
151
  : 24_000,
150
152
  assistantRetentionPerMessageTokenCap: positiveInteger(input.assistantRetentionPerMessageTokenCap, 3_000),
153
+ toolUpdates: DEFAULT_TOOL_UPDATES,
151
154
  };
152
155
  }
153
156
  function webError(message, code, cause) {
@@ -551,19 +554,38 @@ function mergeMap(target, source) {
551
554
  target.set(key, value);
552
555
  }
553
556
  /**
554
- * The model facts a GPT route serializes and sends with. What DSH reported for the exact routed model
555
- * (reasoning capability, context window) wins over the bundled catalog entry, and a profile's declared
556
- * `reasoningEfforts` (wire mapping, or `false` for a non-reasoning model) wins over both. A model with no
557
- * catalog entry follows what DSH itself resolved for it instead of being assumed to reason.
557
+ * Follow traced item positions through the Grok replay restoration, which may splice items. When the
558
+ * item count is unchanged the positions are exact; otherwise each traced item is found again by exact
559
+ * structural equality in order (restoration never alters instruction items).
560
+ */
561
+ function locateSupersededItems(original, restored, positions) {
562
+ if (positions.length === 0 || original.length === restored.length)
563
+ return [...positions];
564
+ const located = [];
565
+ let cursor = 0;
566
+ for (const position of positions) {
567
+ const wanted = JSON.stringify(original[position]);
568
+ while (cursor < restored.length && JSON.stringify(restored[cursor]) !== wanted)
569
+ cursor += 1;
570
+ if (cursor >= restored.length)
571
+ throw Object.assign(new Error("LCX lost track of a superseded system prompt item"), { code: "LCX_RESPONSES_INVALID_INPUT" });
572
+ located.push(cursor);
573
+ cursor += 1;
574
+ }
575
+ return located;
576
+ }
577
+ /**
578
+ * The reasoning projection a GPT route serializes and sends with. A profile's declared
579
+ * `reasoningEfforts` wins (wire mapping, or `false` for a non-reasoning model); otherwise the catalog
580
+ * entry stands, and a model with no catalog entry follows what DSH itself resolved for it instead of
581
+ * being assumed to reason.
558
582
  */
559
583
  function gptModelOverride(routeConfig, facts) {
584
+ if (routeConfig.modelControls !== undefined)
585
+ return { ...routeConfig.modelControls };
560
586
  if (!facts.resolved)
561
- return routeConfig.modelControls === undefined ? undefined : { ...routeConfig.modelControls };
562
- return {
563
- reasoning: facts.reasoning !== undefined,
564
- ...(facts.contextWindow === undefined ? {} : { contextWindow: facts.contextWindow }),
565
- ...routeConfig.modelControls,
566
- };
587
+ return undefined;
588
+ return { reasoning: facts.reasoning !== undefined };
567
589
  }
568
590
  /** Refuse an effort DSH does not offer for this model, the way the DSH adapter and the Grok route do. */
569
591
  function assertSupportedReasoningEffort(effort, facts) {
@@ -574,42 +596,77 @@ function assertSupportedReasoningEffort(effort, facts) {
574
596
  return;
575
597
  throw Object.assign(new Error(`Responses route does not support reasoning effort "${String(effort)}"`), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
576
598
  }
577
- const reportedCatalogGaps = new Set();
599
+ const reportedToolSurfaceFallbacks = new Set();
578
600
  /**
579
- * Say once per route model that LCX's bundled Pi catalog does not describe it, so its wire capabilities
580
- * (strict tools, grammar tools, cache mode) come from the profile and from what DSH declares instead.
601
+ * Say why a request carries the complete tool list although the policy asked for addition-only updates.
602
+ * One line per session and reason: a warning when the host declared addition-only (LCX should have been able to
603
+ * follow), a debug line otherwise. The reasons are fixed phrases; no request content, key or header is logged.
581
604
  */
582
- function reportCatalogGap(ctx, route) {
583
- const source = resolveCatalogModel(route.provider, route.model, true).source;
584
- if (source !== "catalog-provider-default" && source !== "frozen-pi086")
585
- return;
586
- const key = `${route.provider}/${route.model}`;
587
- if (reportedCatalogGaps.has(key))
605
+ function reportToolSurfaceFallback(ctx, route, surface) {
606
+ const reason = surface.fallbackReason ?? "unknown";
607
+ const key = `${route.sessionId}${reason}`;
608
+ if (reportedToolSurfaceFallbacks.has(key))
588
609
  return;
589
- if (reportedCatalogGaps.size >= 256)
590
- reportedCatalogGaps.clear();
591
- reportedCatalogGaps.add(key);
592
- ctx.logger?.debug?.(`[lcx-codex] ${key} is not described by LCX's bundled model catalog; its wire capabilities follow the profile and what DSH declares for the model`);
610
+ if (reportedToolSurfaceFallbacks.size >= 256)
611
+ reportedToolSurfaceFallbacks.clear();
612
+ reportedToolSurfaceFallbacks.add(key);
613
+ const message = `[lcx-codex] tool updates: sending the complete tool list on ${route.provider}/${route.model} (${reason})`;
614
+ if (surface.fallbackUnexpected === true)
615
+ ctx.logger?.warn?.(message);
616
+ else
617
+ ctx.logger?.debug?.(message);
593
618
  }
594
- async function serializeNativeAware(messages, route, routeConfig, ctx, options) {
619
+ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, options) {
595
620
  const session = sessionFor(ctx, route.sessionId);
621
+ // One adapter-owned lookup supplies image support and the route's system-prompt update mode. On an
622
+ // "in-history" route DSH appends a changed prompt as a later full system message; it must reach the
623
+ // model in place with instruction authority instead of being demoted to user context.
596
624
  const modelFacts = await resolveRouteModelFacts(ctx, route, options.signal);
597
- if (options.grokNativeReplayRoute === undefined)
598
- reportCatalogGap(ctx, route);
599
- const { imageSupport } = modelFacts;
625
+ const { imageSupport, systemPromptUpdate, resolved } = modelFacts;
600
626
  const modelOverride = options.grokNativeReplayRoute === undefined
601
627
  ? gptModelOverride(routeConfig, modelFacts)
602
628
  : undefined;
629
+ let messages = rawMessages;
630
+ let tools = options.tools;
631
+ let anchoredTools = false;
632
+ let toolSurfaceCompat;
633
+ if (options.toolSurface !== undefined) {
634
+ const surface = planToolSurface({
635
+ mode: routeConfig.toolUpdates ?? DEFAULT_TOOL_UPDATES,
636
+ messages: rawMessages,
637
+ tools: options.tools,
638
+ toolHistory: options.toolSurface.history,
639
+ facts: modelFacts,
640
+ compat: routeConfig.responsesCompat,
641
+ });
642
+ messages = surface.messages;
643
+ tools = surface.tools;
644
+ anchoredTools = surface.anchored;
645
+ toolSurfaceCompat = surface.compat;
646
+ if (surface.fallbackReason !== undefined)
647
+ reportToolSurfaceFallback(ctx, route, surface);
648
+ }
649
+ // An unreadable model mode must not demote instructions: history that holds a DSH prompt snapshot after
650
+ // the head can only come from an in-history route, and sending it as user text would silently lower its
651
+ // authority. Without such history the conservative default is harmless, so ordinary requests proceed.
652
+ if (!resolved && hasMidHistoryPromptSnapshot(messages))
653
+ throw Object.assign(new Error("LCX could not read the route's model information, so in-history system prompt updates cannot be sent safely; retry once the route is available"), { code: "LCX_SYSTEM_PROMPT_MODE_UNKNOWN" });
603
654
  const input = [];
604
655
  const imageMap = new Map();
605
656
  let nativeTools;
606
657
  let nativeModel;
607
658
  let grammarToolInputProperties;
608
659
  let normal = [];
660
+ /** Absolute `input` positions rendered from DSH prompt snapshots that a checkpoint supersedes. */
661
+ const supersededInstructionItems = [];
609
662
  const serializeOptions = (imageMapOverride = undefined, extra = {}) => requestImageOptions(routeConfig, imageSupport, options.signal, imageMapOverride, {
610
663
  route,
611
- tools: options.tools,
612
- responsesCompat: routeConfig.responsesCompat,
664
+ tools,
665
+ responsesCompat: toolSurfaceCompat === undefined
666
+ ? routeConfig.responsesCompat
667
+ : { ...routeConfig.responsesCompat, ...toolSurfaceCompat },
668
+ systemPromptUpdate,
669
+ ...(anchoredTools ? { toolUpdate: "addition-only" } : {}),
613
670
  ...(modelOverride === undefined ? {} : { model: modelOverride }),
614
671
  ...extra,
615
672
  });
@@ -640,11 +697,18 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
640
697
  normal = [];
641
698
  const serialized = await serializeDshMessages(batch, ctx, serializeOptions());
642
699
  surfaceImageBytes.push(...inputImageBase64Lengths(serialized.input));
700
+ // Every batch derives its request tools from the same immediate declarations; a difference would
701
+ // change the cached prefix between batches of one request.
702
+ if (anchoredTools && JSON.stringify(serialized.tools) !== JSON.stringify(nativeTools))
703
+ throw Object.assign(new Error("LCX anchored tool declarations diverged between serialization batches"), { code: "LCX_RESPONSES_INVALID_INPUT" });
643
704
  nativeTools = serialized.tools ?? nativeTools;
644
705
  nativeModel = serialized.model ?? nativeModel;
645
706
  grammarToolInputProperties =
646
707
  serialized.grammarToolInputProperties ?? grammarToolInputProperties;
647
- input.push(...restoreGrokNativeReplay(serialized.input, batch.filter(isPersistedMessage), options.grokNativeReplayRoute));
708
+ const restored = restoreGrokNativeReplay(serialized.input, batch.filter(isPersistedMessage), options.grokNativeReplayRoute);
709
+ const base = input.length;
710
+ supersededInstructionItems.push(...locateSupersededItems(serialized.input, restored, serialized.supersededInstructionItems).map((position) => base + position));
711
+ input.push(...restored);
648
712
  mergeMap(imageMap, serialized.imageMap);
649
713
  };
650
714
  for (const message of surfaceMessages ?? []) {
@@ -671,7 +735,17 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
671
735
  }
672
736
  else {
673
737
  const portable = await portableMessagesForCheckpoint(ctx, session, checkpointId, { maxChars: routeConfig.portableReplayMaxChars });
674
- const serialized = await serializeDshMessages(portable, ctx, serializeOptions());
738
+ // Shadowed history is superseded by the head DSH re-rendered when the compaction began its new
739
+ // series, so its old system messages stay ordinary context instead of regaining instruction
740
+ // authority behind the current prompt.
741
+ const serialized = await serializeDshMessages(portable, ctx,
742
+ // Portable history is plain context: it carries no developer updates, and deferred declarations are
743
+ // only meaningful to the anchored surface itself.
744
+ serializeOptions(undefined, {
745
+ systemPromptUpdate: undefined,
746
+ toolUpdate: undefined,
747
+ tools: tools?.map((tool) => withoutDeferLoading(tool)),
748
+ }));
675
749
  for (const bytes of inputImageBase64Lengths(serialized.input))
676
750
  fixedImageBytes += bytes;
677
751
  input.push(...serialized.input);
@@ -694,6 +768,7 @@ async function serializeNativeAware(messages, route, routeConfig, ctx, options)
694
768
  return {
695
769
  input,
696
770
  ephemeralPreludeItemCount,
771
+ supersededInstructionItems,
697
772
  imageMap,
698
773
  imageSupport,
699
774
  tools: nativeTools,
@@ -713,11 +788,14 @@ function fallbackEligible(error, signal) {
713
788
  return ["LCX_HTTP_RETRYABLE", "LCX_RETRY_EXHAUSTED"].includes(String(details.code ?? ""));
714
789
  }
715
790
  async function* remoteCompactionStream(options, routeConfig, ctx, next) {
716
- const projected = projectToolUpdates(options.messages, options.tools, undefined, options.toolHistory);
717
- const history = stripCompactionDirective(projected.messages);
791
+ const history = stripCompactionDirective(options.messages);
718
792
  const route = currentRoute(options, routeConfig);
719
793
  try {
720
- const prepared = await serializeNativeAware(history, route, routeConfig, ctx, { signal: options.signal, tools: projected.tools && [...projected.tools] });
794
+ const prepared = await serializeNativeAware(history, route, routeConfig, ctx, {
795
+ signal: options.signal,
796
+ tools: options.tools && [...options.tools],
797
+ toolSurface: { history: options.toolHistory },
798
+ });
721
799
  const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
722
800
  const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId);
723
801
  const generation = generationControlsFromSession(sessionFor(ctx, route.sessionId), route);
@@ -726,7 +804,7 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
726
804
  model: route.model,
727
805
  modelDescriptor: prepared.model,
728
806
  input: prepared.input,
729
- tools: prepared.tools ?? (projected.tools && [...projected.tools]),
807
+ tools: prepared.tools,
730
808
  promptCacheKey: promptCacheKey(route, routeConfig, ctx),
731
809
  promptCacheRetention: promptCacheRetention(routeConfig),
732
810
  cacheRetention: routeConfig.cacheRetention,
@@ -750,6 +828,7 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
750
828
  result,
751
829
  input: prepared.input,
752
830
  ephemeralPreludeItemCount: prepared.ephemeralPreludeItemCount,
831
+ supersededItemIndices: prepared.supersededInstructionItems,
753
832
  imageMap: prepared.imageMap,
754
833
  retentionOptions: {
755
834
  tokenBudget: routeConfig.nativeRetentionTokenBudget,
@@ -990,7 +1069,7 @@ function inputHasNativeState(input) {
990
1069
  /**
991
1070
  * One redacted line per failed provider request. Only the LCX class and machine-safe provider
992
1071
  * identifiers are logged: never the provider message, a payload, a header or a credential.
993
- * `warn` matches the compaction failure line; DSH ships no persistent plugin-logger exporter by default
1072
+ * `warn` matches the compaction failure line; DSH 0.2.1-alpha.2 ships no persistent logger exporter
994
1073
  * (see ERRORS.md), so the line is visible wherever the host attaches one.
995
1074
  */
996
1075
  function logStreamFailure(ctx, route, info) {
@@ -1014,15 +1093,18 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
1014
1093
  try {
1015
1094
  if (options.stop !== undefined)
1016
1095
  throw Object.assign(new Error("LCX Responses does not support GenerateOptions.stop"), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
1017
- const projected = projectToolUpdates(options.messages, options.tools, undefined, options.toolHistory);
1018
- const prepared = await serializeNativeAware(projected.messages, route, routeConfig, ctx, { signal: options.signal, tools: projected.tools && [...projected.tools] });
1096
+ const prepared = await serializeNativeAware(options.messages, route, routeConfig, ctx, {
1097
+ signal: options.signal,
1098
+ tools: options.tools && [...options.tools],
1099
+ toolSurface: { history: options.toolHistory },
1100
+ });
1019
1101
  assertSupportedReasoningEffort(options.reasoningEffort, prepared.modelFacts);
1020
1102
  const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
1021
1103
  const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId);
1022
1104
  const body = buildResponsesBody({
1023
1105
  model: prepared.model,
1024
1106
  input: prepared.input,
1025
- tools: prepared.tools ?? (projected.tools && [...projected.tools]),
1107
+ tools: prepared.tools,
1026
1108
  sessionId: cacheSessionId,
1027
1109
  promptCacheKey: promptCacheKey(route, routeConfig, ctx),
1028
1110
  promptCacheRetention: promptCacheRetention(routeConfig),
@@ -1132,21 +1214,8 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
1132
1214
  function isGptLifecycleTarget(options) {
1133
1215
  return /^gpt-/iu.test(String(options.model ?? ""));
1134
1216
  }
1135
- async function* unavailableManagedRouteStream(options, diagnosis = "incomplete") {
1136
- // Say what to change: the profile's models list leaves the model out, or no protocol can be inferred for it.
1137
- const notConfigured = diagnosis === "model-not-configured";
1138
- const apiUnresolved = diagnosis === "api-unresolved";
1139
- yield managedFailureChunk(Object.assign(new Error(notConfigured
1140
- ? "LCX is enabled but the selected model is not configured for this provider: the provider profile lists other models. Add the model to the profile's models list or select a listed model"
1141
- : apiUnresolved
1142
- ? "LCX could not determine the wire protocol of the selected GPT route: the provider profile sets no api and the model is unknown to the catalog. Add api: openai-responses to the provider profile, or select a model DSH recognizes"
1143
- : "LCX is enabled but the selected DSH route cannot be resolved as an authenticated OpenAI Responses wire route"), {
1144
- code: notConfigured
1145
- ? "LCX_RESPONSES_MODEL_NOT_CONFIGURED"
1146
- : apiUnresolved
1147
- ? "LCX_RESPONSES_API_UNRESOLVED"
1148
- : "LCX_RESPONSES_ROUTE_UNAVAILABLE",
1149
- }), options.signal);
1217
+ async function* unavailableManagedRouteStream(options) {
1218
+ yield managedFailureChunk(Object.assign(new Error("LCX is enabled but the selected DSH route cannot be resolved as an authenticated OpenAI Responses wire route"), { code: "LCX_RESPONSES_ROUTE_UNAVAILABLE" }), options.signal);
1150
1219
  }
1151
1220
  function installInjected(ctx, configInput) {
1152
1221
  installSearchUsage(ctx);
@@ -1200,7 +1269,11 @@ function installInjected(ctx, configInput) {
1200
1269
  state.alphaSearch = configInput.alphaSearch.get();
1201
1270
  state.grokNativeWebSearch = configInput.grokNativeWebSearch.get();
1202
1271
  state.grokNativeXSearch = configInput.grokNativeXSearch.get();
1203
- runtimeConfig = baseConfig;
1272
+ // Absent (older config shapes) means the default: addition-only. Only an explicit false selects the full list.
1273
+ runtimeConfig =
1274
+ configInput.incrementalToolUpdates?.get() === false
1275
+ ? { ...baseConfig, toolUpdates: "full" }
1276
+ : baseConfig;
1204
1277
  refreshTools();
1205
1278
  };
1206
1279
  syncSettings();
@@ -1262,11 +1335,11 @@ function installInjected(ctx, configInput) {
1262
1335
  const routeConfig = resolveResponsesRouteConfig(ctx, options, runtimeConfig);
1263
1336
  if (options.purpose === "compaction") {
1264
1337
  if (!routeConfig)
1265
- return unavailableManagedRouteStream(options, diagnoseResponsesRoute(ctx, options));
1338
+ return unavailableManagedRouteStream(options);
1266
1339
  return remoteCompactionStream(options, routeWithPolicies(routeConfig, runtimeConfig), ctx, next);
1267
1340
  }
1268
1341
  if (!routeConfig)
1269
- return unavailableManagedRouteStream(options, diagnoseResponsesRoute(ctx, options));
1342
+ return unavailableManagedRouteStream(options);
1270
1343
  return managedResponsesStream(options, routeWithPolicies(routeConfig, runtimeConfig), ctx);
1271
1344
  });
1272
1345
  ctx.effect?.(() => async () => {
@@ -321,11 +321,32 @@ export async function activeCompactionId(ctx, session) {
321
321
  lease[Symbol.dispose]();
322
322
  }
323
323
  }
324
+ /**
325
+ * On "in-history" routes DSH records every system prompt change as a full later `system` message
326
+ * (source `system-prompt`). A checkpoint begins a new DSH series whose head is re-rendered with the
327
+ * CURRENT prompt, so those snapshots in the retained window are superseded by that head. Retaining
328
+ * one, as developer/system or even as user text, would keep a stale full prompt (for example plan-mode
329
+ * policy that has since been switched off) in front of the model after the compaction.
330
+ *
331
+ * Only items the serializer traced back to such snapshots (`supersededItemIndices`, absolute positions
332
+ * in `input`) are removed. Any other retained developer/system item is genuine client context and
333
+ * stays. The ephemeral prelude is removed by the caller's slice, and the retention budget is computed
334
+ * over what is actually kept.
335
+ */
336
+ function dropSupersededItems(input, preludeCount, superseded) {
337
+ const drop = new Set();
338
+ for (const position of superseded) {
339
+ if (!Number.isSafeInteger(position) || position < preludeCount || position >= input.length)
340
+ throw Object.assign(new Error("Native compaction superseded-item provenance is invalid"), { code: "LCX_COMPACTION_PRELUDE_PROVENANCE_INVALID" });
341
+ drop.add(position);
342
+ }
343
+ return input.slice(preludeCount).filter((_, offset) => !drop.has(preludeCount + offset));
344
+ }
324
345
  /**
325
346
  * @param {CreateCheckpointOptions} options
326
347
  * @returns {Promise<NativeCheckpointV5>}
327
348
  */
328
- export async function createNativeCheckpointBlock({ ctx, session, route, result, input = [], ephemeralPreludeItemCount = 0, imageMap, retentionOptions = {}, }) {
349
+ export async function createNativeCheckpointBlock({ ctx, session, route, result, input = [], ephemeralPreludeItemCount = 0, supersededItemIndices = [], imageMap, retentionOptions = {}, }) {
329
350
  const compactionId = await activeCompactionId(ctx, session);
330
351
  if (!compactionId) {
331
352
  const error = new Error("Native compaction could not correlate the active DSH compaction transaction");
@@ -337,7 +358,7 @@ export async function createNativeCheckpointBlock({ ctx, session, route, result,
337
358
  ephemeralPreludeItemCount > input.length) {
338
359
  throw Object.assign(new Error("Native compaction prelude provenance is invalid"), { code: "LCX_COMPACTION_PRELUDE_PROVENANCE_INVALID" });
339
360
  }
340
- const retention = retainedConversationPlan(input.slice(ephemeralPreludeItemCount), retentionOptions);
361
+ const retention = retainedConversationPlan(dropSupersededItems(input, ephemeralPreludeItemCount, supersededItemIndices), retentionOptions);
341
362
  /** @type {NativeOutputItem[]} */
342
363
  const nativeOutput = persistNativeImageReferences([...retention.items, structuredClone(result.compaction)], imageMap ?? new Map());
343
364
  return {
@@ -694,6 +715,22 @@ function groupMessages(messages) {
694
715
  }
695
716
  return groups;
696
717
  }
718
+ /**
719
+ * A shadowed `system/message` (source `system-prompt`) is a full system prompt snapshot DSH appended
720
+ * when the prompt changed on an "in-history" route. Portable replay is only used when the checkpoint
721
+ * cannot be replayed natively (fork, route or model migration), and the request that follows always
722
+ * carries a head rendered with the current prompt, so every snapshot in the shadowed range is
723
+ * superseded: replaying it, even as user text, would put stale instructions (for example plan-mode
724
+ * policy that was switched off) back in front of the model.
725
+ *
726
+ * Only messages DSH itself marks as system-prompt snapshots are dropped. The head is never inside a
727
+ * compaction range, and a Session V3 log (where later system messages were ordinary context) cannot
728
+ * reach this path: LCX refuses V3 checkpoints before transport. Other system-role messages without that
729
+ * source keep the previous ordinary-context mapping.
730
+ */
731
+ export function withoutSupersededPromptSnapshots(messages) {
732
+ return messages.filter((message) => !(message.role === "system" && message.source?.kind === "system-prompt"));
733
+ }
697
734
  /**
698
735
  * @param {Session} session
699
736
  * @param {string} compactionId
@@ -708,7 +745,8 @@ export async function portableMessagesForCheckpoint(ctx, session, compactionId,
708
745
  ? configuredMaxChars
709
746
  : 80_000;
710
747
  const maxTokens = portableTokenCeiling(maxChars) ?? 1;
711
- const expanded = await shadowedMessagesForCheckpoint(ctx, session, compactionId);
748
+ // Superseded prompt snapshots are removed before grouping so they never consume the replay budget.
749
+ const expanded = withoutSupersededPromptSnapshots(await shadowedMessagesForCheckpoint(ctx, session, compactionId));
712
750
  if (expanded.length === 0)
713
751
  return [];
714
752
  const groups = groupMessages(expanded);
@@ -1705,9 +1705,6 @@ var models = {
1705
1705
  "opencode-go": responsesOnly(RESPONSE_CATALOGS["opencode-go"]),
1706
1706
  xai: responsesOnly(RESPONSE_CATALOGS.xai)
1707
1707
  };
1708
- function getCatalogModels(provider) {
1709
- return Object.hasOwn(RESPONSE_CATALOGS, provider) ? Object.values(RESPONSE_CATALOGS[provider]) : [];
1710
- }
1711
1708
  function getBuiltinProviders() {
1712
1709
  return providers;
1713
1710
  }
@@ -1722,7 +1719,6 @@ export {
1722
1719
  createGrammarToolInputProperties,
1723
1720
  getBuiltinModels,
1724
1721
  getBuiltinProviders,
1725
- getCatalogModels,
1726
1722
  getSupportedThinkingLevels,
1727
1723
  normalizeContext,
1728
1724
  processResponsesStream
@@ -210,9 +210,8 @@ export function managedFailure(error, signal, upstream) {
210
210
  else if (sourceCodes.has("LCX_GROK_NATIVE_PROTOCOL_ERROR"))
211
211
  code = "GROK_NATIVE_PROTOCOL_ERROR";
212
212
  else if (sourceCodes.has("LCX_RESPONSES_ROUTE_UNAVAILABLE") ||
213
- sourceCodes.has("LCX_RESPONSES_API_UNRESOLVED") ||
214
- sourceCodes.has("LCX_RESPONSES_MODEL_NOT_CONFIGURED") ||
215
- sourceCodes.has("LCX_RESPONSES_MODEL_UNAVAILABLE"))
213
+ sourceCodes.has("LCX_RESPONSES_MODEL_UNAVAILABLE") ||
214
+ sourceCodes.has("LCX_SYSTEM_PROMPT_MODE_UNKNOWN"))
216
215
  code = "NO_ADAPTER";
217
216
  else if (sourceCodes.has("LCX_INSUFFICIENT_QUOTA"))
218
217
  code = "INSUFFICIENT_QUOTA";
@@ -255,11 +254,9 @@ export function managedFailure(error, signal, upstream) {
255
254
  AUTH: "Responses request was rejected by authentication",
256
255
  INSUFFICIENT_QUOTA: "服务商额度不足或未满足请求预留额度,请检查中转额度(并非 API 密钥无效) / Provider quota is insufficient for this request",
257
256
  UNSUPPORTED_OPTION: "LCX Responses does not support this request option",
258
- NO_ADAPTER: sourceCodes.has("LCX_RESPONSES_MODEL_NOT_CONFIGURED")
259
- ? "LCX is enabled but the selected model is not configured for this provider (the provider profile lists other models); add it to the profile's models list or select a listed model"
260
- : sourceCodes.has("LCX_RESPONSES_API_UNRESOLVED")
261
- ? "LCX could not determine the wire protocol of the selected GPT route (the provider profile sets no api and the model is unknown to the catalog); add api: openai-responses to the provider profile or select a model DSH recognizes"
262
- : "LCX could not resolve the selected Responses route",
257
+ NO_ADAPTER: sourceCodes.has("LCX_SYSTEM_PROMPT_MODE_UNKNOWN")
258
+ ? "LCX could not read the route's model information, so in-history system prompt updates cannot be sent safely; retry once the route is available"
259
+ : "LCX could not resolve the selected Responses route",
263
260
  RATE_LIMIT: "Responses provider rate limit was reached",
264
261
  SERVER: "Responses provider returned a server failure",
265
262
  INVALID_REQUEST: sourceCodes.has("LCX_CHECKPOINT_UNSUPPORTED")
package/lib/route.js CHANGED
@@ -3,7 +3,8 @@ import { credentialRef } from "@deepseek-ai/dsh-credentials";
3
3
  import { SessionId } from "@deepseek-ai/dsh-session";
4
4
  import "@deepseek-ai/dsh-settings";
5
5
  import { attributionHeaders, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
6
- import { resolveCatalogModel } from "./model-catalog.js";
6
+ import { getBuiltinModels, getBuiltinProviders, } from "./pi-responses-runtime.js";
7
+ import { wasPi086ResponsesModel } from "./pi086-responses-allowlist.js";
7
8
  /** @param {unknown} value */
8
9
  function isRecord(value) {
9
10
  return value !== null && typeof value === "object" && !Array.isArray(value);
@@ -124,6 +125,17 @@ function copyResponsesCompat(target, source) {
124
125
  Object.assign(target, { [field]: value });
125
126
  }
126
127
  }
128
+ /** @param {unknown} provider @param {unknown} modelId */
129
+ function builtinResponsesModel(provider, modelId) {
130
+ const providerName = String(provider ?? "");
131
+ // A newer plugin-Pi catalog must not grant LCX a route that Pi 0.86 lacked.
132
+ if (!wasPi086ResponsesModel(providerName, String(modelId ?? "")))
133
+ return undefined;
134
+ const builtinProvider = getBuiltinProviders().find((candidate) => candidate === providerName);
135
+ if (builtinProvider === undefined)
136
+ return undefined;
137
+ return getBuiltinModels(builtinProvider).find((model) => model.id === String(modelId ?? "") && model.api === "openai-responses");
138
+ }
127
139
  function modelDescriptorDefaults(model) {
128
140
  if (!model)
129
141
  return undefined;
@@ -132,18 +144,6 @@ function modelDescriptorDefaults(model) {
132
144
  thinkingLevelMap: model.thinkingLevelMap,
133
145
  };
134
146
  }
135
- /** The profile lists this model in `models`: the one way DSH serves a model its catalog does not describe. */
136
- function profileDeclaresModel(profile, modelId) {
137
- return Array.isArray(profile?.models) && profile.models.some((entry) => String(entry?.id ?? "") === String(modelId));
138
- }
139
- /**
140
- * A nonempty `models` list replaces the served catalog: DSH serves exactly the listed models, and LCX answers
141
- * `llm/stream` before DSH's adapter does, so a claim here would send a request DSH itself refuses.
142
- */
143
- function profileExcludesModel(profile, modelId) {
144
- return Array.isArray(profile?.models) && profile.models.length > 0 &&
145
- !profile.models.some((entry) => String(entry?.id ?? "") === String(modelId));
146
- }
147
147
  function configuredModelProfile(profile, modelId) {
148
148
  const configuredModels = Array.isArray(profile?.models) ? profile.models : [];
149
149
  return configuredModels.length > 0
@@ -227,15 +227,11 @@ export function resolveResponsesRouteConfig(ctx, options, policy) {
227
227
  if (!isRecord(profile))
228
228
  return undefined;
229
229
  const configured = profile;
230
- if (profileExcludesModel(configured, model))
231
- return undefined;
232
- // Protocol and endpoint: the profile, then what DSH resolves for the model (see `resolveCatalogModel`).
233
- const catalog = resolveCatalogModel(provider, model, profileDeclaresModel(configured, model));
234
- const builtin = catalog.descriptor;
235
- const api = configured.api ?? catalog.api;
230
+ const builtin = builtinResponsesModel(provider, model);
231
+ const api = configured.api ?? builtin?.api;
236
232
  if (api !== "openai-responses")
237
233
  return undefined;
238
- const baseURL = configured.baseURL ?? catalog.baseUrl;
234
+ const baseURL = configured.baseURL ?? builtin?.baseUrl;
239
235
  const apiKeyEnv = configured.apiKeyEnv;
240
236
  if (typeof baseURL !== "string" || !normalizeBaseURL(baseURL) || typeof apiKeyEnv !== "string" || !apiKeyEnv.trim())
241
237
  return undefined;
@@ -243,7 +239,7 @@ export function resolveResponsesRouteConfig(ctx, options, policy) {
243
239
  copyResponsesCompat(responsesCompat, builtin?.compat);
244
240
  copyResponsesCompat(responsesCompat, lcxCapabilityRoute ? policy.responsesCompat : undefined);
245
241
  copyResponsesCompat(responsesCompat, configuredResponsesCompat(configured, model));
246
- const promptCacheModel = lcxCapabilityRoute && policy.supportsExplicitPromptCacheMode === true ? resolveCatalogModel("openai", model).descriptor : undefined;
242
+ const promptCacheModel = lcxCapabilityRoute && policy.supportsExplicitPromptCacheMode === true ? builtinResponsesModel("openai", model) : undefined;
247
243
  const promptCacheRaw = promptCacheModel;
248
244
  const promptCacheCompat = isRecord(promptCacheRaw)
249
245
  ? promptCacheRaw.compat
@@ -281,23 +277,6 @@ export function resolveResponsesRouteConfig(ctx, options, policy) {
281
277
  requestImageMaxBytes: isPositiveInteger(configured.requestImageMaxBytes) ? configured.requestImageMaxBytes : policy.requestImageMaxBytes,
282
278
  };
283
279
  }
284
- /**
285
- * Name the reason `resolveResponsesRouteConfig` returned nothing, so the failure can tell the user what to
286
- * change: `api-unresolved` means neither the profile nor the model catalog names a protocol for this model.
287
- */
288
- export function diagnoseResponsesRoute(ctx, options) {
289
- const provider = String(options?.provider ?? "");
290
- const model = String(options?.model ?? "");
291
- const profile = provider.trim() ? configuredProviderProfile(ctx, provider) : undefined;
292
- if (!isRecord(profile))
293
- return "no-profile";
294
- if (profileExcludesModel(profile, model))
295
- return "model-not-configured";
296
- const api = profile.api ?? resolveCatalogModel(provider, model, profileDeclaresModel(profile, model)).api;
297
- if (api === undefined)
298
- return "api-unresolved";
299
- return api === "openai-responses" ? "incomplete" : "api-not-responses";
300
- }
301
280
  /** Resolve a Grok-prefixed model through its selected DSH Responses profile. */
302
281
  export function resolveGrokResponsesRouteConfig(ctx, options, policy) {
303
282
  const provider = String(options?.provider ?? "");
@@ -307,14 +286,11 @@ export function resolveGrokResponsesRouteConfig(ctx, options, policy) {
307
286
  const configured = configuredProviderProfile(ctx, provider);
308
287
  if (!isRecord(configured))
309
288
  return undefined;
310
- if (profileExcludesModel(configured, model))
311
- return undefined;
312
- const catalog = resolveCatalogModel(provider, model, profileDeclaresModel(configured, model));
313
- const selectedBuiltin = catalog.descriptor;
314
- const api = configured.api ?? catalog.api;
289
+ const selectedBuiltin = builtinResponsesModel(provider, model);
290
+ const api = configured.api ?? selectedBuiltin?.api;
315
291
  if (api !== "openai-responses")
316
292
  return undefined;
317
- const baseURL = configured.baseURL ?? catalog.baseUrl;
293
+ const baseURL = configured.baseURL ?? selectedBuiltin?.baseUrl;
318
294
  const apiKeyEnv = configured.apiKeyEnv;
319
295
  if (typeof baseURL !== "string" ||
320
296
  !normalizeBaseURL(baseURL) ||
@@ -8,14 +8,27 @@ export function sanitizeContextDetails(value) {
8
8
  }
9
9
  export const zeroBuckets = () => ({ uncachedInputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 });
10
10
  export const isSearchTool = (name) => name === 'web_search' || name === 'websearch_gpt_advanced';
11
+ /** DSH 0.2.1 settles a tool called from inside programmatic tool calling (`run_code`) with this event, not `tool/result`. */
12
+ export const NESTED_SETTLE_EVENT = 'tool/ptc-dispatch';
13
+ /**
14
+ * A search call's settlement and its tool-owned presentation `meta`, from either event that carries one: a
15
+ * model-direct call settles as `tool/result` (its name comes from the earlier `tool/call`); a nested call settles
16
+ * as `tool/ptc-dispatch`, whose `name`, `subCallId` and `meta` are its own fields.
17
+ */
18
+ export function searchSettlementOf(event, pendingName) {
19
+ if (!object(event) || !object(event.data) || !object(event.data.meta))
20
+ return;
21
+ // Official ToolMessageSource carries only callId; a direct result resolves its name from tool/call.
22
+ // A nested settlement states the tool it dispatched, so it needs no pending entry.
23
+ const name = event.type === NESTED_SETTLE_EVENT ? event.data.name : event.type === 'tool/result' ? pendingName : undefined;
24
+ // Only our owned search results may contribute auxiliary billing or media.
25
+ return isSearchTool(name) ? { name, meta: event.data.meta } : undefined;
26
+ }
11
27
  export function auxiliaryUsageOf(event, toolName) {
12
- if (!object(event) || event.type !== 'tool/result' || !object(event.data?.meta))
13
- return [];
14
- // Only our owned search results may contribute auxiliary billing.
15
- // Official ToolMessageSource carries only callId; resolve its name from tool/call.
16
- if (!isSearchTool(toolName))
28
+ const settled = searchSettlementOf(event, toolName);
29
+ if (!settled)
17
30
  return [];
18
- const raw = event.data.meta.auxiliaryUsage;
31
+ const raw = settled.meta.auxiliaryUsage;
19
32
  if (!Array.isArray(raw))
20
33
  return [];
21
34
  const ids = new Set(), result = [];