From 744182d851eaf6b007f9f5921a3035b05668413d Mon Sep 17 00:00:00 2001 From: Ruslan Fialkovskii Date: Mon, 8 Sep 2025 01:30:24 +0300 Subject: [PATCH 1/3] fix(codecompanion): improve error handling for empty stderr output - Enhanced flatten_table_to_string to filter out empty error messages - Added logic to distinguish actual errors from empty stderr output - Prevents false error reporting when VectorCode operations succeed but have no stderr - Provides meaningful fallback error message for truly empty error conditions - Fixes issue where successful operations with empty stderr were reported as { "" } errors Resolves the confusing empty error message issue in CodeCompanion integration. --- .../integrations/codecompanion/common.lua | 13 ++++++- .../integrations/codecompanion/query_tool.lua | 37 +++++++++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lua/vectorcode/integrations/codecompanion/common.lua b/lua/vectorcode/integrations/codecompanion/common.lua index 89840022..8cb5d33e 100644 --- a/lua/vectorcode/integrations/codecompanion/common.lua +++ b/lua/vectorcode/integrations/codecompanion/common.lua @@ -16,7 +16,18 @@ return { if type(t) == "string" then return t end - return table.concat(vim.iter(t):flatten(math.huge):totable(), "\n") + + -- Handle empty tables or tables with empty strings + local flattened = vim.iter(t):flatten(math.huge):totable() + local non_empty = vim.iter(flattened):filter(function(item) + return type(item) == "string" and vim.trim(item) ~= "" + end):totable() + + if #non_empty == 0 then + return "Unknown error occurred" + end + + return table.concat(non_empty, "\n") end, ---@param use_lsp boolean diff --git a/lua/vectorcode/integrations/codecompanion/query_tool.lua b/lua/vectorcode/integrations/codecompanion/query_tool.lua index e76c0ed2..b83dcddf 100644 --- a/lua/vectorcode/integrations/codecompanion/query_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/query_tool.lua @@ -462,7 +462,21 @@ return check_cli_wrap(function(opts) args ) - job_runner.run_async(args, function(result, error) + job_runner.run_async(args, function(result, error, code) + -- Check if this is actually an error or just empty stderr + local is_actual_error = code ~= nil and code ~= 0 + local has_meaningful_error = false + + if type(error) == "table" then + -- Check if the error table contains any non-empty strings + local flattened = vim.iter(error):flatten(math.huge):totable() + has_meaningful_error = vim.iter(flattened):any(function(item) + return type(item) == "string" and vim.trim(item) ~= "" + end) + elseif type(error) == "string" and vim.trim(error) ~= "" then + has_meaningful_error = true + end + if vim.islist(result) and #result > 0 and result[1].path ~= nil then ---@cast result VectorCode.QueryResult[] local summary_opts = vim.deepcopy(opts.summarise) or {} if type(summary_opts.enabled) == "function" then @@ -496,13 +510,22 @@ return check_cli_wrap(function(opts) }) end) else - if type(error) == "table" then - error = cc_common.flatten_table_to_string(error) + -- Only report as error if we have a meaningful error or non-zero exit code + if is_actual_error or has_meaningful_error then + if type(error) == "table" then + error = cc_common.flatten_table_to_string(error) + end + cb({ + status = "error", + data = error or "VectorCode command failed", + }) + else + -- Successful operation but no results found + cb({ + status = "success", + data = { raw_results = {}, count = 0, summary = "" }, + }) end - cb({ - status = "error", - data = error, - }) end end, tools.chat.bufnr) end, From 9351db6cd9dd16a5e9697f99f598a89d3e803674 Mon Sep 17 00:00:00 2001 From: Zhe Yu Date: Sat, 20 Sep 2025 11:12:51 +0800 Subject: [PATCH 2/3] refactor(nvim): cleanup `flatten_table_to_string` --- .../integrations/codecompanion/common.lua | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lua/vectorcode/integrations/codecompanion/common.lua b/lua/vectorcode/integrations/codecompanion/common.lua index 8cb5d33e..1e5a7e7e 100644 --- a/lua/vectorcode/integrations/codecompanion/common.lua +++ b/lua/vectorcode/integrations/codecompanion/common.lua @@ -16,18 +16,21 @@ return { if type(t) == "string" then return t end - + -- Handle empty tables or tables with empty strings - local flattened = vim.iter(t):flatten(math.huge):totable() - local non_empty = vim.iter(flattened):filter(function(item) - return type(item) == "string" and vim.trim(item) ~= "" - end):totable() - - if #non_empty == 0 then + local flattened = vim + .iter(t) + :flatten(math.huge) + :filter(function(item) + return type(item) == "string" and vim.trim(item) ~= "" + end) + :totable() + + if #flattened == 0 then return "Unknown error occurred" end - - return table.concat(non_empty, "\n") + + return table.concat(flattened, "\n") end, ---@param use_lsp boolean From c13159670f9e5914de2cfc7c3c55b77746b8d8de Mon Sep 17 00:00:00 2001 From: Zhe Yu Date: Sat, 20 Sep 2025 11:18:37 +0800 Subject: [PATCH 3/3] refactor(nvim): Simplify error handling in `query_tool` --- .../integrations/codecompanion/query_tool.lua | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/lua/vectorcode/integrations/codecompanion/query_tool.lua b/lua/vectorcode/integrations/codecompanion/query_tool.lua index b83dcddf..1ece23b7 100644 --- a/lua/vectorcode/integrations/codecompanion/query_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/query_tool.lua @@ -463,20 +463,8 @@ return check_cli_wrap(function(opts) ) job_runner.run_async(args, function(result, error, code) - -- Check if this is actually an error or just empty stderr - local is_actual_error = code ~= nil and code ~= 0 - local has_meaningful_error = false - - if type(error) == "table" then - -- Check if the error table contains any non-empty strings - local flattened = vim.iter(error):flatten(math.huge):totable() - has_meaningful_error = vim.iter(flattened):any(function(item) - return type(item) == "string" and vim.trim(item) ~= "" - end) - elseif type(error) == "string" and vim.trim(error) ~= "" then - has_meaningful_error = true - end - + local err_string = cc_common.flatten_table_to_string(error) + if vim.islist(result) and #result > 0 and result[1].path ~= nil then ---@cast result VectorCode.QueryResult[] local summary_opts = vim.deepcopy(opts.summarise) or {} if type(summary_opts.enabled) == "function" then @@ -511,19 +499,17 @@ return check_cli_wrap(function(opts) end) else -- Only report as error if we have a meaningful error or non-zero exit code - if is_actual_error or has_meaningful_error then - if type(error) == "table" then - error = cc_common.flatten_table_to_string(error) - end + if (err_string and err_string ~= "") or code ~= 0 then cb({ status = "error", - data = error or "VectorCode command failed", + data = ((err_string ~= "") and err_string) + or "VectorCode query tool failed to execute.", }) else -- Successful operation but no results found cb({ status = "success", - data = { raw_results = {}, count = 0, summary = "" }, + data = { raw_results = {}, count = 0 }, }) end end