dsh-lcx-codex 0.6.0-pre.1 → 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,13 +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
- 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";
18
+ import { providerDynamicHeaders } from "./provider-headers.js";
19
+ import { resolveCatalogModel } from "./model-catalog.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";
20
22
  import { mergeFeatureHeader, requestNativeCompaction } from "./compact-v2.js";
21
- import { buildResponsesBody } from "./responses-request.js";
23
+ import { buildResponsesBody, configurationUpdateEffort } from "./responses-request.js";
22
24
  import { managedFailureChunk, streamResponsesRequest, } from "./responses-stream.js";
23
25
  import { assertSupportedCheckpointMessage, checkpointStateForMessage, compactCheckpointId, createNativeCheckpointBlock, nativeCheckpointChunks, portableMessagesForCheckpoint, stateRouteCompatible, } from "./native-checkpoint.js";
24
26
  import { HOSTED_SEARCH_OUTPUT, HOSTED_SEARCH_PARAMETERS, buildHostedSearchBody, hostedMediaPresentationMeta, normalizeHostedSearchArgs, parseHostedSearchResponse, renderHostedSearchResult, } from "./web-search-hosted.js";
@@ -28,6 +30,7 @@ import { AlphaCapabilityStore, alphaCapabilityFingerprint, alphaCapabilityUsable
28
30
  import { AlphaRefStore } from "./web-search-ref-store.js";
29
31
  import { ServiceMutex } from "./service-mutex.js";
30
32
  import { GROK_NATIVE_SERVER_TOOL_TYPES, grokNativeSearchEnabled, grokVisibleFunctionTools, isGrokNativeServerToolItem, grokWireTools, restoreGrokNativeReplay, } from "./grok-native-search.js";
33
+ import { DEFAULTS } from "./defaults.js";
31
34
  export const name = "lcx-codex";
32
35
  export const inject = ["llm", "web", "sessions", "tools", "settings", "credentials", "attachments", "fs"];
33
36
  const ADVANCED_HOSTED_TOOL = "websearch_gpt_advanced";
@@ -72,9 +75,6 @@ function grokReplayRoute(route, config) {
72
75
  function isRecord(value) {
73
76
  return value !== null && typeof value === "object";
74
77
  }
75
- function asAbortSignal(value) {
76
- return value instanceof AbortSignal ? value : undefined;
77
- }
78
78
  function errorDetails(value) {
79
79
  return isRecord(value)
80
80
  ? {
@@ -109,21 +109,21 @@ export const Config = z.object({
109
109
  alphaRefPath: z.string().default(""),
110
110
  alphaProfile: z.string().default(""),
111
111
  alphaGroup: z.string().default(""),
112
- alphaMaxOutputTokens: z.number().default(2500),
113
- webSearchProvider: z.string().default("lcx-responses"),
114
- webMaxResults: z.number().default(8),
115
- timeoutMs: z.number().default(300000),
116
- maxResponseBytes: z.number().default(8 * 1024 * 1024),
117
- maxAttempts: z.number().default(3),
112
+ alphaMaxOutputTokens: z.number().default(DEFAULTS.alphaMaxOutputTokens),
113
+ webSearchProvider: z.string().default(DEFAULTS.webSearchProvider),
114
+ webMaxResults: z.number().default(DEFAULTS.webMaxResults),
115
+ timeoutMs: z.number().default(DEFAULTS.requestTimeoutMs),
116
+ maxResponseBytes: z.number().default(DEFAULTS.maxResponseBytes),
117
+ maxAttempts: z.number().default(DEFAULTS.maxAttempts),
118
118
  maxRequestImageBytes: z.number().default(DEFAULT_MAX_REQUEST_IMAGE_BYTES),
119
119
  requestImagePixelBudget: z
120
120
  .number()
121
121
  .default(DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET),
122
122
  requestImageMaxBytes: z.number().default(DEFAULT_REQUEST_IMAGE_MAX_BYTES),
123
- portableReplayMaxChars: z.number().default(80_000),
124
- nativeRetentionTokenBudget: z.number().default(64_000),
125
- assistantRetentionTokenReserve: z.number().default(24_000),
126
- assistantRetentionPerMessageTokenCap: z.number().default(3_000),
123
+ portableReplayMaxChars: z.number().default(DEFAULTS.portableReplayMaxChars),
124
+ nativeRetentionTokenBudget: z.number().default(DEFAULTS.nativeRetentionTokenBudget),
125
+ assistantRetentionTokenReserve: z.number().default(DEFAULTS.assistantRetentionTokenReserve),
126
+ assistantRetentionPerMessageTokenCap: z.number().default(DEFAULTS.assistantRetentionPerMessageTokenCap),
127
127
  });
128
128
  function normalizeConfig(input = {}) {
129
129
  return {
@@ -133,23 +133,23 @@ function normalizeConfig(input = {}) {
133
133
  alphaRefPath: stringValue(input.alphaRefPath, defaultAlphaRefPath()),
134
134
  alphaProfile: String(input.alphaProfile ?? ""),
135
135
  alphaGroup: String(input.alphaGroup ?? ""),
136
- alphaMaxOutputTokens: positiveInteger(input.alphaMaxOutputTokens, 2500, 32_000),
137
- webSearchProvider: stringValue(input.webSearchProvider, "lcx-responses"),
138
- webMaxResults: positiveInteger(input.webMaxResults, 8),
139
- timeoutMs: positiveInteger(input.timeoutMs, 300000),
140
- maxResponseBytes: positiveInteger(input.maxResponseBytes, 8 * 1024 * 1024),
141
- maxAttempts: positiveInteger(input.maxAttempts, 3, 6),
136
+ alphaMaxOutputTokens: positiveInteger(input.alphaMaxOutputTokens, DEFAULTS.alphaMaxOutputTokens, DEFAULTS.alphaMaxOutputTokensCap),
137
+ webSearchProvider: stringValue(input.webSearchProvider, DEFAULTS.webSearchProvider),
138
+ webMaxResults: positiveInteger(input.webMaxResults, DEFAULTS.webMaxResults),
139
+ timeoutMs: positiveInteger(input.timeoutMs, DEFAULTS.requestTimeoutMs),
140
+ maxResponseBytes: positiveInteger(input.maxResponseBytes, DEFAULTS.maxResponseBytes),
141
+ maxAttempts: positiveInteger(input.maxAttempts, DEFAULTS.maxAttempts, DEFAULTS.maxAttemptsCap),
142
142
  maxRequestImageBytes: positiveInteger(input.maxRequestImageBytes, DEFAULT_MAX_REQUEST_IMAGE_BYTES),
143
143
  requestImagePixelBudget: positiveInteger(input.requestImagePixelBudget, DEFAULT_REQUEST_IMAGE_PIXEL_BUDGET),
144
144
  requestImageMaxBytes: positiveInteger(input.requestImageMaxBytes, DEFAULT_REQUEST_IMAGE_MAX_BYTES),
145
- portableReplayMaxChars: positiveInteger(input.portableReplayMaxChars, 80_000),
146
- nativeRetentionTokenBudget: positiveInteger(input.nativeRetentionTokenBudget, 64_000),
145
+ portableReplayMaxChars: positiveInteger(input.portableReplayMaxChars, DEFAULTS.portableReplayMaxChars),
146
+ nativeRetentionTokenBudget: positiveInteger(input.nativeRetentionTokenBudget, DEFAULTS.nativeRetentionTokenBudget),
147
147
  assistantRetentionTokenReserve: typeof input.assistantRetentionTokenReserve === "number" &&
148
148
  Number.isSafeInteger(input.assistantRetentionTokenReserve) &&
149
149
  input.assistantRetentionTokenReserve >= 0
150
150
  ? input.assistantRetentionTokenReserve
151
- : 24_000,
152
- assistantRetentionPerMessageTokenCap: positiveInteger(input.assistantRetentionPerMessageTokenCap, 3_000),
151
+ : DEFAULTS.assistantRetentionTokenReserve,
152
+ assistantRetentionPerMessageTokenCap: positiveInteger(input.assistantRetentionPerMessageTokenCap, DEFAULTS.assistantRetentionPerMessageTokenCap),
153
153
  toolUpdates: DEFAULT_TOOL_UPDATES,
154
154
  };
155
155
  }
@@ -227,7 +227,8 @@ class LcxResponsesSearchProvider {
227
227
  if (!active)
228
228
  return false;
229
229
  const config = this.getConfig();
230
- const route = resolveResponsesRouteConfig(this.ctx, active, config);
230
+ // Tool-assembly question, answered before DSH can be asked: see `assumeHostServes` in route.ts.
231
+ const route = resolveResponsesRouteConfig(this.ctx, active, config, true);
231
232
  return this.enabled() && Boolean(route);
232
233
  }
233
234
  async search(request, signal) {
@@ -321,7 +322,9 @@ function disposeGptToolsForAgent(agent, registrations) {
321
322
  function syncGptToolsForAgent(ctx, agent, state, getConfig, provider, registrations) {
322
323
  const config = getConfig();
323
324
  const active = selectedAgentRoute(agent);
324
- const route = resolveResponsesRouteConfig(ctx, active, config);
325
+ // Registering the tools cannot wait for DSH's answer about a model only its own catalog knows: the optimistic
326
+ // answer is exact (see `assumeHostServes` in route.ts), and the stream awaits DSH before LCX takes the request.
327
+ const route = resolveResponsesRouteConfig(ctx, active, config, true);
325
328
  const webSearch = state.enabled && state.webSearch && Boolean(route);
326
329
  const advanced = webSearch && state.advancedHostedSearch;
327
330
  if (!webSearch && !advanced) {
@@ -574,19 +577,6 @@ function locateSupersededItems(original, restored, positions) {
574
577
  }
575
578
  return located;
576
579
  }
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.
582
- */
583
- function gptModelOverride(routeConfig, facts) {
584
- if (routeConfig.modelControls !== undefined)
585
- return { ...routeConfig.modelControls };
586
- if (!facts.resolved)
587
- return undefined;
588
- return { reasoning: facts.reasoning !== undefined };
589
- }
590
580
  /** Refuse an effort DSH does not offer for this model, the way the DSH adapter and the Grok route do. */
591
581
  function assertSupportedReasoningEffort(effort, facts) {
592
582
  if (effort === undefined || !facts.resolved)
@@ -604,7 +594,7 @@ const reportedToolSurfaceFallbacks = new Set();
604
594
  */
605
595
  function reportToolSurfaceFallback(ctx, route, surface) {
606
596
  const reason = surface.fallbackReason ?? "unknown";
607
- const key = `${route.sessionId}${reason}`;
597
+ const key = `${route.sessionId}\u001f${reason}`;
608
598
  if (reportedToolSurfaceFallbacks.has(key))
609
599
  return;
610
600
  if (reportedToolSurfaceFallbacks.size >= 256)
@@ -616,20 +606,36 @@ function reportToolSurfaceFallback(ctx, route, surface) {
616
606
  else
617
607
  ctx.logger?.debug?.(message);
618
608
  }
609
+ const reportedCatalogGaps = new Set();
610
+ /**
611
+ * Say once per route model that LCX's bundled Pi catalog does not describe it, so its wire capabilities
612
+ * (strict tools, grammar tools, cache mode) come from the profile and from what DSH declares instead.
613
+ */
614
+ function reportCatalogGap(ctx, route) {
615
+ const source = resolveCatalogModel(route.provider, route.model, true).source;
616
+ if (source !== "catalog-provider-default")
617
+ return;
618
+ const key = `${route.provider}/${route.model}`;
619
+ if (reportedCatalogGaps.has(key))
620
+ return;
621
+ if (reportedCatalogGaps.size >= 256)
622
+ reportedCatalogGaps.clear();
623
+ reportedCatalogGaps.add(key);
624
+ 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`);
625
+ }
619
626
  async function serializeNativeAware(rawMessages, route, routeConfig, ctx, options) {
620
627
  const session = sessionFor(ctx, route.sessionId);
621
628
  // One adapter-owned lookup supplies image support and the route's system-prompt update mode. On an
622
629
  // "in-history" route DSH appends a changed prompt as a later full system message; it must reach the
623
630
  // model in place with instruction authority instead of being demoted to user context.
624
631
  const modelFacts = await resolveRouteModelFacts(ctx, route, options.signal);
632
+ if (options.grokNativeReplayRoute === undefined)
633
+ reportCatalogGap(ctx, route);
625
634
  const { imageSupport, systemPromptUpdate, resolved } = modelFacts;
626
- const modelOverride = options.grokNativeReplayRoute === undefined
627
- ? gptModelOverride(routeConfig, modelFacts)
628
- : undefined;
635
+ const modelOverride = routeModelOverride(routeConfig, modelFacts, options.grokNativeReplayRoute !== undefined);
629
636
  let messages = rawMessages;
630
637
  let tools = options.tools;
631
638
  let anchoredTools = false;
632
- let toolSurfaceCompat;
633
639
  if (options.toolSurface !== undefined) {
634
640
  const surface = planToolSurface({
635
641
  mode: routeConfig.toolUpdates ?? DEFAULT_TOOL_UPDATES,
@@ -642,7 +648,6 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
642
648
  messages = surface.messages;
643
649
  tools = surface.tools;
644
650
  anchoredTools = surface.anchored;
645
- toolSurfaceCompat = surface.compat;
646
651
  if (surface.fallbackReason !== undefined)
647
652
  reportToolSurfaceFallback(ctx, route, surface);
648
653
  }
@@ -656,106 +661,91 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
656
661
  let nativeTools;
657
662
  let nativeModel;
658
663
  let grammarToolInputProperties;
659
- let normal = [];
660
- /** Absolute `input` positions rendered from DSH prompt snapshots that a checkpoint supersedes. */
664
+ /** Absolute input positions rendered from prompt snapshots superseded by a checkpoint. */
661
665
  const supersededInstructionItems = [];
662
- const serializeOptions = (imageMapOverride = undefined, extra = {}) => requestImageOptions(routeConfig, imageSupport, options.signal, imageMapOverride, {
663
- route,
664
- tools,
665
- responsesCompat: toolSurfaceCompat === undefined
666
- ? routeConfig.responsesCompat
667
- : { ...routeConfig.responsesCompat, ...toolSurfaceCompat },
666
+ const serializeOptions = (extra = {}) => requestImageOptions(routeConfig, imageSupport, options.signal, undefined, {
667
+ route, tools,
668
+ responsesCompat: routeConfig.responsesCompat,
668
669
  systemPromptUpdate,
669
670
  ...(anchoredTools ? { toolUpdate: "addition-only" } : {}),
670
671
  ...(modelOverride === undefined ? {} : { model: modelOverride }),
671
672
  ...extra,
672
673
  });
673
- const firstMessage = messages[0];
674
- const systemHead = firstMessage?.role === "system" ? firstMessage : undefined;
675
- const surfaceMessages = systemHead ? messages.slice(1) : messages;
676
- const systemPrompt = systemHead?.content
677
- .filter((block) => block.type === "text")
678
- .map((block) => block.text)
679
- .join("") || undefined;
680
- const prelude = await serializeDshMessages([], ctx, serializeOptions(undefined, {
681
- systemPrompt,
682
- includeSystemPrompt: true,
683
- }));
684
- const ephemeralPreludeItemCount = prelude.input.length;
685
- input.push(...prelude.input);
686
- nativeTools = prelude.tools;
687
- nativeModel = prelude.model;
688
- grammarToolInputProperties = prelude.grammarToolInputProperties;
689
- /** 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();
690
686
  let fixedImageBytes = 0;
691
- /** Base64 length of each offloadable surface image, oldest first, across all batches. */
692
- const surfaceImageBytes = [];
693
- const flush = async () => {
694
- if (!normal.length)
695
- return;
696
- const batch = normal;
697
- normal = [];
698
- const serialized = await serializeDshMessages(batch, ctx, serializeOptions());
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" });
704
- nativeTools = serialized.tools ?? nativeTools;
705
- nativeModel = serialized.model ?? nativeModel;
706
- grammarToolInputProperties =
707
- serialized.grammarToolInputProperties ?? grammarToolInputProperties;
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);
712
- mergeMap(imageMap, serialized.imageMap);
713
- };
714
- for (const message of surfaceMessages ?? []) {
687
+ for (const message of surfaceMessages) {
715
688
  assertSupportedCheckpointMessage(message);
716
- const state = session && isPersistedMessage(message) && compactCheckpointId(message)
717
- ? await checkpointStateForMessage(ctx, session, message)
718
- : undefined;
719
- if (state && session) {
720
- const checkpointId = compactCheckpointId(message);
721
- if (!checkpointId) {
722
- normal.push(message);
723
- continue;
724
- }
725
- await flush();
726
- if (stateRouteCompatible(state, route, ctx)) {
727
- const nativeOutput = state.nativeOutput;
728
- const hydratedMap = new Map();
729
- const hydrated = await hydrateNativeImageReferences(nativeOutput, ctx, requestImageOptions(routeConfig, imageSupport, options.signal, hydratedMap));
730
- const hydratedItems = responseInputItems(hydrated);
731
- for (const bytes of inputImageBase64Lengths(hydratedItems))
732
- fixedImageBytes += bytes;
733
- input.push(...hydratedItems);
734
- mergeMap(imageMap, hydratedMap);
735
- }
736
- else {
737
- const portable = await portableMessagesForCheckpoint(ctx, session, checkpointId, { maxChars: routeConfig.portableReplayMaxChars });
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
- }));
749
- for (const bytes of inputImageBase64Lengths(serialized.input))
750
- fixedImageBytes += bytes;
751
- input.push(...serialized.input);
752
- mergeMap(imageMap, serialized.imageMap);
753
- }
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);
754
693
  continue;
755
694
  }
756
- normal.push(message);
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));
741
+ continue;
742
+ }
743
+ if (snapshotPositions.has(position))
744
+ supersededInstructionItems.push(input.length);
745
+ input.push(item);
757
746
  }
758
- 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" });
759
749
  assertAggregateImageBudget({
760
750
  maxBytes: Number.isSafeInteger(routeConfig.maxRequestImageBytes) && Number(routeConfig.maxRequestImageBytes) > 0
761
751
  ? Number(routeConfig.maxRequestImageBytes)
@@ -765,8 +755,12 @@ async function serializeNativeAware(rawMessages, route, routeConfig, ctx, option
765
755
  });
766
756
  if (!nativeModel)
767
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 } });
768
761
  return {
769
762
  input,
763
+ configurationUpdateEffort,
770
764
  ephemeralPreludeItemCount,
771
765
  supersededInstructionItems,
772
766
  imageMap,
@@ -793,11 +787,12 @@ async function* remoteCompactionStream(options, routeConfig, ctx, next) {
793
787
  try {
794
788
  const prepared = await serializeNativeAware(history, route, routeConfig, ctx, {
795
789
  signal: options.signal,
790
+ system: options.system,
796
791
  tools: options.tools && [...options.tools],
797
792
  toolSurface: { history: options.toolHistory },
798
793
  });
799
794
  const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
800
- const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId);
795
+ const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined);
801
796
  const generation = generationControlsFromSession(sessionFor(ctx, route.sessionId), route);
802
797
  const result = await requestNativeCompaction({
803
798
  baseURL: routeConfig.baseURL,
@@ -1095,12 +1090,14 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
1095
1090
  throw Object.assign(new Error("LCX Responses does not support GenerateOptions.stop"), { code: "LCX_RESPONSES_UNSUPPORTED_OPTION" });
1096
1091
  const prepared = await serializeNativeAware(options.messages, route, routeConfig, ctx, {
1097
1092
  signal: options.signal,
1093
+ system: options.system,
1098
1094
  tools: options.tools && [...options.tools],
1099
1095
  toolSurface: { history: options.toolHistory },
1096
+ configurationUpdate: (model) => configurationUpdateEffort(model, options.reasoningEffort, () => resolveApiKey(ctx, routeConfig)),
1100
1097
  });
1101
1098
  assertSupportedReasoningEffort(options.reasoningEffort, prepared.modelFacts);
1102
1099
  const cacheSessionId = promptCacheSessionId(route, routeConfig, ctx);
1103
- const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId);
1100
+ const headers = await authenticatedHeaders(ctx, routeConfig, cacheSessionId, cacheSessionId === undefined ? null : route.sessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined);
1104
1101
  const body = buildResponsesBody({
1105
1102
  model: prepared.model,
1106
1103
  input: prepared.input,
@@ -1112,6 +1109,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
1112
1109
  reasoningEffort: options.reasoningEffort,
1113
1110
  temperature: options.temperature,
1114
1111
  maxTokens: options.maxTokens,
1112
+ ...(prepared.configurationUpdateEffort === undefined ? {} : { configurationUpdateEffort: prepared.configurationUpdateEffort }),
1115
1113
  });
1116
1114
  const nativeReplay = inputHasNativeState(prepared.input);
1117
1115
  if (nativeReplay) {
@@ -1130,6 +1128,7 @@ async function* managedResponsesStream(options, routeConfig, ctx) {
1130
1128
  timeoutMs: routeConfig.timeoutMs,
1131
1129
  maxAttempts: 1,
1132
1130
  maxResponseBytes: routeConfig.maxResponseBytes,
1131
+ ...(prepared.configurationUpdateEffort === undefined ? {} : { providerThinkingLevel: prepared.configurationUpdateEffort }),
1133
1132
  onFailure: (info) => logStreamFailure(ctx, "gpt", info),
1134
1133
  });
1135
1134
  }
@@ -1148,11 +1147,12 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
1148
1147
  const declaredToolNames = new Set(visibleTools?.map((tool) => tool.name) ?? []);
1149
1148
  const prepared = await serializeNativeAware(projected.messages, route, routeConfig, ctx, {
1150
1149
  signal: options.signal,
1150
+ system: options.system,
1151
1151
  tools: visibleTools,
1152
1152
  grokNativeReplayRoute: nativeReplayRoute,
1153
1153
  });
1154
1154
  const cacheSessionId = grokPromptCacheSessionId(route, routeConfig);
1155
- const headers = await authenticatedGrokHeaders(ctx, routeConfig, cacheSessionId);
1155
+ const headers = await authenticatedGrokHeaders(ctx, routeConfig, cacheSessionId, providerDynamicHeaders(routeConfig.provider, options.messages), route.sessionId || undefined);
1156
1156
  const model = {
1157
1157
  ...prepared.model,
1158
1158
  ...routeConfig.modelDefaults,
@@ -1214,8 +1214,41 @@ async function* managedGrokNativeSearchStream(options, routeConfig, nativeSearch
1214
1214
  function isGptLifecycleTarget(options) {
1215
1215
  return /^gpt-/iu.test(String(options.model ?? ""));
1216
1216
  }
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);
1217
+ async function* unavailableManagedRouteStream(options, diagnosis = "incomplete") {
1218
+ // DSH names no protocol for a model its catalog lacks and the profile sets no `api`: say what to change.
1219
+ const apiUnresolved = diagnosis === "api-unresolved";
1220
+ yield managedFailureChunk(Object.assign(new Error(apiUnresolved
1221
+ ? "LCX could not determine the wire protocol of the selected GPT route: the provider profile sets no api and LCX could not infer one for this model. Add api: openai-responses to the provider profile, or select another model"
1222
+ : "LCX is enabled but the selected DSH route cannot be resolved as an authenticated OpenAI Responses wire route"), { code: apiUnresolved ? "LCX_RESPONSES_API_UNRESOLVED" : "LCX_RESPONSES_ROUTE_UNAVAILABLE" }), options.signal);
1223
+ }
1224
+ /**
1225
+ * Re-resolve a route whose protocol LCX could not infer after asking DSH whether it resolves the exact model.
1226
+ * DSH resolving it counts as the profile declaring it: the provider-wide protocol then applies, as in DSH.
1227
+ */
1228
+ async function* hostRecognizedRouteStream(options, ctx, policy, next) {
1229
+ const facts = await resolveRouteModelFacts(ctx, options, options.signal);
1230
+ if (facts.resolved)
1231
+ markHostRecognizedModel(ctx, options.provider, options.model);
1232
+ const routeConfig = facts.resolved
1233
+ ? resolveResponsesRouteConfig(ctx, options, policy())
1234
+ : undefined;
1235
+ if (!routeConfig) {
1236
+ yield* unavailableManagedRouteStream(options, diagnoseResponsesRoute(ctx, options));
1237
+ return;
1238
+ }
1239
+ yield* options.purpose === "compaction"
1240
+ ? remoteCompactionStream(options, routeWithPolicies(routeConfig, policy()), ctx, next)
1241
+ : managedResponsesStream(options, routeWithPolicies(routeConfig, policy()), ctx);
1242
+ }
1243
+ /** The Grok counterpart of `hostRecognizedRouteStream`: DSH not resolving the model is DSH's to report. */
1244
+ async function* hostRecognizedGrokStream(options, ctx, policy, nativeSearch, next) {
1245
+ const facts = await resolveRouteModelFacts(ctx, options, options.signal);
1246
+ if (facts.resolved)
1247
+ markHostRecognizedModel(ctx, options.provider, options.model);
1248
+ const grokRoute = facts.resolved ? resolveGrokResponsesRouteConfig(ctx, options, policy()) : undefined;
1249
+ yield* grokRoute
1250
+ ? managedGrokNativeSearchStream(options, routeWithPolicies(grokRoute, policy()), nativeSearch, ctx)
1251
+ : next();
1219
1252
  }
1220
1253
  function installInjected(ctx, configInput) {
1221
1254
  installSearchUsage(ctx);
@@ -1255,7 +1288,7 @@ function installInjected(ctx, configInput) {
1255
1288
  ctx.on("session/event", (session, event) => {
1256
1289
  recordAgentRouteEvent(session, event);
1257
1290
  // DSH 0.2 session/selectModel appends model/selection before the next
1258
- // prompt assembly projects tools (dsh-api-session-controller/lib/index.js:320).
1291
+ // prompt assembly projects tools (dsh-api-session-controller `selectModel`).
1259
1292
  if (event.type === "model/selection" || event.type === "request/header")
1260
1293
  for (const agent of managedAgents)
1261
1294
  if (agentUsesSession(agent, session))
@@ -1276,15 +1309,37 @@ function installInjected(ctx, configInput) {
1276
1309
  : baseConfig;
1277
1310
  refreshTools();
1278
1311
  };
1312
+ /**
1313
+ * Tool ownership is decided synchronously when an agent's tools are assembled, but whether DSH serves a model
1314
+ * that LCX's bundled catalog does not describe is only known by asking DSH. So ask ahead of time, at start and
1315
+ * whenever the provider configuration or the provider set changes, and resynchronise the tools when the
1316
+ * remembered answers change; a first request on such a model then already carries the LCX tools.
1317
+ */
1318
+ const prewarmHostModels = () => {
1319
+ void warmHostRecognizedModels(ctx)
1320
+ .then((changed) => {
1321
+ if (changed)
1322
+ refreshTools();
1323
+ })
1324
+ .catch((error) => ctx.logger?.debug?.(`[lcx-codex] host model prewarm skipped: ${String(error)}`));
1325
+ };
1279
1326
  syncSettings();
1280
- ctx.on("loader/volatile-update", syncSettings);
1281
- ctx.on("settings/document-updated", refreshTools);
1327
+ prewarmHostModels();
1328
+ ctx.on("loader/volatile-update", () => {
1329
+ syncSettings();
1330
+ prewarmHostModels();
1331
+ });
1332
+ ctx.on("settings/document-updated", () => {
1333
+ refreshTools();
1334
+ prewarmHostModels();
1335
+ });
1336
+ ctx.on("llm/adapters-updated", prewarmHostModels);
1282
1337
  ctx.inject(["compaction"], (compactionCtx) => {
1283
1338
  patchCompactionPressureService(resolveContextService(compactionCtx, "compaction"), state, () => runtimeConfig, ctx, compactionPatchRecords);
1284
1339
  });
1285
1340
  /**
1286
1341
  * LCX instrumentation is optional capability, never a creation precondition.
1287
- * DSH 0.1.6 awaits `agent/created` serially, so an escaping failure would make
1342
+ * DSH awaits `agent/created` serially, so an escaping failure would make
1288
1343
  * an otherwise valid Agent uncreatable; `agent/status` stays a contained emit.
1289
1344
  * Both edges therefore fail closed locally: the Agent runs uninstrumented and
1290
1345
  * the reason is logged, rather than propagating into the host lifecycle.
@@ -1301,7 +1356,7 @@ function installInjected(ctx, configInput) {
1301
1356
  ctx.logger?.warn?.(`[lcx-codex] agent instrumentation skipped on ${edge}: ${String(error)}`);
1302
1357
  }
1303
1358
  };
1304
- /** DSH 0.1.6 lifecycle source, recorded for diagnostics only. */
1359
+ /** DSH lifecycle source, recorded for diagnostics only. */
1305
1360
  const startSource = (value) => typeof value === "string" && /^[a-z]+$/u.test(value) ? value : "unknown";
1306
1361
  ctx.on("agent/created", (payload) => {
1307
1362
  const agent = payload?.agent;
@@ -1329,17 +1384,32 @@ function installInjected(ctx, configInput) {
1329
1384
  const grokRoute = resolveGrokResponsesRouteConfig(ctx, options, runtimeConfig);
1330
1385
  if (grokRoute)
1331
1386
  return managedGrokNativeSearchStream(options, routeWithPolicies(grokRoute, runtimeConfig), grokNativeSearch, ctx);
1387
+ // A Grok model only DSH's own catalog may know: ask DSH now, so the first request is not left to a race.
1388
+ if (/^grok/iu.test(String(options.model ?? "")) && diagnoseResponsesRoute(ctx, options) === "api-unresolved")
1389
+ return hostRecognizedGrokStream(options, ctx, () => runtimeConfig, grokNativeSearch, next);
1332
1390
  }
1333
1391
  if (!state.enabled || !isGptLifecycleTarget(options))
1334
1392
  return next();
1335
1393
  const routeConfig = resolveResponsesRouteConfig(ctx, options, runtimeConfig);
1394
+ if (!routeConfig) {
1395
+ const diagnosis = diagnoseResponsesRoute(ctx, options);
1396
+ // DSH owns these calls: a model the provider's models list leaves out (DSH serves only the listed
1397
+ // models and answers UNKNOWN_MODEL, nothing is sent) and a provider whose authentication only Pi's own
1398
+ // client can perform (see HOST_AUTH_PROVIDERS). LCX does not own a route it cannot serve like DSH.
1399
+ if (diagnosis === "model-not-configured" || diagnosis === "host-auth")
1400
+ return next();
1401
+ }
1402
+ // The profile names no api and LCX's catalog does not describe the model: DSH's own catalog may still
1403
+ // resolve it, which only a lookup tells (once per failing request; resolving routes never pay it).
1404
+ if (!routeConfig && diagnoseResponsesRoute(ctx, options) === "api-unresolved")
1405
+ return hostRecognizedRouteStream(options, ctx, () => runtimeConfig, next);
1336
1406
  if (options.purpose === "compaction") {
1337
1407
  if (!routeConfig)
1338
- return unavailableManagedRouteStream(options);
1408
+ return unavailableManagedRouteStream(options, diagnoseResponsesRoute(ctx, options));
1339
1409
  return remoteCompactionStream(options, routeWithPolicies(routeConfig, runtimeConfig), ctx, next);
1340
1410
  }
1341
1411
  if (!routeConfig)
1342
- return unavailableManagedRouteStream(options);
1412
+ return unavailableManagedRouteStream(options, diagnoseResponsesRoute(ctx, options));
1343
1413
  return managedResponsesStream(options, routeWithPolicies(routeConfig, runtimeConfig), ctx);
1344
1414
  });
1345
1415
  ctx.effect?.(() => async () => {
@@ -0,0 +1,46 @@
1
+ import { getCatalogModels } from "./pi-responses-runtime.js";
2
+ const providerDefaultsCache = new Map();
3
+ function sharedValue(values) {
4
+ const distinct = new Set(values);
5
+ const [only] = distinct;
6
+ return distinct.size === 1 && typeof only === "string" && only.length > 0 ? only : undefined;
7
+ }
8
+ function providerDefaults(provider) {
9
+ const cached = providerDefaultsCache.get(provider);
10
+ if (cached !== undefined)
11
+ return cached;
12
+ const models = getCatalogModels(provider);
13
+ const defaults = {
14
+ api: sharedValue(models.map((model) => model.api)),
15
+ baseUrl: sharedValue(models.map((model) => model.baseUrl)),
16
+ };
17
+ providerDefaultsCache.set(provider, defaults);
18
+ return defaults;
19
+ }
20
+ /**
21
+ * Resolve a route model's protocol, endpoint and catalog descriptor from the bundled Pi catalog.
22
+ * @param dshServes DSH serves this model although the bundled catalog does not describe it: the provider profile
23
+ * lists it in `models`, or DSH's own catalog resolves it (`resolveModelInfo` succeeds). Only then may the
24
+ * provider-wide default apply.
25
+ */
26
+ export function resolveCatalogModel(provider, modelId, dshServes = false) {
27
+ const providerName = String(provider ?? "");
28
+ const id = String(modelId ?? "");
29
+ const exact = getCatalogModels(providerName).find((model) => model.id === id);
30
+ if (exact !== undefined)
31
+ return {
32
+ source: "catalog-model",
33
+ api: exact.api,
34
+ baseUrl: exact.baseUrl,
35
+ descriptor: exact.api === "openai-responses" ? exact : undefined,
36
+ };
37
+ const defaults = providerDefaults(providerName);
38
+ if (dshServes && defaults.api !== undefined)
39
+ return {
40
+ source: "catalog-provider-default",
41
+ api: defaults.api,
42
+ baseUrl: defaults.baseUrl,
43
+ descriptor: undefined,
44
+ };
45
+ return { source: "unknown", api: undefined, baseUrl: undefined, descriptor: undefined };
46
+ }