blun-king-cli 9.1.638 → 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/blun.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- // BLUN_BUILD_INPUT_SHA256:efe2db101f5ddf60971c8504d9cd59f86444a9b24bae53a5d1e1032307d4171a
2
+ // BLUN_BUILD_INPUT_SHA256:4ddd869ff27cd786caab65fef2e4a43ddeb636eb25fdea7ff03b52a5a716faab
3
3
  /*! wasm-webp 0.1.0
4
4
  MIT License
5
5
 
@@ -2751,21 +2751,23 @@ var init_blun = __esmMin((() => {
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 init_blun = __esmMin((() => {
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;
@@ -16094,13 +16119,25 @@ function transformLoopControlData(data) {
16094
16119
  return out;
16095
16120
  }
16096
16121
  async function writeConfigFile(filePath, config) {
16097
- const validated = validateConfig(stripEnvModelConfig(config));
16098
- const configDirectory = dirname$3(filePath);
16099
- if (await mkdir(configDirectory, {
16100
- recursive: true,
16101
- mode: 448
16102
- }) !== void 0) await hardenPrivatePath(configDirectory, "directory");
16103
- await atomicWritePrivate$1(filePath, `${stringify$1(configToTomlData(validated))}\n`);
16122
+ return t3458WithConfigLock(filePath, async () => {
16123
+ let latestTable;
16124
+ try {
16125
+ latestTable = parse$7(await t3458Fs.readFile(filePath, "utf8")).request_body_byte_limits;
16126
+ }
16127
+ catch (error) {
16128
+ /* Unreadable or invalid previous TOML: preserve the Registry639 overwrite behavior. */
16129
+ }
16130
+ if (latestTable !== undefined)
16131
+ config = { ...config, raw: { ...config.raw, request_body_byte_limits: latestTable } };
16132
+ const validated = validateConfig(stripEnvModelConfig(config));
16133
+ const configDirectory = dirname$3(filePath);
16134
+ if (await mkdir(configDirectory, {
16135
+ recursive: true,
16136
+ mode: 448
16137
+ }) !== void 0)
16138
+ await hardenPrivatePath(configDirectory, "directory");
16139
+ await atomicWritePrivate$1(filePath, `${stringify$1(configToTomlData(validated))}\n`);
16140
+ });
16104
16141
  }
16105
16142
  function configToTomlData(config) {
16106
16143
  const out = cloneRecord$2(config.raw);
@@ -229695,8 +229732,8 @@ var init_full = __esmMin((() => {
229695
229732
  };
229696
229733
  }
229697
229734
  shouldRecoverFromContextOverflow(error) {
229698
- return error instanceof APIContextOverflowError;
229699
- }
229735
+ return error instanceof APIContextOverflowError || error?.requestBodyPressure === true;
229736
+ }
229700
229737
  observeContextOverflow(error, modelAlias = this.agent.activeResponderModel ?? this.agent.config.modelAlias) {
229701
229738
  if (!(error instanceof APIContextOverflowError)) return;
229702
229739
  const observed = error.maxContextTokens;
@@ -229712,63 +229749,70 @@ var init_full = __esmMin((() => {
229712
229749
  if (this.lastCompactedState?.modelAlias === modelAlias) this.lastCompactedState = null;
229713
229750
  }
229714
229751
  begin(data) {
229715
- this.assertAdmissionOpen();
229716
- if (this.compacting) return;
229717
- if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES) throw this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED);
229718
- if (data.source === "manual") this.compactionCountInTurn = 0;
229719
- else this.compactionCountInTurn += 1;
229720
- if (this.compactionCountInTurn > this.strategy.maxCompactionPerTurn) return;
229721
- if (this.agent.records.restoring) {
229722
- this.agent.replayBuilder.push({
229723
- type: "compaction",
229724
- instruction: data.instruction
229725
- });
229726
- return;
229727
- }
229728
- if (this.agent.context.history.length === 0) throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "No messages to compact in current history.");
229729
- 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.");
229730
- this.agent.records.logRecord({
229731
- type: "full_compaction.begin",
229732
- ...data
229733
- });
229734
- this.assertAdmissionOpen();
229735
- const abortController = new AbortController();
229736
- const executionId = ++this.nextExecutionId;
229737
- let resolve;
229738
- let reject;
229739
- const promise = new Promise((yes, no) => {
229740
- resolve = yes;
229741
- reject = no;
229742
- });
229743
- this.executions.track({
229744
- kind: "worker",
229745
- id: executionId,
229746
- source: data.source
229747
- }, promise);
229748
- this.executingControllers.add(abortController);
229749
- this.compacting = {
229750
- abortController,
229751
- promise,
229752
- blockedByTurn: false
229753
- };
229754
- const started = {
229755
- type: "compaction.started",
229756
- trigger: data.source,
229757
- instruction: data.instruction
229758
- };
229759
- this.agent.emitEvent(this.agent.experimentalFlags.enabled("queue_policies") ? {
229760
- ...started,
229761
- executionId,
229762
- contextTurnId: this.agent.turn.currentId
229763
- } : started);
229764
- this.compactionWorker(abortController.signal, data, executionId).then(() => {
229765
- this.executingControllers.delete(abortController);
229766
- resolve();
229767
- }, (error) => {
229768
- this.executingControllers.delete(abortController);
229769
- reject(error);
229770
- });
229771
- }
229752
+ this.assertAdmissionOpen();
229753
+ if (this.compacting)
229754
+ return;
229755
+ if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES)
229756
+ throw this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED);
229757
+ if (data.source === "manual")
229758
+ this.compactionCountInTurn = 0;
229759
+ else if (!data.requestBodyPressure)
229760
+ this.compactionCountInTurn += 1;
229761
+ if (this.compactionCountInTurn > this.strategy.maxCompactionPerTurn)
229762
+ return;
229763
+ if (this.agent.records.restoring) {
229764
+ this.agent.replayBuilder.push({
229765
+ type: "compaction",
229766
+ instruction: data.instruction
229767
+ });
229768
+ return;
229769
+ }
229770
+ if (this.agent.context.history.length === 0)
229771
+ throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "No messages to compact in current history.");
229772
+ if (data.source === "manual" && this.agent.turn.hasActiveTurn)
229773
+ throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "Cannot compact while a turn is active. Wait for it to finish, then retry.");
229774
+ this.agent.records.logRecord({
229775
+ type: "full_compaction.begin",
229776
+ ...data
229777
+ });
229778
+ this.assertAdmissionOpen();
229779
+ const abortController = new AbortController();
229780
+ const executionId = ++this.nextExecutionId;
229781
+ let resolve;
229782
+ let reject;
229783
+ const promise = new Promise((yes, no) => {
229784
+ resolve = yes;
229785
+ reject = no;
229786
+ });
229787
+ this.executions.track({
229788
+ kind: "worker",
229789
+ id: executionId,
229790
+ source: data.source
229791
+ }, promise);
229792
+ this.executingControllers.add(abortController);
229793
+ this.compacting = {
229794
+ abortController,
229795
+ promise,
229796
+ blockedByTurn: false
229797
+ };
229798
+ const started = {
229799
+ type: "compaction.started",
229800
+ trigger: data.source,
229801
+ instruction: data.instruction
229802
+ };
229803
+ this.agent.emitEvent(this.agent.experimentalFlags.enabled("queue_policies") ? {
229804
+ ...started,
229805
+ executionId,
229806
+ contextTurnId: this.agent.turn.currentId
229807
+ } : started);
229808
+ this.compactionWorker(abortController.signal, data, executionId).then(() => {
229809
+ this.executingControllers.delete(abortController);
229810
+ resolve();
229811
+ }, (error) => {
229812
+ this.executingControllers.delete(abortController);
229813
+ reject(error);
229814
+ });
229815
+ }
229772
229816
  cancel() {
229773
229817
  this.agent.replayBuilder.patchLast("compaction", { result: "cancelled" });
229774
229818
  if (!this.compacting) return;
@@ -229801,12 +229845,28 @@ var init_full = __esmMin((() => {
229801
229845
  this.sessionCompactionFailures = Math.max(this.sessionCompactionFailures, state.sessionCompactionFailures);
229802
229846
  }
229803
229847
  async handleOverflowError(signal, error) {
229804
- this.consecutiveOverflowCompactions += 1;
229805
- const maxAttempts = this.strategy.maxOverflowCompactionAttempts;
229806
- 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 });
229807
- if (!this.beginAutoCompaction() && !this.compacting) throw error;
229808
- await this.block(signal);
229809
- }
229848
+ if (error?.requestBodyPressure === true && error.byteLength < error.limitBytes) {
229849
+ signal.throwIfAborted();
229850
+ if (!this.hasShrinkableHistory(this.agent.context.history) || this.isSessionCompactionStopped)
229851
+ return;
229852
+ try {
229853
+ this.begin({ source: "auto", instruction: undefined, requestBodyPressure: true });
229854
+ await this.block(signal);
229855
+ }
229856
+ catch (failure) {
229857
+ signal.throwIfAborted();
229858
+ this.agent.log.warn("optional request-body compaction failed; valid request remains sendable", { error: failure });
229859
+ }
229860
+ return;
229861
+ }
229862
+ this.consecutiveOverflowCompactions += 1;
229863
+ const maxAttempts = this.strategy.maxOverflowCompactionAttempts;
229864
+ if (this.consecutiveOverflowCompactions > maxAttempts)
229865
+ 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 });
229866
+ if (!this.beginAutoCompaction() && !this.compacting)
229867
+ throw error;
229868
+ await this.block(signal);
229869
+ }
229810
229870
  async beforeStep(signal, requestContext = {}) {
229811
229871
  signal.throwIfAborted();
229812
229872
  const historyBefore = this.agent.context.history;
@@ -229859,10 +229919,28 @@ var init_full = __esmMin((() => {
229859
229919
  if (this.requiresShrinkableHistory && !this.hasShrinkableHistory(messages)) return false;
229860
229920
  return this.beginAutoCompaction(throwOnLimit);
229861
229921
  }
229862
- hasShrinkableHistory(messages) {
229863
- if (messages.some((message) => message.role === "assistant" || message.role === "tool")) return true;
229864
- return estimateTokensForMessages(messages.filter(isRealUserInput)) > COMPACT_USER_MESSAGE_MAX_TOKENS;
229865
- }
229922
+ shouldCompactRequestBody(endpoint, bytes, limit) {
229923
+ if (bytes >= limit)
229924
+ return true;
229925
+ const attempts = this.t3458PressureAttempts ??= new Map();
229926
+ const key = endpoint + "\0" + String(limit);
229927
+ if (bytes < limit * 0.75) {
229928
+ attempts.delete(key);
229929
+ return false;
229930
+ }
229931
+ if (this.isSessionCompactionStopped || !this.hasShrinkableHistory(this.agent.context.history))
229932
+ return false;
229933
+ const previous = attempts.get(key);
229934
+ if (previous !== undefined && bytes < previous + Math.ceil(previous / 10))
229935
+ return false;
229936
+ attempts.set(key, bytes);
229937
+ return true;
229938
+ }
229939
+ hasShrinkableHistory(messages) {
229940
+ 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))))
229941
+ return true;
229942
+ return estimateTokensForMessages(messages.filter(isRealUserInput)) > COMPACT_USER_MESSAGE_MAX_TOKENS;
229943
+ }
229866
229944
  beginAutoCompaction(throwOnLimit = true) {
229867
229945
  if (this.compacting) return true;
229868
229946
  if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES) {
@@ -229906,52 +229984,57 @@ var init_full = __esmMin((() => {
229906
229984
  }
229907
229985
  }
229908
229986
  async compactionWorker(signal, data, executionId) {
229909
- try {
229910
- const output = await this.compactionRound(signal, data, executionId);
229911
- if (!output) return;
229912
- const { result, stageCount } = output;
229913
- try {
229914
- await this.agent.refreshSystemPrompt();
229915
- } catch (error) {
229916
- this.agent.log.error("failed to refresh system prompt after compaction", { error });
229917
- }
229918
- await this.agent.injection.injectAfterCompaction();
229919
- this.fitRetainedInput({}, result.keptHeadUserMessageCount !== void 0);
229920
- this.lastCompactedState = {
229921
- tokenCount: this.tokenCountWithPending,
229922
- modelAlias: this.agent.activeResponderModel ?? this.agent.config.modelAlias,
229923
- maxContextTokens: this.getEffectiveMaxContextTokens()
229924
- };
229925
- this.markCompleted();
229926
- const { contextSummary: _contextSummary, ...eventResult } = result;
229927
- const projectedContextTokens = this.estimateCurrentRequestTokens();
229928
- this.agent.emitEvent({
229929
- type: "compaction.completed",
229930
- result: eventResult,
229931
- projectedContextTokens,
229932
- ...stageCount > 1 ? { stageCount } : {}
229933
- });
229934
- this.triggerPostCompactHook(data, result);
229935
- } catch (error) {
229936
- if (isAbortError$4(error) || signal.aborted) return;
229937
- this.sessionCompactionFailures += 1;
229938
- this.agent.records.logRecord({
229939
- type: "full_compaction.session_failures",
229940
- count: this.sessionCompactionFailures
229941
- });
229942
- const blockedByTurn = this.compacting?.blockedByTurn === true;
229943
- this.cancel();
229944
- this.agent.log.error("compaction failed", { error });
229945
- const visibleError = this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES ? this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED, error) : error;
229946
- if (blockedByTurn) throw visibleError;
229947
- this.agent.emitEvent({
229948
- type: "error",
229949
- ...toBlunErrorPayload(visibleError)
229950
- });
229951
- } finally {
229952
- this.agent.turn.onCompactionFinished();
229953
- }
229954
- }
229987
+ try {
229988
+ const output = await this.compactionRound(signal, data, executionId);
229989
+ if (!output)
229990
+ return;
229991
+ const { result, stageCount } = output;
229992
+ try {
229993
+ await this.agent.refreshSystemPrompt();
229994
+ }
229995
+ catch (error) {
229996
+ this.agent.log.error("failed to refresh system prompt after compaction", { error });
229997
+ }
229998
+ await this.agent.injection.injectAfterCompaction();
229999
+ this.fitRetainedInput({}, result.keptHeadUserMessageCount !== void 0);
230000
+ this.lastCompactedState = {
230001
+ tokenCount: this.tokenCountWithPending,
230002
+ modelAlias: this.agent.activeResponderModel ?? this.agent.config.modelAlias,
230003
+ maxContextTokens: this.getEffectiveMaxContextTokens()
230004
+ };
230005
+ this.markCompleted();
230006
+ const { contextSummary: _contextSummary, ...eventResult } = result;
230007
+ const projectedContextTokens = this.estimateCurrentRequestTokens();
230008
+ this.agent.emitEvent({
230009
+ type: "compaction.completed",
230010
+ result: eventResult,
230011
+ projectedContextTokens,
230012
+ ...stageCount > 1 ? { stageCount } : {}
230013
+ });
230014
+ this.triggerPostCompactHook(data, result);
230015
+ }
230016
+ catch (error) {
230017
+ if (isAbortError$4(error) || signal.aborted)
230018
+ return;
230019
+ if (!data.requestBodyPressure) {
230020
+ this.sessionCompactionFailures += 1;
230021
+ this.agent.records.logRecord({ type: "full_compaction.session_failures", count: this.sessionCompactionFailures });
230022
+ }
230023
+ const blockedByTurn = this.compacting?.blockedByTurn === true;
230024
+ this.cancel();
230025
+ this.agent.log.error("compaction failed", { error });
230026
+ const visibleError = this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES ? this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED, error) : error;
230027
+ if (blockedByTurn)
230028
+ throw visibleError;
230029
+ this.agent.emitEvent({
230030
+ type: "error",
230031
+ ...toBlunErrorPayload(visibleError)
230032
+ });
230033
+ }
230034
+ finally {
230035
+ this.agent.turn.onCompactionFinished();
230036
+ }
230037
+ }
229955
230038
  fitRetainedInput(context = {}, freshBudgetedSelection = false) {
229956
230039
  const budget = this.getCompactionBudgetTokens();
229957
230040
  const window = this.getEffectiveMaxContextTokens();
@@ -229975,336 +230058,367 @@ var init_full = __esmMin((() => {
229975
230058
  return `${summary.trim()}\n\n${todoMarkdown}`;
229976
230059
  }
229977
230060
  async compactionRound(signal, data, executionId) {
229978
- const startedAt = Date.now();
229979
- const originalHistory = [...this.agent.context.history];
229980
- const tokensBefore = estimateTokensForMessages(originalHistory);
229981
- let retryCount = 0;
229982
- try {
229983
- await this.triggerPreCompactHook(data, tokensBefore, signal);
229984
- const model = resolveCompactionModelAlias(this.agent.blunConfig?.loopControl?.compactionModel, this.agent.activeResponderModel ?? this.agent.config.model);
229985
- const runtimeModel = this.agent.config.runtimeModel(model);
229986
- const capability = runtimeModel.modelCapabilities;
229987
- const buildCompactionProvider = (usedContextTokens) => {
229988
- const currentMaxContextTokens = this.getEffectiveMaxContextTokens(model);
229989
- const defaultCompactionCap = currentMaxContextTokens > 0 ? Math.min(currentMaxContextTokens, DEFAULT_COMPACTION_MAX_COMPLETION_TOKENS) : void 0;
229990
- const requestCapability = currentMaxContextTokens > 0 ? {
229991
- ...capability,
229992
- max_context_tokens: currentMaxContextTokens
229993
- } : capability;
229994
- return applyCompletionBudget({
229995
- provider: runtimeModel.provider,
229996
- budget: resolveCompletionBudget({
229997
- maxOutputSize: runtimeModel.maxOutputSize ?? defaultCompactionCap,
229998
- reservedContextSize: this.agent.blunConfig?.loopControl?.reservedContextSize
229999
- }),
230000
- capability: requestCapability,
230001
- usedContextTokens
230002
- });
230003
- };
230004
- let provider = buildCompactionProvider();
230005
- const instruction = this.buildInstruction(data.instruction);
230006
- const delays = retryBackoffDelays(5);
230007
- let usage = null;
230008
- let summary;
230009
- let historyForModel = originalHistory.filter((message) => {
230010
- const origin = message.origin;
230011
- return !(origin?.kind === "injection" && REBUILT_AFTER_COMPACTION_INJECTION_VARIANTS.has(origin.variant));
230012
- });
230013
- let droppedCount = 0;
230014
- let overflowShrinkCount = 0;
230015
- let incompleteSummaryAttempts = 0;
230016
- let hierarchicalPassCount = 0;
230017
- const typicalDurationMs = readTypicalCompactionDuration(provider.name, provider.modelName, this.agent.blunHomeDir)?.typicalDurationMs;
230018
- let attemptCount = 0;
230019
- let charsReceived = 0;
230020
- let lastProgressEmitAt = 0;
230021
- let initialCompactionRequestTokens;
230022
- const compactionTools = [];
230023
- const buildRequestMessages = (history, requestInstruction) => {
230024
- return stripMediaForCompaction(downgradeUnsupportedMedia([...this.agent.context.project(history, {
230025
- synthesizeMissing: true,
230026
- dropOrphanResults: true
230027
- }), createUserMessage(requestInstruction)], capability));
230028
- };
230029
- while (true) {
230030
- signal.throwIfAborted();
230031
- 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: {
230032
- contextUnchanged: true,
230033
- hierarchicalPassCount
230034
- } });
230035
- const compactionRequestLimit = this.getEffectiveMaxContextTokens(model);
230036
- const safeCompactionRequestLimit = compactionRequestLimit > 0 ? Math.max(1, Math.floor(compactionRequestLimit * (1 - COMPACTION_SUMMARY_RESERVE_RATIO))) : compactionRequestLimit;
230037
- let messages = buildRequestMessages(historyForModel, instruction);
230038
- let estimatedCompactionRequestTokens = this.estimateRequestTokens(messages, this.agent.effectiveSystemPrompt, compactionTools);
230039
- initialCompactionRequestTokens ??= estimatedCompactionRequestTokens;
230040
- let hierarchicalChunkEnd;
230041
- if (safeCompactionRequestLimit > 0 && estimatedCompactionRequestTokens >= safeCompactionRequestLimit) {
230042
- if (historyForModel.length > 1) {
230043
- 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.`;
230044
- const chunk = selectHierarchicalCompactionChunk(historyForModel, safeCompactionRequestLimit, safeCompactionRequestLimit, (candidate) => {
230045
- const candidateMessages = buildRequestMessages(candidate, chunkInstruction);
230046
- return {
230047
- messages: candidateMessages,
230048
- estimatedTokens: this.estimateRequestTokens(candidateMessages, this.agent.effectiveSystemPrompt, compactionTools)
230049
- };
230050
- });
230051
- if (chunk !== void 0) {
230052
- hierarchicalChunkEnd = chunk.end;
230053
- messages = chunk.messages;
230054
- estimatedCompactionRequestTokens = chunk.estimatedTokens;
230055
- }
230056
- }
230057
- 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: {
230058
- contextBrakeBlocked: true,
230059
- estimatedRequestTokens: estimatedCompactionRequestTokens,
230060
- maxContextTokens: compactionRequestLimit,
230061
- requestBudgetTokens: safeCompactionRequestLimit,
230062
- summaryReserveTokens: compactionRequestLimit - safeCompactionRequestLimit,
230063
- contextUnchanged: true
230064
- } });
230065
- }
230066
- provider = buildCompactionProvider(estimatedCompactionRequestTokens);
230067
- attemptCount += 1;
230068
- charsReceived = 0;
230069
- const stage = hierarchicalPassCount + 1;
230070
- const estimatedStageCount = estimateCompactionStageCount(initialCompactionRequestTokens, safeCompactionRequestLimit, stage);
230071
- const estimatedProgressPercent = estimateCompactionProgressPercent(stage, estimatedStageCount);
230072
- const windowUsagePercent = estimateCompactionWindowUsagePercent(estimatedCompactionRequestTokens, compactionRequestLimit);
230073
- this.agent.log.info("compaction stage request", {
230074
- source: data.source,
230075
- stage,
230076
- estimatedStageCount,
230077
- activeContextTokens: this.estimateProjectedRequestTokens(),
230078
- estimatedInputTokens: estimatedCompactionRequestTokens,
230079
- windowUsagePercent,
230080
- safeInputLimitTokens: safeCompactionRequestLimit,
230081
- maxContextTokens: compactionRequestLimit
230082
- });
230083
- const emitCompactionProgress = (force = false) => {
230084
- const now = Date.now();
230085
- if (!force && now - lastProgressEmitAt < 150) return;
230086
- lastProgressEmitAt = now;
230087
- this.agent.emitEvent({
230088
- type: "compaction.progress",
230089
- stage,
230090
- estimatedStageCount,
230091
- estimatedInputTokens: estimatedCompactionRequestTokens,
230092
- estimatedProgressPercent,
230093
- ...windowUsagePercent === void 0 ? {} : { windowUsagePercent },
230094
- charsReceived,
230095
- attempt: attemptCount,
230096
- ...typicalDurationMs !== void 0 ? { typicalDurationMs } : {}
230097
- });
230098
- };
230099
- emitCompactionProgress(true);
230100
- const timingKey = compactionTimingKey(provider);
230101
- const stallPolicy = this.measuredStallPolicy(timingKey);
230102
- let lastProgressAt = Date.now();
230103
- let maxObservedIdleMs = 0;
230104
- const attemptController = new AbortController();
230105
- const onParentAbort = () => {
230106
- attemptController.abort();
230107
- };
230108
- signal.addEventListener("abort", onParentAbort, { once: true });
230109
- let stalled = false;
230110
- let stallTimer;
230111
- const armStallTimer = () => {
230112
- if (stallPolicy === void 0) return;
230113
- if (stallTimer !== void 0) clearTimeout(stallTimer);
230114
- stallTimer = setTimeout(() => {
230115
- stalled = true;
230116
- attemptController.abort();
230117
- }, stallPolicy.timeoutMs);
230118
- stallTimer.unref?.();
230119
- };
230120
- const observeProgress = (part) => {
230121
- const now = Date.now();
230122
- maxObservedIdleMs = Math.max(maxObservedIdleMs, now - lastProgressAt);
230123
- lastProgressAt = now;
230124
- if (part.type === "text") charsReceived += part.text.length;
230125
- emitCompactionProgress();
230126
- armStallTimer();
230127
- };
230128
- armStallTimer();
230129
- try {
230130
- const response = await this.agent.generate(provider, this.agent.config.systemPrompt, compactionTools, messages, { onMessagePart: observeProgress }, {
230131
- signal: attemptController.signal,
230132
- onExecution: (completion) => {
230133
- this.executions.track({
230134
- kind: "provider",
230135
- id: executionId,
230136
- source: data.source
230137
- }, completion);
230138
- }
230139
- });
230140
- signal.throwIfAborted();
230141
- if (stalled && !signal.aborted && stallPolicy !== void 0) throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
230142
- maxObservedIdleMs = Math.max(maxObservedIdleMs, Date.now() - lastProgressAt);
230143
- if (response.finishReason === "truncated") throw new CompactionTruncatedError();
230144
- if (response.usage !== null) usage = usage === null ? response.usage : addUsage$1(usage, response.usage);
230145
- const extractedSummary = extractCompactionSummary(response);
230146
- this.observeCompactionTiming(timingKey, maxObservedIdleMs);
230147
- if (hierarchicalChunkEnd !== void 0) {
230148
- hierarchicalPassCount += 1;
230149
- const previousTokens = estimateTokensForMessages(historyForModel);
230150
- const nextHistory = [createUserMessage(`${HIERARCHICAL_COMPACTION_PREFIX}\n${extractedSummary}`), ...historyForModel.slice(hierarchicalChunkEnd)];
230151
- const nextTokens = estimateTokensForMessages(nextHistory);
230152
- 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: {
230153
- contextUnchanged: true,
230154
- hierarchicalPassCount,
230155
- previousTokens,
230156
- nextTokens
230157
- } });
230158
- historyForModel = nextHistory;
230159
- incompleteSummaryAttempts = 0;
230160
- retryCount = 0;
230161
- continue;
230162
- }
230163
- summary = extractedSummary;
230164
- appendCompactionTiming({
230165
- ts: Date.now(),
230166
- provider: provider.name,
230167
- model: provider.modelName,
230168
- estimatedInputTokens: tokensBefore,
230169
- durationMs: Date.now() - startedAt
230170
- }, this.agent.blunHomeDir);
230171
- break;
230172
- } catch (error) {
230173
- if (stalled && !signal.aborted && stallPolicy !== void 0) throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
230174
- if (isModelFallbackStatus(error)) throw error;
230175
- const isContextOverflow = this.shouldRecoverFromContextOverflow(error);
230176
- if (isContextOverflow) this.observeContextOverflow(error, model);
230177
- if (isContextOverflow && historyForModel.length > 1) {
230178
- if (data.source === "auto") {
230179
- const learnedMaxContextTokens = this.getEffectiveMaxContextTokens(model);
230180
- if (learnedMaxContextTokens > 0 && learnedMaxContextTokens < compactionRequestLimit) {
230181
- retryCount = 0;
230182
- continue;
230183
- }
230184
- throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, "The active model rejected the full compaction request; refusing to drop unsummarized history.", {
230185
- cause: error,
230186
- details: {
230187
- contextBrakeBlocked: true,
230188
- estimatedRequestTokens: estimatedCompactionRequestTokens,
230189
- maxContextTokens: learnedMaxContextTokens,
230190
- requestBudgetTokens: learnedMaxContextTokens,
230191
- contextUnchanged: true
230192
- }
230193
- });
230194
- }
230195
- overflowShrinkCount += 1;
230196
- if (overflowShrinkCount > MAX_COMPACTION_OVERFLOW_SHRINK_ATTEMPTS) throw error;
230197
- const before = historyForModel.length;
230198
- historyForModel = shrinkCompactionHistoryAfterOverflow(historyForModel, overflowShrinkCount);
230199
- droppedCount += before - historyForModel.length;
230200
- retryCount = 0;
230201
- continue;
230202
- }
230203
- if (error instanceof CompactionTruncatedError || error instanceof APIEmptyResponseError) {
230204
- incompleteSummaryAttempts += 1;
230205
- if (incompleteSummaryAttempts === 1) continue;
230206
- throw new BlunError(ErrorCodes.COMPACTION_FAILED, "The provider returned an empty or truncated compaction response twice. The conversation history was not changed.", {
230207
- cause: error,
230208
- details: {
230209
- contextUnchanged: true,
230210
- lossyRetryPrevented: true,
230211
- compactionFailure: error.name,
230212
- incompleteSummaryAttempts
230213
- }
230214
- });
230215
- }
230216
- if (!isRetryableGenerateError(error)) throw error;
230217
- if (retryCount + 1 >= 5) throw error;
230218
- await sleepForRetry(delays[retryCount], signal);
230219
- retryCount += 1;
230220
- } finally {
230221
- if (stallTimer !== void 0) clearTimeout(stallTimer);
230222
- signal.removeEventListener("abort", onParentAbort);
230223
- }
230224
- }
230225
- if (usage !== null) this.agent.usage.record(model, usage);
230226
- const newHistory = this.agent.context.history;
230227
- for (let i = 0; i < originalHistory.length; i++) if (newHistory[i] !== originalHistory[i]) {
230228
- this.cancel();
230229
- return;
230230
- }
230231
- if (newHistory.slice(originalHistory.length).some((message) => !isRealUserInput(message))) {
230232
- this.cancel();
230233
- return;
230234
- }
230235
- const rawSummary = this.postProcessSummary(summary ?? "");
230236
- const contextSummary = buildCompactionSummaryText(rawSummary);
230237
- const result = this.agent.context.applyCompaction({
230238
- summary: rawSummary,
230239
- contextSummary,
230240
- compactedCount: originalHistory.length,
230241
- tokensBefore,
230242
- droppedCount: droppedCount === 0 ? void 0 : droppedCount
230243
- });
230244
- this.agent.telemetry.track("compaction_finished", {
230245
- source: data.source,
230246
- tokens_before: result.tokensBefore,
230247
- tokens_after: result.tokensAfter,
230248
- duration_ms: Date.now() - startedAt,
230249
- compacted_count: result.compactedCount,
230250
- dropped_count: result.droppedCount,
230251
- retry_count: retryCount,
230252
- round: 1,
230253
- thinking_effort: this.agent.config.thinkingEffort,
230254
- ...usage === null ? {} : {
230255
- input_tokens: inputTotal(usage),
230256
- output_tokens: usage.output
230257
- }
230258
- });
230259
- return {
230260
- result,
230261
- stageCount: hierarchicalPassCount + 1
230262
- };
230263
- } catch (error) {
230264
- if (isAbortError$4(error) || signal.aborted) return void 0;
230265
- this.agent.telemetry.track("compaction_failed", {
230266
- source: data.source,
230267
- tokens_before: tokensBefore,
230268
- duration_ms: Date.now() - startedAt,
230269
- round: 1,
230270
- retry_count: retryCount,
230271
- thinking_effort: this.agent.config.thinkingEffort,
230272
- error_type: error instanceof Error ? error.name : "Unknown"
230273
- });
230274
- if (isBlunError(error) && (error.code === ErrorCodes.AUTH_LOGIN_REQUIRED || error.code === ErrorCodes.COMPACTION_FAILED)) throw error;
230275
- if (isBlunError(error) && error.code === ErrorCodes.CONTEXT_OVERFLOW && error.details?.["contextBrakeBlocked"] === true) throw error;
230276
- if (isModelFallbackStatus(error)) throw error;
230277
- 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.`, {
230278
- cause: error,
230279
- details: {
230280
- reason: "no_provider_data",
230281
- timeoutSource: "measured_provider_behavior",
230282
- measuredIdleMs: error.measuredIdleMs,
230283
- timeoutMs: error.timeoutMs,
230284
- contextUnchanged: true
230285
- }
230286
- });
230287
- 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.", {
230288
- cause: error,
230289
- details: {
230290
- statusCode: error.statusCode,
230291
- requestId: error.requestId,
230292
- action: "switch_model_and_retry",
230293
- contextUnchanged: true
230294
- }
230295
- });
230296
- 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.`, {
230297
- cause: error,
230298
- details: {
230299
- statusCode: error.statusCode,
230300
- requestId: error.requestId,
230301
- estimatedHistoryTokens: tokensBefore,
230302
- contextUnchanged: true
230303
- }
230304
- });
230305
- throw new BlunError(ErrorCodes.COMPACTION_FAILED, String(error), { cause: error });
230306
- }
230307
- }
230061
+ const startedAt = Date.now();
230062
+ const originalHistory = [...this.agent.context.history];
230063
+ const tokensBefore = estimateTokensForMessages(originalHistory);
230064
+ let retryCount = 0;
230065
+ try {
230066
+ await this.triggerPreCompactHook(data, tokensBefore, signal);
230067
+ const model = resolveCompactionModelAlias(this.agent.blunConfig?.loopControl?.compactionModel, this.agent.activeResponderModel ?? this.agent.config.model);
230068
+ const runtimeModel = this.agent.config.runtimeModel(model);
230069
+ const capability = runtimeModel.modelCapabilities;
230070
+ const buildCompactionProvider = (usedContextTokens) => {
230071
+ const currentMaxContextTokens = this.getEffectiveMaxContextTokens(model);
230072
+ const defaultCompactionCap = currentMaxContextTokens > 0 ? Math.min(currentMaxContextTokens, DEFAULT_COMPACTION_MAX_COMPLETION_TOKENS) : void 0;
230073
+ const requestCapability = currentMaxContextTokens > 0 ? {
230074
+ ...capability,
230075
+ max_context_tokens: currentMaxContextTokens
230076
+ } : capability;
230077
+ return applyCompletionBudget({
230078
+ provider: runtimeModel.provider,
230079
+ budget: resolveCompletionBudget({
230080
+ maxOutputSize: runtimeModel.maxOutputSize ?? defaultCompactionCap,
230081
+ reservedContextSize: this.agent.blunConfig?.loopControl?.reservedContextSize
230082
+ }),
230083
+ capability: requestCapability,
230084
+ usedContextTokens
230085
+ });
230086
+ };
230087
+ let provider = buildCompactionProvider();
230088
+ const instruction = this.buildInstruction(data.instruction);
230089
+ const delays = retryBackoffDelays(5);
230090
+ let usage = null;
230091
+ let summary;
230092
+ let historyForModel = originalHistory.filter((message) => {
230093
+ const origin = message.origin;
230094
+ return !(origin?.kind === "injection" && REBUILT_AFTER_COMPACTION_INJECTION_VARIANTS.has(origin.variant));
230095
+ });
230096
+ let droppedCount = 0;
230097
+ let overflowShrinkCount = 0;
230098
+ let incompleteSummaryAttempts = 0;
230099
+ let hierarchicalPassCount = 0;
230100
+ const typicalDurationMs = readTypicalCompactionDuration(provider.name, provider.modelName, this.agent.blunHomeDir)?.typicalDurationMs;
230101
+ let attemptCount = 0;
230102
+ let charsReceived = 0;
230103
+ let lastProgressEmitAt = 0;
230104
+ let initialCompactionRequestTokens;
230105
+ const compactionTools = [];
230106
+ const buildRequestMessages = (history, requestInstruction) => {
230107
+ return stripMediaForCompaction(downgradeUnsupportedMedia([...this.agent.context.project(history, {
230108
+ synthesizeMissing: true,
230109
+ dropOrphanResults: true
230110
+ }), createUserMessage(requestInstruction)], capability));
230111
+ };
230112
+ while (true) {
230113
+ signal.throwIfAborted();
230114
+ if (hierarchicalPassCount >= MAX_HIERARCHICAL_COMPACTION_PASSES)
230115
+ 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: {
230116
+ contextUnchanged: true,
230117
+ hierarchicalPassCount
230118
+ } });
230119
+ const compactionRequestLimit = this.getEffectiveMaxContextTokens(model);
230120
+ const safeCompactionRequestLimit = compactionRequestLimit > 0 ? Math.max(1, Math.floor(compactionRequestLimit * (1 - COMPACTION_SUMMARY_RESERVE_RATIO))) : compactionRequestLimit;
230121
+ let messages = buildRequestMessages(historyForModel, instruction);
230122
+ let estimatedCompactionRequestTokens = this.estimateRequestTokens(messages, this.agent.effectiveSystemPrompt, compactionTools);
230123
+ initialCompactionRequestTokens ??= estimatedCompactionRequestTokens;
230124
+ let hierarchicalChunkEnd;
230125
+ if (safeCompactionRequestLimit > 0 && estimatedCompactionRequestTokens >= safeCompactionRequestLimit) {
230126
+ if (historyForModel.length > 1) {
230127
+ 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.`;
230128
+ const chunk = selectHierarchicalCompactionChunk(historyForModel, safeCompactionRequestLimit, safeCompactionRequestLimit, (candidate) => {
230129
+ const candidateMessages = buildRequestMessages(candidate, chunkInstruction);
230130
+ return {
230131
+ messages: candidateMessages,
230132
+ estimatedTokens: this.estimateRequestTokens(candidateMessages, this.agent.effectiveSystemPrompt, compactionTools)
230133
+ };
230134
+ });
230135
+ if (chunk !== void 0) {
230136
+ hierarchicalChunkEnd = chunk.end;
230137
+ messages = chunk.messages;
230138
+ estimatedCompactionRequestTokens = chunk.estimatedTokens;
230139
+ }
230140
+ }
230141
+ if (hierarchicalChunkEnd === void 0)
230142
+ 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: {
230143
+ contextBrakeBlocked: true,
230144
+ estimatedRequestTokens: estimatedCompactionRequestTokens,
230145
+ maxContextTokens: compactionRequestLimit,
230146
+ requestBudgetTokens: safeCompactionRequestLimit,
230147
+ summaryReserveTokens: compactionRequestLimit - safeCompactionRequestLimit,
230148
+ contextUnchanged: true
230149
+ } });
230150
+ }
230151
+ provider = buildCompactionProvider(estimatedCompactionRequestTokens);
230152
+ attemptCount += 1;
230153
+ charsReceived = 0;
230154
+ const stage = hierarchicalPassCount + 1;
230155
+ const estimatedStageCount = estimateCompactionStageCount(initialCompactionRequestTokens, safeCompactionRequestLimit, stage);
230156
+ const estimatedProgressPercent = estimateCompactionProgressPercent(stage, estimatedStageCount);
230157
+ const windowUsagePercent = estimateCompactionWindowUsagePercent(estimatedCompactionRequestTokens, compactionRequestLimit);
230158
+ this.agent.log.info("compaction stage request", {
230159
+ source: data.source,
230160
+ stage,
230161
+ estimatedStageCount,
230162
+ activeContextTokens: this.estimateProjectedRequestTokens(),
230163
+ estimatedInputTokens: estimatedCompactionRequestTokens,
230164
+ windowUsagePercent,
230165
+ safeInputLimitTokens: safeCompactionRequestLimit,
230166
+ maxContextTokens: compactionRequestLimit
230167
+ });
230168
+ const emitCompactionProgress = (force = false) => {
230169
+ const now = Date.now();
230170
+ if (!force && now - lastProgressEmitAt < 150)
230171
+ return;
230172
+ lastProgressEmitAt = now;
230173
+ this.agent.emitEvent({
230174
+ type: "compaction.progress",
230175
+ stage,
230176
+ estimatedStageCount,
230177
+ estimatedInputTokens: estimatedCompactionRequestTokens,
230178
+ estimatedProgressPercent,
230179
+ ...windowUsagePercent === void 0 ? {} : { windowUsagePercent },
230180
+ charsReceived,
230181
+ attempt: attemptCount,
230182
+ ...typicalDurationMs !== void 0 ? { typicalDurationMs } : {}
230183
+ });
230184
+ };
230185
+ emitCompactionProgress(true);
230186
+ const timingKey = compactionTimingKey(provider);
230187
+ const stallPolicy = this.measuredStallPolicy(timingKey);
230188
+ let lastProgressAt = Date.now();
230189
+ let maxObservedIdleMs = 0;
230190
+ const attemptController = new AbortController();
230191
+ const onParentAbort = () => {
230192
+ attemptController.abort();
230193
+ };
230194
+ signal.addEventListener("abort", onParentAbort, { once: true });
230195
+ let stalled = false;
230196
+ let stallTimer;
230197
+ const armStallTimer = () => {
230198
+ if (stallPolicy === void 0)
230199
+ return;
230200
+ if (stallTimer !== void 0)
230201
+ clearTimeout(stallTimer);
230202
+ stallTimer = setTimeout(() => {
230203
+ stalled = true;
230204
+ attemptController.abort();
230205
+ }, stallPolicy.timeoutMs);
230206
+ stallTimer.unref?.();
230207
+ };
230208
+ const observeProgress = (part) => {
230209
+ const now = Date.now();
230210
+ maxObservedIdleMs = Math.max(maxObservedIdleMs, now - lastProgressAt);
230211
+ lastProgressAt = now;
230212
+ if (part.type === "text")
230213
+ charsReceived += part.text.length;
230214
+ emitCompactionProgress();
230215
+ armStallTimer();
230216
+ };
230217
+ armStallTimer();
230218
+ try {
230219
+ const response = await this.agent.generate(provider, this.agent.config.systemPrompt, compactionTools, messages, { onMessagePart: observeProgress }, {
230220
+ signal: attemptController.signal,
230221
+ isCompactionRequest: true,
230222
+ onExecution: (completion) => {
230223
+ this.executions.track({
230224
+ kind: "provider",
230225
+ id: executionId,
230226
+ source: data.source
230227
+ }, completion);
230228
+ }
230229
+ });
230230
+ signal.throwIfAborted();
230231
+ if (stalled && !signal.aborted && stallPolicy !== void 0)
230232
+ throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
230233
+ maxObservedIdleMs = Math.max(maxObservedIdleMs, Date.now() - lastProgressAt);
230234
+ if (response.finishReason === "truncated")
230235
+ throw new CompactionTruncatedError();
230236
+ if (response.usage !== null)
230237
+ usage = usage === null ? response.usage : addUsage$1(usage, response.usage);
230238
+ const extractedSummary = extractCompactionSummary(response);
230239
+ this.observeCompactionTiming(timingKey, maxObservedIdleMs);
230240
+ if (hierarchicalChunkEnd !== void 0) {
230241
+ hierarchicalPassCount += 1;
230242
+ const previousTokens = estimateTokensForMessages(historyForModel);
230243
+ const nextHistory = [createUserMessage(`${HIERARCHICAL_COMPACTION_PREFIX}\n${extractedSummary}`), ...historyForModel.slice(hierarchicalChunkEnd)];
230244
+ const nextTokens = estimateTokensForMessages(nextHistory);
230245
+ if (nextTokens >= previousTokens)
230246
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, "A staged compaction response did not reduce the pending history. The conversation history was not changed.", { details: {
230247
+ contextUnchanged: true,
230248
+ hierarchicalPassCount,
230249
+ previousTokens,
230250
+ nextTokens
230251
+ } });
230252
+ historyForModel = nextHistory;
230253
+ incompleteSummaryAttempts = 0;
230254
+ retryCount = 0;
230255
+ continue;
230256
+ }
230257
+ summary = extractedSummary;
230258
+ appendCompactionTiming({
230259
+ ts: Date.now(),
230260
+ provider: provider.name,
230261
+ model: provider.modelName,
230262
+ estimatedInputTokens: tokensBefore,
230263
+ durationMs: Date.now() - startedAt
230264
+ }, this.agent.blunHomeDir);
230265
+ break;
230266
+ }
230267
+ catch (error) {
230268
+ if (stalled && !signal.aborted && stallPolicy !== void 0)
230269
+ throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
230270
+ if (isModelFallbackStatus(error))
230271
+ throw error;
230272
+ const isContextOverflow = this.shouldRecoverFromContextOverflow(error);
230273
+ if (isContextOverflow)
230274
+ this.observeContextOverflow(error, model);
230275
+ if (isContextOverflow && historyForModel.length > 1) {
230276
+ if (data.source === "auto") {
230277
+ const learnedMaxContextTokens = this.getEffectiveMaxContextTokens(model);
230278
+ if (learnedMaxContextTokens > 0 && learnedMaxContextTokens < compactionRequestLimit) {
230279
+ retryCount = 0;
230280
+ continue;
230281
+ }
230282
+ throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, "The active model rejected the full compaction request; refusing to drop unsummarized history.", {
230283
+ cause: error,
230284
+ details: {
230285
+ contextBrakeBlocked: true,
230286
+ estimatedRequestTokens: estimatedCompactionRequestTokens,
230287
+ maxContextTokens: learnedMaxContextTokens,
230288
+ requestBudgetTokens: learnedMaxContextTokens,
230289
+ contextUnchanged: true
230290
+ }
230291
+ });
230292
+ }
230293
+ overflowShrinkCount += 1;
230294
+ if (overflowShrinkCount > MAX_COMPACTION_OVERFLOW_SHRINK_ATTEMPTS)
230295
+ throw error;
230296
+ const before = historyForModel.length;
230297
+ historyForModel = shrinkCompactionHistoryAfterOverflow(historyForModel, overflowShrinkCount);
230298
+ droppedCount += before - historyForModel.length;
230299
+ retryCount = 0;
230300
+ continue;
230301
+ }
230302
+ if (error instanceof CompactionTruncatedError || error instanceof APIEmptyResponseError) {
230303
+ incompleteSummaryAttempts += 1;
230304
+ if (incompleteSummaryAttempts === 1)
230305
+ continue;
230306
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, "The provider returned an empty or truncated compaction response twice. The conversation history was not changed.", {
230307
+ cause: error,
230308
+ details: {
230309
+ contextUnchanged: true,
230310
+ lossyRetryPrevented: true,
230311
+ compactionFailure: error.name,
230312
+ incompleteSummaryAttempts
230313
+ }
230314
+ });
230315
+ }
230316
+ if (!isRetryableGenerateError(error))
230317
+ throw error;
230318
+ if (retryCount + 1 >= 5)
230319
+ throw error;
230320
+ await sleepForRetry(delays[retryCount], signal);
230321
+ retryCount += 1;
230322
+ }
230323
+ finally {
230324
+ if (stallTimer !== void 0)
230325
+ clearTimeout(stallTimer);
230326
+ signal.removeEventListener("abort", onParentAbort);
230327
+ }
230328
+ }
230329
+ if (usage !== null)
230330
+ this.agent.usage.record(model, usage);
230331
+ const newHistory = this.agent.context.history;
230332
+ for (let i = 0; i < originalHistory.length; i++)
230333
+ if (newHistory[i] !== originalHistory[i]) {
230334
+ this.cancel();
230335
+ return;
230336
+ }
230337
+ if (newHistory.slice(originalHistory.length).some((message) => !isRealUserInput(message))) {
230338
+ this.cancel();
230339
+ return;
230340
+ }
230341
+ const rawSummary = this.postProcessSummary(summary ?? "");
230342
+ const contextSummary = buildCompactionSummaryText(rawSummary);
230343
+ const result = this.agent.context.applyCompaction({
230344
+ summary: rawSummary,
230345
+ contextSummary,
230346
+ compactedCount: originalHistory.length,
230347
+ tokensBefore,
230348
+ droppedCount: droppedCount === 0 ? void 0 : droppedCount
230349
+ });
230350
+ this.agent.telemetry.track("compaction_finished", {
230351
+ source: data.source,
230352
+ tokens_before: result.tokensBefore,
230353
+ tokens_after: result.tokensAfter,
230354
+ duration_ms: Date.now() - startedAt,
230355
+ compacted_count: result.compactedCount,
230356
+ dropped_count: result.droppedCount,
230357
+ retry_count: retryCount,
230358
+ round: 1,
230359
+ thinking_effort: this.agent.config.thinkingEffort,
230360
+ ...usage === null ? {} : {
230361
+ input_tokens: inputTotal(usage),
230362
+ output_tokens: usage.output
230363
+ }
230364
+ });
230365
+ return {
230366
+ result,
230367
+ stageCount: hierarchicalPassCount + 1
230368
+ };
230369
+ }
230370
+ catch (error) {
230371
+ if (isAbortError$4(error) || signal.aborted)
230372
+ return void 0;
230373
+ this.agent.telemetry.track("compaction_failed", {
230374
+ source: data.source,
230375
+ tokens_before: tokensBefore,
230376
+ duration_ms: Date.now() - startedAt,
230377
+ round: 1,
230378
+ retry_count: retryCount,
230379
+ thinking_effort: this.agent.config.thinkingEffort,
230380
+ error_type: error instanceof Error ? error.name : "Unknown"
230381
+ });
230382
+ if (isBlunError(error) && (error.code === ErrorCodes.AUTH_LOGIN_REQUIRED || error.code === ErrorCodes.COMPACTION_FAILED))
230383
+ throw error;
230384
+ if (isBlunError(error) && error.code === ErrorCodes.CONTEXT_OVERFLOW && error.details?.["contextBrakeBlocked"] === true)
230385
+ throw error;
230386
+ if (isModelFallbackStatus(error))
230387
+ throw error;
230388
+ if (error instanceof CompactionStallError)
230389
+ 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.`, {
230390
+ cause: error,
230391
+ details: {
230392
+ reason: "no_provider_data",
230393
+ timeoutSource: "measured_provider_behavior",
230394
+ measuredIdleMs: error.measuredIdleMs,
230395
+ timeoutMs: error.timeoutMs,
230396
+ contextUnchanged: true
230397
+ }
230398
+ });
230399
+ if (error instanceof APIStatusError && error.statusCode === 402)
230400
+ 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.", {
230401
+ cause: error,
230402
+ details: {
230403
+ statusCode: error.statusCode,
230404
+ requestId: error.requestId,
230405
+ action: "switch_model_and_retry",
230406
+ contextUnchanged: true
230407
+ }
230408
+ });
230409
+ if (error instanceof APIStatusError && error.statusCode === 408)
230410
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, `Compaction stopped: the conversation (${String(tokensBefore)} tokens) could not be sent completely. The conversation history was not changed.`, {
230411
+ cause: error,
230412
+ details: {
230413
+ statusCode: error.statusCode,
230414
+ requestId: error.requestId,
230415
+ estimatedHistoryTokens: tokensBefore,
230416
+ contextUnchanged: true
230417
+ }
230418
+ });
230419
+ throw new BlunError(ErrorCodes.COMPACTION_FAILED, String(error), { cause: error });
230420
+ }
230421
+ }
230308
230422
  measuredStallPolicy(_timingKey) {}
230309
230423
  observeCompactionTiming(timingKey, maxIdleMs) {
230310
230424
  if (!Number.isFinite(maxIdleMs) || maxIdleMs <= 0) return;
@@ -270360,54 +270474,67 @@ var init_agent = __esmMin((() => {
270360
270474
  return this.generateForModel(this.activeResponderModel ?? this.config.modelAlias);
270361
270475
  }
270362
270476
  generateForModel(modelAlias) {
270363
- return async (provider, systemPrompt, tools, history, callbacks, options) => {
270364
- const { requestLogFields, generateOptions } = splitGenerateOptions(options);
270365
- const retryScope = providerRetryScope(modelAlias, provider);
270366
- await this.providerRetryWait.wait(retryScope, generateOptions?.signal);
270367
- const callerTransportAttempt = generateOptions?.onTransportAttempt;
270368
- const ownedGenerateOptions = {
270369
- ...generateOptions,
270370
- transportMaxRetries: 0,
270371
- onTransportAttempt: (stats) => {
270372
- callerTransportAttempt?.(stats);
270373
- this.llmRequestLogger.logTransportAttempt(stats, requestLogFields);
270374
- }
270375
- };
270376
- const run = async (requestOptions) => {
270377
- let requestStarted = false;
270378
- this.llmRequestLogger.logRequest({
270379
- provider,
270380
- modelAlias,
270381
- systemPrompt,
270382
- tools,
270383
- messages: history,
270384
- fields: requestLogFields
270385
- });
270386
- try {
270387
- return await this.rawGenerate(provider, systemPrompt, tools, history, callbacks, {
270388
- ...requestOptions,
270389
- onRequestStart: () => {
270390
- requestOptions?.onRequestStart?.();
270391
- requestStarted = true;
270392
- }
270393
- });
270394
- } catch (error) {
270395
- if (error instanceof APIProviderRateLimitError && error.retryAfterMs !== null) await this.providerRetryWait.defer(retryScope, error.retryAfterMs);
270396
- if (requestStarted && requestOptions?.signal?.aborted !== true) this.turn.noteProviderRequestFailure(requestLogFields?.turnStep, error);
270397
- throw error;
270398
- }
270399
- };
270400
- if (ownedGenerateOptions.auth !== void 0) return run(ownedGenerateOptions);
270401
- const withAuth = modelAlias === void 0 ? void 0 : this.modelProvider?.resolveAuth?.(modelAlias, { log: this.log });
270402
- if (withAuth === void 0) return run(ownedGenerateOptions);
270403
- return withAuth((auth) => {
270404
- return run({
270405
- ...ownedGenerateOptions,
270406
- auth
270407
- });
270408
- });
270409
- };
270410
- }
270477
+ return async (provider, systemPrompt, tools, history, callbacks, options) => {
270478
+ const { requestLogFields, generateOptions } = splitGenerateOptions(options);
270479
+ const retryScope = providerRetryScope(modelAlias, provider);
270480
+ await this.providerRetryWait.wait(retryScope, generateOptions?.signal);
270481
+ const callerTransportAttempt = generateOptions?.onTransportAttempt;
270482
+ const ownedGenerateOptions = {
270483
+ ...generateOptions,
270484
+ transportMaxRetries: 0,
270485
+ requestBodyBudget: {
270486
+ ...generateOptions?.requestBodyBudget,
270487
+ onMeasured: (bytes, limit, endpoint) => {
270488
+ generateOptions?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
270489
+ if (!generateOptions?.isCompactionRequest && limit !== undefined && this.fullCompaction.shouldCompactRequestBody(endpoint, bytes, limit))
270490
+ throw t3458Pressure(bytes, limit, endpoint);
270491
+ }
270492
+ },
270493
+ onTransportAttempt: (stats) => {
270494
+ callerTransportAttempt?.(stats);
270495
+ this.llmRequestLogger.logTransportAttempt(stats, requestLogFields);
270496
+ }
270497
+ };
270498
+ const run = async (requestOptions) => {
270499
+ let requestStarted = false;
270500
+ this.llmRequestLogger.logRequest({
270501
+ provider,
270502
+ modelAlias,
270503
+ systemPrompt,
270504
+ tools,
270505
+ messages: history,
270506
+ fields: requestLogFields
270507
+ });
270508
+ try {
270509
+ return await this.rawGenerate(provider, systemPrompt, tools, history, callbacks, {
270510
+ ...requestOptions,
270511
+ onRequestStart: () => {
270512
+ requestOptions?.onRequestStart?.();
270513
+ requestStarted = true;
270514
+ }
270515
+ });
270516
+ }
270517
+ catch (error) {
270518
+ if (error instanceof APIProviderRateLimitError && error.retryAfterMs !== null)
270519
+ await this.providerRetryWait.defer(retryScope, error.retryAfterMs);
270520
+ if (requestStarted && requestOptions?.signal?.aborted !== true)
270521
+ this.turn.noteProviderRequestFailure(requestLogFields?.turnStep, error);
270522
+ throw error;
270523
+ }
270524
+ };
270525
+ if (ownedGenerateOptions.auth !== void 0)
270526
+ return run(ownedGenerateOptions);
270527
+ const withAuth = modelAlias === void 0 ? void 0 : this.modelProvider?.resolveAuth?.(modelAlias, { log: this.log });
270528
+ if (withAuth === void 0)
270529
+ return run(ownedGenerateOptions);
270530
+ return withAuth((auth) => {
270531
+ return run({
270532
+ ...ownedGenerateOptions,
270533
+ auth
270534
+ });
270535
+ });
270536
+ };
270537
+ }
270411
270538
  get llm() {
270412
270539
  return this.llmForTurn();
270413
270540
  }
@@ -301723,30 +301850,36 @@ var init_provider_manager = __esmMin((() => {
301723
301850
  return typeof config === "function" ? config() : config;
301724
301851
  }
301725
301852
  resolveProviderConfig(model) {
301726
- const alias = this.config.models?.[model];
301727
- 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.`);
301728
- const effectiveAlias = effectiveModelAlias(alias);
301729
- const providerName = alias.provider ?? this.config.defaultProvider;
301730
- if (providerName === void 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a provider in config.toml.`);
301731
- const providerConfig = this.config.providers[providerName];
301732
- if (providerConfig === void 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, `Provider "${providerName}" for model "${model}" is not configured.`);
301733
- assertBlunProviderType(providerConfig);
301734
- const runtimeContext = this.runtimeModelContexts.get(model);
301735
- const maxContextSize = runtimeContext?.providerName === providerName && runtimeContext.model === alias.model ? runtimeContext.maxContextTokens : effectiveAlias.maxContextSize;
301736
- if (!Number.isInteger(maxContextSize) || maxContextSize <= 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a positive max_context_size in config.toml.`);
301737
- const provider = toKosongProviderConfig(providerConfig, alias.model, this.options.blunRequestHeaders, this.options.promptCacheKey, effectiveAlias.supportEfforts);
301738
- return {
301739
- providerName,
301740
- provider,
301741
- modelCapabilities: resolveModelCapabilities({
301742
- ...effectiveAlias,
301743
- maxContextSize
301744
- }, provider),
301745
- alwaysThinking: (effectiveAlias.capabilities ?? []).some((c) => c.trim().toLowerCase() === "always_thinking"),
301746
- maxOutputSize: effectiveAlias.maxOutputSize,
301747
- type: "blun"
301748
- };
301749
- }
301853
+ const alias = this.config.models?.[model];
301854
+ if (alias === void 0)
301855
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" is not configured in config.toml. Add a [models."${model}"] entry with max_context_size.`);
301856
+ const effectiveAlias = effectiveModelAlias(alias);
301857
+ const providerName = alias.provider ?? this.config.defaultProvider;
301858
+ if (providerName === void 0)
301859
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a provider in config.toml.`);
301860
+ const providerConfig = this.config.providers[providerName];
301861
+ if (providerConfig === void 0)
301862
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Provider "${providerName}" for model "${model}" is not configured.`);
301863
+ assertBlunProviderType(providerConfig);
301864
+ const runtimeContext = this.runtimeModelContexts.get(model);
301865
+ const maxContextSize = runtimeContext?.providerName === providerName && runtimeContext.model === alias.model ? runtimeContext.maxContextTokens : effectiveAlias.maxContextSize;
301866
+ if (!Number.isInteger(maxContextSize) || maxContextSize <= 0)
301867
+ throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a positive max_context_size in config.toml.`);
301868
+ const provider = toKosongProviderConfig(providerConfig, alias.model, this.options.blunRequestHeaders, this.options.promptCacheKey, effectiveAlias.supportEfforts);
301869
+ if (this.options.configPath !== undefined)
301870
+ provider.requestBodyByteLimitStore = t3458Store(this.options.configPath, parse$7, stringify$1, atomicWritePrivate$1);
301871
+ return {
301872
+ providerName,
301873
+ provider,
301874
+ modelCapabilities: resolveModelCapabilities({
301875
+ ...effectiveAlias,
301876
+ maxContextSize
301877
+ }, provider),
301878
+ alwaysThinking: (effectiveAlias.capabilities ?? []).some((c) => c.trim().toLowerCase() === "always_thinking"),
301879
+ maxOutputSize: effectiveAlias.maxOutputSize,
301880
+ type: "blun"
301881
+ };
301882
+ }
301750
301883
  setRuntimeModelContext(input) {
301751
301884
  if (!MANAGED_BLUN_MODEL_ALIASES$1.has(input.modelAlias)) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime context metadata is supported only for internal BLUN model aliases.");
301752
301885
  if (!Number.isSafeInteger(input.maxContextTokens) || input.maxContextTokens <= 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime model context must be a positive integer.");
@@ -338157,14 +338290,15 @@ var init_core_impl = __esmMin((() => {
338157
338290
  };
338158
338291
  }
338159
338292
  resolveProviderManager(sessionId) {
338160
- return new ProviderManager({
338161
- config: () => this.config,
338162
- blunRequestHeaders: this.blunRequestHeaders,
338163
- resolveOAuthTokenProvider: this.resolveOAuthTokenProvider,
338164
- managedApiKeyRevocationStore: this.managedApiKeyRevocationStore,
338165
- promptCacheKey: sessionId
338166
- });
338167
- }
338293
+ return new ProviderManager({
338294
+ config: () => this.config,
338295
+ configPath: this.configPath,
338296
+ blunRequestHeaders: this.blunRequestHeaders,
338297
+ resolveOAuthTokenProvider: this.resolveOAuthTokenProvider,
338298
+ managedApiKeyRevocationStore: this.managedApiKeyRevocationStore,
338299
+ promptCacheKey: sessionId
338300
+ });
338301
+ }
338168
338302
  mergePluginMcpConfig(base) {
338169
338303
  const pluginEntries = this.plugins.mcpServerConfigs();
338170
338304
  if (pluginEntries.length === 0) return base;
@@ -368239,7 +368373,7 @@ function optionalBuildString(value) {
368239
368373
  var BLUN_BUILD_INFO;
368240
368374
  var init_build_info = __esmMin((() => {
368241
368375
  BLUN_BUILD_INFO = {
368242
- version: optionalBuildString("9.1.628-pruefpaket.modules.r8"),
368376
+ version: optionalBuildString("9.1.640"),
368243
368377
  channel: typeof __BLUN_CHANNEL__ === "string" ? optionalBuildString(__BLUN_CHANNEL__) : void 0,
368244
368378
  commit: typeof __BLUN_COMMIT__ === "string" ? optionalBuildString(__BLUN_COMMIT__) : void 0,
368245
368379
  buildTarget: typeof __BLUN_BUILD_TARGET__ === "string" ? optionalBuildString(__BLUN_BUILD_TARGET__) : void 0
@@ -379760,6 +379894,18 @@ function extractCompleteSequences(buffer) {
379760
379894
  let pos = 0;
379761
379895
  while (pos < buffer.length) {
379762
379896
  const remaining = buffer.slice(pos);
379897
+ // Keep an interrupted keyboard CSI while delivering an embedded terminal reply.
379898
+ const nestedEscape = remaining.indexOf(ESC$1, 2);
379899
+ if (nestedEscape > 1 && /^\x1b\[[0-9:;]*$/.test(remaining.slice(0, nestedEscape))) {
379900
+ const nested = remaining.slice(nestedEscape);
379901
+ const reply = nested.match(/^\x1b\[(?:8;\d+;\d+t|\?[\d;]*[cu])/);
379902
+ if (reply !== null) {
379903
+ sequences.push(reply[0]);
379904
+ buffer = buffer.slice(0, pos) + remaining.slice(0, nestedEscape) + nested.slice(reply[0].length);
379905
+ continue;
379906
+ }
379907
+ if (/^\x1b(?:\[(?:\?[\d;]*|8(?:;\d*){0,2})?)?$/.test(nested)) return { sequences, remainder: remaining };
379908
+ }
379763
379909
  if (remaining.startsWith(ESC$1)) {
379764
379910
  let seqEnd = 1;
379765
379911
  while (seqEnd <= remaining.length) {
@@ -379874,7 +380020,7 @@ var init_stdin_buffer = __esmMin((() => {
379874
380020
  if (this.buffer.length > 0) this.timeout = setTimeout(() => {
379875
380021
  const flushed = this.flush();
379876
380022
  for (const sequence of flushed) this.emitDataSequence(sequence);
379877
- }, this.timeoutMs);
380023
+ }, this.buffer.startsWith(`${ESC$1}[`) ? Math.max(this.timeoutMs, 100) : this.timeoutMs);
379878
380024
  }
379879
380025
  emitDataSequence(sequence) {
379880
380026
  const rawCodepoint = sequence.length === 1 ? sequence.codePointAt(0) : void 0;
@@ -536295,7 +536441,7 @@ var init_session_event_handler = __esmMin((() => {
536295
536441
  });
536296
536442
  this.host.resetLivePane();
536297
536443
  this.host.requestQueueDrain();
536298
- } else this.host.setAppState({ isCompacting: false });
536444
+ } else { this.host.setAppState({ isCompacting: false }); this.host.steerQueueFlushPrefix(); }
536299
536445
  }
536300
536446
  handleBackgroundTaskEvent(event) {
536301
536447
  const { state } = this.host;
@@ -542716,7 +542862,10 @@ function isGroupSuppressed(text, _contextOnly) {
542716
542862
  return isGroupNoiseReply(text);
542717
542863
  }
542718
542864
  async function sendReplyFallback(chatId, text, contextOnly = false, requireDelivery = false, progress) {
542719
- if (typeof text === "string" && text.trim() === "[[BLUN_TELEGRAM_SILENT]]") return true;
542865
+ if (typeof text === "string") {
542866
+ text = text.split("[[BLUN_TELEGRAM_SILENT]]").join("");
542867
+ if (text.trim().length === 0) return true;
542868
+ }
542720
542869
  if (isGroupChat(chatId) && isGroupSuppressed(text, contextOnly)) {
542721
542870
  try {
542722
542871
  appendFileSync(outboxPath(), `${JSON.stringify({
@@ -562500,3 +562649,169 @@ async function logStartupFailure(operation, error) {
562500
562649
  } catch {}
562501
562650
  }
562502
562651
  export { configureTelegramPluginLaunchMode, handleMainCommand, handleUpgradeCommand, main };
562652
+
562653
+ import * as t3458Fs from "node:fs/promises";
562654
+ import * as t3458Path from "node:path";
562655
+ import * as t3458Crypto from "node:crypto";
562656
+ // Neue, selektive Verdichtungshelfer. Keine Quellübernahme aus Arbeitsbaum oder v2/v3.
562657
+ function t3458ValidLimit(value) {
562658
+ return Number.isSafeInteger(value) && value > 0 ? value : undefined;
562659
+ }
562660
+ function t3458Learned(value) {
562661
+ return t3458ValidLimit(value) !== undefined && value >= 65536 ? value : undefined;
562662
+ }
562663
+ function t3458Endpoint(baseUrl) {
562664
+ try {
562665
+ const url = new URL(baseUrl);
562666
+ if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password || url.search || url.hash)
562667
+ return undefined;
562668
+ return `${url.origin}${url.pathname.replace(/\/+$/, '')}`;
562669
+ }
562670
+ catch {
562671
+ return undefined;
562672
+ }
562673
+ }
562674
+ function t3458Key(baseUrl) {
562675
+ const endpoint = t3458Endpoint(baseUrl);
562676
+ return endpoint === undefined ? undefined : t3458Crypto.createHash('sha256').update(endpoint).digest('hex');
562677
+ }
562678
+ async function t3458WithConfigLock(filePath, action, learning = false) {
562679
+ await t3458Fs.mkdir(t3458Path.dirname(filePath), { recursive: true, mode: 0o700 });
562680
+ const lock = `${filePath}.request-body.lock`;
562681
+ const token = t3458Crypto.randomUUID();
562682
+ // Small scheduling reserve: bounded contention attempts stay below 2s / 12s.
562683
+ const deadline = performance.now() + (learning ? 1950 : 11950);
562684
+ let handle;
562685
+ while (handle === undefined) {
562686
+ if (performance.now() >= deadline)
562687
+ return learning ? undefined : await action();
562688
+ try {
562689
+ handle = await t3458Fs.open(lock, 'wx', 0o600);
562690
+ }
562691
+ catch (error) {
562692
+ if (error.code !== 'EEXIST' && !(process.platform === 'win32' && ['EPERM', 'EACCES', 'EBUSY'].includes(error.code)))
562693
+ throw error;
562694
+ try {
562695
+ const observed = await t3458Fs.stat(lock);
562696
+ if (Date.now() - observed.mtimeMs > 10000) {
562697
+ const owner = await t3458Fs.readFile(lock, 'utf8');
562698
+ const current = await t3458Fs.stat(lock);
562699
+ // Best effort only: the accepted check/unlink race is not fencing.
562700
+ if (current.mtimeMs === observed.mtimeMs && await t3458Fs.readFile(lock, 'utf8') === owner) {
562701
+ await t3458Fs.unlink(lock);
562702
+ continue;
562703
+ }
562704
+ }
562705
+ }
562706
+ catch (failure) {
562707
+ if (failure.code !== 'ENOENT')
562708
+ throw failure;
562709
+ }
562710
+ const remaining = deadline - performance.now();
562711
+ if (remaining > 0)
562712
+ await new Promise(resolve => setTimeout(resolve, Math.min(25, remaining)));
562713
+ }
562714
+ }
562715
+ try {
562716
+ await handle.writeFile(token, 'utf8');
562717
+ return await action();
562718
+ }
562719
+ finally {
562720
+ await handle.close();
562721
+ try {
562722
+ if (await t3458Fs.readFile(lock, 'utf8') === token)
562723
+ await t3458Fs.unlink(lock);
562724
+ }
562725
+ catch (error) {
562726
+ if (error.code !== 'ENOENT')
562727
+ throw error;
562728
+ }
562729
+ }
562730
+ }
562731
+ function t3458Store(filePath, parseToml, stringifyToml, atomicWrite) {
562732
+ return {
562733
+ async load(key) {
562734
+ try {
562735
+ return t3458Learned(parseToml(await t3458Fs.readFile(filePath, 'utf8')).request_body_byte_limits?.[key]);
562736
+ }
562737
+ catch (error) {
562738
+ if (error.code === 'ENOENT')
562739
+ return undefined;
562740
+ throw error;
562741
+ }
562742
+ },
562743
+ async save(key, value) {
562744
+ if (!/^[a-f0-9]{64}$/.test(key) || t3458Learned(value) === undefined)
562745
+ return;
562746
+ await t3458WithConfigLock(filePath, async () => {
562747
+ let data;
562748
+ try {
562749
+ data = parseToml(await t3458Fs.readFile(filePath, 'utf8'));
562750
+ }
562751
+ catch (error) {
562752
+ if (error.code !== 'ENOENT')
562753
+ throw error;
562754
+ data = {};
562755
+ }
562756
+ const table = data.request_body_byte_limits ?? {};
562757
+ const previous = t3458Learned(table[key]);
562758
+ data.request_body_byte_limits = { ...table, [key]: previous === undefined ? value : Math.min(previous, value) };
562759
+ await atomicWrite(filePath, `${stringifyToml(data)}\n`);
562760
+ }, true);
562761
+ }
562762
+ };
562763
+ }
562764
+ async function t3458Measure(provider, params, options) {
562765
+ const endpoint = t3458Endpoint(provider._baseUrl);
562766
+ const key = t3458Key(provider._baseUrl);
562767
+ const option = typeof provider._requestBodyByteLimit === 'function' ? provider._requestBodyByteLimit() : provider._requestBodyByteLimit;
562768
+ const configured = t3458ValidLimit(options?.requestBodyBudget?.limitBytes) ?? t3458ValidLimit(option) ?? t3458ValidLimit(Number(process.env.BLUN_REQUEST_BODY_BYTE_LIMIT));
562769
+ let persisted;
562770
+ if (key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
562771
+ try {
562772
+ persisted = t3458Learned(await provider._requestBodyByteLimitStore.load(key));
562773
+ }
562774
+ catch { }
562775
+ }
562776
+ const learned = t3458Learned(provider._t3458ObservedLimit);
562777
+ const observed = learned === undefined ? persisted : persisted === undefined ? learned : Math.min(learned, persisted);
562778
+ const fallback = endpoint === 'https://api.blun.ai/coding/v1' ? 500000 : undefined;
562779
+ const limit = configured === undefined ? observed ?? fallback : observed === undefined ? configured : Math.min(configured, observed);
562780
+ const bytes = Buffer.byteLength(JSON.stringify(params), 'utf8');
562781
+ options?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
562782
+ if (!options?.isCompactionRequest && limit !== undefined && bytes >= limit)
562783
+ throw t3458Pressure(bytes, limit, endpoint);
562784
+ return { key, limit, bytes };
562785
+ }
562786
+ function t3458Pressure(byteLength, limitBytes, endpoint) {
562787
+ const error = new ChatProviderError(`Request body ${byteLength} bytes reaches limit ${limitBytes} bytes.`);
562788
+ error.name = 'APIRequestBodyPressureError';
562789
+ error.requestBodyPressure = true;
562790
+ error.byteLength = byteLength;
562791
+ error.limitBytes = limitBytes;
562792
+ error.endpoint = endpoint;
562793
+ return error;
562794
+ }
562795
+ function t3458Observed(error) {
562796
+ const message = error instanceof Error ? error.message : '';
562797
+ if (!/(?:request\s*body|payload|body\s*size)/i.test(message))
562798
+ return undefined;
562799
+ const comparison = /\(\s*\d+\s*>\s*(\d+)\s*bytes?\s*\)/i.exec(message);
562800
+ if (comparison !== null)
562801
+ return t3458Learned(Number(comparison[1]));
562802
+ const match = /(?:limit|maximum|max|up to)[^\d]{0,30}(\d{2,12})\s*(?:bytes?|b)\b/i.exec(message);
562803
+ return match === null ? undefined : t3458Learned(Number(match[1]));
562804
+ }
562805
+ async function t3458Learn(provider, error, measured, options) {
562806
+ const limit = t3458Observed(error);
562807
+ if (limit === undefined)
562808
+ return;
562809
+ provider._t3458ObservedLimit = Math.min(t3458Learned(provider._t3458ObservedLimit) ?? Infinity, limit);
562810
+ options?.requestBodyBudget?.onLimitObserved?.(limit);
562811
+ if (measured.key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
562812
+ try {
562813
+ await provider._requestBodyByteLimitStore.save(measured.key, limit);
562814
+ }
562815
+ catch { }
562816
+ }
562817
+ }