diff --git a/lua/opencode/services/agent_model.lua b/lua/opencode/services/agent_model.lua index b29914fa..bb2fee10 100644 --- a/lua/opencode/services/agent_model.lua +++ b/lua/opencode/services/agent_model.lua @@ -174,8 +174,13 @@ M.initialize_current_model = Promise.async(function(opts) state.model.set_model(model_str) end if msg.info.mode and state.current_mode ~= msg.info.mode then - local available_agents = config_file.get_opencode_agents():await() - if vim.tbl_contains(available_agents, msg.info.mode) then + local active_session = state.active_session + local should_restore_mode = active_session and active_session.parentID + if not should_restore_mode then + local available_agents = config_file.get_opencode_agents():await() + should_restore_mode = vim.tbl_contains(available_agents, msg.info.mode) + end + if should_restore_mode then state.model.set_mode(msg.info.mode) end end diff --git a/tests/unit/services_agent_model_spec.lua b/tests/unit/services_agent_model_spec.lua index 08098af7..7eb07c44 100644 --- a/tests/unit/services_agent_model_spec.lua +++ b/tests/unit/services_agent_model_spec.lua @@ -144,7 +144,6 @@ describe('opencode.services.agent_model', function() it('restores the latest session model and mode when explicitly requested', function() state.model.set_model('openai/gpt-4.1') state.model.set_mode('plan') - stub(config_file, 'get_opencode_agents').returns(Promise.new():resolve({ 'plan', 'build' })) state.renderer.set_messages({ @@ -167,10 +166,35 @@ describe('opencode.services.agent_model', function() config_file.get_opencode_agents:revert() end) - it('does not restore mode to a hidden agent from messages', function() + it('restores hidden mode from messages for child sessions', function() state.model.set_model('openai/gpt-4.1') state.model.set_mode('build') + state.session.set_active({ id = 'child', parentID = 'parent' }) + state.renderer.set_messages({ + { + info = { + id = 'm1', + providerID = 'anthropic', + modelID = 'claude-3-opus', + mode = 'hidden-xyz', + }, + }, + }) + + local model = agent_model.initialize_current_model({ restore_from_messages = true }):wait() + + assert.equal('anthropic/claude-3-opus', model) + assert.equal('anthropic/claude-3-opus', state.current_model) + assert.equal('hidden-xyz', state.current_mode) + + state.session.clear_active() + end) + + it('does not restore hidden mode from messages for primary sessions', function() + state.model.set_model('openai/gpt-4.1') + state.model.set_mode('build') + state.session.set_active({ id = 'primary' }) stub(config_file, 'get_opencode_agents').returns(Promise.new():resolve({ 'plan', 'build' })) state.renderer.set_messages({ @@ -191,5 +215,6 @@ describe('opencode.services.agent_model', function() assert.equal('build', state.current_mode) config_file.get_opencode_agents:revert() + state.session.clear_active() end) end)