Skip to content

Commit f798294

Browse files
committed
fix(v2): forward selected agent to submit and ignore non-user inbox deliveries
1 parent 9f38638 commit f798294

5 files changed

Lines changed: 78 additions & 7 deletions

File tree

‎lua/opencode/protocols/v2/observation/actions.lua‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,11 @@ function M.attach(observation, connection)
147147

148148
---@param input OpencodeV2SubmitInput
149149
---@return Promise<OpencodeSubmission>
150-
---@param selected? {model?: string, variant?: string}
150+
---@param selected? {mode?: string, model?: string, variant?: string}
151151
function observation:submit(input, selected)
152+
if selected and selected.mode then
153+
input = vim.tbl_extend('force', {}, input, { agent = selected.mode })
154+
end
152155
if selected and selected.model then
153156
local provider, model = selected.model:match('^(.-)/(.+)$')
154157
if provider and model then

‎lua/opencode/protocols/v2/observation/events.lua‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ local function finish_inbox(status)
141141
---@param data table
142142
return function(observation, event, data)
143143
terminal_inbox(observation, data.inboxID, status, event.created)
144-
if status == 'delivered' then
144+
local item = observation:read().inbox.items_by_id[data.inboxID]
145+
if status == 'delivered' and (item.kind == 'user' or item.kind == 'unknown') then
145146
actions.delivered(observation, data.inboxID)
146147
end
147148
end

‎lua/opencode/services/messaging.lua‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,19 @@ end
3131
---@class PreparedMessage
3232
---@field params table
3333
---@field submission_context OpencodeContext
34-
---@field selected_model {model?: string, variant?: string}
34+
---@field selected_model {mode?: string, model?: string, variant?: string}
3535
---@field model_update OpencodeSessionTabModelUpdate
3636

3737
---@param observation OpencodeObservation
3838
---@param prompt string
3939
---@param opts SendMessageOpts
4040
---@return PreparedMessage
4141
local function prepare_message(observation, prompt, opts)
42-
local selected_model = { model = state.current_model, variant = state.current_variant }
42+
local selected_model = {
43+
mode = state.current_mode or config.default_mode,
44+
model = state.current_model,
45+
variant = state.current_variant,
46+
}
4347
observation:validate_message_options(opts, config.default_system_prompt)
4448

4549
opts.context = vim.tbl_deep_extend('force', {}, state.current_context_config or {}, opts.context or {})

‎tests/unit/protocol_v2_observation_runtime_spec.lua‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ describe('V2 protocol Observation runtime', function()
561561
assert.is_false(observed:_watches('messages'))
562562
end)
563563

564-
it('passes selected model through the V2 submit operation', function()
564+
it('passes selected agent and model through the V2 submit operation', function()
565565
local value = connection()
566566
local sent
567567
install_operations(value, {
@@ -573,13 +573,32 @@ describe('V2 protocol Observation runtime', function()
573573
local observed = value:observe({ id = 'ses-main' })
574574
local input = { text = 'hello', context = {}, files = {}, agents = {} }
575575

576-
observed:submit(input, { model = 'provider/selected-model', variant = 'high' }):wait()
576+
observed:submit(input, { mode = 'plan', model = 'provider/selected-model', variant = 'high' }):wait()
577577

578+
assert.equals('plan', sent.agent)
578579
assert.same({ providerID = 'provider', modelID = 'selected-model' }, sent.model)
579580
assert.equals('high', sent.variant)
581+
assert.is_nil(input.agent)
580582
assert.is_nil(input.model)
581583
end)
582584

585+
it('passes selected agent without a selected model through V2 submit', function()
586+
local value = connection()
587+
local sent
588+
install_operations(value, {
589+
submit = function(_, _, input)
590+
sent = input
591+
return resolved({ id = 'msg-local', delivery = 'queue' })
592+
end,
593+
})
594+
local observed = value:observe({ id = 'ses-main' })
595+
596+
observed:submit({ text = 'hello', context = {}, files = {}, agents = {} }, { mode = 'plan' }):wait()
597+
598+
assert.equals('plan', sent.agent)
599+
assert.is_nil(sent.model)
600+
end)
601+
583602
it('correlates only a delivered admission with the following same-session terminal', function()
584603
local value = connection()
585604
local admission = Promise.new()
@@ -644,6 +663,28 @@ describe('V2 protocol Observation runtime', function()
644663
assert.is_true(streams[1].handle.stopped)
645664
end)
646665

666+
it('does not count non-user inbox deliveries as competing prompt inputs', function()
667+
local value = connection()
668+
local streams = install_operations(value, {
669+
submit = function()
670+
return resolved({ id = 'msg-local', delivery = 'queue' })
671+
end,
672+
})
673+
local observed = value:observe({ id = 'ses-main' })
674+
local accepted = observed:submit({ text = 'hello' }):wait()
675+
emit(streams[1], event('ses-main', 'session.inbox.enqueued', {
676+
inboxID = 'synthetic-1',
677+
item = { type = 'synthetic', payload = { text = 'agent changed' }, delivery = 'queue' },
678+
}, 10))
679+
emit(streams[1], event('ses-main', 'session.inbox.delivered', { inboxID = 'synthetic-1' }, 11))
680+
emit(streams[1], event('ses-main', 'session.inbox.delivered', { inboxID = 'msg-local' }, 12))
681+
emit(streams[1], event('ses-main', 'session.execution.started', {}, 13))
682+
emit(streams[1], event('ses-main', 'session.execution.succeeded', {}, 14))
683+
684+
assert.equals('succeeded', accepted.completion:wait().outcome)
685+
assert.is_nil(observed._v2_delivered['synthetic-1'])
686+
end)
687+
647688
it('rejects ambiguous delivery even when the HTTP admissions arrive after the terminal', function()
648689
local value = connection()
649690
local http = { Promise.new(), Promise.new() }

‎tests/unit/services_messaging_spec.lua‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,34 @@ describe('opencode.services.messaging', function()
118118

119119
messaging.send_message('hello'):wait()
120120

121-
assert.same({ { operation = 'submit', selected = { model = 'provider/selected-model', variant = 'high' } } }, calls)
121+
assert.same({
122+
{ operation = 'submit', selected = { mode = state.current_mode, model = 'provider/selected-model', variant = 'high' } },
123+
}, calls)
122124
observation.submit = original_submit
123125
state.model.set_model(previous_model)
124126
state.model.set_variant(previous_variant)
125127
end)
126128

129+
it('passes the selected mode to the V2 submission', function()
130+
state.session.set_active({ id = 'sess-v2' })
131+
connection.protocol = 'v2'
132+
local previous_mode = state.current_mode
133+
state.model.set_mode('plan')
134+
local observation = state.session.active_observation()
135+
local original_submit = observation.submit
136+
local selected_mode
137+
observation.submit = function(_, _, selected)
138+
selected_mode = selected.mode
139+
return successful_submission()
140+
end
141+
142+
messaging.send_message('hello'):wait()
143+
144+
assert.equal('plan', selected_mode)
145+
observation.submit = original_submit
146+
state.model.set_mode(previous_mode)
147+
end)
148+
127149
it('rejects a V2 default system prompt before submitting', function()
128150
state.session.set_active({ id = 'sess-v2' })
129151
connection.protocol = 'v2'

0 commit comments

Comments
 (0)