Skip to content

Commit 5e70a09

Browse files
authored
fix(session): restore subagent mode from messages (#397)
* fix(session): restore subagent mode from messages Hidden subagents are filtered out of the visible primary-agent list so primary agents cannot discover them by default. That visibility rule should not affect restoring an existing session's agent type. When restoring a session, the tabbar/footer and subsequent message routing should use the agent type recorded on the session messages. Filtering msg.info.mode through the visible primary-agent list caused hidden subagent sessions to keep a stale primary mode in state.current_mode. * fix(session): scope hidden mode restore to child sessions
1 parent 0c31d11 commit 5e70a09

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

lua/opencode/services/agent_model.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,13 @@ M.initialize_current_model = Promise.async(function(opts)
174174
state.model.set_model(model_str)
175175
end
176176
if msg.info.mode and state.current_mode ~= msg.info.mode then
177-
local available_agents = config_file.get_opencode_agents():await()
178-
if vim.tbl_contains(available_agents, msg.info.mode) then
177+
local active_session = state.active_session
178+
local should_restore_mode = active_session and active_session.parentID
179+
if not should_restore_mode then
180+
local available_agents = config_file.get_opencode_agents():await()
181+
should_restore_mode = vim.tbl_contains(available_agents, msg.info.mode)
182+
end
183+
if should_restore_mode then
179184
state.model.set_mode(msg.info.mode)
180185
end
181186
end

tests/unit/services_agent_model_spec.lua

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ describe('opencode.services.agent_model', function()
144144
it('restores the latest session model and mode when explicitly requested', function()
145145
state.model.set_model('openai/gpt-4.1')
146146
state.model.set_mode('plan')
147-
148147
stub(config_file, 'get_opencode_agents').returns(Promise.new():resolve({ 'plan', 'build' }))
149148

150149
state.renderer.set_messages({
@@ -167,10 +166,35 @@ describe('opencode.services.agent_model', function()
167166
config_file.get_opencode_agents:revert()
168167
end)
169168

170-
it('does not restore mode to a hidden agent from messages', function()
169+
it('restores hidden mode from messages for child sessions', function()
171170
state.model.set_model('openai/gpt-4.1')
172171
state.model.set_mode('build')
172+
state.session.set_active({ id = 'child', parentID = 'parent' })
173173

174+
state.renderer.set_messages({
175+
{
176+
info = {
177+
id = 'm1',
178+
providerID = 'anthropic',
179+
modelID = 'claude-3-opus',
180+
mode = 'hidden-xyz',
181+
},
182+
},
183+
})
184+
185+
local model = agent_model.initialize_current_model({ restore_from_messages = true }):wait()
186+
187+
assert.equal('anthropic/claude-3-opus', model)
188+
assert.equal('anthropic/claude-3-opus', state.current_model)
189+
assert.equal('hidden-xyz', state.current_mode)
190+
191+
state.session.clear_active()
192+
end)
193+
194+
it('does not restore hidden mode from messages for primary sessions', function()
195+
state.model.set_model('openai/gpt-4.1')
196+
state.model.set_mode('build')
197+
state.session.set_active({ id = 'primary' })
174198
stub(config_file, 'get_opencode_agents').returns(Promise.new():resolve({ 'plan', 'build' }))
175199

176200
state.renderer.set_messages({
@@ -191,5 +215,6 @@ describe('opencode.services.agent_model', function()
191215
assert.equal('build', state.current_mode)
192216

193217
config_file.get_opencode_agents:revert()
218+
state.session.clear_active()
194219
end)
195220
end)

0 commit comments

Comments
 (0)