repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

fix(neovim): stop pivi saying terminal-only commands to the model

19e84617e2bdb6afb8fc699c2556223beac27fa4

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (9)

neovim/lua/bugabinga/pivi/AGENTS.md

Modified
99 99 27. **A session may be a stand-in.** The drive harness passes a stub session, so code reached from a command must not assume `session.buffer` is a live buffer.
100 100 This is exactly the class of regression the drive harness exists to catch; it caught it.
102 -28. **Contract tests guard assumptions,** not features. They skip unless `pi` is on `PATH` and opt-in is set. When Pi changes behavior, they fail before users do.
102 +28. **Pi runs the commands it owns and says the rest.** A `/name` sent as a prompt is dispatched when Pi owns the command and becomes a user message when it does not, so a name must be checked against `get_commands` before it is sent. Terminal-only commands such as `/model` are absent from that list; offer the capability natively instead of forwarding the name.
104 +29. **A picker filters the text it is shown.** Formatting an item as `name description` makes every description compete with the name; show the name alone.
106 +30. **Contract tests guard assumptions,** not features. They skip unless `pi` is on `PATH` and opt-in is set. When Pi changes behavior, they fail before users do.
104 108 ## Commands

neovim/lua/bugabinga/pivi/command.lua

Modified
15 15 vim.notify( 'pivi: ' .. message, level or vim.log.levels.INFO )
18 +local fetch_commands = function( current, callback )
19 + current.request( { type = 'get_commands', }, function( response )
20 + local commands = {}
21 + if response.success and response.data then
22 + for _, entry in ipairs( response.data.commands or {} ) do
23 + table.insert( commands, entry )
24 + end
25 + end
26 + current.commands = commands
27 + if callback then callback( commands ) end
28 + end )
29 +end
18 31 --- Returns the active session, starting one when none exists.
19 32 --- A missing or unusable Pi is reported, never raised.
20 33 local require_session = function()
40 53 return nil
56 + -- Warm the command list so completion has something true to offer.
57 + fetch_commands( created )
43 58 return created
53 68 return col == 0
56 -local fetch_commands = function( current, callback )
57 - current.request( { type = 'get_commands', }, function( response )
58 - local commands = {}
59 - if response.success and response.data then
60 - for _, entry in ipairs( response.data.commands or {} ) do
61 - table.insert( commands, entry )
62 - end
63 - end
64 - current.commands = commands
65 - if callback then callback( commands ) end
66 - end )
71 +local command_names = function( current )
72 + local found = {}
73 + for _, entry in ipairs( current and current.commands or {} ) do
74 + table.insert( found, entry.name )
75 + end
76 + table.sort( found )
77 + return found
80 +local knows = function( current, name )
81 + for _, entry in ipairs( current and current.commands or {} ) do
82 + if entry.name == name then return true end
83 + end
84 + return false
85 +end
87 +--- Runs a Pi command.
88 +--- Pi dispatches a command it owns and treats anything else as something the
89 +--- user said, so an unknown name would quietly become a message to the model.
69 90 local run_slash_command = function( current, name, arguments )
91 + if not knows( current, name ) then
92 + notify(
93 + ( 'pi has no command "%s" here; terminal-only commands are not offered over this channel' ):format( name ),
94 + vim.log.levels.WARN
95 + )
96 + return false
97 + end
70 99 local message = '/' .. name
71 100 if arguments and arguments ~= '' then message = message .. ' ' .. arguments end
72 101 local accepted, rejection = current.submit( message, { capture = false, } )
73 102 if not accepted then notify( rejection, vim.log.levels.WARN ) end
103 + return accepted
76 106 local pick_slash_command = function( current, on_pick )
79 109 notify( 'no pi commands available', vim.log.levels.WARN )
80 110 return
112 + -- A picker filters the text it is shown, so showing a description here
113 + -- means typing a command name competes with every description that
114 + -- happens to contain the same letters.
82 115 vim.ui.select( commands, {
83 116 prompt = 'pi command',
84 - format_item = function( entry )
85 - return '/' .. entry.name .. ( entry.description and ( ' ' .. entry.description ) or '' )
86 - end,
117 + format_item = function( entry ) return '/' .. entry.name end,
87 118 }, function( choice )
88 119 if not choice then return end
89 120 on_pick( choice )
174 205 transcript.decorate( win, current.name )
175 206 vim.wo[win].winbar = '%#Title#π ' .. current.name ..
176 207 '%* %#Comment#' .. ( vim.g.pivi_submit_hint or 'send' ) .. ' / commands%*'
177 - vim.cmd.startinsert()
208 + if vim.g.pivi_start_insert ~= false then vim.cmd.startinsert() end
179 210 return bufnr
233 264 local name, rest = arguments:match '^(%S+)%s*(.*)$'
234 265 if name then
235 - run_slash_command( current, name, rest )
236 - transcript.show( current.buffer )
266 + -- The list may not have arrived yet, so it is fetched before judging a name.
267 + fetch_commands( current, function()
268 + if run_slash_command( current, name, rest ) then transcript.show( current.buffer ) end
269 + end )
237 270 return
239 272 pick_slash_command( current, function( choice )
252 285 local created = session.create { name = arguments ~= '' and arguments or nil, }
286 + fetch_commands( created )
253 287 publish.set_active( created.name )
254 288 transcript.show( created.buffer )
255 289 notify( 'session ' .. created.name )
383 +--- Switches model.
384 +--- `/model` is a terminal-only command, but the capability itself is on this
385 +--- channel, so pivi offers it natively instead of pretending the command works.
386 +subcommands.model = function( arguments )
387 + local current = require_session()
388 + if not current then return end
390 + current.request( { type = 'get_available_models', }, function( response )
391 + if not response.success or not response.data then
392 + notify( 'pi did not offer a model list', vim.log.levels.WARN )
393 + return
394 + end
396 + local models = response.data.models or {}
397 + if #models == 0 then
398 + notify( 'pi has no models configured', vim.log.levels.WARN )
399 + return
400 + end
402 + local choose = function( model )
403 + current.request(
404 + { type = 'set_model', provider = model.provider, modelId = model.id, },
405 + function( answer )
406 + if answer.success then
407 + notify( model.provider .. '/' .. model.id )
408 + else
409 + notify( answer.error or 'pi refused the model', vim.log.levels.ERROR )
410 + end
411 + end
413 + end
415 + if arguments ~= '' then
416 + for _, model in ipairs( models ) do
417 + if ( model.provider .. '/' .. model.id ) == arguments or model.id == arguments then
418 + choose( model )
419 + return
420 + end
421 + end
422 + notify( 'no model matches ' .. arguments, vim.log.levels.WARN )
423 + return
424 + end
426 + vim.ui.select( models, {
427 + prompt = 'pi model',
428 + format_item = function( model ) return model.provider .. '/' .. model.id end,
429 + }, function( model )
430 + if model then choose( model ) end
431 + end )
432 + end )
433 +end
349 435 subcommands.guide = function()
350 436 guide.show( vim.api.nvim_get_current_buf() )
385 471 range = true,
386 472 desc = 'pivi native Pi integration',
387 473 complete = function( lead, line )
388 - local before = line:match '^%s*Pi%s+(%S*)$'
389 - if before == nil then return {} end
390 - return vim.tbl_filter( function( candidate )
391 - return candidate:sub( 1, #lead ) == lead
392 - end, names() )
474 + local matching = function( candidates )
475 + return vim.tbl_filter( function( candidate )
476 + return candidate:sub( 1, #lead ) == lead
477 + end, candidates )
478 + end
480 + -- Completing an argument needs the session's own knowledge, so the list
481 + -- is kept warm and offered as it is, rather than blocking on a request.
482 + local subcommand = line:match '^%s*Pi%s+(%S+)%s+%S*$'
483 + if subcommand == 'command' then return matching( command_names( session.active() ) ) end
484 + if subcommand == 'stop' or subcommand == 'sessions' then return matching( session.list() ) end
485 + if subcommand == 'follow' then return matching { 'window', 'tab', 'current', } end
487 + if line:match '^%s*Pi%s+%S*$' == nil then return {} end
488 + return matching( names() )

neovim/lua/bugabinga/pivi/guide.lua

Modified
84 84 table.insert( written, '' )
87 + if vim.bo[bufnr].filetype == 'pivi-input' then
88 + table.insert( written, '## ' .. glyph.insert .. ' while writing' )
89 + table.insert( written, '' )
90 + table.insert( written, 'The prompt opens in insert mode, so keys behind a leader need `<Esc>` first.' )
91 + table.insert( written, 'Set `vim.g.pivi_start_insert = false` to open in normal mode instead.' )
92 + table.insert( written, '' )
93 + end
87 95 table.insert( written, '## ' .. glyph.list .. ' ordinary neovim' )
88 96 table.insert( written, '' )
89 97 table.insert( written, '- `za` `zo` `zc` collapse or expand the entry under the cursor' )

neovim/lua/bugabinga/pivi/rpc.lua

Modified
37 37 local guard = function( handler, ... )
38 38 if not handler then return end
39 39 local ok, failure = pcall( handler, ... )
40 - if not ok then table.insert( failures, tostring( failure ) ) end
40 + if not ok then
41 + local reported = tostring( failure )
42 + table.insert( failures, reported )
43 + -- A swallowed handler error is a bug that looks like nothing happening,
44 + -- so it is reported rather than only recorded.
45 + if options.on_failure then pcall( options.on_failure, reported ) end
46 + end
43 49 local dispatch = function( records )

neovim/lua/bugabinga/pivi/session.lua

Modified
251 251 spawn = options.spawn,
252 252 on_event = function( event ) handle_event( self, event ) end,
253 253 on_stderr = function( data ) self.last_stderr = data end,
254 + on_failure = function( failure )
255 + vim.notify( 'pivi: ' .. failure, vim.log.levels.ERROR )
256 + end,
254 257 on_exit = function( code )
255 258 self.status = status.stopped( self.status, code )
256 259 publish_status( self )

neovim/lua/bugabinga/pivi/skills/pivi/SKILL.md

Modified
58 58 | show this guide | `<leader>p?` |
59 59 | collapse or expand | `za` `zo` `zc` `zR` `zM`, because entries are real folds |
61 -In the prompt, `<c-y>` accepts: the completion when one is offered, the prompt otherwise. `<CR>` in normal mode sends it.
61 +In the prompt, `<c-y>` accepts: the completion when one is offered, the prompt otherwise. `<CR>` in normal mode sends it too, so both work. The prompt opens in insert mode, so keys behind a leader need `<Esc>` first; `vim.g.pivi_start_insert = false` opens it in normal mode instead.
63 +`/model` and other terminal-only commands are not offered over this channel and are refused rather than said to the model; `:Pi model` switches model natively.
63 65 pivi chooses none of these. It publishes `<Plug>(pivi-goto-location)`, `<Plug>(pivi-inspect-entry)`, `<Plug>(pivi-next-entry)`, `<Plug>(pivi-previous-entry)`, `<Plug>(pivi-next-tool)`, `<Plug>(pivi-previous-tool)`, `<Plug>(pivi-compare-change)`, `<Plug>(pivi-toggle-entry)`, `<Plug>(pivi-guide)`, `<Plug>(pivi-submit)` and `<Plug>(pivi-accept)`, and the configuration decides the keys. A key is only reused where the local behaviour is the same idea as the global one.

neovim/lua/bugabinga/pivi_keys.lua

Modified
64 64 -- What the prompt window says about sending, so the hint follows the choice.
65 -vim.g.pivi_submit_hint = '<c-y> send'
65 +vim.g.pivi_submit_hint = '<c-y> or <cr> send'
67 67 map.normal {
68 68 description = 'Open the pi prompt',

neovim/tests/bugabinga/pivi/command_spec.lua

Modified
172 172 describe( 'slash commands', function()
173 - it( 'runs a named pi command through the session', function()
174 - local channel = fake.new()
175 - session.create { spawn = channel.spawn, name = 'spec', extension = false, skill = false, env = {}, }
177 - command.subcommands.command 'compact focus on edits'
179 - local prompts = {}
180 - for _, entry in ipairs( channel.commands ) do
181 - if entry.type == 'prompt' then table.insert( prompts, entry ) end
182 - end
183 - assert.equal( 1, #prompts )
184 - assert.equal( '/compact focus on edits', prompts[1].message )
185 - end )
187 - it( 'offers only the commands pi reports for this session', function()
173 + it( 'refuses a name pi does not offer, instead of saying it to the model', function()
188 174 local channel = fake.new()
189 175 local current = session.create { spawn = channel.spawn, name = 'spec', extension = false, skill = false, env = {}, }
191 - local offered
192 - vim.ui.select = function( items, _, on_choice )
193 - offered = items
194 - on_choice( items[1] )
195 - end
197 - command.subcommands.command ''
177 + command.subcommands.command 'model'
199 179 local request
200 180 for _, entry in ipairs( channel.commands ) do
203 183 assert.is_table( request )
185 + channel.frame {
186 + id = request.id,
187 + type = 'response',
188 + command = 'get_commands',
189 + success = true,
190 + data = { commands = { { name = 'compact', source = 'extension', }, }, },
192 + assert.is_true( channel.settle( function() return #notices > 0 end ) )
194 + -- Nothing was said to the model.
195 + for _, entry in ipairs( channel.commands ) do
196 + assert.is_not.equal( 'prompt', entry.type )
197 + end
198 + assert.equal( vim.log.levels.WARN, notices[#notices][2] )
199 + assert.is_true( notices[#notices][1]:find( 'model', 1, true ) ~= nil )
200 + end )
202 + it( 'runs a name pi does offer', function()
203 + local channel = fake.new()
204 + local current = session.create { spawn = channel.spawn, name = 'spec', extension = false, skill = false, env = {}, }
206 + command.subcommands.command 'compact focus on edits'
208 + local request
209 + for _, entry in ipairs( channel.commands ) do
210 + if entry.type == 'get_commands' then request = entry end
211 + end
212 + channel.frame {
213 + id = request.id,
214 + type = 'response',
215 + command = 'get_commands',
216 + success = true,
217 + data = { commands = { { name = 'compact', source = 'extension', }, }, },
220 + assert.is_true( channel.settle( function()
221 + for _, entry in ipairs( channel.commands ) do
222 + if entry.type == 'prompt' then return true end
223 + end
224 + return false
225 + end ) )
227 + local prompts = {}
228 + for _, entry in ipairs( channel.commands ) do
229 + if entry.type == 'prompt' then table.insert( prompts, entry ) end
230 + end
231 + assert.equal( 1, #prompts )
232 + assert.equal( '/compact focus on edits', prompts[1].message )
233 + end )
235 + it( 'filters the picker on the command name alone', function()
236 + local channel = fake.new()
237 + local current = session.create { spawn = channel.spawn, name = 'spec', extension = false, skill = false, env = {}, }
239 + local offered
240 + vim.ui.select = function( items, options, on_choice )
241 + offered = vim.tbl_map( options.format_item, items )
242 + on_choice( nil )
243 + end
245 + command.subcommands.command ''
247 + local request
248 + for _, entry in ipairs( channel.commands ) do
249 + if entry.type == 'get_commands' then request = entry end
250 + end
205 251 channel.frame {
206 252 id = request.id,
207 253 type = 'response',
208 254 command = 'get_commands',
209 255 success = true,
210 256 data = { commands = {
211 - { name = 'compact', description = 'Compact context', source = 'extension', },
212 - { name = 'skill:wiki', description = 'Wiki skill', source = 'skill', },
257 + { name = 'compact', description = 'Compact the conversation context', source = 'extension', },
216 261 assert.is_true( channel.settle( function() return offered ~= nil end ) )
217 - assert.equal( 2, #offered )
218 - assert.equal( 'compact', offered[1].name )
219 - assert.equal( 2, #current.commands )
262 + assert.same( { '/compact', }, offered )
222 265 it( 'reports when pi offers no commands', function()
288 + describe( 'completion', function()
289 + it( 'completes subcommands at the first word', function()
290 + command.setup()
291 + local offered = vim.fn.getcompletion( 'Pi ', 'cmdline' )
292 + assert.is_true( vim.list_contains( offered, 'command' ) )
293 + assert.is_true( vim.list_contains( offered, 'model' ) )
294 + end )
296 + it( 'completes the names pi actually offers after `command`', function()
297 + command.setup()
298 + local channel = fake.new()
299 + local current = session.create { spawn = channel.spawn, name = 'spec', extension = false, skill = false, env = {}, }
300 + current.commands = {
301 + { name = 'compact', source = 'extension', },
302 + { name = 'skill:wiki', source = 'skill', },
305 + local offered = vim.fn.getcompletion( 'Pi command ', 'cmdline' )
307 + assert.is_true( vim.list_contains( offered, 'compact' ) )
308 + assert.is_true( vim.list_contains( offered, 'skill:wiki' ) )
309 + end )
311 + it( 'completes follow modes', function()
312 + command.setup()
313 + local offered = vim.fn.getcompletion( 'Pi follow ', 'cmdline' )
315 + assert.is_true( vim.list_contains( offered, 'window' ) )
316 + assert.is_true( vim.list_contains( offered, 'tab' ) )
317 + end )
318 + end )
245 320 describe( 'follow', function()
246 321 it( 'toggles follow mode and reports it', function()
247 322 command.subcommands.follow ''

neovim/tests/bugabinga/pivi/rpc_spec.lua

Modified
165 165 assert.is_true( vim.wait( 2000, function() return exited end, 5 ) )
168 + it( 'reports a handler that throws instead of swallowing it', function()
169 + local channel = fake.new()
170 + local reported = {}
171 + start( channel, {
172 + on_event = function() error 'handler blew up' end,
173 + on_failure = function( failure ) table.insert( reported, failure ) end,
174 + } )
176 + channel.frame { type = 'agent_start', }
178 + assert.is_true( channel.settle( function() return #reported == 1 end ) )
179 + assert.is_true( reported[1]:find( 'handler blew up', 1, true ) ~= nil )
180 + end )
168 182 it( 'closes stdin first so the child can exit on its own', function()
169 183 local channel = fake.new()
170 184 local client = start( channel )