blun-king-cli 9.1.639 → 9.1.640

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:0b1614e40c5a323438b4f9cd742ea9a101c55f926d094603efc957fad265637a
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;
@@ -269642,54 +269756,67 @@ var Agent$1 = class {
269642
269756
  return this.generateForModel(this.activeResponderModel ?? this.config.modelAlias);
269643
269757
  }
269644
269758
  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
- }
269759
+ return async (provider, systemPrompt, tools, history, callbacks, options) => {
269760
+ const { requestLogFields, generateOptions } = splitGenerateOptions(options);
269761
+ const retryScope = providerRetryScope(modelAlias, provider);
269762
+ await this.providerRetryWait.wait(retryScope, generateOptions?.signal);
269763
+ const callerTransportAttempt = generateOptions?.onTransportAttempt;
269764
+ const ownedGenerateOptions = {
269765
+ ...generateOptions,
269766
+ transportMaxRetries: 0,
269767
+ requestBodyBudget: {
269768
+ ...generateOptions?.requestBodyBudget,
269769
+ onMeasured: (bytes, limit, endpoint) => {
269770
+ generateOptions?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
269771
+ if (!generateOptions?.isCompactionRequest && limit !== undefined && this.fullCompaction.shouldCompactRequestBody(endpoint, bytes, limit))
269772
+ throw t3458Pressure(bytes, limit, endpoint);
269773
+ }
269774
+ },
269775
+ onTransportAttempt: (stats) => {
269776
+ callerTransportAttempt?.(stats);
269777
+ this.llmRequestLogger.logTransportAttempt(stats, requestLogFields);
269778
+ }
269779
+ };
269780
+ const run = async (requestOptions) => {
269781
+ let requestStarted = false;
269782
+ this.llmRequestLogger.logRequest({
269783
+ provider,
269784
+ modelAlias,
269785
+ systemPrompt,
269786
+ tools,
269787
+ messages: history,
269788
+ fields: requestLogFields
269789
+ });
269790
+ try {
269791
+ return await this.rawGenerate(provider, systemPrompt, tools, history, callbacks, {
269792
+ ...requestOptions,
269793
+ onRequestStart: () => {
269794
+ requestOptions?.onRequestStart?.();
269795
+ requestStarted = true;
269796
+ }
269797
+ });
269798
+ }
269799
+ catch (error) {
269800
+ if (error instanceof APIProviderRateLimitError && error.retryAfterMs !== null)
269801
+ await this.providerRetryWait.defer(retryScope, error.retryAfterMs);
269802
+ if (requestStarted && requestOptions?.signal?.aborted !== true)
269803
+ this.turn.noteProviderRequestFailure(requestLogFields?.turnStep, error);
269804
+ throw error;
269805
+ }
269806
+ };
269807
+ if (ownedGenerateOptions.auth !== void 0)
269808
+ return run(ownedGenerateOptions);
269809
+ const withAuth = modelAlias === void 0 ? void 0 : this.modelProvider?.resolveAuth?.(modelAlias, { log: this.log });
269810
+ if (withAuth === void 0)
269811
+ return run(ownedGenerateOptions);
269812
+ return withAuth((auth) => {
269813
+ return run({
269814
+ ...ownedGenerateOptions,
269815
+ auth
269816
+ });
269817
+ });
269818
+ };
269819
+ }
269693
269820
  get llm() {
269694
269821
  return this.llmForTurn();
269695
269822
  }
@@ -300629,30 +300756,36 @@ var ProviderManager = class {
300629
300756
  return typeof config === "function" ? config() : config;
300630
300757
  }
300631
300758
  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
- }
300759
+ const alias = this.config.models?.[model];
300760
+ if (alias === void 0)
300761
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" is not configured in config.toml. Add a [models."${model}"] entry with max_context_size.`);
300762
+ const effectiveAlias = effectiveModelAlias(alias);
300763
+ const providerName = alias.provider ?? this.config.defaultProvider;
300764
+ if (providerName === void 0)
300765
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a provider in config.toml.`);
300766
+ const providerConfig = this.config.providers[providerName];
300767
+ if (providerConfig === void 0)
300768
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Provider "${providerName}" for model "${model}" is not configured.`);
300769
+ assertBlunProviderType(providerConfig);
300770
+ const runtimeContext = this.runtimeModelContexts.get(model);
300771
+ const maxContextSize = runtimeContext?.providerName === providerName && runtimeContext.model === alias.model ? runtimeContext.maxContextTokens : effectiveAlias.maxContextSize;
300772
+ if (!Number.isInteger(maxContextSize) || maxContextSize <= 0)
300773
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a positive max_context_size in config.toml.`);
300774
+ const provider = toKosongProviderConfig(providerConfig, alias.model, this.options.blunRequestHeaders, this.options.promptCacheKey, effectiveAlias.supportEfforts);
300775
+ if (this.options.configPath !== undefined)
300776
+ provider.requestBodyByteLimitStore = t3458Store(this.options.configPath, parse$6, stringify$1, atomicWritePrivate);
300777
+ return {
300778
+ providerName,
300779
+ provider,
300780
+ modelCapabilities: resolveModelCapabilities({
300781
+ ...effectiveAlias,
300782
+ maxContextSize
300783
+ }, provider),
300784
+ alwaysThinking: (effectiveAlias.capabilities ?? []).some((c) => c.trim().toLowerCase() === "always_thinking"),
300785
+ maxOutputSize: effectiveAlias.maxOutputSize,
300786
+ type: "blun"
300787
+ };
300788
+ }
300656
300789
  setRuntimeModelContext(input) {
300657
300790
  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
300791
  if (!Number.isSafeInteger(input.maxContextTokens) || input.maxContextTokens <= 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime model context must be a positive integer.");
@@ -336332,14 +336465,15 @@ var BlunCore = class {
336332
336465
  };
336333
336466
  }
336334
336467
  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
- }
336468
+ return new ProviderManager({
336469
+ config: () => this.config,
336470
+ configPath: this.configPath,
336471
+ blunRequestHeaders: this.blunRequestHeaders,
336472
+ resolveOAuthTokenProvider: this.resolveOAuthTokenProvider,
336473
+ managedApiKeyRevocationStore: this.managedApiKeyRevocationStore,
336474
+ promptCacheKey: sessionId
336475
+ });
336476
+ }
336343
336477
  mergePluginMcpConfig(base) {
336344
336478
  const pluginEntries = this.plugins.mcpServerConfigs();
336345
336479
  if (pluginEntries.length === 0) return base;
@@ -350032,7 +350166,7 @@ function optionalBuildString(value) {
350032
350166
  return typeof value === "string" && value.length > 0 ? value : void 0;
350033
350167
  }
350034
350168
  const BLUN_BUILD_INFO = {
350035
- version: optionalBuildString("9.1.628-pruefpaket.modules.r8"),
350169
+ version: optionalBuildString("9.1.640"),
350036
350170
  channel: typeof __BLUN_CHANNEL__ === "string" ? optionalBuildString(__BLUN_CHANNEL__) : void 0,
350037
350171
  commit: typeof __BLUN_COMMIT__ === "string" ? optionalBuildString(__BLUN_COMMIT__) : void 0,
350038
350172
  buildTarget: typeof __BLUN_BUILD_TARGET__ === "string" ? optionalBuildString(__BLUN_BUILD_TARGET__) : void 0
@@ -350084,3 +350218,169 @@ function createKingWorkerHarness(options) {
350084
350218
  });
350085
350219
  }
350086
350220
  export { LocalKaos, createKingWorkerHarness };
350221
+
350222
+ import * as t3458Fs from "node:fs/promises";
350223
+ import * as t3458Path from "node:path";
350224
+ import * as t3458Crypto from "node:crypto";
350225
+ // Neue, selektive Verdichtungshelfer. Keine Quellübernahme aus Arbeitsbaum oder v2/v3.
350226
+ function t3458ValidLimit(value) {
350227
+ return Number.isSafeInteger(value) && value > 0 ? value : undefined;
350228
+ }
350229
+ function t3458Learned(value) {
350230
+ return t3458ValidLimit(value) !== undefined && value >= 65536 ? value : undefined;
350231
+ }
350232
+ function t3458Endpoint(baseUrl) {
350233
+ try {
350234
+ const url = new URL(baseUrl);
350235
+ if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password || url.search || url.hash)
350236
+ return undefined;
350237
+ return `${url.origin}${url.pathname.replace(/\/+$/, '')}`;
350238
+ }
350239
+ catch {
350240
+ return undefined;
350241
+ }
350242
+ }
350243
+ function t3458Key(baseUrl) {
350244
+ const endpoint = t3458Endpoint(baseUrl);
350245
+ return endpoint === undefined ? undefined : t3458Crypto.createHash('sha256').update(endpoint).digest('hex');
350246
+ }
350247
+ async function t3458WithConfigLock(filePath, action, learning = false) {
350248
+ await t3458Fs.mkdir(t3458Path.dirname(filePath), { recursive: true, mode: 0o700 });
350249
+ const lock = `${filePath}.request-body.lock`;
350250
+ const token = t3458Crypto.randomUUID();
350251
+ // Small scheduling reserve: bounded contention attempts stay below 2s / 12s.
350252
+ const deadline = performance.now() + (learning ? 1950 : 11950);
350253
+ let handle;
350254
+ while (handle === undefined) {
350255
+ if (performance.now() >= deadline)
350256
+ return learning ? undefined : await action();
350257
+ try {
350258
+ handle = await t3458Fs.open(lock, 'wx', 0o600);
350259
+ }
350260
+ catch (error) {
350261
+ if (error.code !== 'EEXIST' && !(process.platform === 'win32' && ['EPERM', 'EACCES', 'EBUSY'].includes(error.code)))
350262
+ throw error;
350263
+ try {
350264
+ const observed = await t3458Fs.stat(lock);
350265
+ if (Date.now() - observed.mtimeMs > 10000) {
350266
+ const owner = await t3458Fs.readFile(lock, 'utf8');
350267
+ const current = await t3458Fs.stat(lock);
350268
+ // Best effort only: the accepted check/unlink race is not fencing.
350269
+ if (current.mtimeMs === observed.mtimeMs && await t3458Fs.readFile(lock, 'utf8') === owner) {
350270
+ await t3458Fs.unlink(lock);
350271
+ continue;
350272
+ }
350273
+ }
350274
+ }
350275
+ catch (failure) {
350276
+ if (failure.code !== 'ENOENT')
350277
+ throw failure;
350278
+ }
350279
+ const remaining = deadline - performance.now();
350280
+ if (remaining > 0)
350281
+ await new Promise(resolve => setTimeout(resolve, Math.min(25, remaining)));
350282
+ }
350283
+ }
350284
+ try {
350285
+ await handle.writeFile(token, 'utf8');
350286
+ return await action();
350287
+ }
350288
+ finally {
350289
+ await handle.close();
350290
+ try {
350291
+ if (await t3458Fs.readFile(lock, 'utf8') === token)
350292
+ await t3458Fs.unlink(lock);
350293
+ }
350294
+ catch (error) {
350295
+ if (error.code !== 'ENOENT')
350296
+ throw error;
350297
+ }
350298
+ }
350299
+ }
350300
+ function t3458Store(filePath, parseToml, stringifyToml, atomicWrite) {
350301
+ return {
350302
+ async load(key) {
350303
+ try {
350304
+ return t3458Learned(parseToml(await t3458Fs.readFile(filePath, 'utf8')).request_body_byte_limits?.[key]);
350305
+ }
350306
+ catch (error) {
350307
+ if (error.code === 'ENOENT')
350308
+ return undefined;
350309
+ throw error;
350310
+ }
350311
+ },
350312
+ async save(key, value) {
350313
+ if (!/^[a-f0-9]{64}$/.test(key) || t3458Learned(value) === undefined)
350314
+ return;
350315
+ await t3458WithConfigLock(filePath, async () => {
350316
+ let data;
350317
+ try {
350318
+ data = parseToml(await t3458Fs.readFile(filePath, 'utf8'));
350319
+ }
350320
+ catch (error) {
350321
+ if (error.code !== 'ENOENT')
350322
+ throw error;
350323
+ data = {};
350324
+ }
350325
+ const table = data.request_body_byte_limits ?? {};
350326
+ const previous = t3458Learned(table[key]);
350327
+ data.request_body_byte_limits = { ...table, [key]: previous === undefined ? value : Math.min(previous, value) };
350328
+ await atomicWrite(filePath, `${stringifyToml(data)}\n`);
350329
+ }, true);
350330
+ }
350331
+ };
350332
+ }
350333
+ async function t3458Measure(provider, params, options) {
350334
+ const endpoint = t3458Endpoint(provider._baseUrl);
350335
+ const key = t3458Key(provider._baseUrl);
350336
+ const option = typeof provider._requestBodyByteLimit === 'function' ? provider._requestBodyByteLimit() : provider._requestBodyByteLimit;
350337
+ const configured = t3458ValidLimit(options?.requestBodyBudget?.limitBytes) ?? t3458ValidLimit(option) ?? t3458ValidLimit(Number(process.env.BLUN_REQUEST_BODY_BYTE_LIMIT));
350338
+ let persisted;
350339
+ if (key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
350340
+ try {
350341
+ persisted = t3458Learned(await provider._requestBodyByteLimitStore.load(key));
350342
+ }
350343
+ catch { }
350344
+ }
350345
+ const learned = t3458Learned(provider._t3458ObservedLimit);
350346
+ const observed = learned === undefined ? persisted : persisted === undefined ? learned : Math.min(learned, persisted);
350347
+ const fallback = endpoint === 'https://api.blun.ai/coding/v1' ? 500000 : undefined;
350348
+ const limit = configured === undefined ? observed ?? fallback : observed === undefined ? configured : Math.min(configured, observed);
350349
+ const bytes = Buffer.byteLength(JSON.stringify(params), 'utf8');
350350
+ options?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
350351
+ if (!options?.isCompactionRequest && limit !== undefined && bytes >= limit)
350352
+ throw t3458Pressure(bytes, limit, endpoint);
350353
+ return { key, limit, bytes };
350354
+ }
350355
+ function t3458Pressure(byteLength, limitBytes, endpoint) {
350356
+ const error = new ChatProviderError(`Request body ${byteLength} bytes reaches limit ${limitBytes} bytes.`);
350357
+ error.name = 'APIRequestBodyPressureError';
350358
+ error.requestBodyPressure = true;
350359
+ error.byteLength = byteLength;
350360
+ error.limitBytes = limitBytes;
350361
+ error.endpoint = endpoint;
350362
+ return error;
350363
+ }
350364
+ function t3458Observed(error) {
350365
+ const message = error instanceof Error ? error.message : '';
350366
+ if (!/(?:request\s*body|payload|body\s*size)/i.test(message))
350367
+ return undefined;
350368
+ const comparison = /\(\s*\d+\s*>\s*(\d+)\s*bytes?\s*\)/i.exec(message);
350369
+ if (comparison !== null)
350370
+ return t3458Learned(Number(comparison[1]));
350371
+ const match = /(?:limit|maximum|max|up to)[^\d]{0,30}(\d{2,12})\s*(?:bytes?|b)\b/i.exec(message);
350372
+ return match === null ? undefined : t3458Learned(Number(match[1]));
350373
+ }
350374
+ async function t3458Learn(provider, error, measured, options) {
350375
+ const limit = t3458Observed(error);
350376
+ if (limit === undefined)
350377
+ return;
350378
+ provider._t3458ObservedLimit = Math.min(t3458Learned(provider._t3458ObservedLimit) ?? Infinity, limit);
350379
+ options?.requestBodyBudget?.onLimitObserved?.(limit);
350380
+ if (measured.key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
350381
+ try {
350382
+ await provider._requestBodyByteLimitStore.save(measured.key, limit);
350383
+ }
350384
+ catch { }
350385
+ }
350386
+ }