From 4ca86fae3db16dee4781f7632062244850ca738e Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 11 Mar 2026 22:21:08 -0400 Subject: [PATCH] refactor(agents): move calibration ratio capture to finally block Reorganized the logic for capturing the calibration ratio in the AgentClient class to ensure it is executed in the finally block. This change guarantees that the ratio is captured even if the run is aborted, enhancing the reliability of the response message persistence. Removed redundant code and improved clarity in the handling of context metadata. --- api/server/controllers/agents/client.js | 26 ++++++++++++------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/api/server/controllers/agents/client.js b/api/server/controllers/agents/client.js index 1a97fb8e57..2b649ec96d 100644 --- a/api/server/controllers/agents/client.js +++ b/api/server/controllers/agents/client.js @@ -873,20 +873,6 @@ class AgentClient extends BaseClient { const hideSequentialOutputs = config.configurable.hide_sequential_outputs; await runAgents(initialMessages); - /** Capture calibration ratio from the completed run for persistence on the response message */ - if (this.run) { - const ratio = this.run.getCalibrationRatio(); - if (ratio > 0 && ratio !== 1) { - this.contextMeta = { - calibrationRatio: Math.round(ratio * 1000) / 1000, - encoding: this.getEncoding(), - }; - logger.debug( - `[AgentClient] contextMeta to persist: ratio=${this.contextMeta.calibrationRatio}, encoding=${this.contextMeta.encoding}`, - ); - } - } - /** @deprecated Agent Chain */ if (hideSequentialOutputs) { this.contentParts = this.contentParts.filter((part, index) => { @@ -917,6 +903,18 @@ class AgentClient extends BaseClient { }); } } finally { + /** Capture calibration ratio from the run for persistence on the response message. + * Runs in finally so the ratio is captured even on abort. */ + if (this.run) { + const ratio = this.run.getCalibrationRatio(); + if (ratio > 0 && ratio !== 1) { + this.contextMeta = { + calibrationRatio: Math.round(ratio * 1000) / 1000, + encoding: this.getEncoding(), + }; + } + } + try { const attachments = await this.awaitMemoryWithTimeout(memoryPromise); if (attachments && attachments.length > 0) {