dsh-lcx-codex 0.6.0-pre.2 → 0.6.0

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,15 +12,15 @@ 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
+ import { DEFAULT_TOOL_UPDATES, planToolSurface } from "./tool-surface.js";
16
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";
17
17
  import { InvocationPolicyScope } from "./invocation-policy-scope.js";
18
18
  import { providerDynamicHeaders } from "./provider-headers.js";
19
19
  import { resolveCatalogModel } from "./model-catalog.js";
20
- import { authenticatedGrokHeaders, authenticatedHeaders, currentRoute, generationControlsFromSession, grokPromptCacheSessionId, promptCacheKey, promptCacheRetention, promptCacheSessionId, resolveGrokResponsesRouteConfig, diagnoseResponsesRoute, markHostRecognizedModel, warmHostRecognizedModels, resolveResponsesRouteConfig, routeFingerprint, } from "./route.js";
21
- import { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, assertAggregateImageBudget, hydrateNativeImageReferences, inputImageBase64Lengths, resolveRouteModelFacts, routeModelOverride, hasMidHistoryPromptSnapshot, responseInputItems, serializeDshMessages, } from "./dsh-responses.js";
20
+ import { authenticatedGrokHeaders, authenticatedHeaders, currentRoute, generationControlsFromSession, grokPromptCacheSessionId, promptCacheKey, promptCacheRetention, promptCacheSessionId, resolveGrokResponsesRouteConfig, diagnoseResponsesRoute, markHostRecognizedModel, warmHostRecognizedModels, resolveApiKey, resolveResponsesRouteConfig, routeFingerprint, } from "./route.js";
21
+ import { DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET, DEFAULT_REQUEST_IMAGE_MAX_BYTES, assertAggregateImageBudget, hydrateNativeImageReferences, inputImageBase64Lengths, resolveRouteModelFacts, routeModelOverride, hasMidHistoryPromptSnapshot, responseInputItems, serializeDshMessages, splitSystemPrompt, } from "./dsh-responses.js";
22
22
  import { mergeFeatureHeader, requestNativeCompaction } from "./compact-v2.js";
23
- import { buildResponsesBody } from "./responses-request.js";
23
+ import { buildResponsesBody, configurationUpdateEffort } from "./responses-request.js";
24
24
  import { managedFailureChunk, streamResponsesRequest, } from "./responses-stream.js";
25
25
  import { assertSupportedCheckpointMessage, checkpointStateForMessage, compactCheckpointId, createNativeCheckpointBlock, nativeCheckpointChunks, portableMessagesForCheckpoint, stateRouteCompatible, } from "./native-checkpoint.js";
26
26
  import { HOSTED_SEARCH_OUTPUT, HOSTED_SEARCH_PARAMETERS, buildHostedSearchBody, hostedMediaPresentationMeta, normalizeHostedSearchArgs, parseHostedSearchResponse, renderHostedSearchResult, } from "./web-search-hosted.js";
@@ -636,7 +636,6 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
636
636
  let messages = rawMessages;
637
637
  let tools = options.tools;
638
638
  let anchoredTools = false;
639
- let toolSurfaceCompat;
640
639
  if (options.toolSurface !== undefined) {
641
640
  const surface = planToolSurface({
642
641
  mode: routeConfig.toolUpdates ?? DEFAULT_TOOL_UPDATES,
@@ -649,7 +648,6 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
649
648
  messages = surface.messages;
650
649
  tools = surface.tools;
651
650
  anchoredTools = surface.anchored;
652
- toolSurfaceCompat = surface.compat;
653
651
  if (surface.fallbackReason !== undefined)
654
652
  reportToolSurfaceFallback(ctx, route, surface);
655
653
  }
@@ -663,106 +661,91 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
663
661
  let nativeTools;
664
662
  let nativeModel;
665
663
  let grammarToolInputProperties;
666
- let normal = [];
667
- /** Absolute `input` positions rendered from DSH prompt snapshots that a checkpoint supersedes. */
664
+ /** Absolute input positions rendered from prompt snapshots superseded by a checkpoint. */
668
665
  const supersededInstructionItems = [];
669
- const serializeOptions = (imageMapOverride = undefined, extra = {}) => requestImageOptions(routeConfig, imageSupport, options.signal, imageMapOverride, {
670
- route,
671
- tools,
672
- responsesCompat: toolSurfaceCompat === undefined
673
- ? routeConfig.responsesCompat
674
- : { ...routeConfig.responsesCompat, ...toolSurfaceCompat },
666
+ const serializeOptions = (extra = {}) => requestImageOptions(routeConfig, imageSupport, options.signal, undefined, {
667
+ route, tools,
668
+ responsesCompat: routeConfig.responsesCompat,
675
669
  systemPromptUpdate,
676
670
  ...(anchoredTools ? { toolUpdate: "addition-only" } : {}),
677
671
  ...(modelOverride === undefined ? {} : { model: modelOverride }),
678
672
  ...extra,
679
673
  });
680
- const firstMessage = messages[0];
681
- const systemHead = firstMessage?.role === "system" ? firstMessage : undefined;
682
- const surfaceMessages = systemHead ? messages.slice(1) : messages;
683
- const systemPrompt = systemHead?.content
684
- .filter((block) => block.type === "text")
685
- .map((block) => block.text)
686
- .join("") || undefined;
687
- const prelude = await serializeDshMessages([], ctx, serializeOptions(undefined, {
688
- systemPrompt,
689
- includeSystemPrompt: true,
690
- }));
691
- const ephemeralPreludeItemCount = prelude.input.length;
692
- input.push(...prelude.input);
693
- nativeTools = prelude.tools;
694
- nativeModel = prelude.model;
695
- grammarToolInputProperties = prelude.grammarToolInputProperties;
696
- /** Base64 length of images DSH cannot offload (hydrated checkpoint, portable replay). */
674
+ const { systemPrompt, messages: surfaceMessages } = splitSystemPrompt(options.system, messages);
675
+ // Resolve the descriptor before selecting effort. This conversion has no tool-bearing transcript.
676
+ const descriptor = await serializeDshMessages([], ctx, serializeOptions({ tools: undefined }));
677
+ const configurationUpdateEffort = await options.configurationUpdate?.(descriptor.model);
678
+ const nonce = `lcx-request-boundary:${globalThis.crypto.randomUUID()}:`;
679
+ const marker = (id) => ({
680
+ role: "user", content: [{ type: "text", text: nonce + id }],
681
+ });
682
+ // Checkpoint bodies are already serialized. Hold their positions while ONE Pi conversion handles
683
+ // the entire tool-bearing surface; Pi alone owns declarations, removals and incremental notices.
684
+ const transcript = [];
685
+ const replacements = new Map();
697
686
  let fixedImageBytes = 0;
698
- /** Base64 length of each offloadable surface image, oldest first, across all batches. */
699
- const surfaceImageBytes = [];
700
- const flush = async () => {
701
- if (!normal.length)
702
- return;
703
- const batch = normal;
704
- normal = [];
705
- const serialized = await serializeDshMessages(batch, ctx, serializeOptions());
706
- surfaceImageBytes.push(...inputImageBase64Lengths(serialized.input));
707
- // Every batch derives its request tools from the same immediate declarations; a difference would
708
- // change the cached prefix between batches of one request.
709
- if (anchoredTools && JSON.stringify(serialized.tools) !== JSON.stringify(nativeTools))
710
- throw Object.assign(new Error("LCX anchored tool declarations diverged between serialization batches"), { code: "LCX_RESPONSES_INVALID_INPUT" });
711
- nativeTools = serialized.tools ?? nativeTools;
712
- nativeModel = serialized.model ?? nativeModel;
713
- grammarToolInputProperties =
714
- serialized.grammarToolInputProperties ?? grammarToolInputProperties;
715
- const restored = restoreGrokNativeReplay(serialized.input, batch.filter(isPersistedMessage), options.grokNativeReplayRoute);
716
- const base = input.length;
717
- supersededInstructionItems.push(...locateSupersededItems(serialized.input, restored, serialized.supersededInstructionItems).map((position) => base + position));
718
- input.push(...restored);
719
- mergeMap(imageMap, serialized.imageMap);
720
- };
721
- for (const message of surfaceMessages ?? []) {
687
+ for (const message of surfaceMessages) {
722
688
  assertSupportedCheckpointMessage(message);
723
- const state = session && isPersistedMessage(message) && compactCheckpointId(message)
724
- ? await checkpointStateForMessage(ctx, session, message)
725
- : undefined;
726
- if (state && session) {
727
- const checkpointId = compactCheckpointId(message);
728
- if (!checkpointId) {
729
- normal.push(message);
730
- continue;
731
- }
732
- await flush();
733
- if (stateRouteCompatible(state, route, ctx)) {
734
- const nativeOutput = state.nativeOutput;
735
- const hydratedMap = new Map();
736
- const hydrated = await hydrateNativeImageReferences(nativeOutput, ctx, requestImageOptions(routeConfig, imageSupport, options.signal, hydratedMap));
737
- const hydratedItems = responseInputItems(hydrated);
738
- for (const bytes of inputImageBase64Lengths(hydratedItems))
739
- fixedImageBytes += bytes;
740
- input.push(...hydratedItems);
741
- mergeMap(imageMap, hydratedMap);
742
- }
743
- else {
744
- const portable = await portableMessagesForCheckpoint(ctx, session, checkpointId, { maxChars: routeConfig.portableReplayMaxChars });
745
- // Shadowed history is superseded by the head DSH re-rendered when the compaction began its new
746
- // series, so its old system messages stay ordinary context instead of regaining instruction
747
- // authority behind the current prompt.
748
- const serialized = await serializeDshMessages(portable, ctx,
749
- // Portable history is plain context: it carries no developer updates, and deferred declarations are
750
- // only meaningful to the anchored surface itself.
751
- serializeOptions(undefined, {
752
- systemPromptUpdate: undefined,
753
- toolUpdate: undefined,
754
- tools: tools?.map((tool) => withoutDeferLoading(tool)),
755
- }));
756
- for (const bytes of inputImageBase64Lengths(serialized.input))
757
- fixedImageBytes += bytes;
758
- input.push(...serialized.input);
759
- mergeMap(imageMap, serialized.imageMap);
760
- }
689
+ const checkpointId = session && isPersistedMessage(message) ? compactCheckpointId(message) : undefined;
690
+ const state = checkpointId && session && isPersistedMessage(message) ? await checkpointStateForMessage(ctx, session, message) : undefined;
691
+ if (!state || !session || !checkpointId) {
692
+ transcript.push(message);
693
+ continue;
694
+ }
695
+ let items;
696
+ if (stateRouteCompatible(state, route, ctx)) {
697
+ const hydratedMap = new Map();
698
+ items = responseInputItems(await hydrateNativeImageReferences(state.nativeOutput, ctx, requestImageOptions(routeConfig, imageSupport, options.signal, hydratedMap)));
699
+ mergeMap(imageMap, hydratedMap);
700
+ }
701
+ else {
702
+ const portable = await portableMessagesForCheckpoint(ctx, session, checkpointId, {
703
+ maxChars: routeConfig.portableReplayMaxChars,
704
+ });
705
+ // Shadowed history is plain context. Its old tools and prompt snapshots cannot re-declare
706
+ // the current series' surface or regain instruction authority on a different route.
707
+ const serialized = await serializeDshMessages(portable, ctx, serializeOptions({
708
+ systemPromptUpdate: undefined, toolUpdate: undefined, tools: undefined,
709
+ }));
710
+ items = serialized.input;
711
+ mergeMap(imageMap, serialized.imageMap);
712
+ }
713
+ for (const bytes of inputImageBase64Lengths(items))
714
+ fixedImageBytes += bytes;
715
+ const id = String(replacements.size);
716
+ replacements.set(id, items);
717
+ transcript.push(marker(id));
718
+ }
719
+ const serialized = await serializeDshMessages(transcript, ctx, serializeOptions({
720
+ systemPrompt, includeSystemPrompt: true, tracePrelude: true,
721
+ ...(configurationUpdateEffort === undefined ? {} : { configurationUpdateEffort }),
722
+ }));
723
+ const surfaceImageBytes = inputImageBase64Lengths(serialized.input);
724
+ nativeTools = serialized.tools;
725
+ nativeModel = serialized.model;
726
+ grammarToolInputProperties = serialized.grammarToolInputProperties;
727
+ mergeMap(imageMap, serialized.imageMap);
728
+ const restored = restoreGrokNativeReplay(serialized.input, surfaceMessages.filter(isPersistedMessage), options.grokNativeReplayRoute);
729
+ const snapshotPositions = new Set(locateSupersededItems(serialized.input, restored, serialized.supersededInstructionItems));
730
+ const ephemeralPreludeItemCount = serialized.preludeItemCount;
731
+ const seen = new Set();
732
+ for (const [position, item] of restored.entries()) {
733
+ const text = isRecord(item) && item.role === "user" && Array.isArray(item.content) && item.content.length === 1
734
+ && isRecord(item.content[0]) ? item.content[0].text : undefined;
735
+ if (typeof text === "string" && text.startsWith(nonce)) {
736
+ const id = text.slice(nonce.length);
737
+ if (seen.has(id) || !replacements.has(id))
738
+ throw Object.assign(new Error("LCX could not trace a request boundary"), { code: "LCX_RESPONSES_INVALID_INPUT" });
739
+ seen.add(id);
740
+ input.push(...replacements.get(id));
761
741
  continue;
762
742
  }
763
- normal.push(message);
743
+ if (snapshotPositions.has(position))
744
+ supersededInstructionItems.push(input.length);
745
+ input.push(item);
764
746
  }
765
- await flush();
747
+ if (ephemeralPreludeItemCount < 0 || seen.size !== replacements.size || JSON.stringify(input).includes(nonce))
748
+ throw Object.assign(new Error("LCX could not remove request boundary markers"), { code: "LCX_RESPONSES_INVALID_INPUT" });
766
749
  assertAggregateImageBudget({
767
750
  maxBytes: Number.isSafeInteger(routeConfig.maxRequestImageBytes) && Number(routeConfig.maxRequestImageBytes) > 0
768
751
  ? Number(routeConfig.maxRequestImageBytes)
@@ -772,8 +755,12 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
772
755
  });
773
756
  if (!nativeModel)
774
757
  throw Object.assign(new Error("LCX could not resolve the Pi Responses model descriptor"), { code: "LCX_RESPONSES_MODEL_UNAVAILABLE" });
758
+ // Pi ends the input with the request's own level, after everything else it serializes.
759
+ if (configurationUpdateEffort !== undefined)
760
+ input.push({ type: "configuration_update", reasoning: { effort: configurationUpdateEffort } });
775
761
  return {
776
762
  input,
763
+ configurationUpdateEffort,
777
764
  ephemeralPreludeItemCount,
778
765
  supersededInstructionItems,
779
766
  imageMap,
@@ -800,6 +787,7 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
800
787
  try {
801
788
  const prepared = await serializeNativeAware(history, route, routeConfig, ctx, {
802
789
  signal: options.signal,
790
+ system: options.system,
803
791
  tools: options.tools && [...options.tools],
804
792
  toolSurface: { history: options.toolHistory },
805
793
  });
@@ -1102,8 +1090,10 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
1102
1090
  throw Object.assign(new Error("LCX Responses does not support GenerateOptions.stop"), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
1103
1091
  const prepared = await serializeNativeAware(options.messages, route, routeConfig, ctx, {
1104
1092
  signal: options.signal,
1093
+ system: options.system,
1105
1094
  tools: options.tools && [...options.tools],
1106
1095
  toolSurface: { history: options.toolHistory },
1096
+ configurationUpdate: (model) => configurationUpdateEffort(model, options.reasoningEffort, () => resolveApiKey(ctx, routeConfig)),
1107
1097
  });
1108
1098
  assertSupportedReasoningEffort(options.reasoningEffort, prepared.modelFacts);
1109
1099
  const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
@@ -1119,6 +1109,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
1119
1109
  reasoningEffort: options.reasoningEffort,
1120
1110
  temperature: options.temperature,
1121
1111
  maxTokens: options.maxTokens,
1112
+ ...(prepared.configurationUpdateEffort === undefined ? {} : { configurationUpdateEffort: prepared.configurationUpdateEffort }),
1122
1113
  });
1123
1114
  const nativeReplay = inputHasNativeState(prepared.input);
1124
1115
  if (nativeReplay) {
@@ -1137,6 +1128,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
1137
1128
  timeoutMs: routeConfig.timeoutMs,
1138
1129
  maxAttempts: 1,
1139
1130
  maxResponseBytes: routeConfig.maxResponseBytes,
1131
+ ...(prepared.configurationUpdateEffort === undefined ? {} : { providerThinkingLevel: prepared.configurationUpdateEffort }),
1140
1132
  onFailure: (info) => logStreamFailure(ctx, "gpt", info),
1141
1133
  });
1142
1134
  }
@@ -1155,6 +1147,7 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
1155
1147
  const declaredToolNames = new Set(visibleTools?.map((tool) => tool.name) ?? []);
1156
1148
  const prepared = await serializeNativeAware(projected.messages, route, routeConfig, ctx, {
1157
1149
  signal: options.signal,
1150
+ system: options.system,
1158
1151
  tools: visibleTools,
1159
1152
  grokNativeReplayRoute: nativeReplayRoute,
1160
1153
  });
@@ -1295,7 +1288,7 @@ function installInjected(ctx, configInput) {
1295
1288
  ctx.on("session/event", (session, event) => {
1296
1289
  recordAgentRouteEvent(session, event);
1297
1290
  // DSH 0.2 session/selectModel appends model/selection before the next
1298
- // prompt assembly projects tools (dsh-api-session-controller/lib/index.js:320).
1291
+ // prompt assembly projects tools (dsh-api-session-controller `selectModel`).
1299
1292
  if (event.type === "model/selection" || event.type === "request/header")
1300
1293
  for (const agent of managedAgents)
1301
1294
  if (agentUsesSession(agent, session))
@@ -222,8 +222,9 @@ export async function activeCompactionId(ctx, session) {
222
222
  * one, as developer/system or even as user text, would keep a stale full prompt (for example plan-mode
223
223
  * policy that has since been switched off) in front of the model after the compaction.
224
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
225
+ * Only items traced to such snapshots or Pi tool-update notices (`supersededItemIndices`, absolute positions
226
+ * in `input`) are removed. Tool notices belong to the old series, whose declarations retention drops.
227
+ * Any other retained developer/system item is genuine client context and
227
228
  * stays. The ephemeral prelude is removed by the caller's slice, and the retention budget is computed
228
229
  * over what is actually kept.
229
230
  */