blun-king-cli 9.1.639 → 9.1.641

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/worker-host.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- // BLUN_BUILD_INPUT_SHA256:295bff16bb3a40df46f164599652a975e64b232ee91e1b245fa87962a4b61885
2
+ // BLUN_BUILD_INPUT_SHA256:d4df2815be55011d12f15cf3f9b01e9c4c1ba80d84ef9322ad9e8fd20b13e018
3
3
  /*! wasm-webp 0.1.0
4
4
  MIT License
5
5
 
@@ -2751,21 +2751,23 @@ var BlunChatProvider = class {
2751
2751
  _clientFactory;
2752
2752
  _files;
2753
2753
  constructor(options) {
2754
- const apiKey = options.apiKey ?? process.env["BLUN_API_KEY"];
2755
- this._apiKey = apiKey === void 0 || apiKey.length === 0 ? void 0 : apiKey;
2756
- this._baseUrl = options.baseUrl ?? process.env["BLUN_BASE_URL"] ?? "https://api.blun.ai/v1";
2757
- this._defaultHeaders = options.defaultHeaders;
2758
- this._clientFactory = options.clientFactory;
2759
- this._model = options.model;
2760
- this._stream = options.stream ?? true;
2761
- this._generationKwargs = { ...options.generationKwargs };
2762
- this._supportEfforts = options.supportEfforts ?? [];
2763
- this._client = this._apiKey === void 0 ? void 0 : createFetchHttpClient({
2764
- apiKey: this._apiKey,
2765
- baseUrl: this._baseUrl,
2766
- defaultHeaders: this._defaultHeaders
2767
- });
2768
- }
2754
+ const apiKey = options.apiKey ?? process.env["BLUN_API_KEY"];
2755
+ this._apiKey = apiKey === void 0 || apiKey.length === 0 ? void 0 : apiKey;
2756
+ this._baseUrl = options.baseUrl ?? process.env["BLUN_BASE_URL"] ?? "https://api.blun.ai/v1";
2757
+ this._defaultHeaders = options.defaultHeaders;
2758
+ this._clientFactory = options.clientFactory;
2759
+ this._requestBodyByteLimit = options.requestBodyByteLimit;
2760
+ this._requestBodyByteLimitStore = options.requestBodyByteLimitStore;
2761
+ this._model = options.model;
2762
+ this._stream = options.stream ?? true;
2763
+ this._generationKwargs = { ...options.generationKwargs };
2764
+ this._supportEfforts = options.supportEfforts ?? [];
2765
+ this._client = this._apiKey === void 0 ? void 0 : createFetchHttpClient({
2766
+ apiKey: this._apiKey,
2767
+ baseUrl: this._baseUrl,
2768
+ defaultHeaders: this._defaultHeaders
2769
+ });
2770
+ }
2769
2771
  get modelName() {
2770
2772
  return this._model;
2771
2773
  }
@@ -2802,116 +2804,139 @@ var BlunChatProvider = class {
2802
2804
  };
2803
2805
  }
2804
2806
  async generate(systemPrompt, tools, history, options) {
2805
- const messages = [];
2806
- if (systemPrompt) messages.push({
2807
- role: "system",
2808
- content: systemPrompt
2809
- });
2810
- const normalizedHistory = normalizeToolCallIdsForProvider(history, BLUN_TOOL_CALL_ID_POLICY);
2811
- for (const msg of normalizedHistory) {
2812
- if (isEmptyAssistantHistoryMessage(msg)) continue;
2813
- messages.push(convertMessage(msg));
2814
- }
2815
- const kwargs = { ...this._generationKwargs };
2816
- for (const key of Object.keys(kwargs)) if (kwargs[key] === void 0) delete kwargs[key];
2817
- if (kwargs["max_completion_tokens"] === void 0 && kwargs["max_tokens"] !== void 0) kwargs["max_completion_tokens"] = kwargs["max_tokens"];
2818
- delete kwargs["max_tokens"];
2819
- const { extra_body: extraBody, ...requestKwargs } = kwargs;
2820
- const extraBodyRecord = typeof extraBody === "object" && extraBody !== null ? { ...extraBody } : {};
2821
- const thinking = sanitizeThinkingConfig(extraBodyRecord.thinking);
2822
- const thinkingRecord = thinking;
2823
- const rawThinkingType = thinkingRecord?.["type"];
2824
- const thinkingType = rawThinkingType === "enabled" || rawThinkingType === "disabled" ? rawThinkingType : void 0;
2825
- const rawThinkingEffort = thinkingRecord?.["effort"];
2826
- const thinkingEffort = typeof rawThinkingEffort === "string" ? rawThinkingEffort : void 0;
2827
- const rawReasoningEffort = requestKwargs["reasoning_effort"];
2828
- const extraReasoningEffort = extraBodyRecord["reasoning_effort"];
2829
- const effectiveThinkingEffort = thinkingEffort ?? (thinkingType === void 0 && typeof extraReasoningEffort === "string" ? extraReasoningEffort : void 0) ?? (thinkingType === void 0 && typeof rawReasoningEffort === "string" ? rawReasoningEffort : void 0);
2830
- const configuredTemplateKwargs = requestKwargs["chat_template_kwargs"];
2831
- const extraTemplateKwargs = extraBodyRecord["chat_template_kwargs"];
2832
- let safeTemplateKwargs = {};
2833
- let templateThinking;
2834
- if (typeof configuredTemplateKwargs === "object" && configuredTemplateKwargs !== null) {
2835
- const { reasoning_effort: _nestedReasoningEffort, enable_thinking: configuredEnableThinking, thinking: configuredThinking, ...rest } = configuredTemplateKwargs;
2836
- safeTemplateKwargs = rest;
2837
- if (typeof configuredThinking === "boolean") templateThinking = configuredThinking;
2838
- else if (typeof configuredEnableThinking === "boolean") templateThinking = configuredEnableThinking;
2839
- }
2840
- if (typeof extraTemplateKwargs === "object" && extraTemplateKwargs !== null) {
2841
- const { reasoning_effort: _extraNestedReasoningEffort, enable_thinking: extraEnableThinking, thinking: extraThinking, ...rest } = extraTemplateKwargs;
2842
- safeTemplateKwargs = {
2843
- ...safeTemplateKwargs,
2844
- ...rest
2845
- };
2846
- if (typeof extraThinking === "boolean") templateThinking = extraThinking;
2847
- else if (typeof extraEnableThinking === "boolean") templateThinking = extraEnableThinking;
2848
- }
2849
- const hasThinkingIntent = thinkingType !== void 0 || effectiveThinkingEffort !== void 0 || templateThinking !== void 0;
2850
- const thinkingDisabled = thinkingType === "disabled" || effectiveThinkingEffort === "off" || effectiveThinkingEffort === "none" || thinkingType === void 0 && effectiveThinkingEffort === void 0 && templateThinking === false;
2851
- const templateReasoningEffort = !thinkingDisabled && typeof thinkingEffort === "string" && this._supportEfforts.includes(thinkingEffort) ? toBlunTemplateEffort(thinkingEffort) : void 0;
2852
- delete requestKwargs["reasoning_effort"];
2853
- delete requestKwargs["thinking"];
2854
- delete requestKwargs["enable_thinking"];
2855
- delete requestKwargs["stream"];
2856
- delete requestKwargs["model"];
2857
- delete requestKwargs["messages"];
2858
- if (hasThinkingIntent) requestKwargs["chat_template_kwargs"] = {
2859
- ...safeTemplateKwargs,
2860
- thinking: !thinkingDisabled,
2861
- ...templateReasoningEffort === void 0 ? {} : { reasoning_effort: templateReasoningEffort }
2862
- };
2863
- else if (typeof configuredTemplateKwargs === "object" || typeof extraTemplateKwargs === "object") if (Object.keys(safeTemplateKwargs).length > 0) requestKwargs["chat_template_kwargs"] = safeTemplateKwargs;
2864
- else delete requestKwargs["chat_template_kwargs"];
2865
- const requestExtraBody = extraBodyRecord;
2866
- delete requestExtraBody["reasoning_effort"];
2867
- delete requestExtraBody["chat_template_kwargs"];
2868
- delete requestExtraBody["enable_thinking"];
2869
- delete requestExtraBody["thinking"];
2870
- delete requestExtraBody["stream"];
2871
- delete requestExtraBody["model"];
2872
- delete requestExtraBody["messages"];
2873
- if (!thinkingDisabled && thinking?.keep !== void 0) requestExtraBody["thinking"] = { keep: thinking.keep };
2874
- const effectiveTemplateKwargs = requestKwargs["chat_template_kwargs"];
2875
- const reasoningRequested = typeof effectiveTemplateKwargs === "object" && effectiveTemplateKwargs !== null && effectiveTemplateKwargs.thinking === true;
2876
- const createParams = {
2877
- model: this._model,
2878
- messages,
2879
- stream: this._stream,
2880
- ...requestKwargs,
2881
- ...requestExtraBody
2882
- };
2883
- const visionAttachments = collectBlunVisionAttachments(normalizedHistory);
2884
- if (visionAttachments.length > 0) createParams["visionAttachments"] = visionAttachments;
2885
- if (tools.length > 0) createParams["tools"] = tools.map((t) => convertTool(t));
2886
- if (this._stream) createParams["stream_options"] = { include_usage: true };
2887
- try {
2888
- const client = this._createClient(options?.auth);
2889
- const requestOptions = options?.signal !== void 0 || options?.transportMaxRetries !== void 0 || options?.onTransportAttempt !== void 0 ? {
2890
- ...options.signal !== void 0 ? { signal: options.signal } : {},
2891
- ...options.transportMaxRetries !== void 0 ? { maxRetries: options.transportMaxRetries } : {},
2892
- ...options.onTransportAttempt !== void 0 ? { onTransportAttempt: options.onTransportAttempt } : {}
2893
- } : void 0;
2894
- options?.onRequestSent?.();
2895
- let response;
2896
- try {
2897
- response = await client.chat.completions.create(createParams, requestOptions);
2898
- } catch (error) {
2899
- const rejection = normalizeFetchHttpError(error);
2900
- const cap = rejection instanceof APIContextOverflowError ? contextOverflowOutputCap(rejection, createParams["max_completion_tokens"]) : void 0;
2901
- if (cap === void 0) throw rejection;
2902
- options?.signal?.throwIfAborted();
2903
- options?.onRequestSent?.();
2904
- response = await client.chat.completions.create({
2905
- ...createParams,
2906
- max_completion_tokens: cap
2907
- }, requestOptions);
2908
- }
2909
- return new BlunStreamedMessage(response, this._stream, reasoningRequested);
2910
- } catch (error) {
2911
- if (options?.signal?.aborted === true) throw options.signal.reason ?? error;
2912
- throw normalizeFetchHttpError(error);
2913
- }
2914
- }
2807
+ const messages = [];
2808
+ if (systemPrompt)
2809
+ messages.push({
2810
+ role: "system",
2811
+ content: systemPrompt
2812
+ });
2813
+ const normalizedHistory = normalizeToolCallIdsForProvider(history, BLUN_TOOL_CALL_ID_POLICY);
2814
+ for (const msg of normalizedHistory) {
2815
+ if (isEmptyAssistantHistoryMessage(msg))
2816
+ continue;
2817
+ messages.push(convertMessage(msg));
2818
+ }
2819
+ const kwargs = { ...this._generationKwargs };
2820
+ for (const key of Object.keys(kwargs))
2821
+ if (kwargs[key] === void 0)
2822
+ delete kwargs[key];
2823
+ if (kwargs["max_completion_tokens"] === void 0 && kwargs["max_tokens"] !== void 0)
2824
+ kwargs["max_completion_tokens"] = kwargs["max_tokens"];
2825
+ delete kwargs["max_tokens"];
2826
+ const { extra_body: extraBody, ...requestKwargs } = kwargs;
2827
+ const extraBodyRecord = typeof extraBody === "object" && extraBody !== null ? { ...extraBody } : {};
2828
+ const thinking = sanitizeThinkingConfig(extraBodyRecord.thinking);
2829
+ const thinkingRecord = thinking;
2830
+ const rawThinkingType = thinkingRecord?.["type"];
2831
+ const thinkingType = rawThinkingType === "enabled" || rawThinkingType === "disabled" ? rawThinkingType : void 0;
2832
+ const rawThinkingEffort = thinkingRecord?.["effort"];
2833
+ const thinkingEffort = typeof rawThinkingEffort === "string" ? rawThinkingEffort : void 0;
2834
+ const rawReasoningEffort = requestKwargs["reasoning_effort"];
2835
+ const extraReasoningEffort = extraBodyRecord["reasoning_effort"];
2836
+ const effectiveThinkingEffort = thinkingEffort ?? (thinkingType === void 0 && typeof extraReasoningEffort === "string" ? extraReasoningEffort : void 0) ?? (thinkingType === void 0 && typeof rawReasoningEffort === "string" ? rawReasoningEffort : void 0);
2837
+ const configuredTemplateKwargs = requestKwargs["chat_template_kwargs"];
2838
+ const extraTemplateKwargs = extraBodyRecord["chat_template_kwargs"];
2839
+ let safeTemplateKwargs = {};
2840
+ let templateThinking;
2841
+ if (typeof configuredTemplateKwargs === "object" && configuredTemplateKwargs !== null) {
2842
+ const { reasoning_effort: _nestedReasoningEffort, enable_thinking: configuredEnableThinking, thinking: configuredThinking, ...rest } = configuredTemplateKwargs;
2843
+ safeTemplateKwargs = rest;
2844
+ if (typeof configuredThinking === "boolean")
2845
+ templateThinking = configuredThinking;
2846
+ else if (typeof configuredEnableThinking === "boolean")
2847
+ templateThinking = configuredEnableThinking;
2848
+ }
2849
+ if (typeof extraTemplateKwargs === "object" && extraTemplateKwargs !== null) {
2850
+ const { reasoning_effort: _extraNestedReasoningEffort, enable_thinking: extraEnableThinking, thinking: extraThinking, ...rest } = extraTemplateKwargs;
2851
+ safeTemplateKwargs = {
2852
+ ...safeTemplateKwargs,
2853
+ ...rest
2854
+ };
2855
+ if (typeof extraThinking === "boolean")
2856
+ templateThinking = extraThinking;
2857
+ else if (typeof extraEnableThinking === "boolean")
2858
+ templateThinking = extraEnableThinking;
2859
+ }
2860
+ const hasThinkingIntent = thinkingType !== void 0 || effectiveThinkingEffort !== void 0 || templateThinking !== void 0;
2861
+ const thinkingDisabled = thinkingType === "disabled" || effectiveThinkingEffort === "off" || effectiveThinkingEffort === "none" || thinkingType === void 0 && effectiveThinkingEffort === void 0 && templateThinking === false;
2862
+ const templateReasoningEffort = !thinkingDisabled && typeof thinkingEffort === "string" && this._supportEfforts.includes(thinkingEffort) ? toBlunTemplateEffort(thinkingEffort) : void 0;
2863
+ delete requestKwargs["reasoning_effort"];
2864
+ delete requestKwargs["thinking"];
2865
+ delete requestKwargs["enable_thinking"];
2866
+ delete requestKwargs["stream"];
2867
+ delete requestKwargs["model"];
2868
+ delete requestKwargs["messages"];
2869
+ if (hasThinkingIntent)
2870
+ requestKwargs["chat_template_kwargs"] = {
2871
+ ...safeTemplateKwargs,
2872
+ thinking: !thinkingDisabled,
2873
+ ...templateReasoningEffort === void 0 ? {} : { reasoning_effort: templateReasoningEffort }
2874
+ };
2875
+ else if (typeof configuredTemplateKwargs === "object" || typeof extraTemplateKwargs === "object")
2876
+ if (Object.keys(safeTemplateKwargs).length > 0)
2877
+ requestKwargs["chat_template_kwargs"] = safeTemplateKwargs;
2878
+ else
2879
+ delete requestKwargs["chat_template_kwargs"];
2880
+ const requestExtraBody = extraBodyRecord;
2881
+ delete requestExtraBody["reasoning_effort"];
2882
+ delete requestExtraBody["chat_template_kwargs"];
2883
+ delete requestExtraBody["enable_thinking"];
2884
+ delete requestExtraBody["thinking"];
2885
+ delete requestExtraBody["stream"];
2886
+ delete requestExtraBody["model"];
2887
+ delete requestExtraBody["messages"];
2888
+ if (!thinkingDisabled && thinking?.keep !== void 0)
2889
+ requestExtraBody["thinking"] = { keep: thinking.keep };
2890
+ const effectiveTemplateKwargs = requestKwargs["chat_template_kwargs"];
2891
+ const reasoningRequested = typeof effectiveTemplateKwargs === "object" && effectiveTemplateKwargs !== null && effectiveTemplateKwargs.thinking === true;
2892
+ const createParams = {
2893
+ model: this._model,
2894
+ messages,
2895
+ stream: this._stream,
2896
+ ...requestKwargs,
2897
+ ...requestExtraBody
2898
+ };
2899
+ const visionAttachments = collectBlunVisionAttachments(normalizedHistory);
2900
+ if (visionAttachments.length > 0)
2901
+ createParams["visionAttachments"] = visionAttachments;
2902
+ if (tools.length > 0)
2903
+ createParams["tools"] = tools.map((t) => convertTool(t));
2904
+ if (this._stream)
2905
+ createParams["stream_options"] = { include_usage: true };
2906
+ try {
2907
+ const measured = await t3458Measure(this, createParams, options);
2908
+ const client = this._createClient(options?.auth);
2909
+ const requestOptions = options?.signal !== void 0 || options?.transportMaxRetries !== void 0 || options?.onTransportAttempt !== void 0 ? {
2910
+ ...options.signal !== void 0 ? { signal: options.signal } : {},
2911
+ ...options.transportMaxRetries !== void 0 ? { maxRetries: options.transportMaxRetries } : {},
2912
+ ...options.onTransportAttempt !== void 0 ? { onTransportAttempt: options.onTransportAttempt } : {}
2913
+ } : void 0;
2914
+ options?.onRequestSent?.();
2915
+ let response;
2916
+ try {
2917
+ response = await client.chat.completions.create(createParams, requestOptions);
2918
+ }
2919
+ catch (error) {
2920
+ await t3458Learn(this, error, { key: t3458Key(this._baseUrl) }, options);
2921
+ const rejection = normalizeFetchHttpError(error);
2922
+ const cap = rejection instanceof APIContextOverflowError ? contextOverflowOutputCap(rejection, createParams["max_completion_tokens"]) : void 0;
2923
+ if (cap === void 0)
2924
+ throw rejection;
2925
+ options?.signal?.throwIfAborted();
2926
+ options?.onRequestSent?.();
2927
+ response = await client.chat.completions.create({
2928
+ ...createParams,
2929
+ max_completion_tokens: cap
2930
+ }, requestOptions);
2931
+ }
2932
+ return new BlunStreamedMessage(response, this._stream, reasoningRequested);
2933
+ }
2934
+ catch (error) {
2935
+ if (options?.signal?.aborted === true)
2936
+ throw options.signal.reason ?? error;
2937
+ throw normalizeFetchHttpError(error);
2938
+ }
2939
+ }
2915
2940
  withThinking(effort) {
2916
2941
  if (typeof effort !== "string") return this._clone();
2917
2942
  let thinking;
@@ -15871,13 +15896,25 @@ function transformLoopControlData(data) {
15871
15896
  return out;
15872
15897
  }
15873
15898
  async function writeConfigFile(filePath, config) {
15874
- const validated = validateConfig(stripEnvModelConfig(config));
15875
- const configDirectory = dirname$3(filePath);
15876
- if (await mkdir(configDirectory, {
15877
- recursive: true,
15878
- mode: 448
15879
- }) !== void 0) await hardenPrivatePath(configDirectory, "directory");
15880
- await atomicWritePrivate(filePath, `${stringify$1(configToTomlData(validated))}\n`);
15899
+ return t3458WithConfigLock(filePath, async () => {
15900
+ let latestTable;
15901
+ try {
15902
+ latestTable = parse$6(await t3458Fs.readFile(filePath, "utf8")).request_body_byte_limits;
15903
+ }
15904
+ catch (error) {
15905
+ /* Unreadable or invalid previous TOML: preserve the Registry639 overwrite behavior. */
15906
+ }
15907
+ if (latestTable !== undefined)
15908
+ config = { ...config, raw: { ...config.raw, request_body_byte_limits: latestTable } };
15909
+ const validated = validateConfig(stripEnvModelConfig(config));
15910
+ const configDirectory = dirname$3(filePath);
15911
+ if (await mkdir(configDirectory, {
15912
+ recursive: true,
15913
+ mode: 448
15914
+ }) !== void 0)
15915
+ await hardenPrivatePath(configDirectory, "directory");
15916
+ await atomicWritePrivate(filePath, `${stringify$1(configToTomlData(validated))}\n`);
15917
+ });
15881
15918
  }
15882
15919
  function configToTomlData(config) {
15883
15920
  const out = cloneRecord$1(config.raw);
@@ -229222,8 +229259,8 @@ var FullCompaction = class {
229222
229259
  };
229223
229260
  }
229224
229261
  shouldRecoverFromContextOverflow(error) {
229225
- return error instanceof APIContextOverflowError;
229226
- }
229262
+ return error instanceof APIContextOverflowError || error?.requestBodyPressure === true;
229263
+ }
229227
229264
  observeContextOverflow(error, modelAlias = this.agent.activeResponderModel ?? this.agent.config.modelAlias) {
229228
229265
  if (!(error instanceof APIContextOverflowError)) return;
229229
229266
  const observed = error.maxContextTokens;
@@ -229239,63 +229276,70 @@ var FullCompaction = class {
229239
229276
  if (this.lastCompactedState?.modelAlias === modelAlias) this.lastCompactedState = null;
229240
229277
  }
229241
229278
  begin(data) {
229242
- this.assertAdmissionOpen();
229243
- if (this.compacting) return;
229244
- if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES) throw this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED);
229245
- if (data.source === "manual") this.compactionCountInTurn = 0;
229246
- else this.compactionCountInTurn += 1;
229247
- if (this.compactionCountInTurn > this.strategy.maxCompactionPerTurn) return;
229248
- if (this.agent.records.restoring) {
229249
- this.agent.replayBuilder.push({
229250
- type: "compaction",
229251
- instruction: data.instruction
229252
- });
229253
- return;
229254
- }
229255
- if (this.agent.context.history.length === 0) throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "No messages to compact in current history.");
229256
- if (data.source === "manual" && this.agent.turn.hasActiveTurn) throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "Cannot compact while a turn is active. Wait for it to finish, then retry.");
229257
- this.agent.records.logRecord({
229258
- type: "full_compaction.begin",
229259
- ...data
229260
- });
229261
- this.assertAdmissionOpen();
229262
- const abortController = new AbortController();
229263
- const executionId = ++this.nextExecutionId;
229264
- let resolve;
229265
- let reject;
229266
- const promise = new Promise((yes, no) => {
229267
- resolve = yes;
229268
- reject = no;
229269
- });
229270
- this.executions.track({
229271
- kind: "worker",
229272
- id: executionId,
229273
- source: data.source
229274
- }, promise);
229275
- this.executingControllers.add(abortController);
229276
- this.compacting = {
229277
- abortController,
229278
- promise,
229279
- blockedByTurn: false
229280
- };
229281
- const started = {
229282
- type: "compaction.started",
229283
- trigger: data.source,
229284
- instruction: data.instruction
229285
- };
229286
- this.agent.emitEvent(this.agent.experimentalFlags.enabled("queue_policies") ? {
229287
- ...started,
229288
- executionId,
229289
- contextTurnId: this.agent.turn.currentId
229290
- } : started);
229291
- this.compactionWorker(abortController.signal, data, executionId).then(() => {
229292
- this.executingControllers.delete(abortController);
229293
- resolve();
229294
- }, (error) => {
229295
- this.executingControllers.delete(abortController);
229296
- reject(error);
229297
- });
229298
- }
229279
+ this.assertAdmissionOpen();
229280
+ if (this.compacting)
229281
+ return;
229282
+ if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES)
229283
+ throw this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED);
229284
+ if (data.source === "manual")
229285
+ this.compactionCountInTurn = 0;
229286
+ else if (!data.requestBodyPressure)
229287
+ this.compactionCountInTurn += 1;
229288
+ if (this.compactionCountInTurn > this.strategy.maxCompactionPerTurn)
229289
+ return;
229290
+ if (this.agent.records.restoring) {
229291
+ this.agent.replayBuilder.push({
229292
+ type: "compaction",
229293
+ instruction: data.instruction
229294
+ });
229295
+ return;
229296
+ }
229297
+ if (this.agent.context.history.length === 0)
229298
+ throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "No messages to compact in current history.");
229299
+ if (data.source === "manual" && this.agent.turn.hasActiveTurn)
229300
+ throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "Cannot compact while a turn is active. Wait for it to finish, then retry.");
229301
+ this.agent.records.logRecord({
229302
+ type: "full_compaction.begin",
229303
+ ...data
229304
+ });
229305
+ this.assertAdmissionOpen();
229306
+ const abortController = new AbortController();
229307
+ const executionId = ++this.nextExecutionId;
229308
+ let resolve;
229309
+ let reject;
229310
+ const promise = new Promise((yes, no) => {
229311
+ resolve = yes;
229312
+ reject = no;
229313
+ });
229314
+ this.executions.track({
229315
+ kind: "worker",
229316
+ id: executionId,
229317
+ source: data.source
229318
+ }, promise);
229319
+ this.executingControllers.add(abortController);
229320
+ this.compacting = {
229321
+ abortController,
229322
+ promise,
229323
+ blockedByTurn: false
229324
+ };
229325
+ const started = {
229326
+ type: "compaction.started",
229327
+ trigger: data.source,
229328
+ instruction: data.instruction
229329
+ };
229330
+ this.agent.emitEvent(this.agent.experimentalFlags.enabled("queue_policies") ? {
229331
+ ...started,
229332
+ executionId,
229333
+ contextTurnId: this.agent.turn.currentId
229334
+ } : started);
229335
+ this.compactionWorker(abortController.signal, data, executionId).then(() => {
229336
+ this.executingControllers.delete(abortController);
229337
+ resolve();
229338
+ }, (error) => {
229339
+ this.executingControllers.delete(abortController);
229340
+ reject(error);
229341
+ });
229342
+ }
229299
229343
  cancel() {
229300
229344
  this.agent.replayBuilder.patchLast("compaction", { result: "cancelled" });
229301
229345
  if (!this.compacting) return;
@@ -229328,12 +229372,28 @@ var FullCompaction = class {
229328
229372
  this.sessionCompactionFailures = Math.max(this.sessionCompactionFailures, state.sessionCompactionFailures);
229329
229373
  }
229330
229374
  async handleOverflowError(signal, error) {
229331
- this.consecutiveOverflowCompactions += 1;
229332
- const maxAttempts = this.strategy.maxOverflowCompactionAttempts;
229333
- if (this.consecutiveOverflowCompactions > maxAttempts) throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, `Compaction failed to bring the context under the model window after ${String(maxAttempts)} attempts.`, { cause: error instanceof Error ? error : void 0 });
229334
- if (!this.beginAutoCompaction() && !this.compacting) throw error;
229335
- await this.block(signal);
229336
- }
229375
+ if (error?.requestBodyPressure === true && error.byteLength < error.limitBytes) {
229376
+ signal.throwIfAborted();
229377
+ if (!this.hasShrinkableHistory(this.agent.context.history) || this.isSessionCompactionStopped)
229378
+ return;
229379
+ try {
229380
+ this.begin({ source: "auto", instruction: undefined, requestBodyPressure: true });
229381
+ await this.block(signal);
229382
+ }
229383
+ catch (failure) {
229384
+ signal.throwIfAborted();
229385
+ this.agent.log.warn("optional request-body compaction failed; valid request remains sendable", { error: failure });
229386
+ }
229387
+ return;
229388
+ }
229389
+ this.consecutiveOverflowCompactions += 1;
229390
+ const maxAttempts = this.strategy.maxOverflowCompactionAttempts;
229391
+ if (this.consecutiveOverflowCompactions > maxAttempts)
229392
+ throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, `Compaction failed to bring the context under the model window after ${String(maxAttempts)} attempts.`, { cause: error instanceof Error ? error : void 0 });
229393
+ if (!this.beginAutoCompaction() && !this.compacting)
229394
+ throw error;
229395
+ await this.block(signal);
229396
+ }
229337
229397
  async beforeStep(signal, requestContext = {}) {
229338
229398
  signal.throwIfAborted();
229339
229399
  const historyBefore = this.agent.context.history;
@@ -229386,10 +229446,28 @@ var FullCompaction = class {
229386
229446
  if (this.requiresShrinkableHistory && !this.hasShrinkableHistory(messages)) return false;
229387
229447
  return this.beginAutoCompaction(throwOnLimit);
229388
229448
  }
229389
- hasShrinkableHistory(messages) {
229390
- if (messages.some((message) => message.role === "assistant" || message.role === "tool")) return true;
229391
- return estimateTokensForMessages(messages.filter(isRealUserInput)) > COMPACT_USER_MESSAGE_MAX_TOKENS;
229392
- }
229449
+ shouldCompactRequestBody(endpoint, bytes, limit) {
229450
+ if (bytes >= limit)
229451
+ return true;
229452
+ const attempts = this.t3458PressureAttempts ??= new Map();
229453
+ const key = endpoint + "\0" + String(limit);
229454
+ if (bytes < limit * 0.75) {
229455
+ attempts.delete(key);
229456
+ return false;
229457
+ }
229458
+ if (this.isSessionCompactionStopped || !this.hasShrinkableHistory(this.agent.context.history))
229459
+ return false;
229460
+ const previous = attempts.get(key);
229461
+ if (previous !== undefined && bytes < previous + Math.ceil(previous / 10))
229462
+ return false;
229463
+ attempts.set(key, bytes);
229464
+ return true;
229465
+ }
229466
+ hasShrinkableHistory(messages) {
229467
+ if (messages.some((message) => message.role === "tool" || message.role === "assistant" && ((message.toolCalls?.length ?? 0) > 0 || message.content.some(part => part.type !== "text" || part.text.trim().length > 0))))
229468
+ return true;
229469
+ return estimateTokensForMessages(messages.filter(isRealUserInput)) > COMPACT_USER_MESSAGE_MAX_TOKENS;
229470
+ }
229393
229471
  beginAutoCompaction(throwOnLimit = true) {
229394
229472
  if (this.compacting) return true;
229395
229473
  if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES) {
@@ -229433,52 +229511,57 @@ var FullCompaction = class {
229433
229511
  }
229434
229512
  }
229435
229513
  async compactionWorker(signal, data, executionId) {
229436
- try {
229437
- const output = await this.compactionRound(signal, data, executionId);
229438
- if (!output) return;
229439
- const { result, stageCount } = output;
229440
- try {
229441
- await this.agent.refreshSystemPrompt();
229442
- } catch (error) {
229443
- this.agent.log.error("failed to refresh system prompt after compaction", { error });
229444
- }
229445
- await this.agent.injection.injectAfterCompaction();
229446
- this.fitRetainedInput({}, result.keptHeadUserMessageCount !== void 0);
229447
- this.lastCompactedState = {
229448
- tokenCount: this.tokenCountWithPending,
229449
- modelAlias: this.agent.activeResponderModel ?? this.agent.config.modelAlias,
229450
- maxContextTokens: this.getEffectiveMaxContextTokens()
229451
- };
229452
- this.markCompleted();
229453
- const { contextSummary: _contextSummary, ...eventResult } = result;
229454
- const projectedContextTokens = this.estimateCurrentRequestTokens();
229455
- this.agent.emitEvent({
229456
- type: "compaction.completed",
229457
- result: eventResult,
229458
- projectedContextTokens,
229459
- ...stageCount > 1 ? { stageCount } : {}
229460
- });
229461
- this.triggerPostCompactHook(data, result);
229462
- } catch (error) {
229463
- if (isAbortError(error) || signal.aborted) return;
229464
- this.sessionCompactionFailures += 1;
229465
- this.agent.records.logRecord({
229466
- type: "full_compaction.session_failures",
229467
- count: this.sessionCompactionFailures
229468
- });
229469
- const blockedByTurn = this.compacting?.blockedByTurn === true;
229470
- this.cancel();
229471
- this.agent.log.error("compaction failed", { error });
229472
- const visibleError = this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES ? this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED, error) : error;
229473
- if (blockedByTurn) throw visibleError;
229474
- this.agent.emitEvent({
229475
- type: "error",
229476
- ...toBlunErrorPayload(visibleError)
229477
- });
229478
- } finally {
229479
- this.agent.turn.onCompactionFinished();
229480
- }
229481
- }
229514
+ try {
229515
+ const output = await this.compactionRound(signal, data, executionId);
229516
+ if (!output)
229517
+ return;
229518
+ const { result, stageCount } = output;
229519
+ try {
229520
+ await this.agent.refreshSystemPrompt();
229521
+ }
229522
+ catch (error) {
229523
+ this.agent.log.error("failed to refresh system prompt after compaction", { error });
229524
+ }
229525
+ await this.agent.injection.injectAfterCompaction();
229526
+ this.fitRetainedInput({}, result.keptHeadUserMessageCount !== void 0);
229527
+ this.lastCompactedState = {
229528
+ tokenCount: this.tokenCountWithPending,
229529
+ modelAlias: this.agent.activeResponderModel ?? this.agent.config.modelAlias,
229530
+ maxContextTokens: this.getEffectiveMaxContextTokens()
229531
+ };
229532
+ this.markCompleted();
229533
+ const { contextSummary: _contextSummary, ...eventResult } = result;
229534
+ const projectedContextTokens = this.estimateCurrentRequestTokens();
229535
+ this.agent.emitEvent({
229536
+ type: "compaction.completed",
229537
+ result: eventResult,
229538
+ projectedContextTokens,
229539
+ ...stageCount > 1 ? { stageCount } : {}
229540
+ });
229541
+ this.triggerPostCompactHook(data, result);
229542
+ }
229543
+ catch (error) {
229544
+ if (isAbortError(error) || signal.aborted)
229545
+ return;
229546
+ if (!data.requestBodyPressure) {
229547
+ this.sessionCompactionFailures += 1;
229548
+ this.agent.records.logRecord({ type: "full_compaction.session_failures", count: this.sessionCompactionFailures });
229549
+ }
229550
+ const blockedByTurn = this.compacting?.blockedByTurn === true;
229551
+ this.cancel();
229552
+ this.agent.log.error("compaction failed", { error });
229553
+ const visibleError = this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES ? this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED, error) : error;
229554
+ if (blockedByTurn)
229555
+ throw visibleError;
229556
+ this.agent.emitEvent({
229557
+ type: "error",
229558
+ ...toBlunErrorPayload(visibleError)
229559
+ });
229560
+ }
229561
+ finally {
229562
+ this.agent.turn.onCompactionFinished();
229563
+ }
229564
+ }
229482
229565
  fitRetainedInput(context = {}, freshBudgetedSelection = false) {
229483
229566
  const budget = this.getCompactionBudgetTokens();
229484
229567
  const window = this.getEffectiveMaxContextTokens();
@@ -229502,336 +229585,367 @@ var FullCompaction = class {
229502
229585
  return `${summary.trim()}\n\n${todoMarkdown}`;
229503
229586
  }
229504
229587
  async compactionRound(signal, data, executionId) {
229505
- const startedAt = Date.now();
229506
- const originalHistory = [...this.agent.context.history];
229507
- const tokensBefore = estimateTokensForMessages(originalHistory);
229508
- let retryCount = 0;
229509
- try {
229510
- await this.triggerPreCompactHook(data, tokensBefore, signal);
229511
- const model = resolveCompactionModelAlias(this.agent.blunConfig?.loopControl?.compactionModel, this.agent.activeResponderModel ?? this.agent.config.model);
229512
- const runtimeModel = this.agent.config.runtimeModel(model);
229513
- const capability = runtimeModel.modelCapabilities;
229514
- const buildCompactionProvider = (usedContextTokens) => {
229515
- const currentMaxContextTokens = this.getEffectiveMaxContextTokens(model);
229516
- const defaultCompactionCap = currentMaxContextTokens > 0 ? Math.min(currentMaxContextTokens, DEFAULT_COMPACTION_MAX_COMPLETION_TOKENS) : void 0;
229517
- const requestCapability = currentMaxContextTokens > 0 ? {
229518
- ...capability,
229519
- max_context_tokens: currentMaxContextTokens
229520
- } : capability;
229521
- return applyCompletionBudget({
229522
- provider: runtimeModel.provider,
229523
- budget: resolveCompletionBudget({
229524
- maxOutputSize: runtimeModel.maxOutputSize ?? defaultCompactionCap,
229525
- reservedContextSize: this.agent.blunConfig?.loopControl?.reservedContextSize
229526
- }),
229527
- capability: requestCapability,
229528
- usedContextTokens
229529
- });
229530
- };
229531
- let provider = buildCompactionProvider();
229532
- const instruction = this.buildInstruction(data.instruction);
229533
- const delays = retryBackoffDelays(5);
229534
- let usage = null;
229535
- let summary;
229536
- let historyForModel = originalHistory.filter((message) => {
229537
- const origin = message.origin;
229538
- return !(origin?.kind === "injection" && REBUILT_AFTER_COMPACTION_INJECTION_VARIANTS.has(origin.variant));
229539
- });
229540
- let droppedCount = 0;
229541
- let overflowShrinkCount = 0;
229542
- let incompleteSummaryAttempts = 0;
229543
- let hierarchicalPassCount = 0;
229544
- const typicalDurationMs = readTypicalCompactionDuration(provider.name, provider.modelName, this.agent.blunHomeDir)?.typicalDurationMs;
229545
- let attemptCount = 0;
229546
- let charsReceived = 0;
229547
- let lastProgressEmitAt = 0;
229548
- let initialCompactionRequestTokens;
229549
- const compactionTools = [];
229550
- const buildRequestMessages = (history, requestInstruction) => {
229551
- return stripMediaForCompaction(downgradeUnsupportedMedia([...this.agent.context.project(history, {
229552
- synthesizeMissing: true,
229553
- dropOrphanResults: true
229554
- }), createUserMessage(requestInstruction)], capability));
229555
- };
229556
- while (true) {
229557
- signal.throwIfAborted();
229558
- if (hierarchicalPassCount >= MAX_HIERARCHICAL_COMPACTION_PASSES) throw new BlunError(ErrorCodes.COMPACTION_FAILED, `Compaction stopped after ${String(MAX_HIERARCHICAL_COMPACTION_PASSES)} staged requests without reaching a final summary. The conversation history was not changed.`, { details: {
229559
- contextUnchanged: true,
229560
- hierarchicalPassCount
229561
- } });
229562
- const compactionRequestLimit = this.getEffectiveMaxContextTokens(model);
229563
- const safeCompactionRequestLimit = compactionRequestLimit > 0 ? Math.max(1, Math.floor(compactionRequestLimit * (1 - COMPACTION_SUMMARY_RESERVE_RATIO))) : compactionRequestLimit;
229564
- let messages = buildRequestMessages(historyForModel, instruction);
229565
- let estimatedCompactionRequestTokens = this.estimateRequestTokens(messages, this.agent.effectiveSystemPrompt, compactionTools);
229566
- initialCompactionRequestTokens ??= estimatedCompactionRequestTokens;
229567
- let hierarchicalChunkEnd;
229568
- if (safeCompactionRequestLimit > 0 && estimatedCompactionRequestTokens >= safeCompactionRequestLimit) {
229569
- if (historyForModel.length > 1) {
229570
- const chunkInstruction = `${instruction}\n\nThis request contains only the oldest chronological segment. Summarize every fact needed by a later merge pass. Do not assume later messages are visible.`;
229571
- const chunk = selectHierarchicalCompactionChunk(historyForModel, safeCompactionRequestLimit, safeCompactionRequestLimit, (candidate) => {
229572
- const candidateMessages = buildRequestMessages(candidate, chunkInstruction);
229573
- return {
229574
- messages: candidateMessages,
229575
- estimatedTokens: this.estimateRequestTokens(candidateMessages, this.agent.effectiveSystemPrompt, compactionTools)
229576
- };
229577
- });
229578
- if (chunk !== void 0) {
229579
- hierarchicalChunkEnd = chunk.end;
229580
- messages = chunk.messages;
229581
- estimatedCompactionRequestTokens = chunk.estimatedTokens;
229582
- }
229583
- }
229584
- if (hierarchicalChunkEnd === void 0) throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, `Compaction stopped before upload: the conversation (${String(tokensBefore)} tokens) could not be divided into a request below the active model window (${String(compactionRequestLimit)} tokens).`, { details: {
229585
- contextBrakeBlocked: true,
229586
- estimatedRequestTokens: estimatedCompactionRequestTokens,
229587
- maxContextTokens: compactionRequestLimit,
229588
- requestBudgetTokens: safeCompactionRequestLimit,
229589
- summaryReserveTokens: compactionRequestLimit - safeCompactionRequestLimit,
229590
- contextUnchanged: true
229591
- } });
229592
- }
229593
- provider = buildCompactionProvider(estimatedCompactionRequestTokens);
229594
- attemptCount += 1;
229595
- charsReceived = 0;
229596
- const stage = hierarchicalPassCount + 1;
229597
- const estimatedStageCount = estimateCompactionStageCount(initialCompactionRequestTokens, safeCompactionRequestLimit, stage);
229598
- const estimatedProgressPercent = estimateCompactionProgressPercent(stage, estimatedStageCount);
229599
- const windowUsagePercent = estimateCompactionWindowUsagePercent(estimatedCompactionRequestTokens, compactionRequestLimit);
229600
- this.agent.log.info("compaction stage request", {
229601
- source: data.source,
229602
- stage,
229603
- estimatedStageCount,
229604
- activeContextTokens: this.estimateProjectedRequestTokens(),
229605
- estimatedInputTokens: estimatedCompactionRequestTokens,
229606
- windowUsagePercent,
229607
- safeInputLimitTokens: safeCompactionRequestLimit,
229608
- maxContextTokens: compactionRequestLimit
229609
- });
229610
- const emitCompactionProgress = (force = false) => {
229611
- const now = Date.now();
229612
- if (!force && now - lastProgressEmitAt < 150) return;
229613
- lastProgressEmitAt = now;
229614
- this.agent.emitEvent({
229615
- type: "compaction.progress",
229616
- stage,
229617
- estimatedStageCount,
229618
- estimatedInputTokens: estimatedCompactionRequestTokens,
229619
- estimatedProgressPercent,
229620
- ...windowUsagePercent === void 0 ? {} : { windowUsagePercent },
229621
- charsReceived,
229622
- attempt: attemptCount,
229623
- ...typicalDurationMs !== void 0 ? { typicalDurationMs } : {}
229624
- });
229625
- };
229626
- emitCompactionProgress(true);
229627
- const timingKey = compactionTimingKey(provider);
229628
- const stallPolicy = this.measuredStallPolicy(timingKey);
229629
- let lastProgressAt = Date.now();
229630
- let maxObservedIdleMs = 0;
229631
- const attemptController = new AbortController();
229632
- const onParentAbort = () => {
229633
- attemptController.abort();
229634
- };
229635
- signal.addEventListener("abort", onParentAbort, { once: true });
229636
- let stalled = false;
229637
- let stallTimer;
229638
- const armStallTimer = () => {
229639
- if (stallPolicy === void 0) return;
229640
- if (stallTimer !== void 0) clearTimeout(stallTimer);
229641
- stallTimer = setTimeout(() => {
229642
- stalled = true;
229643
- attemptController.abort();
229644
- }, stallPolicy.timeoutMs);
229645
- stallTimer.unref?.();
229646
- };
229647
- const observeProgress = (part) => {
229648
- const now = Date.now();
229649
- maxObservedIdleMs = Math.max(maxObservedIdleMs, now - lastProgressAt);
229650
- lastProgressAt = now;
229651
- if (part.type === "text") charsReceived += part.text.length;
229652
- emitCompactionProgress();
229653
- armStallTimer();
229654
- };
229655
- armStallTimer();
229656
- try {
229657
- const response = await this.agent.generate(provider, this.agent.config.systemPrompt, compactionTools, messages, { onMessagePart: observeProgress }, {
229658
- signal: attemptController.signal,
229659
- onExecution: (completion) => {
229660
- this.executions.track({
229661
- kind: "provider",
229662
- id: executionId,
229663
- source: data.source
229664
- }, completion);
229665
- }
229666
- });
229667
- signal.throwIfAborted();
229668
- if (stalled && !signal.aborted && stallPolicy !== void 0) throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
229669
- maxObservedIdleMs = Math.max(maxObservedIdleMs, Date.now() - lastProgressAt);
229670
- if (response.finishReason === "truncated") throw new CompactionTruncatedError();
229671
- if (response.usage !== null) usage = usage === null ? response.usage : addUsage(usage, response.usage);
229672
- const extractedSummary = extractCompactionSummary(response);
229673
- this.observeCompactionTiming(timingKey, maxObservedIdleMs);
229674
- if (hierarchicalChunkEnd !== void 0) {
229675
- hierarchicalPassCount += 1;
229676
- const previousTokens = estimateTokensForMessages(historyForModel);
229677
- const nextHistory = [createUserMessage(`${HIERARCHICAL_COMPACTION_PREFIX}\n${extractedSummary}`), ...historyForModel.slice(hierarchicalChunkEnd)];
229678
- const nextTokens = estimateTokensForMessages(nextHistory);
229679
- if (nextTokens >= previousTokens) throw new BlunError(ErrorCodes.COMPACTION_FAILED, "A staged compaction response did not reduce the pending history. The conversation history was not changed.", { details: {
229680
- contextUnchanged: true,
229681
- hierarchicalPassCount,
229682
- previousTokens,
229683
- nextTokens
229684
- } });
229685
- historyForModel = nextHistory;
229686
- incompleteSummaryAttempts = 0;
229687
- retryCount = 0;
229688
- continue;
229689
- }
229690
- summary = extractedSummary;
229691
- appendCompactionTiming({
229692
- ts: Date.now(),
229693
- provider: provider.name,
229694
- model: provider.modelName,
229695
- estimatedInputTokens: tokensBefore,
229696
- durationMs: Date.now() - startedAt
229697
- }, this.agent.blunHomeDir);
229698
- break;
229699
- } catch (error) {
229700
- if (stalled && !signal.aborted && stallPolicy !== void 0) throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
229701
- if (isModelFallbackStatus(error)) throw error;
229702
- const isContextOverflow = this.shouldRecoverFromContextOverflow(error);
229703
- if (isContextOverflow) this.observeContextOverflow(error, model);
229704
- if (isContextOverflow && historyForModel.length > 1) {
229705
- if (data.source === "auto") {
229706
- const learnedMaxContextTokens = this.getEffectiveMaxContextTokens(model);
229707
- if (learnedMaxContextTokens > 0 && learnedMaxContextTokens < compactionRequestLimit) {
229708
- retryCount = 0;
229709
- continue;
229710
- }
229711
- throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, "The active model rejected the full compaction request; refusing to drop unsummarized history.", {
229712
- cause: error,
229713
- details: {
229714
- contextBrakeBlocked: true,
229715
- estimatedRequestTokens: estimatedCompactionRequestTokens,
229716
- maxContextTokens: learnedMaxContextTokens,
229717
- requestBudgetTokens: learnedMaxContextTokens,
229718
- contextUnchanged: true
229719
- }
229720
- });
229721
- }
229722
- overflowShrinkCount += 1;
229723
- if (overflowShrinkCount > MAX_COMPACTION_OVERFLOW_SHRINK_ATTEMPTS) throw error;
229724
- const before = historyForModel.length;
229725
- historyForModel = shrinkCompactionHistoryAfterOverflow(historyForModel, overflowShrinkCount);
229726
- droppedCount += before - historyForModel.length;
229727
- retryCount = 0;
229728
- continue;
229729
- }
229730
- if (error instanceof CompactionTruncatedError || error instanceof APIEmptyResponseError) {
229731
- incompleteSummaryAttempts += 1;
229732
- if (incompleteSummaryAttempts === 1) continue;
229733
- throw new BlunError(ErrorCodes.COMPACTION_FAILED, "The provider returned an empty or truncated compaction response twice. The conversation history was not changed.", {
229734
- cause: error,
229735
- details: {
229736
- contextUnchanged: true,
229737
- lossyRetryPrevented: true,
229738
- compactionFailure: error.name,
229739
- incompleteSummaryAttempts
229740
- }
229741
- });
229742
- }
229743
- if (!isRetryableGenerateError(error)) throw error;
229744
- if (retryCount + 1 >= 5) throw error;
229745
- await sleepForRetry(delays[retryCount], signal);
229746
- retryCount += 1;
229747
- } finally {
229748
- if (stallTimer !== void 0) clearTimeout(stallTimer);
229749
- signal.removeEventListener("abort", onParentAbort);
229750
- }
229751
- }
229752
- if (usage !== null) this.agent.usage.record(model, usage);
229753
- const newHistory = this.agent.context.history;
229754
- for (let i = 0; i < originalHistory.length; i++) if (newHistory[i] !== originalHistory[i]) {
229755
- this.cancel();
229756
- return;
229757
- }
229758
- if (newHistory.slice(originalHistory.length).some((message) => !isRealUserInput(message))) {
229759
- this.cancel();
229760
- return;
229761
- }
229762
- const rawSummary = this.postProcessSummary(summary ?? "");
229763
- const contextSummary = buildCompactionSummaryText(rawSummary);
229764
- const result = this.agent.context.applyCompaction({
229765
- summary: rawSummary,
229766
- contextSummary,
229767
- compactedCount: originalHistory.length,
229768
- tokensBefore,
229769
- droppedCount: droppedCount === 0 ? void 0 : droppedCount
229770
- });
229771
- this.agent.telemetry.track("compaction_finished", {
229772
- source: data.source,
229773
- tokens_before: result.tokensBefore,
229774
- tokens_after: result.tokensAfter,
229775
- duration_ms: Date.now() - startedAt,
229776
- compacted_count: result.compactedCount,
229777
- dropped_count: result.droppedCount,
229778
- retry_count: retryCount,
229779
- round: 1,
229780
- thinking_effort: this.agent.config.thinkingEffort,
229781
- ...usage === null ? {} : {
229782
- input_tokens: inputTotal(usage),
229783
- output_tokens: usage.output
229784
- }
229785
- });
229786
- return {
229787
- result,
229788
- stageCount: hierarchicalPassCount + 1
229789
- };
229790
- } catch (error) {
229791
- if (isAbortError(error) || signal.aborted) return void 0;
229792
- this.agent.telemetry.track("compaction_failed", {
229793
- source: data.source,
229794
- tokens_before: tokensBefore,
229795
- duration_ms: Date.now() - startedAt,
229796
- round: 1,
229797
- retry_count: retryCount,
229798
- thinking_effort: this.agent.config.thinkingEffort,
229799
- error_type: error instanceof Error ? error.name : "Unknown"
229800
- });
229801
- if (isBlunError(error) && (error.code === ErrorCodes.AUTH_LOGIN_REQUIRED || error.code === ErrorCodes.COMPACTION_FAILED)) throw error;
229802
- if (isBlunError(error) && error.code === ErrorCodes.CONTEXT_OVERFLOW && error.details?.["contextBrakeBlocked"] === true) throw error;
229803
- if (isModelFallbackStatus(error)) throw error;
229804
- if (error instanceof CompactionStallError) throw new BlunError(ErrorCodes.COMPACTION_STALLED, `Compaction was aborted because no provider data arrived for ${String(error.timeoutMs)}ms. The conversation history was not changed.`, {
229805
- cause: error,
229806
- details: {
229807
- reason: "no_provider_data",
229808
- timeoutSource: "measured_provider_behavior",
229809
- measuredIdleMs: error.measuredIdleMs,
229810
- timeoutMs: error.timeoutMs,
229811
- contextUnchanged: true
229812
- }
229813
- });
229814
- if (error instanceof APIStatusError && error.statusCode === 402) throw new BlunError(ErrorCodes.COMPACTION_FAILED, "The selected provider rejected compaction because its usage quota or payment is unavailable. Switch to an available model and run /compact again. The conversation history was not changed.", {
229815
- cause: error,
229816
- details: {
229817
- statusCode: error.statusCode,
229818
- requestId: error.requestId,
229819
- action: "switch_model_and_retry",
229820
- contextUnchanged: true
229821
- }
229822
- });
229823
- if (error instanceof APIStatusError && error.statusCode === 408) throw new BlunError(ErrorCodes.COMPACTION_FAILED, `Compaction stopped: the conversation (${String(tokensBefore)} tokens) could not be sent completely. The conversation history was not changed.`, {
229824
- cause: error,
229825
- details: {
229826
- statusCode: error.statusCode,
229827
- requestId: error.requestId,
229828
- estimatedHistoryTokens: tokensBefore,
229829
- contextUnchanged: true
229830
- }
229831
- });
229832
- throw new BlunError(ErrorCodes.COMPACTION_FAILED, String(error), { cause: error });
229833
- }
229834
- }
229588
+ const startedAt = Date.now();
229589
+ const originalHistory = [...this.agent.context.history];
229590
+ const tokensBefore = estimateTokensForMessages(originalHistory);
229591
+ let retryCount = 0;
229592
+ try {
229593
+ await this.triggerPreCompactHook(data, tokensBefore, signal);
229594
+ const model = resolveCompactionModelAlias(this.agent.blunConfig?.loopControl?.compactionModel, this.agent.activeResponderModel ?? this.agent.config.model);
229595
+ const runtimeModel = this.agent.config.runtimeModel(model);
229596
+ const capability = runtimeModel.modelCapabilities;
229597
+ const buildCompactionProvider = (usedContextTokens) => {
229598
+ const currentMaxContextTokens = this.getEffectiveMaxContextTokens(model);
229599
+ const defaultCompactionCap = currentMaxContextTokens > 0 ? Math.min(currentMaxContextTokens, DEFAULT_COMPACTION_MAX_COMPLETION_TOKENS) : void 0;
229600
+ const requestCapability = currentMaxContextTokens > 0 ? {
229601
+ ...capability,
229602
+ max_context_tokens: currentMaxContextTokens
229603
+ } : capability;
229604
+ return applyCompletionBudget({
229605
+ provider: runtimeModel.provider,
229606
+ budget: resolveCompletionBudget({
229607
+ maxOutputSize: runtimeModel.maxOutputSize ?? defaultCompactionCap,
229608
+ reservedContextSize: this.agent.blunConfig?.loopControl?.reservedContextSize
229609
+ }),
229610
+ capability: requestCapability,
229611
+ usedContextTokens
229612
+ });
229613
+ };
229614
+ let provider = buildCompactionProvider();
229615
+ const instruction = this.buildInstruction(data.instruction);
229616
+ const delays = retryBackoffDelays(5);
229617
+ let usage = null;
229618
+ let summary;
229619
+ let historyForModel = originalHistory.filter((message) => {
229620
+ const origin = message.origin;
229621
+ return !(origin?.kind === "injection" && REBUILT_AFTER_COMPACTION_INJECTION_VARIANTS.has(origin.variant));
229622
+ });
229623
+ let droppedCount = 0;
229624
+ let overflowShrinkCount = 0;
229625
+ let incompleteSummaryAttempts = 0;
229626
+ let hierarchicalPassCount = 0;
229627
+ const typicalDurationMs = readTypicalCompactionDuration(provider.name, provider.modelName, this.agent.blunHomeDir)?.typicalDurationMs;
229628
+ let attemptCount = 0;
229629
+ let charsReceived = 0;
229630
+ let lastProgressEmitAt = 0;
229631
+ let initialCompactionRequestTokens;
229632
+ const compactionTools = [];
229633
+ const buildRequestMessages = (history, requestInstruction) => {
229634
+ return stripMediaForCompaction(downgradeUnsupportedMedia([...this.agent.context.project(history, {
229635
+ synthesizeMissing: true,
229636
+ dropOrphanResults: true
229637
+ }), createUserMessage(requestInstruction)], capability));
229638
+ };
229639
+ while (true) {
229640
+ signal.throwIfAborted();
229641
+ if (hierarchicalPassCount >= MAX_HIERARCHICAL_COMPACTION_PASSES)
229642
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, `Compaction stopped after ${String(MAX_HIERARCHICAL_COMPACTION_PASSES)} staged requests without reaching a final summary. The conversation history was not changed.`, { details: {
229643
+ contextUnchanged: true,
229644
+ hierarchicalPassCount
229645
+ } });
229646
+ const compactionRequestLimit = this.getEffectiveMaxContextTokens(model);
229647
+ const safeCompactionRequestLimit = compactionRequestLimit > 0 ? Math.max(1, Math.floor(compactionRequestLimit * (1 - COMPACTION_SUMMARY_RESERVE_RATIO))) : compactionRequestLimit;
229648
+ let messages = buildRequestMessages(historyForModel, instruction);
229649
+ let estimatedCompactionRequestTokens = this.estimateRequestTokens(messages, this.agent.effectiveSystemPrompt, compactionTools);
229650
+ initialCompactionRequestTokens ??= estimatedCompactionRequestTokens;
229651
+ let hierarchicalChunkEnd;
229652
+ if (safeCompactionRequestLimit > 0 && estimatedCompactionRequestTokens >= safeCompactionRequestLimit) {
229653
+ if (historyForModel.length > 1) {
229654
+ const chunkInstruction = `${instruction}\n\nThis request contains only the oldest chronological segment. Summarize every fact needed by a later merge pass. Do not assume later messages are visible.`;
229655
+ const chunk = selectHierarchicalCompactionChunk(historyForModel, safeCompactionRequestLimit, safeCompactionRequestLimit, (candidate) => {
229656
+ const candidateMessages = buildRequestMessages(candidate, chunkInstruction);
229657
+ return {
229658
+ messages: candidateMessages,
229659
+ estimatedTokens: this.estimateRequestTokens(candidateMessages, this.agent.effectiveSystemPrompt, compactionTools)
229660
+ };
229661
+ });
229662
+ if (chunk !== void 0) {
229663
+ hierarchicalChunkEnd = chunk.end;
229664
+ messages = chunk.messages;
229665
+ estimatedCompactionRequestTokens = chunk.estimatedTokens;
229666
+ }
229667
+ }
229668
+ if (hierarchicalChunkEnd === void 0)
229669
+ throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, `Compaction stopped before upload: the conversation (${String(tokensBefore)} tokens) could not be divided into a request below the active model window (${String(compactionRequestLimit)} tokens).`, { details: {
229670
+ contextBrakeBlocked: true,
229671
+ estimatedRequestTokens: estimatedCompactionRequestTokens,
229672
+ maxContextTokens: compactionRequestLimit,
229673
+ requestBudgetTokens: safeCompactionRequestLimit,
229674
+ summaryReserveTokens: compactionRequestLimit - safeCompactionRequestLimit,
229675
+ contextUnchanged: true
229676
+ } });
229677
+ }
229678
+ provider = buildCompactionProvider(estimatedCompactionRequestTokens);
229679
+ attemptCount += 1;
229680
+ charsReceived = 0;
229681
+ const stage = hierarchicalPassCount + 1;
229682
+ const estimatedStageCount = estimateCompactionStageCount(initialCompactionRequestTokens, safeCompactionRequestLimit, stage);
229683
+ const estimatedProgressPercent = estimateCompactionProgressPercent(stage, estimatedStageCount);
229684
+ const windowUsagePercent = estimateCompactionWindowUsagePercent(estimatedCompactionRequestTokens, compactionRequestLimit);
229685
+ this.agent.log.info("compaction stage request", {
229686
+ source: data.source,
229687
+ stage,
229688
+ estimatedStageCount,
229689
+ activeContextTokens: this.estimateProjectedRequestTokens(),
229690
+ estimatedInputTokens: estimatedCompactionRequestTokens,
229691
+ windowUsagePercent,
229692
+ safeInputLimitTokens: safeCompactionRequestLimit,
229693
+ maxContextTokens: compactionRequestLimit
229694
+ });
229695
+ const emitCompactionProgress = (force = false) => {
229696
+ const now = Date.now();
229697
+ if (!force && now - lastProgressEmitAt < 150)
229698
+ return;
229699
+ lastProgressEmitAt = now;
229700
+ this.agent.emitEvent({
229701
+ type: "compaction.progress",
229702
+ stage,
229703
+ estimatedStageCount,
229704
+ estimatedInputTokens: estimatedCompactionRequestTokens,
229705
+ estimatedProgressPercent,
229706
+ ...windowUsagePercent === void 0 ? {} : { windowUsagePercent },
229707
+ charsReceived,
229708
+ attempt: attemptCount,
229709
+ ...typicalDurationMs !== void 0 ? { typicalDurationMs } : {}
229710
+ });
229711
+ };
229712
+ emitCompactionProgress(true);
229713
+ const timingKey = compactionTimingKey(provider);
229714
+ const stallPolicy = this.measuredStallPolicy(timingKey);
229715
+ let lastProgressAt = Date.now();
229716
+ let maxObservedIdleMs = 0;
229717
+ const attemptController = new AbortController();
229718
+ const onParentAbort = () => {
229719
+ attemptController.abort();
229720
+ };
229721
+ signal.addEventListener("abort", onParentAbort, { once: true });
229722
+ let stalled = false;
229723
+ let stallTimer;
229724
+ const armStallTimer = () => {
229725
+ if (stallPolicy === void 0)
229726
+ return;
229727
+ if (stallTimer !== void 0)
229728
+ clearTimeout(stallTimer);
229729
+ stallTimer = setTimeout(() => {
229730
+ stalled = true;
229731
+ attemptController.abort();
229732
+ }, stallPolicy.timeoutMs);
229733
+ stallTimer.unref?.();
229734
+ };
229735
+ const observeProgress = (part) => {
229736
+ const now = Date.now();
229737
+ maxObservedIdleMs = Math.max(maxObservedIdleMs, now - lastProgressAt);
229738
+ lastProgressAt = now;
229739
+ if (part.type === "text")
229740
+ charsReceived += part.text.length;
229741
+ emitCompactionProgress();
229742
+ armStallTimer();
229743
+ };
229744
+ armStallTimer();
229745
+ try {
229746
+ const response = await this.agent.generate(provider, this.agent.config.systemPrompt, compactionTools, messages, { onMessagePart: observeProgress }, {
229747
+ signal: attemptController.signal,
229748
+ isCompactionRequest: true,
229749
+ onExecution: (completion) => {
229750
+ this.executions.track({
229751
+ kind: "provider",
229752
+ id: executionId,
229753
+ source: data.source
229754
+ }, completion);
229755
+ }
229756
+ });
229757
+ signal.throwIfAborted();
229758
+ if (stalled && !signal.aborted && stallPolicy !== void 0)
229759
+ throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
229760
+ maxObservedIdleMs = Math.max(maxObservedIdleMs, Date.now() - lastProgressAt);
229761
+ if (response.finishReason === "truncated")
229762
+ throw new CompactionTruncatedError();
229763
+ if (response.usage !== null)
229764
+ usage = usage === null ? response.usage : addUsage(usage, response.usage);
229765
+ const extractedSummary = extractCompactionSummary(response);
229766
+ this.observeCompactionTiming(timingKey, maxObservedIdleMs);
229767
+ if (hierarchicalChunkEnd !== void 0) {
229768
+ hierarchicalPassCount += 1;
229769
+ const previousTokens = estimateTokensForMessages(historyForModel);
229770
+ const nextHistory = [createUserMessage(`${HIERARCHICAL_COMPACTION_PREFIX}\n${extractedSummary}`), ...historyForModel.slice(hierarchicalChunkEnd)];
229771
+ const nextTokens = estimateTokensForMessages(nextHistory);
229772
+ if (nextTokens >= previousTokens)
229773
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, "A staged compaction response did not reduce the pending history. The conversation history was not changed.", { details: {
229774
+ contextUnchanged: true,
229775
+ hierarchicalPassCount,
229776
+ previousTokens,
229777
+ nextTokens
229778
+ } });
229779
+ historyForModel = nextHistory;
229780
+ incompleteSummaryAttempts = 0;
229781
+ retryCount = 0;
229782
+ continue;
229783
+ }
229784
+ summary = extractedSummary;
229785
+ appendCompactionTiming({
229786
+ ts: Date.now(),
229787
+ provider: provider.name,
229788
+ model: provider.modelName,
229789
+ estimatedInputTokens: tokensBefore,
229790
+ durationMs: Date.now() - startedAt
229791
+ }, this.agent.blunHomeDir);
229792
+ break;
229793
+ }
229794
+ catch (error) {
229795
+ if (stalled && !signal.aborted && stallPolicy !== void 0)
229796
+ throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
229797
+ if (isModelFallbackStatus(error))
229798
+ throw error;
229799
+ const isContextOverflow = this.shouldRecoverFromContextOverflow(error);
229800
+ if (isContextOverflow)
229801
+ this.observeContextOverflow(error, model);
229802
+ if (isContextOverflow && historyForModel.length > 1) {
229803
+ if (data.source === "auto") {
229804
+ const learnedMaxContextTokens = this.getEffectiveMaxContextTokens(model);
229805
+ if (learnedMaxContextTokens > 0 && learnedMaxContextTokens < compactionRequestLimit) {
229806
+ retryCount = 0;
229807
+ continue;
229808
+ }
229809
+ throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, "The active model rejected the full compaction request; refusing to drop unsummarized history.", {
229810
+ cause: error,
229811
+ details: {
229812
+ contextBrakeBlocked: true,
229813
+ estimatedRequestTokens: estimatedCompactionRequestTokens,
229814
+ maxContextTokens: learnedMaxContextTokens,
229815
+ requestBudgetTokens: learnedMaxContextTokens,
229816
+ contextUnchanged: true
229817
+ }
229818
+ });
229819
+ }
229820
+ overflowShrinkCount += 1;
229821
+ if (overflowShrinkCount > MAX_COMPACTION_OVERFLOW_SHRINK_ATTEMPTS)
229822
+ throw error;
229823
+ const before = historyForModel.length;
229824
+ historyForModel = shrinkCompactionHistoryAfterOverflow(historyForModel, overflowShrinkCount);
229825
+ droppedCount += before - historyForModel.length;
229826
+ retryCount = 0;
229827
+ continue;
229828
+ }
229829
+ if (error instanceof CompactionTruncatedError || error instanceof APIEmptyResponseError) {
229830
+ incompleteSummaryAttempts += 1;
229831
+ if (incompleteSummaryAttempts === 1)
229832
+ continue;
229833
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, "The provider returned an empty or truncated compaction response twice. The conversation history was not changed.", {
229834
+ cause: error,
229835
+ details: {
229836
+ contextUnchanged: true,
229837
+ lossyRetryPrevented: true,
229838
+ compactionFailure: error.name,
229839
+ incompleteSummaryAttempts
229840
+ }
229841
+ });
229842
+ }
229843
+ if (!isRetryableGenerateError(error))
229844
+ throw error;
229845
+ if (retryCount + 1 >= 5)
229846
+ throw error;
229847
+ await sleepForRetry(delays[retryCount], signal);
229848
+ retryCount += 1;
229849
+ }
229850
+ finally {
229851
+ if (stallTimer !== void 0)
229852
+ clearTimeout(stallTimer);
229853
+ signal.removeEventListener("abort", onParentAbort);
229854
+ }
229855
+ }
229856
+ if (usage !== null)
229857
+ this.agent.usage.record(model, usage);
229858
+ const newHistory = this.agent.context.history;
229859
+ for (let i = 0; i < originalHistory.length; i++)
229860
+ if (newHistory[i] !== originalHistory[i]) {
229861
+ this.cancel();
229862
+ return;
229863
+ }
229864
+ if (newHistory.slice(originalHistory.length).some((message) => !isRealUserInput(message))) {
229865
+ this.cancel();
229866
+ return;
229867
+ }
229868
+ const rawSummary = this.postProcessSummary(summary ?? "");
229869
+ const contextSummary = buildCompactionSummaryText(rawSummary);
229870
+ const result = this.agent.context.applyCompaction({
229871
+ summary: rawSummary,
229872
+ contextSummary,
229873
+ compactedCount: originalHistory.length,
229874
+ tokensBefore,
229875
+ droppedCount: droppedCount === 0 ? void 0 : droppedCount
229876
+ });
229877
+ this.agent.telemetry.track("compaction_finished", {
229878
+ source: data.source,
229879
+ tokens_before: result.tokensBefore,
229880
+ tokens_after: result.tokensAfter,
229881
+ duration_ms: Date.now() - startedAt,
229882
+ compacted_count: result.compactedCount,
229883
+ dropped_count: result.droppedCount,
229884
+ retry_count: retryCount,
229885
+ round: 1,
229886
+ thinking_effort: this.agent.config.thinkingEffort,
229887
+ ...usage === null ? {} : {
229888
+ input_tokens: inputTotal(usage),
229889
+ output_tokens: usage.output
229890
+ }
229891
+ });
229892
+ return {
229893
+ result,
229894
+ stageCount: hierarchicalPassCount + 1
229895
+ };
229896
+ }
229897
+ catch (error) {
229898
+ if (isAbortError(error) || signal.aborted)
229899
+ return void 0;
229900
+ this.agent.telemetry.track("compaction_failed", {
229901
+ source: data.source,
229902
+ tokens_before: tokensBefore,
229903
+ duration_ms: Date.now() - startedAt,
229904
+ round: 1,
229905
+ retry_count: retryCount,
229906
+ thinking_effort: this.agent.config.thinkingEffort,
229907
+ error_type: error instanceof Error ? error.name : "Unknown"
229908
+ });
229909
+ if (isBlunError(error) && (error.code === ErrorCodes.AUTH_LOGIN_REQUIRED || error.code === ErrorCodes.COMPACTION_FAILED))
229910
+ throw error;
229911
+ if (isBlunError(error) && error.code === ErrorCodes.CONTEXT_OVERFLOW && error.details?.["contextBrakeBlocked"] === true)
229912
+ throw error;
229913
+ if (isModelFallbackStatus(error))
229914
+ throw error;
229915
+ if (error instanceof CompactionStallError)
229916
+ throw new BlunError(ErrorCodes.COMPACTION_STALLED, `Compaction was aborted because no provider data arrived for ${String(error.timeoutMs)}ms. The conversation history was not changed.`, {
229917
+ cause: error,
229918
+ details: {
229919
+ reason: "no_provider_data",
229920
+ timeoutSource: "measured_provider_behavior",
229921
+ measuredIdleMs: error.measuredIdleMs,
229922
+ timeoutMs: error.timeoutMs,
229923
+ contextUnchanged: true
229924
+ }
229925
+ });
229926
+ if (error instanceof APIStatusError && error.statusCode === 402)
229927
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, "The selected provider rejected compaction because its usage quota or payment is unavailable. Switch to an available model and run /compact again. The conversation history was not changed.", {
229928
+ cause: error,
229929
+ details: {
229930
+ statusCode: error.statusCode,
229931
+ requestId: error.requestId,
229932
+ action: "switch_model_and_retry",
229933
+ contextUnchanged: true
229934
+ }
229935
+ });
229936
+ if (error instanceof APIStatusError && error.statusCode === 408)
229937
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, `Compaction stopped: the conversation (${String(tokensBefore)} tokens) could not be sent completely. The conversation history was not changed.`, {
229938
+ cause: error,
229939
+ details: {
229940
+ statusCode: error.statusCode,
229941
+ requestId: error.requestId,
229942
+ estimatedHistoryTokens: tokensBefore,
229943
+ contextUnchanged: true
229944
+ }
229945
+ });
229946
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, String(error), { cause: error });
229947
+ }
229948
+ }
229835
229949
  measuredStallPolicy(_timingKey) {}
229836
229950
  observeCompactionTiming(timingKey, maxIdleMs) {
229837
229951
  if (!Number.isFinite(maxIdleMs) || maxIdleMs <= 0) return;
@@ -256287,6 +256401,43 @@ function convertMCPContentBlock(block) {
256287
256401
  * `mcp__github__create_pr`) — embedded into the `<mcp_tool_result name="…">`
256288
256402
  * wrap when the result is media-only, so the model can attribute binary parts.
256289
256403
  */
256404
+ async function retainOriginalText(parts, toolName, homedir) {
256405
+ const textParts = parts.flatMap((part) => {
256406
+ if (part.type !== "text") return [];
256407
+ const chunks = [];
256408
+ for (let offset = 0; offset < part.text.length; offset += 256) chunks.push(part.text.slice(offset, offset + 256));
256409
+ return [chunks];
256410
+ });
256411
+ const archive = JSON.stringify({
256412
+ version: 1,
256413
+ textParts
256414
+ }, null, 2);
256415
+ const outputPath = homedir === void 0 ? void 0 : await saveToolResult({
256416
+ homedir,
256417
+ toolName,
256418
+ toolCallId: "mcp-original",
256419
+ directoryPolicy: "private-retention"
256420
+ }, archive);
256421
+ if (outputPath === void 0) return "\n\n[Originaltext nicht gespeichert: Sitzungsverzeichnis fehlt oder Schreiben fehlgeschlagen. Die Ausgabe bleibt begrenzt.]";
256422
+ return [
256423
+ "",
256424
+ "",
256425
+ "MCP-Originaltext vor Kürzung gespeichert (JSON, version: 1, textParts: Zeichenabschnitte je Textblock; ohne Binärdaten).",
256426
+ `output_path: ${outputPath}`,
256427
+ "Mit Read seitenweise nachlesen. Zeichenabschnitte je Block ohne Trennzeichen verbinden; Blockreihenfolge erhalten."
256428
+ ].join("\n");
256429
+ }
256430
+ function appendTextNotice(parts, text) {
256431
+ const last = parts.at(-1);
256432
+ if (last?.type === "text") parts[parts.length - 1] = {
256433
+ ...last,
256434
+ text: last.text + text
256435
+ };
256436
+ else parts.push({
256437
+ type: "text",
256438
+ text
256439
+ });
256440
+ }
256290
256441
  async function mcpResultToExecutableOutput(result, qualifiedToolName, options = {}) {
256291
256442
  const converted = [];
256292
256443
  for (const block of result.content) {
@@ -256294,8 +256445,10 @@ async function mcpResultToExecutableOutput(result, qualifiedToolName, options =
256294
256445
  if (part !== null) converted.push(part);
256295
256446
  }
256296
256447
  const budgeted = applyTextBudget(wrapMediaOnly(converted, qualifiedToolName));
256448
+ const retentionNotice = budgeted.truncated ? await retainOriginalText(converted, qualifiedToolName, options.homedir) : void 0;
256297
256449
  const capped = applyBinaryPartCap(await compressImageContentParts(budgeted.parts, { annotate: { persistOriginal: (bytes, mimeType) => persistOriginalImage(bytes, mimeType, options.originalsDir === void 0 ? {} : { dir: options.originalsDir }) } }));
256298
256450
  const truncated = budgeted.truncated || capped.truncated;
256451
+ if (retentionNotice !== void 0) appendTextNotice(capped.parts, retentionNotice);
256299
256452
  const output = collapseSingleText(capped.parts);
256300
256453
  return truncated ? {
256301
256454
  output,
@@ -264482,7 +264635,8 @@ async function budgetToolResultForModel(options) {
264482
264635
  const outputPath = await saveToolResult({
264483
264636
  homedir: options.homedir,
264484
264637
  toolName: options.toolName,
264485
- toolCallId: options.toolCallId
264638
+ toolCallId: options.toolCallId,
264639
+ directoryPolicy: "compatible-spill"
264486
264640
  }, text);
264487
264641
  if (outputPath === void 0) return options.result;
264488
264642
  const output = renderPersistedToolResult(options.toolName, options.toolCallId, text, outputPath);
@@ -264501,19 +264655,33 @@ function persistableToolResultText(output) {
264501
264655
  return output.map((part) => part.text).join("");
264502
264656
  }
264503
264657
  async function saveToolResult(options, text) {
264658
+ if (!isAbsolute$1(options.homedir)) return void 0;
264659
+ let createdPath;
264504
264660
  try {
264505
264661
  const dir = join$1(options.homedir, "tool-results");
264506
264662
  await mkdir(dir, {
264507
264663
  recursive: true,
264508
264664
  mode: 448
264509
264665
  });
264666
+ const info = await lstat(dir);
264667
+ if (info.isSymbolicLink()) return void 0;
264668
+ if (process.platform !== "win32") {
264669
+ if (info.uid !== process.getuid?.()) return void 0;
264670
+ if (options.directoryPolicy === "private-retention" && (info.mode & 63) !== 0) return;
264671
+ }
264672
+ const home = await realpath(options.homedir);
264673
+ if (normalize$3(await realpath(dir)) !== join$1(home, "tool-results")) return void 0;
264510
264674
  const outputPath = join$1(dir, `${safeToolResultFileStem(options.toolName, options.toolCallId)}-${randomUUID()}.txt`);
264511
- await writeFile(outputPath, text, {
264512
- encoding: "utf8",
264513
- flag: "wx"
264514
- });
264675
+ const file = await open(outputPath, "wx", 384);
264676
+ createdPath = outputPath;
264677
+ try {
264678
+ await file.writeFile(text, { encoding: "utf8" });
264679
+ } finally {
264680
+ await file.close();
264681
+ }
264515
264682
  return outputPath;
264516
264683
  } catch {
264684
+ if (createdPath !== void 0) await unlink(createdPath).catch(() => void 0);
264517
264685
  return;
264518
264686
  }
264519
264687
  }
@@ -268616,7 +268784,10 @@ var ToolManager = class {
268616
268784
  return {
268617
268785
  approvalRule: qualified,
268618
268786
  execute: async (context) => {
268619
- return mcpResultToExecutableOutput(await client.callTool(tool.name, args ?? {}, context.signal), qualified, { originalsDir: this.agent.mediaOriginalsDir });
268787
+ return mcpResultToExecutableOutput(await client.callTool(tool.name, args ?? {}, context.signal), qualified, {
268788
+ originalsDir: this.agent.mediaOriginalsDir,
268789
+ homedir: this.agent.homedir
268790
+ });
268620
268791
  }
268621
268792
  };
268622
268793
  }
@@ -269642,54 +269813,67 @@ var Agent$1 = class {
269642
269813
  return this.generateForModel(this.activeResponderModel ?? this.config.modelAlias);
269643
269814
  }
269644
269815
  generateForModel(modelAlias) {
269645
- return async (provider, systemPrompt, tools, history, callbacks, options) => {
269646
- const { requestLogFields, generateOptions } = splitGenerateOptions(options);
269647
- const retryScope = providerRetryScope(modelAlias, provider);
269648
- await this.providerRetryWait.wait(retryScope, generateOptions?.signal);
269649
- const callerTransportAttempt = generateOptions?.onTransportAttempt;
269650
- const ownedGenerateOptions = {
269651
- ...generateOptions,
269652
- transportMaxRetries: 0,
269653
- onTransportAttempt: (stats) => {
269654
- callerTransportAttempt?.(stats);
269655
- this.llmRequestLogger.logTransportAttempt(stats, requestLogFields);
269656
- }
269657
- };
269658
- const run = async (requestOptions) => {
269659
- let requestStarted = false;
269660
- this.llmRequestLogger.logRequest({
269661
- provider,
269662
- modelAlias,
269663
- systemPrompt,
269664
- tools,
269665
- messages: history,
269666
- fields: requestLogFields
269667
- });
269668
- try {
269669
- return await this.rawGenerate(provider, systemPrompt, tools, history, callbacks, {
269670
- ...requestOptions,
269671
- onRequestStart: () => {
269672
- requestOptions?.onRequestStart?.();
269673
- requestStarted = true;
269674
- }
269675
- });
269676
- } catch (error) {
269677
- if (error instanceof APIProviderRateLimitError && error.retryAfterMs !== null) await this.providerRetryWait.defer(retryScope, error.retryAfterMs);
269678
- if (requestStarted && requestOptions?.signal?.aborted !== true) this.turn.noteProviderRequestFailure(requestLogFields?.turnStep, error);
269679
- throw error;
269680
- }
269681
- };
269682
- if (ownedGenerateOptions.auth !== void 0) return run(ownedGenerateOptions);
269683
- const withAuth = modelAlias === void 0 ? void 0 : this.modelProvider?.resolveAuth?.(modelAlias, { log: this.log });
269684
- if (withAuth === void 0) return run(ownedGenerateOptions);
269685
- return withAuth((auth) => {
269686
- return run({
269687
- ...ownedGenerateOptions,
269688
- auth
269689
- });
269690
- });
269691
- };
269692
- }
269816
+ return async (provider, systemPrompt, tools, history, callbacks, options) => {
269817
+ const { requestLogFields, generateOptions } = splitGenerateOptions(options);
269818
+ const retryScope = providerRetryScope(modelAlias, provider);
269819
+ await this.providerRetryWait.wait(retryScope, generateOptions?.signal);
269820
+ const callerTransportAttempt = generateOptions?.onTransportAttempt;
269821
+ const ownedGenerateOptions = {
269822
+ ...generateOptions,
269823
+ transportMaxRetries: 0,
269824
+ requestBodyBudget: {
269825
+ ...generateOptions?.requestBodyBudget,
269826
+ onMeasured: (bytes, limit, endpoint) => {
269827
+ generateOptions?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
269828
+ if (!generateOptions?.isCompactionRequest && limit !== undefined && this.fullCompaction.shouldCompactRequestBody(endpoint, bytes, limit))
269829
+ throw t3458Pressure(bytes, limit, endpoint);
269830
+ }
269831
+ },
269832
+ onTransportAttempt: (stats) => {
269833
+ callerTransportAttempt?.(stats);
269834
+ this.llmRequestLogger.logTransportAttempt(stats, requestLogFields);
269835
+ }
269836
+ };
269837
+ const run = async (requestOptions) => {
269838
+ let requestStarted = false;
269839
+ this.llmRequestLogger.logRequest({
269840
+ provider,
269841
+ modelAlias,
269842
+ systemPrompt,
269843
+ tools,
269844
+ messages: history,
269845
+ fields: requestLogFields
269846
+ });
269847
+ try {
269848
+ return await this.rawGenerate(provider, systemPrompt, tools, history, callbacks, {
269849
+ ...requestOptions,
269850
+ onRequestStart: () => {
269851
+ requestOptions?.onRequestStart?.();
269852
+ requestStarted = true;
269853
+ }
269854
+ });
269855
+ }
269856
+ catch (error) {
269857
+ if (error instanceof APIProviderRateLimitError && error.retryAfterMs !== null)
269858
+ await this.providerRetryWait.defer(retryScope, error.retryAfterMs);
269859
+ if (requestStarted && requestOptions?.signal?.aborted !== true)
269860
+ this.turn.noteProviderRequestFailure(requestLogFields?.turnStep, error);
269861
+ throw error;
269862
+ }
269863
+ };
269864
+ if (ownedGenerateOptions.auth !== void 0)
269865
+ return run(ownedGenerateOptions);
269866
+ const withAuth = modelAlias === void 0 ? void 0 : this.modelProvider?.resolveAuth?.(modelAlias, { log: this.log });
269867
+ if (withAuth === void 0)
269868
+ return run(ownedGenerateOptions);
269869
+ return withAuth((auth) => {
269870
+ return run({
269871
+ ...ownedGenerateOptions,
269872
+ auth
269873
+ });
269874
+ });
269875
+ };
269876
+ }
269693
269877
  get llm() {
269694
269878
  return this.llmForTurn();
269695
269879
  }
@@ -300629,30 +300813,36 @@ var ProviderManager = class {
300629
300813
  return typeof config === "function" ? config() : config;
300630
300814
  }
300631
300815
  resolveProviderConfig(model) {
300632
- const alias = this.config.models?.[model];
300633
- if (alias === void 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" is not configured in config.toml. Add a [models."${model}"] entry with max_context_size.`);
300634
- const effectiveAlias = effectiveModelAlias(alias);
300635
- const providerName = alias.provider ?? this.config.defaultProvider;
300636
- if (providerName === void 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a provider in config.toml.`);
300637
- const providerConfig = this.config.providers[providerName];
300638
- if (providerConfig === void 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, `Provider "${providerName}" for model "${model}" is not configured.`);
300639
- assertBlunProviderType(providerConfig);
300640
- const runtimeContext = this.runtimeModelContexts.get(model);
300641
- const maxContextSize = runtimeContext?.providerName === providerName && runtimeContext.model === alias.model ? runtimeContext.maxContextTokens : effectiveAlias.maxContextSize;
300642
- if (!Number.isInteger(maxContextSize) || maxContextSize <= 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a positive max_context_size in config.toml.`);
300643
- const provider = toKosongProviderConfig(providerConfig, alias.model, this.options.blunRequestHeaders, this.options.promptCacheKey, effectiveAlias.supportEfforts);
300644
- return {
300645
- providerName,
300646
- provider,
300647
- modelCapabilities: resolveModelCapabilities({
300648
- ...effectiveAlias,
300649
- maxContextSize
300650
- }, provider),
300651
- alwaysThinking: (effectiveAlias.capabilities ?? []).some((c) => c.trim().toLowerCase() === "always_thinking"),
300652
- maxOutputSize: effectiveAlias.maxOutputSize,
300653
- type: "blun"
300654
- };
300655
- }
300816
+ const alias = this.config.models?.[model];
300817
+ if (alias === void 0)
300818
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" is not configured in config.toml. Add a [models."${model}"] entry with max_context_size.`);
300819
+ const effectiveAlias = effectiveModelAlias(alias);
300820
+ const providerName = alias.provider ?? this.config.defaultProvider;
300821
+ if (providerName === void 0)
300822
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a provider in config.toml.`);
300823
+ const providerConfig = this.config.providers[providerName];
300824
+ if (providerConfig === void 0)
300825
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Provider "${providerName}" for model "${model}" is not configured.`);
300826
+ assertBlunProviderType(providerConfig);
300827
+ const runtimeContext = this.runtimeModelContexts.get(model);
300828
+ const maxContextSize = runtimeContext?.providerName === providerName && runtimeContext.model === alias.model ? runtimeContext.maxContextTokens : effectiveAlias.maxContextSize;
300829
+ if (!Number.isInteger(maxContextSize) || maxContextSize <= 0)
300830
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a positive max_context_size in config.toml.`);
300831
+ const provider = toKosongProviderConfig(providerConfig, alias.model, this.options.blunRequestHeaders, this.options.promptCacheKey, effectiveAlias.supportEfforts);
300832
+ if (this.options.configPath !== undefined)
300833
+ provider.requestBodyByteLimitStore = t3458Store(this.options.configPath, parse$6, stringify$1, atomicWritePrivate);
300834
+ return {
300835
+ providerName,
300836
+ provider,
300837
+ modelCapabilities: resolveModelCapabilities({
300838
+ ...effectiveAlias,
300839
+ maxContextSize
300840
+ }, provider),
300841
+ alwaysThinking: (effectiveAlias.capabilities ?? []).some((c) => c.trim().toLowerCase() === "always_thinking"),
300842
+ maxOutputSize: effectiveAlias.maxOutputSize,
300843
+ type: "blun"
300844
+ };
300845
+ }
300656
300846
  setRuntimeModelContext(input) {
300657
300847
  if (!MANAGED_BLUN_MODEL_ALIASES.has(input.modelAlias)) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime context metadata is supported only for internal BLUN model aliases.");
300658
300848
  if (!Number.isSafeInteger(input.maxContextTokens) || input.maxContextTokens <= 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime model context must be a positive integer.");
@@ -336332,14 +336522,15 @@ var BlunCore = class {
336332
336522
  };
336333
336523
  }
336334
336524
  resolveProviderManager(sessionId) {
336335
- return new ProviderManager({
336336
- config: () => this.config,
336337
- blunRequestHeaders: this.blunRequestHeaders,
336338
- resolveOAuthTokenProvider: this.resolveOAuthTokenProvider,
336339
- managedApiKeyRevocationStore: this.managedApiKeyRevocationStore,
336340
- promptCacheKey: sessionId
336341
- });
336342
- }
336525
+ return new ProviderManager({
336526
+ config: () => this.config,
336527
+ configPath: this.configPath,
336528
+ blunRequestHeaders: this.blunRequestHeaders,
336529
+ resolveOAuthTokenProvider: this.resolveOAuthTokenProvider,
336530
+ managedApiKeyRevocationStore: this.managedApiKeyRevocationStore,
336531
+ promptCacheKey: sessionId
336532
+ });
336533
+ }
336343
336534
  mergePluginMcpConfig(base) {
336344
336535
  const pluginEntries = this.plugins.mcpServerConfigs();
336345
336536
  if (pluginEntries.length === 0) return base;
@@ -350032,7 +350223,7 @@ function optionalBuildString(value) {
350032
350223
  return typeof value === "string" && value.length > 0 ? value : void 0;
350033
350224
  }
350034
350225
  const BLUN_BUILD_INFO = {
350035
- version: optionalBuildString("9.1.628-pruefpaket.modules.r8"),
350226
+ version: optionalBuildString("9.1.640"),
350036
350227
  channel: typeof __BLUN_CHANNEL__ === "string" ? optionalBuildString(__BLUN_CHANNEL__) : void 0,
350037
350228
  commit: typeof __BLUN_COMMIT__ === "string" ? optionalBuildString(__BLUN_COMMIT__) : void 0,
350038
350229
  buildTarget: typeof __BLUN_BUILD_TARGET__ === "string" ? optionalBuildString(__BLUN_BUILD_TARGET__) : void 0
@@ -350084,3 +350275,169 @@ function createKingWorkerHarness(options) {
350084
350275
  });
350085
350276
  }
350086
350277
  export { LocalKaos, createKingWorkerHarness };
350278
+
350279
+ import * as t3458Fs from "node:fs/promises";
350280
+ import * as t3458Path from "node:path";
350281
+ import * as t3458Crypto from "node:crypto";
350282
+ // Neue, selektive Verdichtungshelfer. Keine Quellübernahme aus Arbeitsbaum oder v2/v3.
350283
+ function t3458ValidLimit(value) {
350284
+ return Number.isSafeInteger(value) && value > 0 ? value : undefined;
350285
+ }
350286
+ function t3458Learned(value) {
350287
+ return t3458ValidLimit(value) !== undefined && value >= 65536 ? value : undefined;
350288
+ }
350289
+ function t3458Endpoint(baseUrl) {
350290
+ try {
350291
+ const url = new URL(baseUrl);
350292
+ if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password || url.search || url.hash)
350293
+ return undefined;
350294
+ return `${url.origin}${url.pathname.replace(/\/+$/, '')}`;
350295
+ }
350296
+ catch {
350297
+ return undefined;
350298
+ }
350299
+ }
350300
+ function t3458Key(baseUrl) {
350301
+ const endpoint = t3458Endpoint(baseUrl);
350302
+ return endpoint === undefined ? undefined : t3458Crypto.createHash('sha256').update(endpoint).digest('hex');
350303
+ }
350304
+ async function t3458WithConfigLock(filePath, action, learning = false) {
350305
+ await t3458Fs.mkdir(t3458Path.dirname(filePath), { recursive: true, mode: 0o700 });
350306
+ const lock = `${filePath}.request-body.lock`;
350307
+ const token = t3458Crypto.randomUUID();
350308
+ // Small scheduling reserve: bounded contention attempts stay below 2s / 12s.
350309
+ const deadline = performance.now() + (learning ? 1950 : 11950);
350310
+ let handle;
350311
+ while (handle === undefined) {
350312
+ if (performance.now() >= deadline)
350313
+ return learning ? undefined : await action();
350314
+ try {
350315
+ handle = await t3458Fs.open(lock, 'wx', 0o600);
350316
+ }
350317
+ catch (error) {
350318
+ if (error.code !== 'EEXIST' && !(process.platform === 'win32' && ['EPERM', 'EACCES', 'EBUSY'].includes(error.code)))
350319
+ throw error;
350320
+ try {
350321
+ const observed = await t3458Fs.stat(lock);
350322
+ if (Date.now() - observed.mtimeMs > 10000) {
350323
+ const owner = await t3458Fs.readFile(lock, 'utf8');
350324
+ const current = await t3458Fs.stat(lock);
350325
+ // Best effort only: the accepted check/unlink race is not fencing.
350326
+ if (current.mtimeMs === observed.mtimeMs && await t3458Fs.readFile(lock, 'utf8') === owner) {
350327
+ await t3458Fs.unlink(lock);
350328
+ continue;
350329
+ }
350330
+ }
350331
+ }
350332
+ catch (failure) {
350333
+ if (failure.code !== 'ENOENT')
350334
+ throw failure;
350335
+ }
350336
+ const remaining = deadline - performance.now();
350337
+ if (remaining > 0)
350338
+ await new Promise(resolve => setTimeout(resolve, Math.min(25, remaining)));
350339
+ }
350340
+ }
350341
+ try {
350342
+ await handle.writeFile(token, 'utf8');
350343
+ return await action();
350344
+ }
350345
+ finally {
350346
+ await handle.close();
350347
+ try {
350348
+ if (await t3458Fs.readFile(lock, 'utf8') === token)
350349
+ await t3458Fs.unlink(lock);
350350
+ }
350351
+ catch (error) {
350352
+ if (error.code !== 'ENOENT')
350353
+ throw error;
350354
+ }
350355
+ }
350356
+ }
350357
+ function t3458Store(filePath, parseToml, stringifyToml, atomicWrite) {
350358
+ return {
350359
+ async load(key) {
350360
+ try {
350361
+ return t3458Learned(parseToml(await t3458Fs.readFile(filePath, 'utf8')).request_body_byte_limits?.[key]);
350362
+ }
350363
+ catch (error) {
350364
+ if (error.code === 'ENOENT')
350365
+ return undefined;
350366
+ throw error;
350367
+ }
350368
+ },
350369
+ async save(key, value) {
350370
+ if (!/^[a-f0-9]{64}$/.test(key) || t3458Learned(value) === undefined)
350371
+ return;
350372
+ await t3458WithConfigLock(filePath, async () => {
350373
+ let data;
350374
+ try {
350375
+ data = parseToml(await t3458Fs.readFile(filePath, 'utf8'));
350376
+ }
350377
+ catch (error) {
350378
+ if (error.code !== 'ENOENT')
350379
+ throw error;
350380
+ data = {};
350381
+ }
350382
+ const table = data.request_body_byte_limits ?? {};
350383
+ const previous = t3458Learned(table[key]);
350384
+ data.request_body_byte_limits = { ...table, [key]: previous === undefined ? value : Math.min(previous, value) };
350385
+ await atomicWrite(filePath, `${stringifyToml(data)}\n`);
350386
+ }, true);
350387
+ }
350388
+ };
350389
+ }
350390
+ async function t3458Measure(provider, params, options) {
350391
+ const endpoint = t3458Endpoint(provider._baseUrl);
350392
+ const key = t3458Key(provider._baseUrl);
350393
+ const option = typeof provider._requestBodyByteLimit === 'function' ? provider._requestBodyByteLimit() : provider._requestBodyByteLimit;
350394
+ const configured = t3458ValidLimit(options?.requestBodyBudget?.limitBytes) ?? t3458ValidLimit(option) ?? t3458ValidLimit(Number(process.env.BLUN_REQUEST_BODY_BYTE_LIMIT));
350395
+ let persisted;
350396
+ if (key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
350397
+ try {
350398
+ persisted = t3458Learned(await provider._requestBodyByteLimitStore.load(key));
350399
+ }
350400
+ catch { }
350401
+ }
350402
+ const learned = t3458Learned(provider._t3458ObservedLimit);
350403
+ const observed = learned === undefined ? persisted : persisted === undefined ? learned : Math.min(learned, persisted);
350404
+ const fallback = endpoint === 'https://api.blun.ai/coding/v1' ? 500000 : undefined;
350405
+ const limit = configured === undefined ? observed ?? fallback : observed === undefined ? configured : Math.min(configured, observed);
350406
+ const bytes = Buffer.byteLength(JSON.stringify(params), 'utf8');
350407
+ options?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
350408
+ if (!options?.isCompactionRequest && limit !== undefined && bytes >= limit)
350409
+ throw t3458Pressure(bytes, limit, endpoint);
350410
+ return { key, limit, bytes };
350411
+ }
350412
+ function t3458Pressure(byteLength, limitBytes, endpoint) {
350413
+ const error = new ChatProviderError(`Request body ${byteLength} bytes reaches limit ${limitBytes} bytes.`);
350414
+ error.name = 'APIRequestBodyPressureError';
350415
+ error.requestBodyPressure = true;
350416
+ error.byteLength = byteLength;
350417
+ error.limitBytes = limitBytes;
350418
+ error.endpoint = endpoint;
350419
+ return error;
350420
+ }
350421
+ function t3458Observed(error) {
350422
+ const message = error instanceof Error ? error.message : '';
350423
+ if (!/(?:request\s*body|payload|body\s*size)/i.test(message))
350424
+ return undefined;
350425
+ const comparison = /\(\s*\d+\s*>\s*(\d+)\s*bytes?\s*\)/i.exec(message);
350426
+ if (comparison !== null)
350427
+ return t3458Learned(Number(comparison[1]));
350428
+ const match = /(?:limit|maximum|max|up to)[^\d]{0,30}(\d{2,12})\s*(?:bytes?|b)\b/i.exec(message);
350429
+ return match === null ? undefined : t3458Learned(Number(match[1]));
350430
+ }
350431
+ async function t3458Learn(provider, error, measured, options) {
350432
+ const limit = t3458Observed(error);
350433
+ if (limit === undefined)
350434
+ return;
350435
+ provider._t3458ObservedLimit = Math.min(t3458Learned(provider._t3458ObservedLimit) ?? Infinity, limit);
350436
+ options?.requestBodyBudget?.onLimitObserved?.(limit);
350437
+ if (measured.key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
350438
+ try {
350439
+ await provider._requestBodyByteLimitStore.save(measured.key, limit);
350440
+ }
350441
+ catch { }
350442
+ }
350443
+ }