Skip to content

Commit 5ec2abc

Browse files
committed
fix: retry fetching opencode agents while server initializes
The OpenCode server may return an empty primary-agent list while it is still starting up. Treat that transient state like an error and retry up to 3 times with a 500ms delay before falling back to an empty agent list.
1 parent bf7919d commit 5ec2abc

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎lua/opencode/config_file.lua‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,20 @@ end
141141

142142
---@type fun(): Promise<string[]>
143143
M.get_opencode_agents = Promise.async(function()
144-
return resource('list_primary_agents'):await() or {}
144+
local empty_agents = {}
145+
return Promise.retry(function()
146+
return resource('list_primary_agents'):and_then(function(agents)
147+
if agents and #agents > 0 then
148+
return agents
149+
end
150+
return Promise.new():reject(empty_agents)
151+
end)
152+
end, 3, 500):catch(function(err)
153+
if err == empty_agents then
154+
return {}
155+
end
156+
return Promise.new():reject(err)
157+
end):await()
145158
end)
146159

147160
---@type fun(): Promise<string[]>

‎tests/unit/config_file_spec.lua‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ describe('config_file.setup', function()
8080
end):wait()
8181
end)
8282

83+
it('retries an empty primary-agent response while the server initializes', function()
84+
local original_defer_fn = vim.defer_fn
85+
local attempts = 0
86+
vim.defer_fn = function(callback)
87+
callback()
88+
end
89+
90+
set_operations({
91+
list_primary_agents = function()
92+
attempts = attempts + 1
93+
return Promise.new():resolve(attempts < 3 and {} or { 'build' })
94+
end,
95+
})
96+
97+
local agents = config_file.get_opencode_agents():wait()
98+
99+
vim.defer_fn = original_defer_fn
100+
assert.same({ 'build' }, agents)
101+
assert.equals(3, attempts)
102+
end)
103+
83104
it('gets subagents from the selected protocol', function()
84105
Promise.spawn(function()
85106
set_operations({

0 commit comments

Comments
 (0)