repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(neovim): turn the pivi transcript into a navigable conversation

23f719d6df0404f39cb3facb36128c3199976b6c

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

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (20)

neovim/.system/plans/NL-PLAN-LAJ8PGRK-deliver-pivi-transcript-presentation-and-navigat/index.md

Added
new file mode 100644
1 +---
2 +id: NL-PLAN-LAJ8PGRK
3 +type: plan
4 +title: Deliver pivi transcript presentation and navigation
5 +spec: NL-SPEC-GMWDCMJZ
6 +status: approved
7 +depends_on:
8 + - NL-PLAN-CEJPO_XF
9 +---
11 +# Deliver pivi transcript presentation and navigation
13 +## Intent
15 +Deliver the transcript presentation and navigation behavior of
16 +[NL-SPEC-GMWDCMJZ](../../specs/NL-SPEC-GMWDCMJZ-pivi-native-neovim-integration/index.md).
18 +[NL-PLAN-CEJPO_XF](../NL-PLAN-CEJPO_XF-deliver-pivi-native-neovim-integration/index.md) delivered a working transcript that appends plain text and mixes pivi's own framing into the content.
19 +This plan makes the transcript the conversation document it already is: addressable entries, message text as authored, rendered formatting, and ordinary motion.
21 +Placement continues from the earlier plan: `lua/bugabinga/pivi/`, with module-level testing rules in that directory's `AGENTS.md`.
23 +Measured constraint, not assumption: a transcript that reparses on every streamed delta costs roughly five hundred times a transcript that lets redraw drive parsing.
24 +Presentation must therefore never force parsing per delta.
26 +## Module map
28 +```mermaid
29 +flowchart TB
30 + session["bugabinga.pivi.session"] --> entry["bugabinga.pivi.entry"]
31 + entry --> transcript["bugabinga.pivi.transcript"]
32 + transcript --> render["bugabinga.pivi.render"]
33 + entry --> fold["bugabinga.pivi.fold"]
34 + entry --> navigate["bugabinga.pivi.navigate"]
35 + navigate --> follow["bugabinga.pivi.follow"]
36 + navigate --> detail["bugabinga.pivi.detail"]
37 + command["bugabinga.pivi.command"] --> navigate
38 + command --> fold
39 +```
41 +| Module | Responsibility |
42 +| --- | --- |
43 +| `bugabinga.pivi.entry` | Entry identity, kind, bounds, metadata, and lifecycle over a transcript buffer |
44 +| `bugabinga.pivi.transcript` | Buffer ownership; message text only, with framing delegated to presentation |
45 +| `bugabinga.pivi.render` | Formatting, embedded language presentation, and framing decoration |
46 +| `bugabinga.pivi.fold` | Collapsed and expanded presentation of an entry |
47 +| `bugabinga.pivi.navigate` | Motion between entries and tool calls, and reaching a referenced location |
48 +| `bugabinga.pivi.detail` | Detailed inspection of one entry without leaving the transcript |
49 +| `bugabinga.pivi.session` | Emits entries instead of writing framing text |
50 +| `bugabinga.pivi.command` | `:Pi` subcommands and `<Plug>` mappings for the new behavior |
52 +## Slice order
54 +```mermaid
55 +flowchart LR
56 + s1["1 Addressable entries"] --> s2["2 Pure message text"]
57 + s1 --> s3["3 Collapse and expand"]
58 + s2 --> s4["4 Rendered formatting"]
59 + s1 --> s5["5 Motion between entries"]
60 + s5 --> s6["6 Reach a referenced location"]
61 + s3 --> s7["7 Detailed inspection"]
62 +```
64 +| # | Observable behavior | Modules touched | Verified by |
65 +| --- | --- | --- | --- |
66 +| 1 | Every appended submission, answer, tool call, result, and error is an addressable entry with stable bounds that survive later appends | `bugabinga.pivi.entry`, `bugabinga.pivi.transcript`, `bugabinga.pivi.session` | Entry bounds spec under streaming appends and buffer growth |
67 +| 2 | Transcript text is the message as authored; copying an answer yields the original text without framing | `bugabinga.pivi.transcript`, `bugabinga.pivi.render`, `bugabinga.pivi.session` | Copy spec comparing buffer text with recorded message content |
68 +| 3 | A tool result arrives collapsed to one readable line and expands on request; entry state never alters session history | `bugabinga.pivi.fold`, `bugabinga.pivi.entry`, `bugabinga.pivi.command` | Collapse spec asserting unchanged session entries and message state |
69 +| 4 | Message formatting renders rather than showing markup, embedded code appears in its own language, and unknown formats stay plain | `bugabinga.pivi.render` | Rendering spec plus a streaming budget assertion over a recorded answer |
70 +| 5 | Ordinary motions move between entries and between tool calls in both directions | `bugabinga.pivi.navigate`, `bugabinga.pivi.command` | Motion spec over a replayed recorded run |
71 +| 6 | An entry that names a location navigates there, reusing the existing follow surfaces | `bugabinga.pivi.navigate`, `bugabinga.pivi.follow` | Navigation spec asserting the working window is preserved |
72 +| 7 | An entry can be inspected in full, including tool arguments and results, without leaving the transcript | `bugabinga.pivi.detail`, `bugabinga.pivi.entry` | Inspection spec asserting the transcript window and cursor are unchanged |
74 +Slice 1 is the walking skeleton; every later slice keeps the whole suite green before the next begins.
76 +## Critical paths
78 +- Entry bounds must be extmark-backed, because text appended later shifts every fixed line number.
79 +- Presentation is driven by redraw, never by a forced parse per streamed delta.
80 +- Framing lives in decoration, so the buffer stays copyable message text.
81 +- Collapsing is presentation only; the session transcript and Pi history are untouched.
82 +- Location navigation reuses follow-mode surfaces rather than introducing a second window policy.
84 +## Risks
86 +| Risk | Slice | Mitigation |
87 +| --- | --- | --- |
88 +| Entry bounds drift as the transcript grows | 1 | Extmark-backed ranges, asserted after appends and after edits to earlier entries |
89 +| Moving framing out of the text loses scannability | 2 | Replace it with decoration in the same slice, and assert both text purity and visible framing |
90 +| Rendering cost grows with a long answer | 4 | Assert a streaming budget over a recorded answer; keep parsing redraw-driven |
91 +| Collapsed entries hide errors the user needs | 3 | Collapse voluminous results only; keep errors expanded |
92 +| New mappings collide with user keys | 3, 5, 7 | Buffer-local mappings in pivi buffers plus `<Plug>` mappings; no global keys |

neovim/lua/bugabinga/pivi/AGENTS.md

Modified
68 68 Anything the agent's own operation triggers, including a write, must call `touch` again, or an unrelated plugin reacting to the write wipes the trail.
69 69 A revision recorded before the operation finishes is not enough; re-record after the event loop settles.
71 -18. **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.
71 +18. **The transcript buffer holds message text, nothing else.** Framing, labels, quoting and tool markers are extmark decoration in `bugabinga.pivi.render`.
72 + A spec must assert both that the framing is absent from the text and that it is present as decoration; otherwise the next change silently reintroduces prefixes.
74 +19. **Entry bounds are extmark-backed.** `bugabinga.pivi.entry` re-anchors on every write, and an entry ends at its last non-empty row because appending a complete line always leaves a trailing empty row behind.
75 + Never address an entry by a remembered line number.
77 +20. **Never force a parse per streamed delta.** Markdown rendering is `vim.treesitter.start` and stays redraw-driven; a forced parse per delta measured roughly five hundred times slower.
78 + Keep the streaming budget spec in `tests/bugabinga/pivi/editor/render_spec.lua`.
80 +21. **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.
73 82 ## Commands
104 113 | Headings | write `## pi` only when real text arrives, so a tool-only turn leaves no empty heading |
105 114 | Leading blank lines | models open with them; strip them before the first visible character |
106 115 | Rejections | name the command the user should type, not the internal state |
116 +| Transcript keys | buffer-local only, plus a `<Plug>` mapping; Neovim already ships `]m` and `[m` globally, so assert on mapping descriptions rather than on the absence of a key |
117 +| Folds | one fold per entry through `foldexpr`; appended text does not recompute folds until a redraw, so `apply` runs `zX` first and then drives folds from recorded intent |
108 119 ## Gotchas

neovim/lua/bugabinga/pivi/detail.lua

Added
new file mode 100644
1 +local entry = require 'bugabinga.pivi.entry'
2 +local transcript = require 'bugabinga.pivi.transcript'
3 +local navigate = require 'bugabinga.pivi.navigate'
5 +local MAX_RESULT_LINES = 60
7 +local TITLES = {
8 + prompt = 'submission',
9 + answer = 'answer',
10 + tool = 'tool call',
11 + error = 'error',
14 +--- One readable value.
15 +--- A JSON fence would be concealed by the markdown rendering of the float, so
16 +--- arguments are listed rather than encoded.
17 +local readable = function( value )
18 + if type( value ) == 'string' then return value end
19 + if type( value ) ~= 'table' then return tostring( value ) end
20 + return ( vim.inspect( value, { newline = ' ', indent = '', } ) )
21 +end
23 +local result_text = function( result )
24 + if type( result ) ~= 'table' then return nil end
25 + local pieces = {}
26 + for _, block in ipairs( result.content or {} ) do
27 + if block.type == 'text' and type( block.text ) == 'string' then
28 + table.insert( pieces, block.text )
29 + end
30 + end
31 + local joined = vim.trim( table.concat( pieces, '\n' ) )
32 + if joined == '' then return nil end
33 + return joined
34 +end
36 +--- Everything known about one entry, as readable text.
37 +local describe = function( bufnr, item )
38 + if not item then return {} end
39 + local meta = item.meta or {}
40 + local lines = { '# ' .. ( TITLES[item.kind] or item.kind ), '', }
42 + if meta.tool then table.insert( lines, '- tool: `' .. tostring( meta.tool ) .. '`' ) end
43 + if meta.failed ~= nil then
44 + table.insert( lines, '- outcome: ' .. ( meta.failed and 'failed' or 'succeeded' ) )
45 + end
47 + local location = navigate.location_of( item )
48 + if location then
49 + local place = location.path
50 + if location.line then place = place .. ':' .. tostring( location.line ) end
51 + table.insert( lines, '- location: `' .. place .. '`' )
52 + end
54 + table.insert( lines, '- lines: ' .. tostring( item.first ) .. '-' .. tostring( item.last ) )
56 + if type( meta.args ) == 'table' and next( meta.args ) ~= nil then
57 + table.insert( lines, '' )
58 + table.insert( lines, '## arguments' )
59 + table.insert( lines, '' )
61 + local names = vim.tbl_keys( meta.args )
62 + table.sort( names, function( left, right ) return tostring( left ) < tostring( right ) end )
63 + for _, name in ipairs( names ) do
64 + table.insert( lines, '- ' .. tostring( name ) .. ': ' .. readable( meta.args[name] ) )
65 + end
66 + end
68 + if meta.error then
69 + vim.list_extend( lines, { '', '## error', '', tostring( meta.error ), } )
70 + end
72 + local produced = result_text( meta.result )
73 + if produced then
74 + local shown = vim.split( produced, '\n', { plain = true, } )
75 + local truncated = #shown > MAX_RESULT_LINES
76 + if truncated then shown = vim.list_slice( shown, 1, MAX_RESULT_LINES ) end
77 + vim.list_extend( lines, { '', '## result', '', } )
78 + vim.list_extend( lines, shown )
79 + if truncated then
80 + table.insert( lines, '' )
81 + table.insert( lines, '_the entry itself holds the rest_' )
82 + end
83 + end
85 + if item.kind ~= 'tool' and not meta.error then
86 + local content = vim.api.nvim_buf_get_lines( bufnr, item.first - 1, item.last, false )
87 + vim.list_extend( lines, { '', '## text', '', } )
88 + vim.list_extend( lines, content )
89 + end
91 + return lines
92 +end
94 +--- Inspects the entry under the cursor without leaving the transcript.
95 +--- @return number? bufnr the floating buffer
96 +--- @return number? winid the floating window
97 +local inspect = function( bufnr, line_number )
98 + local item = entry.at( bufnr, line_number or vim.api.nvim_win_get_cursor( 0 )[1] )
99 + if not item then return nil end
101 + local lines = describe( bufnr, item )
102 + if #lines == 0 then return nil end
104 + local ok, float, window = pcall( vim.lsp.util.open_floating_preview, lines, 'markdown', {
105 + border = 'rounded',
106 + focus = false,
107 + focusable = true,
108 + max_width = 100,
109 + max_height = 30,
110 + } )
111 + if not ok then return nil end
112 + return float, window
113 +end
115 +local bind = function( bufnr )
116 + local action = function() inspect( bufnr ) end
117 + vim.keymap.set( 'n', 'K', action, { buffer = bufnr, desc = 'pivi inspect the entry', } )
118 + vim.keymap.set( 'n', '<Plug>(pivi-inspect-entry)', action,
119 + { buffer = bufnr, desc = 'pivi inspect the entry', } )
120 +end
122 +transcript.on_buffer( bind )
124 +return {
125 + describe = describe,
126 + inspect = inspect,
127 + bind = bind,

neovim/lua/bugabinga/pivi/entry.lua

Added
new file mode 100644
1 +local transcript = require 'bugabinga.pivi.transcript'
3 +local NAMESPACE = 'pivi-entry'
5 +local KINDS = {
6 + prompt = true,
7 + answer = true,
8 + tool = true,
9 + error = true,
12 +local records = {}
14 +local namespace = function()
15 + return vim.api.nvim_create_namespace( NAMESPACE )
16 +end
18 +local of_buffer = function( bufnr )
19 + records[bufnr] = records[bufnr] or {}
20 + return records[bufnr]
21 +end
23 +local last_row = function( bufnr )
24 + return vim.api.nvim_buf_line_count( bufnr ) - 1
25 +end
27 +local text_of = function( bufnr, row )
28 + return vim.api.nvim_buf_get_lines( bufnr, row, row + 1, false )[1] or ''
29 +end
31 +--- Anchors an entry's range to the buffer itself.
32 +--- Fixed line numbers would drift as later entries are appended, so bounds
33 +--- are extmark-backed and re-anchored whenever the entry grows.
34 +local anchor = function( bufnr, id, first, last )
35 + vim.api.nvim_buf_set_extmark( bufnr, namespace(), first, 0, {
36 + id = id,
37 + end_row = last,
38 + end_col = #text_of( bufnr, last ),
39 + right_gravity = false,
40 + end_right_gravity = true,
41 + } )
42 +end
44 +--- Starts an entry at the end of the transcript.
45 +--- An entry always begins on its own line, so its bounds never share a line
46 +--- with the entry before it.
47 +--- @param bufnr number
48 +--- @param kind string prompt, answer, tool, or error
49 +--- @param meta table? entry metadata such as tool, path, line, args
50 +--- @return number? id
51 +local open = function( bufnr, kind, meta )
52 + vim.validate( 'bufnr', bufnr, 'number' )
53 + vim.validate( 'kind', kind, 'string' )
54 + if not KINDS[kind] then error( 'unknown entry kind: ' .. kind ) end
55 + if not vim.api.nvim_buf_is_valid( bufnr ) then return nil end
57 + -- Virtual lines above the very first buffer line are not shown once that
58 + -- line is at the top of a window, so an entry never starts on line one.
59 + if last_row( bufnr ) == 0 or text_of( bufnr, last_row( bufnr ) ) ~= '' then
60 + transcript.line( bufnr, '' )
61 + end
63 + local row = last_row( bufnr )
64 + local id = vim.api.nvim_buf_set_extmark( bufnr, namespace(), row, 0, {
65 + end_row = row,
66 + end_col = 0,
67 + right_gravity = false,
68 + end_right_gravity = true,
69 + } )
71 + of_buffer( bufnr )[id] = { id = id, kind = kind, meta = meta or {}, }
72 + return id
73 +end
75 +local start_row = function( bufnr, id )
76 + local mark = vim.api.nvim_buf_get_extmark_by_id( bufnr, namespace(), id, {} )
77 + if not mark or #mark == 0 then return nil end
78 + return mark[1]
79 +end
81 +--- The last row an entry actually shows.
82 +--- Appending a complete line leaves an empty row behind it, and that row is
83 +--- where the next entry starts rather than part of this one.
84 +local content_row = function( bufnr, first )
85 + local row = last_row( bufnr )
86 + while row > first and text_of( bufnr, row ) == '' do row = row - 1 end
87 + return row
88 +end
90 +--- Re-anchors an entry to everything written since it opened.
91 +local extend = function( bufnr, id )
92 + if not id or not vim.api.nvim_buf_is_valid( bufnr ) then return end
93 + if not of_buffer( bufnr )[id] then return end
94 + local first = start_row( bufnr, id )
95 + if not first then return end
96 + anchor( bufnr, id, first, content_row( bufnr, first ) )
97 +end
99 +--- Appends text to an open entry, continuing its final line.
100 +local write = function( bufnr, id, text )
101 + if not id then return end
102 + transcript.append( bufnr, text )
103 + extend( bufnr, id )
104 +end
106 +--- Appends one complete line to an open entry.
107 +local line = function( bufnr, id, text )
108 + if not id then return end
109 + transcript.line( bufnr, text )
110 + extend( bufnr, id )
111 +end
113 +local close = function( bufnr, id )
114 + extend( bufnr, id )
115 + return nil
116 +end
118 +--- @return number? first 1-based first line
119 +--- @return number? last 1-based last line
120 +local range = function( bufnr, id )
121 + if not vim.api.nvim_buf_is_valid( bufnr ) then return nil end
122 + local mark = vim.api.nvim_buf_get_extmark_by_id( bufnr, namespace(), id, { details = true, } )
123 + if not mark or #mark == 0 then return nil end
124 + local details = mark[3] or {}
125 + return mark[1] + 1, ( details.end_row or mark[1] ) + 1
126 +end
128 +local describe = function( bufnr, record )
129 + local first, last = range( bufnr, record.id )
130 + if not first then return nil end
131 + return {
132 + id = record.id,
133 + kind = record.kind,
134 + meta = record.meta,
135 + first = first,
136 + last = last,
138 +end
140 +--- Every entry of a transcript, in buffer order.
141 +local list = function( bufnr )
142 + if not vim.api.nvim_buf_is_valid( bufnr ) then return {} end
143 + local found = {}
144 + for _, record in pairs( of_buffer( bufnr ) ) do
145 + local described = describe( bufnr, record )
146 + if described then table.insert( found, described ) end
147 + end
148 + table.sort( found, function( left, right ) return left.first < right.first end )
149 + return found
150 +end
152 +--- The entry covering a 1-based line, if any.
153 +local at = function( bufnr, line_number )
154 + local found
155 + for _, entry in ipairs( list( bufnr ) ) do
156 + if entry.first <= line_number and line_number <= entry.last then found = entry end
157 + end
158 + return found
159 +end
161 +local meta = function( bufnr, id )
162 + local record = of_buffer( bufnr )[id]
163 + return record and record.meta or nil
164 +end
166 +--- Replaces an entry's metadata fields, leaving its text untouched.
167 +local annotate = function( bufnr, id, fields )
168 + local record = of_buffer( bufnr )[id]
169 + if not record then return end
170 + record.meta = vim.tbl_extend( 'force', record.meta, fields or {} )
171 +end
173 +local clear = function( bufnr )
174 + records[bufnr] = nil
175 + if vim.api.nvim_buf_is_valid( bufnr ) then
176 + vim.api.nvim_buf_clear_namespace( bufnr, namespace(), 0, -1 )
177 + end
178 +end
180 +transcript.on_reset( clear )
182 +return {
183 + namespace = namespace,
184 + kinds = KINDS,
185 + open = open,
186 + write = write,
187 + line = line,
188 + extend = extend,
189 + close = close,
190 + range = range,
191 + list = list,
192 + at = at,
193 + meta = meta,
194 + annotate = annotate,
195 + clear = clear,

neovim/lua/bugabinga/pivi/fold.lua

Added
new file mode 100644
1 +local entry = require 'bugabinga.pivi.entry'
2 +local transcript = require 'bugabinga.pivi.transcript'
4 +local EXPRESSION = "v:lua.require'bugabinga.pivi.fold'.expr()"
5 +local TEXT = "v:lua.require'bugabinga.pivi.fold'.text()"
7 +local ICONS = {
8 + prompt = '\u{258c}',
9 + answer = '\u{258c}',
10 + tool = '\u{25b8}',
11 + error = '!',
14 +local levels = {}
15 +local wanted = {}
17 +--- An entry a reader rarely wants in full on arrival.
18 +--- Tool output is voluminous and secondary; a question, an answer, and an
19 +--- error are the conversation itself and stay open.
20 +local voluminous = function( item )
21 + return item.kind == 'tool' and item.last > item.first
22 +end
24 +local folds_of = function( bufnr )
25 + local tick = vim.api.nvim_buf_get_changedtick( bufnr )
26 + local cached = levels[bufnr]
27 + if cached and cached.tick == tick then return cached.rows end
29 + local rows = {}
30 + for _, item in ipairs( entry.list( bufnr ) ) do
31 + if item.last > item.first then
32 + rows[item.first] = '>1'
33 + for row = item.first + 1, item.last do rows[row] = '1' end
34 + end
35 + end
37 + levels[bufnr] = { tick = tick, rows = rows, }
38 + return rows
39 +end
41 +--- Fold level of the line being evaluated.
42 +--- Levels come from entry bounds, so a fold is always exactly one entry.
43 +local expr = function()
44 + local bufnr = vim.api.nvim_get_current_buf()
45 + return folds_of( bufnr )[vim.v.lnum] or '0'
46 +end
48 +--- One readable line standing in for a collapsed entry.
49 +local text = function()
50 + local bufnr = vim.api.nvim_get_current_buf()
51 + local first = vim.v.foldstart or 1
52 + local item = entry.at( bufnr, first )
53 + local headline = vim.api.nvim_buf_get_lines( bufnr, first - 1, first, false )[1] or ''
54 + local icon = item and ICONS[item.kind] or '\u{25b8}'
55 + return ( '%s %s (%d more lines)' ):format( icon, vim.trim( headline ), vim.v.foldend - first )
56 +end
58 +local windows_of = function( bufnr )
59 + local found = {}
60 + for _, win in ipairs( vim.api.nvim_list_wins() ) do
61 + if vim.api.nvim_win_get_buf( win ) == bufnr then table.insert( found, win ) end
62 + end
63 + return found
64 +end
66 +--- Turns a window into an entry-folding view of a transcript.
67 +local attach = function( win )
68 + vim.wo[win].foldmethod = 'expr'
69 + vim.wo[win].foldexpr = EXPRESSION
70 + vim.wo[win].foldtext = TEXT
71 + vim.wo[win].foldenable = true
72 + vim.wo[win].foldlevel = 99
73 + vim.wo[win].foldcolumn = '0'
74 + vim.wo[win].fillchars = 'eob: ,fold: '
75 +end
77 +--- Whether an entry should currently be collapsed.
78 +--- A reader's choice wins; otherwise only voluminous entries start collapsed.
79 +local collapsed = function( bufnr, item )
80 + local choice = ( wanted[bufnr] or {} )[item.id]
81 + if choice ~= nil then return choice end
82 + return voluminous( item )
83 +end
85 +--- Brings every window on a transcript to the intended fold state.
86 +--- Text appended to a buffer does not recompute folds until a redraw, so the
87 +--- folds are recomputed first and then driven from intent rather than from
88 +--- whatever state a window happened to be left in.
89 +local apply = function( bufnr )
90 + if not vim.api.nvim_buf_is_valid( bufnr ) then return end
91 + local wins = windows_of( bufnr )
92 + if #wins == 0 then return end
94 + local items = entry.list( bufnr )
95 + for _, win in ipairs( wins ) do
96 + pcall( vim.api.nvim_win_call, win, function()
97 + vim.cmd 'normal! zX'
98 + for _, item in ipairs( items ) do
99 + if item.last > item.first and collapsed( bufnr, item ) then
100 + pcall( function() vim.cmd( tostring( item.first ) .. 'foldclose' ) end )
101 + end
102 + end
103 + end )
104 + end
105 +end
107 +--- Collapses an expanded entry, or expands a collapsed one.
108 +local toggle = function( bufnr, line_number )
109 + local item = entry.at( bufnr, line_number )
110 + if not item or item.last == item.first then return false end
112 + wanted[bufnr] = wanted[bufnr] or {}
113 + local target = not collapsed( bufnr, item )
114 + wanted[bufnr][item.id] = target
116 + local command = target and 'foldclose' or 'foldopen'
117 + return ( pcall( function() vim.cmd( tostring( item.first ) .. command ) end ) )
118 +end
120 +local closed = function( line_number )
121 + return vim.fn.foldclosed( line_number ) ~= -1
122 +end
124 +local reset = function( bufnr )
125 + levels[bufnr] = nil
126 + wanted[bufnr] = nil
127 +end
129 +--- Buffer-local keys only: pivi never claims a key outside its own buffers.
130 +local bind = function( bufnr )
131 + vim.keymap.set( 'n', '<CR>', function()
132 + toggle( bufnr, vim.api.nvim_win_get_cursor( 0 )[1] )
133 + end, { buffer = bufnr, desc = 'pivi collapse or expand the entry', } )
135 + vim.keymap.set( 'n', '<Plug>(pivi-toggle-entry)', function()
136 + toggle( bufnr, vim.api.nvim_win_get_cursor( 0 )[1] )
137 + end, { buffer = bufnr, desc = 'pivi collapse or expand the entry', } )
138 +end
140 +transcript.on_reset( reset )
141 +transcript.on_window( attach )
142 +transcript.on_buffer( bind )
144 +return {
145 + expression = EXPRESSION,
146 + fold_text = TEXT,
147 + voluminous = voluminous,
148 + collapsed = collapsed,
149 + expr = expr,
150 + text = text,
151 + attach = attach,
152 + apply = apply,
153 + toggle = toggle,
154 + closed = closed,
155 + reset = reset,

neovim/lua/bugabinga/pivi/follow.lua

Modified
98 98 return target
101 +--- Navigates to a location because the user asked, not because follow mode is on.
102 +--- The same surfaces are reused, so there is only ever one window policy.
103 +local visit = function( location )
104 + local chosen = state.mode
105 + if not chosen then state.mode = 'window' end
106 + local target = to( location )
107 + state.mode = chosen
108 + return target
109 +end
101 111 --- Returns to the location held before following began.
102 112 local back = function()
103 113 local origin = state.origin
135 145 set = set,
136 146 toggle = toggle,
137 147 to = to,
148 + visit = visit,
138 149 back = back,
139 150 reset = reset,
140 151 window = window,

neovim/lua/bugabinga/pivi/help.lua

Modified
2 2 local rpc = require 'bugabinga.pivi.rpc'
3 3 local transcript = require 'bugabinga.pivi.transcript'
4 4 local context = require 'bugabinga.pivi.context'
5 5 local runtime = require 'bugabinga.pivi.runtime'
6 +local entry = require 'bugabinga.pivi.entry'
7 +local render = require 'bugabinga.pivi.render'
7 9 local BUFFER_NAME = 'help'
74 76 local bufnr = transcript.ensure( BUFFER_NAME )
75 77 transcript.clear( bufnr )
76 - transcript.line( bufnr, '? ' .. question )
77 - transcript.line( bufnr, '' )
79 - local run = { buffer = bufnr, done = false, answer = '', answering = false, }
79 + local run = { buffer = bufnr, done = false, answer = '', }
81 + --- Entries here follow the transcript contract: the buffer holds the text as
82 + --- authored, and everything a reader sees around it is decoration.
83 + local begin = function( kind, meta )
84 + if run.entry then entry.close( bufnr, run.entry ) end
85 + run.entry = entry.open( bufnr, kind, meta )
86 + run.kind = kind
87 + render.decorate( bufnr, run.entry )
88 + return run.entry
89 + end
91 + local seal = function()
92 + if not run.entry then return end
93 + entry.close( bufnr, run.entry )
94 + render.decorate( bufnr, run.entry )
95 + run.entry = nil
96 + run.kind = nil
97 + end
99 + begin( 'prompt', { text = question, } )
100 + for _, piece in ipairs( vim.split( question, '\n', { plain = true, } ) ) do
101 + entry.line( bufnr, run.entry, piece )
102 + end
103 + seal()
81 105 -- The window must exist before its state can be shown.
82 106 transcript.show( bufnr )
94 118 local delta = event.assistantMessageEvent or {}
95 119 if delta.type == 'text_delta' and ( delta.delta or '' ) ~= '' then
96 120 local text = delta.delta
97 - if not run.answering then
121 + if run.kind ~= 'answer' then
98 122 text = text:gsub( '^%s*\n', '' )
99 123 if text ~= '' then
100 - run.answering = true
124 + begin( 'answer', {} )
101 125 state 'answering'
102 - transcript.line( bufnr, '' )
105 128 if text ~= '' then
106 129 run.answer = run.answer .. text
107 - transcript.append( bufnr, text )
130 + entry.write( bufnr, run.entry, text )
110 133 elseif event.type == 'tool_execution_start' then
111 - run.answering = false
112 134 local subject = type( event.args ) == 'table' and ( event.args.tag or event.args.path or event.args.pattern ) or nil
113 135 local described = tostring( event.toolName )
114 136 if type( subject ) == 'string' and subject ~= '' then described = described .. ' ' .. subject end
115 137 state( described )
116 - transcript.line( bufnr, '' )
117 - transcript.line( bufnr, ' ▸ ' .. described )
138 + begin( 'tool', { tool = event.toolName, args = event.args, call = event.toolCallId, } )
139 + entry.line( bufnr, run.entry, described )
140 + seal()
118 141 elseif event.type == 'message_end' and event.message and event.message.errorMessage then
119 - transcript.line( bufnr, '' )
120 - transcript.line( bufnr, ' ! ' .. tostring( event.message.errorMessage ) )
142 + begin( 'error', { error = event.message.errorMessage, } )
143 + entry.line( bufnr, run.entry, tostring( event.message.errorMessage ) )
144 + seal()
121 145 elseif event.type == 'agent_settled' then
122 146 run.done = true
147 + seal()
123 148 state( nil )
124 149 run.client.stop()
125 150 if options.on_done then options.on_done( run ) end
128 153 on_exit = function()
129 154 run.done = true
155 + seal()
130 156 state( nil )
134 160 run.client.request( { type = 'prompt', message = compose( question, options ), }, function( response )
135 161 if response.success then return end
136 - transcript.line( bufnr, ' ! ' .. tostring( response.error or 'help run rejected' ) )
162 + begin( 'error', { error = response.error, } )
163 + entry.line( bufnr, run.entry, tostring( response.error or 'help run rejected' ) )
164 + seal()
137 165 run.done = true
138 166 state( nil )
139 167 run.client.stop()

neovim/lua/bugabinga/pivi/init.lua

Modified
12 12 local bridge = require 'bugabinga.pivi.bridge'
13 13 local docs = require 'bugabinga.pivi.docs'
14 14 local runtime = require 'bugabinga.pivi.runtime'
15 +local entry = require 'bugabinga.pivi.entry'
16 +local render = require 'bugabinga.pivi.render'
17 +local fold = require 'bugabinga.pivi.fold'
18 +local navigate = require 'bugabinga.pivi.navigate'
19 +local detail = require 'bugabinga.pivi.detail'
16 21 --- Reads cached status only. Safe to call from a statusline.
17 22 local summary = function( name ) return publish.summary( name ) end
39 44 bridge = bridge,
40 45 docs = docs,
41 46 runtime = runtime,
47 + entry = entry,
48 + render = render,
49 + fold = fold,
50 + navigate = navigate,
51 + detail = detail,
42 52 summary = summary,
43 53 reset = reset,

neovim/lua/bugabinga/pivi/navigate.lua

Added
new file mode 100644
1 +local entry = require 'bugabinga.pivi.entry'
2 +local transcript = require 'bugabinga.pivi.transcript'
3 +local follow = require 'bugabinga.pivi.follow'
5 +local matching = function( bufnr, kind )
6 + local found = {}
7 + for _, item in ipairs( entry.list( bufnr ) ) do
8 + if not kind or item.kind == kind then table.insert( found, item ) end
9 + end
10 + return found
11 +end
13 +--- The next entry strictly after a line, or the first one wrapping around.
14 +local after = function( bufnr, line_number, kind )
15 + local items = matching( bufnr, kind )
16 + for _, item in ipairs( items ) do
17 + if item.first > line_number then return item end
18 + end
19 + return items[1]
20 +end
22 +--- The previous entry strictly before a line, or the last one wrapping around.
23 +local before = function( bufnr, line_number, kind )
24 + local items = matching( bufnr, kind )
25 + local found
26 + for _, item in ipairs( items ) do
27 + if item.first < line_number then found = item end
28 + end
29 + return found or items[#items]
30 +end
32 +--- Moves the cursor to an entry, leaving the window and the buffer alone.
33 +local go = function( item )
34 + if not item then return nil end
35 + pcall( vim.api.nvim_win_set_cursor, 0, { item.first, 0, } )
36 + return item
37 +end
39 +local next_entry = function( bufnr, kind )
40 + return go( after( bufnr, vim.api.nvim_win_get_cursor( 0 )[1], kind ) )
41 +end
43 +local previous_entry = function( bufnr, kind )
44 + return go( before( bufnr, vim.api.nvim_win_get_cursor( 0 )[1], kind ) )
45 +end
47 +--- The place an entry names, if it names one.
48 +local location_of = function( item )
49 + if not item then return nil end
50 + local meta = item.meta or {}
51 + local args = type( meta.args ) == 'table' and meta.args or {}
52 + local path = meta.path or args.path or args.file_path or args.filePath
53 + if type( path ) ~= 'string' or path == '' then return nil end
55 + if meta.cwd and not vim.startswith( path, '/' ) and not path:match '^%a:' then
56 + path = vim.fs.normalize( meta.cwd .. '/' .. path )
57 + end
59 + return { path = path, line = meta.line or args.line or args.lnum, }
60 +end
62 +--- Reaches the place the entry under the cursor names.
63 +--- Navigation reuses follow mode's surfaces, so the working window is kept.
64 +local locate = function( bufnr, line_number )
65 + local item = entry.at( bufnr, line_number or vim.api.nvim_win_get_cursor( 0 )[1] )
66 + local location = location_of( item )
67 + if not location then return nil end
68 + return follow.visit( location )
69 +end
71 +--- Buffer-local motions only: a transcript never claims a key elsewhere.
72 +local bind = function( bufnr )
73 + local motions = {
74 + { keys = ']m', plug = '<Plug>(pivi-next-entry)', kind = nil, move = next_entry,
75 + description = 'pivi next entry', },
76 + { keys = '[m', plug = '<Plug>(pivi-previous-entry)', kind = nil, move = previous_entry,
77 + description = 'pivi previous entry', },
78 + { keys = ']t', plug = '<Plug>(pivi-next-tool)', kind = 'tool', move = next_entry,
79 + description = 'pivi next tool call', },
80 + { keys = '[t', plug = '<Plug>(pivi-previous-tool)', kind = 'tool', move = previous_entry,
81 + description = 'pivi previous tool call', },
82 + }
84 + for _, motion in ipairs( motions ) do
85 + local action = function() motion.move( bufnr, motion.kind ) end
86 + vim.keymap.set( 'n', motion.keys, action, { buffer = bufnr, desc = motion.description, } )
87 + vim.keymap.set( 'n', motion.plug, action, { buffer = bufnr, desc = motion.description, } )
88 + end
90 + local visit = function() locate( bufnr ) end
91 + vim.keymap.set( 'n', 'gf', visit, { buffer = bufnr, desc = 'pivi go to the location of the entry', } )
92 + vim.keymap.set( 'n', '<Plug>(pivi-goto-location)', visit,
93 + { buffer = bufnr, desc = 'pivi go to the location of the entry', } )
94 +end
96 +transcript.on_buffer( bind )
98 +return {
99 + location_of = location_of,
100 + locate = locate,
101 + after = after,
102 + before = before,
103 + next_entry = next_entry,
104 + previous_entry = previous_entry,
105 + bind = bind,

neovim/lua/bugabinga/pivi/render.lua

Added
new file mode 100644
1 +local entry = require 'bugabinga.pivi.entry'
2 +local transcript = require 'bugabinga.pivi.transcript'
4 +local NAMESPACE = 'pivi-render'
6 +--- How each entry kind is framed.
7 +--- Framing is decoration, never buffer text, so the transcript keeps the
8 +--- message exactly as it was authored and copying it stays faithful.
9 +local FRAMES = {
10 + prompt = { label = 'you', label_hl = 'Title', prefix = '\u{258c} ', prefix_hl = 'Identifier', every_line = true, },
11 + answer = { label = 'pi', label_hl = 'Title', },
12 + tool = { prefix = '\u{25b8} ', prefix_hl = 'Function', line_hl = 'Comment', },
13 + error = { prefix = '! ', prefix_hl = 'ErrorMsg', },
16 +local namespace = function()
17 + return vim.api.nvim_create_namespace( NAMESPACE )
18 +end
20 +local clear_range = function( bufnr, first, last )
21 + vim.api.nvim_buf_clear_namespace( bufnr, namespace(), first - 1, last )
22 +end
24 +--- Decorates one entry.
25 +--- Only the entry's own lines are touched, so decoration cost stays
26 +--- proportional to the entry rather than to the whole transcript.
27 +local decorate = function( bufnr, id )
28 + if not vim.api.nvim_buf_is_valid( bufnr ) then return end
30 + local item = nil
31 + for _, candidate in ipairs( entry.list( bufnr ) ) do
32 + if candidate.id == id then item = candidate end
33 + end
34 + if not item then return end
36 + local frame = FRAMES[item.kind]
37 + if not frame then return end
39 + clear_range( bufnr, item.first, item.last )
41 + if frame.label then
42 + pcall( vim.api.nvim_buf_set_extmark, bufnr, namespace(), item.first - 1, 0, {
43 + virt_lines = { { { frame.label, frame.label_hl, }, }, },
44 + virt_lines_above = true,
45 + right_gravity = false,
46 + priority = 60,
47 + } )
48 + end
50 + if not frame.prefix and not frame.line_hl then return end
52 + for row = item.first - 1, item.last - 1 do
53 + local heads = row == item.first - 1
54 + local prefix = frame.prefix and ( heads or frame.every_line ) and frame.prefix or nil
55 + pcall( vim.api.nvim_buf_set_extmark, bufnr, namespace(), row, 0, {
56 + virt_text = prefix and { { prefix, frame.prefix_hl, }, } or nil,
57 + virt_text_pos = prefix and 'inline' or nil,
58 + line_hl_group = frame.line_hl,
59 + right_gravity = false,
60 + priority = 60,
61 + } )
62 + end
63 +end
65 +--- Renders message formatting, and presents embedded code in its own language.
66 +--- Parsing is driven by redraw, never forced per streamed delta, because
67 +--- forcing it costs orders of magnitude more on a growing transcript.
68 +local highlight = function( bufnr, language )
69 + if not vim.api.nvim_buf_is_valid( bufnr ) then return false end
70 + return ( pcall( vim.treesitter.start, bufnr, language or 'markdown' ) )
71 +end
73 +--- Whether formatting is being rendered rather than shown as markup.
74 +local highlighted = function( bufnr )
75 + local active = vim.treesitter.highlighter.active or {}
76 + return active[bufnr] ~= nil
77 +end
79 +local conceal = function( win )
80 + vim.wo[win].conceallevel = 2
81 + vim.wo[win].concealcursor = 'nc'
82 +end
84 +local marks = function( bufnr )
85 + if not vim.api.nvim_buf_is_valid( bufnr ) then return {} end
86 + return vim.api.nvim_buf_get_extmarks( bufnr, namespace(), 0, -1, { details = true, } )
87 +end
89 +local clear = function( bufnr )
90 + if not vim.api.nvim_buf_is_valid( bufnr ) then return end
91 + vim.api.nvim_buf_clear_namespace( bufnr, namespace(), 0, -1 )
92 +end
94 +transcript.on_reset( clear )
95 +transcript.on_buffer( highlight )
96 +transcript.on_window( conceal )
98 +return {
99 + namespace = namespace,
100 + frames = FRAMES,
101 + decorate = decorate,
102 + highlight = highlight,
103 + highlighted = highlighted,
104 + conceal = conceal,
105 + marks = marks,
106 + clear = clear,

neovim/lua/bugabinga/pivi/session.lua

Modified
10 10 local follow = require 'bugabinga.pivi.follow'
11 11 local edit = require 'bugabinga.pivi.edit'
12 12 local runtime = require 'bugabinga.pivi.runtime'
13 +local entry = require 'bugabinga.pivi.entry'
14 +local render = require 'bugabinga.pivi.render'
15 +local fold = require 'bugabinga.pivi.fold'
14 17 local registry = {}
15 18 local counter = 0
92 95 return tostring( name ) .. ' ' .. subject
95 ---- Writes the assistant heading only once real text arrives, so a turn that
96 ---- only calls tools never leaves an empty heading behind.
97 -local open_answer = function( self )
98 - if self.answering then return end
99 - self.answering = true
100 - transcript.line( self.buffer, '' )
101 - transcript.line( self.buffer, '## pi' )
98 +--- The text a tool produced, as the tool produced it.
99 +local result_text = function( result )
100 + if type( result ) ~= 'table' then return nil end
101 + local pieces = {}
102 + for _, block in ipairs( result.content or {} ) do
103 + if block.type == 'text' and type( block.text ) == 'string' then
104 + table.insert( pieces, block.text )
105 + end
106 + end
107 + local joined = vim.trim( table.concat( pieces, '\n' ) )
108 + if joined == '' then return nil end
109 + return joined
110 +end
112 +--- Ends the entry currently being written, if any.
113 +local seal = function( self )
114 + if not self.entry then return end
115 + entry.close( self.buffer, self.entry )
116 + render.decorate( self.buffer, self.entry )
117 + fold.apply( self.buffer )
118 + self.entry = nil
119 + self.entry_kind = nil
120 +end
122 +local begin = function( self, kind, meta )
123 + seal( self )
124 + self.entry = entry.open( self.buffer, kind, meta )
125 + self.entry_kind = kind
126 + render.decorate( self.buffer, self.entry )
127 + return self.entry
128 +end
130 +--- Records a submission as its own entry.
131 +--- The submitted text is written exactly as typed; the quoting a reader sees
132 +--- is decoration.
133 +local render_submission = function( self, text )
134 + begin( self, 'prompt', { text = text, } )
135 + for _, piece in ipairs( vim.split( text, '\n', { plain = true, } ) ) do
136 + entry.line( self.buffer, self.entry, piece )
137 + end
138 + seal( self )
104 141 local render_event = function( self, event )
105 142 local bufnr = self.buffer
107 144 if event.type == 'message_start' and event.message and event.message.role == 'assistant' then
108 - self.answering = false
145 + seal( self )
109 146 elseif event.type == 'message_update' then
110 147 local delta = event.assistantMessageEvent or {}
111 148 if delta.type == 'text_delta' and ( delta.delta or '' ) ~= '' then
112 149 local text = delta.delta
113 - -- Models often open with blank lines; the heading already separates turns.
114 - if not self.answering then text = text:gsub( '^%s*\n', '' ) end
150 + -- Models often open with blank lines; the entry already separates turns.
151 + if self.entry_kind ~= 'answer' then text = text:gsub( '^%s*\n', '' ) end
115 152 if text ~= '' then
116 - open_answer( self )
117 - transcript.append( bufnr, text )
153 + if self.entry_kind ~= 'answer' then begin( self, 'answer', {} ) end
154 + entry.write( bufnr, self.entry, text )
120 157 elseif event.type == 'tool_execution_start' then
121 - self.answering = false
122 - transcript.line( bufnr, '' )
123 - transcript.line( bufnr, ' ▸ ' .. describe_tool( self, event.toolName, event.args ) )
124 - elseif event.type == 'tool_execution_end' and event.isError then
125 - transcript.line( bufnr, ' failed' )
158 + begin( self, 'tool', {
159 + tool = event.toolName,
160 + args = event.args,
161 + call = event.toolCallId,
162 + cwd = self.cwd,
163 + } )
164 + entry.line( bufnr, self.entry, describe_tool( self, event.toolName, event.args ) )
165 + elseif event.type == 'tool_execution_end' then
166 + if self.entry_kind == 'tool' then
167 + entry.annotate( bufnr, self.entry, { result = event.result, failed = event.isError == true, } )
168 + local produced = result_text( event.result )
169 + if produced then
170 + for _, piece in ipairs( vim.split( produced, '\n', { plain = true, } ) ) do
171 + entry.line( bufnr, self.entry, piece )
172 + end
173 + elseif event.isError then
174 + entry.line( bufnr, self.entry, 'failed' )
175 + end
176 + seal( self )
177 + end
126 178 elseif event.type == 'agent_settled' then
127 - self.answering = false
128 - transcript.line( bufnr, '' )
179 + seal( self )
129 180 elseif event.type == 'message_end' and event.message and event.message.errorMessage then
130 - self.answering = false
131 - transcript.line( bufnr, '' )
132 - transcript.line( bufnr, ' ! ' .. tostring( event.message.errorMessage ) )
181 + begin( self, 'error', { error = event.message.errorMessage, } )
182 + entry.line( bufnr, self.entry, tostring( event.message.errorMessage ) )
183 + seal( self )
297 348 message = context.render( snapshot ) .. text
300 - self.answering = false
301 - transcript.line( self.buffer, '' )
302 - for _, line in ipairs( vim.split( text, '\n', { plain = true, } ) ) do
303 - transcript.line( self.buffer, '> ' .. line )
304 - end
351 + render_submission( self, text )
306 353 local command
307 354 if delivery == 'steer' then
317 364 self.client.request( command, function( response )
318 365 if response.success then return end
319 - transcript.line( self.buffer, ' ! ' .. tostring( response.error or 'submission rejected' ) )
366 + local rejected = begin( self, 'error', { error = response.error, } )
367 + entry.line( self.buffer, rejected, tostring( response.error or 'submission rejected' ) )
368 + seal( self )
320 369 self.status = vim.tbl_extend( 'force', self.status, { error = response.error, } )
321 370 publish_status( self )

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

Modified
34 34 - Follow mode is optional. It tracks located events in a dedicated window or tab and never steals the working window.
35 35 - State is published through a native variable and `User` events; consumers read the cache and never trigger RPC.
37 +## The transcript
39 +A transcript is the conversation itself, presented as the document it already is.
41 +- Buffer text is the message as authored. Framing, labels and quoting are decoration, so copying an answer yields the original markdown.
42 +- Markdown is rendered rather than shown as markup, and fenced code is presented in its own language.
43 +- Every submission, answer, tool call and error is an entry with its own bounds and metadata.
44 +- Tool output lands inside its tool entry and arrives collapsed; the conversation itself stays open.
46 +| Key in a transcript | Effect |
47 +| --- | --- |
48 +| `<CR>` | collapse or expand the entry under the cursor |
49 +| `]m` / `[m` | move to the next or previous entry |
50 +| `]t` / `[t` | move to the next or previous tool call |
51 +| `gf` | go to the location the entry names |
52 +| `K` | inspect the entry, including tool arguments and result |
53 +| `zR` / `zM` | expand or collapse everything, as in any buffer |
55 +These keys are buffer-local, and `<Plug>(pivi-toggle-entry)`, `<Plug>(pivi-next-entry)`, `<Plug>(pivi-previous-entry)`, `<Plug>(pivi-next-tool)`, `<Plug>(pivi-previous-tool)`, `<Plug>(pivi-goto-location)` and `<Plug>(pivi-inspect-entry)` exist alongside them.
37 57 ## Surfaces the user has
39 59 | Command | Effect |

neovim/lua/bugabinga/pivi/transcript.lua

Modified
1 1 local FILETYPE = 'pivi'
3 3 local buffers = {}
4 +local setups = {}
6 +--- Registers a listener for a transcript buffer being created.
7 +--- Buffer-local behavior belongs to the layers that define it, so no global
8 +--- mapping is ever claimed on the user's behalf.
9 +local on_buffer = function( listener )
10 + table.insert( setups, listener )
11 +end
5 13 local configure = function( bufnr, name )
6 14 vim.bo[bufnr].buftype = 'nofile'
9 17 vim.bo[bufnr].filetype = FILETYPE
10 18 vim.bo[bufnr].modifiable = false
11 19 pcall( vim.api.nvim_buf_set_name, bufnr, 'pivi://' .. name )
20 + for _, listener in ipairs( setups ) do
21 + pcall( listener, bufnr )
22 + end
14 25 --- Returns the transcript buffer for a name, creating it when missing.
22 33 return bufnr
36 +local resets = {}
38 +--- Registers a listener for transcript content being discarded.
39 +--- Presentation layers own state keyed by buffer, and that state is only
40 +--- correct while the text it describes still exists.
41 +local on_reset = function( listener )
42 + table.insert( resets, listener )
43 +end
45 +local announce_reset = function( bufnr )
46 + for _, listener in ipairs( resets ) do
47 + pcall( listener, bufnr )
48 + end
49 +end
51 +local windows = {}
53 +--- Registers a listener for a window becoming a pivi window.
54 +--- Window-local presentation belongs to the layers that define it, not to the
55 +--- buffer that happens to be shown.
56 +local on_window = function( listener )
57 + table.insert( windows, listener )
58 +end
25 60 local writable = function( bufnr, action )
26 61 vim.bo[bufnr].modifiable = true
27 62 local ok, failure = pcall( action )
74 109 local clear = function( bufnr )
75 110 if not vim.api.nvim_buf_is_valid( bufnr ) then return end
111 + announce_reset( bufnr )
76 112 writable( bufnr, function()
77 113 vim.api.nvim_buf_set_lines( bufnr, 0, -1, false, { '', } )
92 128 vim.wo[win].list = false
93 129 vim.wo[win].spell = false
94 130 vim.wo[win].winbar = '%#Title#π ' .. name .. '%*'
131 + for _, listener in ipairs( windows ) do
132 + pcall( listener, win )
133 + end
97 136 --- Shows what a pivi window is doing right now, in its own winbar.
130 169 local forget = function( name )
131 170 local bufnr = buffers[name]
132 171 buffers[name] = nil
172 + if bufnr then announce_reset( bufnr ) end
133 173 if bufnr and vim.api.nvim_buf_is_valid( bufnr ) then
134 174 pcall( vim.api.nvim_buf_delete, bufnr, { force = true, } )
144 184 return {
145 185 filetype = FILETYPE,
146 186 ensure = ensure,
187 + on_reset = on_reset,
188 + on_window = on_window,
189 + on_buffer = on_buffer,
147 190 decorate = decorate,
148 191 set_state = set_state,
149 192 append = append,

neovim/tests/bugabinga/pivi/editor/detail_spec.lua

Added
new file mode 100644
1 +---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch
2 +local detail = require 'bugabinga.pivi.detail'
3 +local entry = require 'bugabinga.pivi.entry'
4 +local transcript = require 'bugabinga.pivi.transcript'
5 +local fake = dofile 'tests/helpers/pivi_fake.lua'
7 +local block = function( bufnr, kind, meta, lines )
8 + local id = entry.open( bufnr, kind, meta )
9 + for _, text in ipairs( lines ) do entry.line( bufnr, id, text ) end
10 + entry.close( bufnr, id )
11 + return id
12 +end
14 +local text_of = function( lines )
15 + return table.concat( lines, '\n' )
16 +end
18 +describe( 'pivi detail', function()
19 + local bufnr, win
21 + before_each( function()
22 + bufnr = transcript.ensure 'inspecting'
23 + transcript.clear( bufnr )
24 + win = transcript.show( bufnr )
25 + vim.api.nvim_set_current_win( win )
26 + end )
28 + after_each( function()
29 + transcript.forget_all()
30 + fake.close_extra_windows()
31 + end )
33 + it( 'describes a tool call with its arguments, outcome and result', function()
34 + local tool = block( bufnr, 'tool', {
35 + tool = 'read',
36 + args = { path = 'a.lua', line = 3, },
37 + cwd = '/work',
38 + failed = false,
39 + result = { content = { { type = 'text', text = 'file contents', }, }, },
40 + }, { 'read a.lua', 'file contents', } )
42 + local described = text_of( detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, tool ) ) ) ) )
44 + assert.is_true( described:find( 'tool call', 1, true ) ~= nil )
45 + assert.is_true( described:find( 'read', 1, true ) ~= nil )
46 + assert.is_true( described:find( '/work/a.lua:3', 1, true ) ~= nil )
47 + assert.is_true( described:find( 'succeeded', 1, true ) ~= nil )
48 + assert.is_true( described:find( 'file contents', 1, true ) ~= nil )
49 + end )
51 + it( 'reports a failed tool call as failed', function()
52 + local tool = block( bufnr, 'tool', { tool = 'edit', failed = true, }, { 'edit a.lua', } )
53 + local described = text_of( detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, tool ) ) ) ) )
55 + assert.is_true( described:find( 'failed', 1, true ) ~= nil )
56 + end )
58 + it( 'shows the text of an answer', function()
59 + local answer = block( bufnr, 'answer', {}, { 'a distinctive reply', } )
60 + local described = text_of( detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, answer ) ) ) ) )
62 + assert.is_true( described:find( 'a distinctive reply', 1, true ) ~= nil )
63 + end )
65 + it( 'shows an error message', function()
66 + local failure = block( bufnr, 'error', { error = 'overloaded', }, { 'overloaded', } )
67 + local described = text_of( detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, failure ) ) ) ) )
69 + assert.is_true( described:find( 'overloaded', 1, true ) ~= nil )
70 + end )
72 + it( 'keeps a voluminous result readable and points back at the entry', function()
73 + local many = {}
74 + for index = 1, 200 do table.insert( many, 'line ' .. index ) end
76 + local tool = block( bufnr, 'tool', {
77 + tool = 'bash',
78 + result = { content = { { type = 'text', text = table.concat( many, '\n' ), }, }, },
79 + }, { 'bash', } )
81 + local described = detail.describe( bufnr, entry.at( bufnr, select( 1, entry.range( bufnr, tool ) ) ) )
83 + assert.is_true( #described < 200 )
84 + assert.is_true( text_of( described ):find( 'the entry itself holds the rest', 1, true ) ~= nil )
85 + end )
87 + it( 'inspects without leaving the transcript', function()
88 + local tool = block( bufnr, 'tool', { tool = 'read', args = { path = 'a.lua', }, }, { 'read a.lua', } )
89 + local first = select( 1, entry.range( bufnr, tool ) )
90 + vim.api.nvim_win_set_cursor( win, { first, 0, } )
92 + local float, floating = detail.inspect( bufnr, first )
94 + assert.is_number( float )
95 + assert.equal( win, vim.api.nvim_get_current_win() )
96 + assert.equal( first, vim.api.nvim_win_get_cursor( win )[1] )
97 + assert.same( { 'read a.lua', }, vim.list_slice( transcript.lines( bufnr ), first, first ) )
99 + if floating and vim.api.nvim_win_is_valid( floating ) then
100 + vim.api.nvim_win_close( floating, true )
101 + end
102 + end )
104 + it( 'inspects nothing outside an entry', function()
105 + assert.is_nil( detail.inspect( bufnr, 1 ) )
106 + end )
108 + it( 'binds inspection in its own buffers only', function()
109 + local bound = {}
110 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
111 + bound[mapping.lhs] = mapping.desc or ''
112 + end
114 + assert.is_string( bound.K )
115 + assert.is_true( bound.K:find( 'pivi', 1, true ) ~= nil )
117 + for _, mapping in ipairs( vim.api.nvim_get_keymap 'n' ) do
118 + assert.is_nil( ( mapping.desc or '' ):find( 'pivi', 1, true ) )
119 + end
120 + end )
121 +end )

neovim/tests/bugabinga/pivi/editor/fold_spec.lua

Added
new file mode 100644
1 +---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch
2 +local fold = require 'bugabinga.pivi.fold'
3 +local entry = require 'bugabinga.pivi.entry'
4 +local transcript = require 'bugabinga.pivi.transcript'
5 +local fake = dofile 'tests/helpers/pivi_fake.lua'
7 +local block = function( bufnr, kind, meta, lines )
8 + local id = entry.open( bufnr, kind, meta )
9 + for _, text in ipairs( lines ) do entry.line( bufnr, id, text ) end
10 + entry.close( bufnr, id )
11 + return id
12 +end
14 +describe( 'pivi fold', function()
15 + local bufnr
17 + before_each( function()
18 + bufnr = transcript.ensure 'folding'
19 + transcript.clear( bufnr )
20 + end )
22 + after_each( function()
23 + transcript.forget_all()
24 + fake.close_extra_windows()
25 + end )
27 + it( 'gives every multi-line entry its own fold', function()
28 + local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } )
29 + local first, last = entry.range( bufnr, tool )
31 + local win = transcript.show( bufnr )
32 + local rows = vim.api.nvim_win_call( win, function()
33 + local found = {}
34 + for row = first, last do
35 + vim.v.lnum = row
36 + table.insert( found, fold.expr() )
37 + end
38 + return found
39 + end )
41 + assert.same( { '>1', '1', '1', }, rows )
42 + end )
44 + it( 'leaves a single-line entry unfolded', function()
45 + block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', } )
46 + local win = transcript.show( bufnr )
48 + local level = vim.api.nvim_win_call( win, function()
49 + vim.v.lnum = 1
50 + return fold.expr()
51 + end )
53 + assert.equal( '0', level )
54 + end )
56 + it( 'treats only voluminous tool entries as collapsible on arrival', function()
57 + assert.is_true( fold.voluminous { kind = 'tool', first = 1, last = 4, } )
58 + assert.is_false( fold.voluminous { kind = 'tool', first = 1, last = 1, } )
59 + assert.is_false( fold.voluminous { kind = 'answer', first = 1, last = 9, } )
60 + assert.is_false( fold.voluminous { kind = 'error', first = 1, last = 4, } )
61 + end )
63 + it( 'collapses a tool result on arrival and keeps the answer open', function()
64 + local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } )
65 + local answer = block( bufnr, 'answer', {}, { 'first', 'second', 'third', } )
67 + local win = transcript.show( bufnr )
68 + fold.apply( bufnr )
70 + local tool_first = select( 1, entry.range( bufnr, tool ) )
71 + local answer_first = select( 1, entry.range( bufnr, answer ) )
73 + vim.api.nvim_win_call( win, function()
74 + assert.is_true( fold.closed( tool_first ) )
75 + assert.is_false( fold.closed( answer_first ) )
76 + end )
77 + end )
79 + it( 'stands in for a collapsed entry with one readable line', function()
80 + local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } )
81 + local first, last = entry.range( bufnr, tool )
82 + local win = transcript.show( bufnr )
84 + local shown = vim.api.nvim_win_call( win, function()
85 + vim.v.foldstart = first
86 + vim.v.foldend = last
87 + return fold.text()
88 + end )
90 + assert.is_true( shown:find( 'read a.lua', 1, true ) ~= nil )
91 + assert.is_true( shown:find( '2 more lines', 1, true ) ~= nil )
92 + end )
94 + it( 'expands a collapsed entry and never collapses it again by itself', function()
95 + local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } )
96 + local win = transcript.show( bufnr )
97 + fold.apply( bufnr )
99 + local first = select( 1, entry.range( bufnr, tool ) )
101 + vim.api.nvim_win_call( win, function()
102 + assert.is_true( fold.toggle( bufnr, first ) )
103 + assert.is_false( fold.closed( first ) )
104 + end )
106 + fold.apply( bufnr )
107 + vim.api.nvim_win_call( win, function()
108 + assert.is_false( fold.closed( first ) )
109 + end )
110 + end )
112 + it( 'never alters transcript text when an entry is toggled', function()
113 + local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', 'two', } )
114 + local win = transcript.show( bufnr )
115 + local before = transcript.lines( bufnr )
116 + local first = select( 1, entry.range( bufnr, tool ) )
118 + vim.api.nvim_win_call( win, function()
119 + fold.toggle( bufnr, first )
120 + fold.toggle( bufnr, first )
121 + end )
123 + assert.same( before, transcript.lines( bufnr ) )
124 + assert.equal( 'tool', entry.at( bufnr, first ).kind )
125 + end )
127 + it( 'binds a buffer-local key without claiming a global one', function()
128 + local found
129 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
130 + if mapping.lhs == '<CR>' then found = mapping end
131 + end
133 + assert.is_table( found )
134 + for _, mapping in ipairs( vim.api.nvim_get_keymap 'n' ) do
135 + assert.is_nil( ( mapping.desc or '' ):find( 'pivi', 1, true ) )
136 + end
137 + end )
138 +end )

neovim/tests/bugabinga/pivi/editor/navigate_spec.lua

Added
new file mode 100644
1 +---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch, redundant-parameter
2 +local navigate = require 'bugabinga.pivi.navigate'
3 +local entry = require 'bugabinga.pivi.entry'
4 +local transcript = require 'bugabinga.pivi.transcript'
5 +local follow = require 'bugabinga.pivi.follow'
6 +local fake = dofile 'tests/helpers/pivi_fake.lua'
8 +local temp_file = function( lines )
9 + local path = vim.fs.normalize( vim.fn.tempname() .. '.txt' )
10 + vim.fn.writefile( lines, path )
11 + return path
12 +end
14 +local block = function( bufnr, kind, meta, lines )
15 + local id = entry.open( bufnr, kind, meta )
16 + for _, text in ipairs( lines ) do entry.line( bufnr, id, text ) end
17 + entry.close( bufnr, id )
18 + return id
19 +end
21 +describe( 'pivi navigate', function()
22 + local bufnr, win
24 + before_each( function()
25 + bufnr = transcript.ensure 'moving'
26 + transcript.clear( bufnr )
28 + block( bufnr, 'prompt', { text = 'ask', }, { 'ask', } )
29 + block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', 'one', } )
30 + block( bufnr, 'answer', {}, { 'reply', } )
31 + block( bufnr, 'tool', { tool = 'edit', }, { 'edit a.lua', } )
33 + win = transcript.show( bufnr )
34 + vim.api.nvim_set_current_win( win )
35 + end )
37 + after_each( function()
38 + follow.reset()
39 + transcript.forget_all()
40 + fake.close_extra_windows()
41 + end )
43 + describe( 'locations', function()
44 + it( 'reads the place an entry names', function()
45 + local located = navigate.location_of {
46 + kind = 'tool',
47 + meta = { args = { path = 'src/main.lua', line = 12, }, cwd = '/work', },
48 + }
50 + assert.equal( '/work/src/main.lua', located.path )
51 + assert.equal( 12, located.line )
52 + end )
54 + it( 'keeps an absolute path as it is', function()
55 + local located = navigate.location_of {
56 + kind = 'tool',
57 + meta = { args = { path = '/elsewhere/a.lua', }, cwd = '/work', },
58 + }
60 + assert.equal( '/elsewhere/a.lua', located.path )
61 + end )
63 + it( 'reports no place for an entry that names none', function()
64 + assert.is_nil( navigate.location_of { kind = 'answer', meta = {}, } )
65 + assert.is_nil( navigate.location_of( nil ) )
66 + end )
68 + it( 'reaches the place an entry names without taking the working window', function()
69 + local path = temp_file { 'one', 'two', 'three', }
70 + block( bufnr, 'tool', { tool = 'read', args = { path = path, line = 3, }, }, { 'read', } )
72 + local listed = entry.list( bufnr )
73 + local tool = listed[#listed]
74 + vim.api.nvim_win_set_cursor( win, { tool.first, 0, } )
76 + local target = navigate.locate( bufnr, tool.first )
78 + assert.is_number( target )
79 + assert.is_not.equal( win, target )
80 + assert.equal( win, vim.api.nvim_get_current_win() )
81 + assert.equal( 3, vim.api.nvim_win_get_cursor( target )[1] )
82 + end )
84 + it( 'reaches nothing from an entry without a location', function()
85 + local listed = entry.list( bufnr )
86 + local answer = listed[3]
87 + assert.is_nil( navigate.locate( bufnr, answer.first ) )
88 + end )
90 + it( 'binds going to a location in its own buffers only', function()
91 + local bound = {}
92 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
93 + bound[mapping.lhs] = mapping.desc or ''
94 + end
96 + assert.is_string( bound.gf )
97 + assert.is_true( bound.gf:find( 'pivi', 1, true ) ~= nil )
98 + end )
99 + end )
101 + it( 'moves to the next entry regardless of kind', function()
102 + local listed = entry.list( bufnr )
103 + vim.api.nvim_win_set_cursor( win, { listed[1].first, 0, } )
105 + local moved = navigate.next_entry( bufnr )
107 + assert.equal( listed[2].id, moved.id )
108 + assert.equal( listed[2].first, vim.api.nvim_win_get_cursor( win )[1] )
109 + end )
111 + it( 'moves to the previous entry', function()
112 + local listed = entry.list( bufnr )
113 + vim.api.nvim_win_set_cursor( win, { listed[3].first, 0, } )
115 + local moved = navigate.previous_entry( bufnr )
117 + assert.equal( listed[2].id, moved.id )
118 + end )
120 + it( 'moves between tool calls only', function()
121 + local listed = entry.list( bufnr )
122 + vim.api.nvim_win_set_cursor( win, { listed[1].first, 0, } )
124 + local first_tool = navigate.next_entry( bufnr, 'tool' )
125 + assert.equal( 'tool', first_tool.kind )
126 + assert.equal( 'read', first_tool.meta.tool )
128 + local second_tool = navigate.next_entry( bufnr, 'tool' )
129 + assert.equal( 'edit', second_tool.meta.tool )
130 + end )
132 + it( 'moves backwards between tool calls', function()
133 + local listed = entry.list( bufnr )
134 + vim.api.nvim_win_set_cursor( win, { listed[4].first, 0, } )
136 + local moved = navigate.previous_entry( bufnr, 'tool' )
138 + assert.equal( 'read', moved.meta.tool )
139 + end )
141 + it( 'wraps around at both ends', function()
142 + local listed = entry.list( bufnr )
144 + vim.api.nvim_win_set_cursor( win, { listed[#listed].first, 0, } )
145 + assert.equal( listed[1].id, navigate.next_entry( bufnr ).id )
147 + vim.api.nvim_win_set_cursor( win, { listed[1].first, 0, } )
148 + assert.equal( listed[#listed].id, navigate.previous_entry( bufnr ).id )
149 + end )
151 + it( 'moves nowhere in a transcript without matching entries', function()
152 + local empty = transcript.ensure 'nothing'
153 + transcript.clear( empty )
155 + assert.is_nil( navigate.after( empty, 1 ) )
156 + assert.is_nil( navigate.before( empty, 1 ) )
157 + end )
159 + it( 'binds motions in its own buffers without claiming global keys', function()
160 + local bound = {}
161 + for _, mapping in ipairs( vim.api.nvim_buf_get_keymap( bufnr, 'n' ) ) do
162 + bound[mapping.lhs] = true
163 + end
165 + for _, keys in ipairs { ']m', '[m', ']t', '[t', } do
166 + assert.is_true( bound[keys], keys .. ' is not bound in the transcript' )
167 + end
169 + -- Neovim ships its own bracket motions; pivi adds none of its own globally.
170 + for _, mapping in ipairs( vim.api.nvim_get_keymap 'n' ) do
171 + assert.is_nil( ( mapping.desc or '' ):find( 'pivi', 1, true ) )
172 + end
173 + end )
175 + it( 'never changes the window or the buffer while moving', function()
176 + local listed = entry.list( bufnr )
177 + vim.api.nvim_win_set_cursor( win, { listed[1].first, 0, } )
178 + local before = transcript.lines( bufnr )
180 + navigate.next_entry( bufnr )
182 + assert.equal( win, vim.api.nvim_get_current_win() )
183 + assert.equal( bufnr, vim.api.nvim_win_get_buf( win ) )
184 + assert.same( before, transcript.lines( bufnr ) )
185 + end )
186 +end )

neovim/tests/bugabinga/pivi/editor/render_spec.lua

Added
new file mode 100644
1 +---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch, redundant-parameter
2 +local render = require 'bugabinga.pivi.render'
3 +local entry = require 'bugabinga.pivi.entry'
4 +local transcript = require 'bugabinga.pivi.transcript'
5 +local fake = dofile 'tests/helpers/pivi_fake.lua'
7 +describe( 'pivi render', function()
8 + local bufnr
10 + before_each( function()
11 + bufnr = transcript.ensure 'rendering'
12 + transcript.clear( bufnr )
13 + end )
15 + after_each( function()
16 + transcript.forget_all()
17 + fake.close_extra_windows()
18 + end )
20 + it( 'renders message formatting instead of showing markup', function()
21 + assert.is_true( render.highlighted( bufnr ) )
22 + end )
24 + it( 'conceals markup in its own windows only', function()
25 + local working = vim.api.nvim_get_current_win()
26 + local before = vim.wo[working].conceallevel
28 + local win = transcript.show( bufnr )
30 + assert.equal( 2, vim.wo[win].conceallevel )
31 + assert.equal( before, vim.wo[working].conceallevel )
32 + vim.api.nvim_win_close( win, true )
33 + end )
35 + it( 'presents embedded code in its own language', function()
36 + local id = entry.open( bufnr, 'answer' )
37 + for _, text in ipairs { 'text', '', '```lua', 'local value = 1', '```', } do
38 + entry.line( bufnr, id, text )
39 + end
40 + entry.close( bufnr, id )
42 + local parser = vim.treesitter.get_parser( bufnr, 'markdown' )
43 + parser:parse( true )
45 + local languages = {}
46 + parser:for_each_tree( function( _, tree ) languages[tree:lang()] = true end )
48 + assert.is_true( languages.markdown )
49 + assert.is_true( languages.lua )
50 + end )
52 + it( 'degrades to plain readable text when a format cannot be rendered', function()
53 + local plain = vim.api.nvim_create_buf( false, true )
54 + vim.api.nvim_buf_set_lines( plain, 0, -1, false, { 'still readable', } )
56 + assert.is_false( render.highlight( plain, 'definitely-not-a-language' ) or false )
57 + assert.same( { 'still readable', }, vim.api.nvim_buf_get_lines( plain, 0, -1, false ) )
59 + vim.api.nvim_buf_delete( plain, { force = true, } )
60 + end )
62 + it( 'stays responsive while a long answer streams', function()
63 + local id = entry.open( bufnr, 'answer' )
65 + local started = vim.uv.hrtime()
66 + for index = 1, 400 do
67 + entry.write( bufnr, id, 'word ' .. index .. ' ' )
68 + if index % 20 == 0 then entry.write( bufnr, id, '\n' ) end
69 + end
70 + local spent = ( vim.uv.hrtime() - started ) / 1e6
72 + entry.close( bufnr, id )
74 + -- Forcing a parse per delta costs hundreds of times this budget.
75 + assert.is_true( spent < 400, ( '400 deltas took %.1f ms' ):format( spent ) )
76 + end )
78 + it( 'frames an entry without writing anything into the buffer', function()
79 + local id = entry.open( bufnr, 'prompt', { text = 'ask', } )
80 + entry.line( bufnr, id, 'ask' )
81 + entry.close( bufnr, id )
83 + local before = transcript.lines( bufnr )
84 + render.decorate( bufnr, id )
86 + assert.same( before, transcript.lines( bufnr ) )
87 + assert.is_true( #render.marks( bufnr ) > 0 )
88 + end )
90 + it( 'decorates each kind with its own framing', function()
91 + for kind in pairs( entry.kinds ) do
92 + assert.is_table( render.frames[kind] )
93 + end
94 + end )
95 +end )

neovim/tests/bugabinga/pivi/entry_spec.lua

Added
new file mode 100644
1 +---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch
2 +local entry = require 'bugabinga.pivi.entry'
3 +local transcript = require 'bugabinga.pivi.transcript'
5 +describe( 'pivi entry', function()
6 + local bufnr
8 + before_each( function()
9 + bufnr = transcript.ensure 'entries'
10 + transcript.clear( bufnr )
11 + end )
13 + after_each( function()
14 + transcript.forget_all()
15 + end )
17 + it( 'refuses an unknown kind', function()
18 + assert.has_error( function() entry.open( bufnr, 'nonsense' ) end )
19 + end )
21 + it( 'keeps its bounds while it grows', function()
22 + local id = entry.open( bufnr, 'answer' )
23 + entry.line( bufnr, id, 'first' )
24 + local first = select( 1, entry.range( bufnr, id ) )
26 + entry.line( bufnr, id, 'second' )
27 + entry.line( bufnr, id, 'third' )
29 + local opened, last = entry.range( bufnr, id )
30 + assert.equal( first, opened )
31 + assert.equal( first + 2, last )
32 + end )
34 + it( 'keeps its bounds while later entries are appended', function()
35 + local answer = entry.open( bufnr, 'answer' )
36 + entry.line( bufnr, answer, 'the answer' )
37 + local first, last = entry.range( bufnr, answer )
39 + for index = 1, 5 do
40 + local tool = entry.open( bufnr, 'tool', { tool = 'read', } )
41 + entry.line( bufnr, tool, 'tool ' .. index )
42 + entry.close( bufnr, tool )
43 + end
45 + assert.same( { first, last, }, { entry.range( bufnr, answer ) } )
46 + assert.equal( 'the answer', transcript.lines( bufnr )[first] )
47 + end )
49 + it( 'begins every entry on its own line', function()
50 + local prompt = entry.open( bufnr, 'prompt' )
51 + entry.line( bufnr, prompt, 'question' )
53 + local answer = entry.open( bufnr, 'answer' )
54 + entry.line( bufnr, answer, 'reply' )
56 + local _, prompt_last = entry.range( bufnr, prompt )
57 + local answer_first = select( 1, entry.range( bufnr, answer ) )
59 + assert.is_true( answer_first > prompt_last )
60 + end )
62 + it( 'lists entries in buffer order with their kind and metadata', function()
63 + local prompt = entry.open( bufnr, 'prompt', { text = 'do it', } )
64 + entry.line( bufnr, prompt, 'do it' )
65 + local tool = entry.open( bufnr, 'tool', { tool = 'edit', } )
66 + entry.line( bufnr, tool, 'edit greet.lua' )
68 + local listed = entry.list( bufnr )
70 + assert.equal( 2, #listed )
71 + assert.equal( 'prompt', listed[1].kind )
72 + assert.equal( 'do it', listed[1].meta.text )
73 + assert.equal( 'tool', listed[2].kind )
74 + assert.equal( 'edit', listed[2].meta.tool )
75 + assert.is_true( listed[1].first < listed[2].first )
76 + end )
78 + it( 'finds the entry covering a line', function()
79 + local answer = entry.open( bufnr, 'answer' )
80 + entry.line( bufnr, answer, 'one' )
81 + entry.line( bufnr, answer, 'two' )
82 + local tool = entry.open( bufnr, 'tool', { tool = 'read', } )
83 + entry.line( bufnr, tool, 'read a.lua' )
85 + local first, last = entry.range( bufnr, answer )
86 + assert.equal( answer, entry.at( bufnr, first ).id )
87 + assert.equal( answer, entry.at( bufnr, last ).id )
88 + assert.equal( tool, entry.at( bufnr, select( 1, entry.range( bufnr, tool ) ) ).id )
89 + end )
91 + it( 'reports no entry outside every range', function()
92 + assert.is_nil( entry.at( bufnr, 1 ) )
93 + end )
95 + it( 'annotates an entry without changing its text', function()
96 + local tool = entry.open( bufnr, 'tool', { tool = 'read', } )
97 + entry.line( bufnr, tool, 'read a.lua' )
98 + local before = transcript.lines( bufnr )
100 + entry.annotate( bufnr, tool, { result = 'contents', failed = false, } )
102 + assert.equal( 'contents', entry.meta( bufnr, tool ).result )
103 + assert.equal( 'read', entry.meta( bufnr, tool ).tool )
104 + assert.same( before, transcript.lines( bufnr ) )
105 + end )
107 + it( 'forgets its entries when the transcript is cleared', function()
108 + local answer = entry.open( bufnr, 'answer' )
109 + entry.line( bufnr, answer, 'gone soon' )
110 + assert.equal( 1, #entry.list( bufnr ) )
112 + transcript.clear( bufnr )
114 + assert.same( {}, entry.list( bufnr ) )
115 + end )
116 +end )

neovim/tests/bugabinga/pivi/help_spec.lua

Modified
77 77 channel.flush()
79 79 assert.is_true( vim.wo[win].winbar:find( 'quickfix', 1, true ) ~= nil )
80 - assert.is_true( vim.list_contains( transcript.lines( run.buffer ), ' ▸ nvim_help quickfix' ) )
80 + assert.is_true( vim.list_contains( transcript.lines( run.buffer ), 'nvim_help quickfix' ) )
82 82 channel.frame { type = 'agent_settled', }
83 83 channel.flush()
184 184 channel.frame { id = id, type = 'response', command = 'prompt', success = false, error = 'help unavailable', }
186 186 assert.is_true( channel.settle( function() return run.done end ) )
187 - assert.is_true( vim.list_contains( transcript.lines( run.buffer ), ' ! help unavailable' ) )
187 + assert.is_true( vim.list_contains( transcript.lines( run.buffer ), 'help unavailable' ) )
188 188 assert.equal( 'running', working.status.connection )
189 189 assert.is_nil( working.status.error )

neovim/tests/bugabinga/pivi/session_spec.lua

Modified
5 5 local activity = require 'bugabinga.pivi.activity'
6 6 local follow = require 'bugabinga.pivi.follow'
7 7 local dialog = require 'bugabinga.pivi.dialog'
8 +local entry = require 'bugabinga.pivi.entry'
9 +local render = require 'bugabinga.pivi.render'
10 +local fold = require 'bugabinga.pivi.fold'
8 11 local fake = dofile 'tests/helpers/pivi_fake.lua'
9 12 local fixture = dofile 'tests/helpers/pivi_fixture.lua'
180 183 end ) )
183 - it( 'writes no heading for a turn that only calls tools', function()
186 + it( 'opens no answer entry for a turn that only calls tools', function()
184 187 local channel = fake.new()
185 188 local current = create( channel )
189 192 channel.frame { type = 'tool_execution_end', toolCallId = 'c1', toolName = 'read', isError = false, }
190 193 channel.flush()
192 - local rendered = table.concat( transcript.lines( current.buffer ), '\n' )
193 - assert.is_nil( rendered:find( '## pi', 1, true ) )
194 - assert.is_true( rendered:find( 'read', 1, true ) ~= nil )
195 + local kinds = {}
196 + for _, item in ipairs( entry.list( current.buffer ) ) do table.insert( kinds, item.kind ) end
198 + assert.same( { 'tool', }, kinds )
199 + assert.is_true( table.concat( transcript.lines( current.buffer ), '\n' ):find( 'read', 1, true ) ~= nil )
197 - it( 'writes one heading once real text arrives', function()
202 + it( 'opens exactly one answer entry once real text arrives', function()
198 203 local channel = fake.new()
199 204 local current = create( channel )
208 213 channel.flush()
210 - local lines = transcript.lines( current.buffer )
211 - local headings = 0
212 - for _, line in ipairs( lines ) do
213 - if line == '## pi' then headings = headings + 1 end
215 + local answers = {}
216 + for _, item in ipairs( entry.list( current.buffer ) ) do
217 + if item.kind == 'answer' then table.insert( answers, item ) end
216 - assert.equal( 1, headings )
217 - assert.is_true( vim.list_contains( lines, 'all done' ) )
220 + assert.equal( 1, #answers )
221 + assert.is_true( vim.list_contains( transcript.lines( current.buffer ), 'all done' ) )
220 224 it( 'drops the blank lines a model opens with', function()
229 233 channel.flush()
231 235 local lines = transcript.lines( current.buffer )
232 - local heading
233 - for index, line in ipairs( lines ) do
234 - if line == '## pi' then heading = index end
235 - end
236 + local answer = entry.list( current.buffer )[1]
237 - assert.is_number( heading )
238 - assert.equal( 'Done.', lines[heading + 1] )
238 + assert.equal( 'answer', answer.kind )
239 + assert.equal( 'Done.', lines[answer.first] )
240 + assert.equal( answer.first, answer.last )
241 243 it( 'ignores an empty text delta', function()
250 252 channel.flush()
252 - assert.is_nil(
253 - table.concat( transcript.lines( current.buffer ), '\n' ):find( '## pi', 1, true )
254 - )
254 + assert.same( {}, entry.list( current.buffer ) )
257 257 it( 'names what a tool acts on, relative to the session directory', function()
279 279 channel.frame { type = 'tool_execution_end', toolCallId = 'c1', toolName = 'read', isError = true, }
280 280 channel.flush()
282 - assert.is_true( vim.list_contains( transcript.lines( current.buffer ), ' failed' ) )
282 + assert.is_true( vim.list_contains( transcript.lines( current.buffer ), 'failed' ) )
283 + assert.is_true( entry.list( current.buffer )[1].meta.failed )
285 - it( 'quotes a multi-line submission line by line', function()
286 + it( 'writes a submission exactly as it was typed', function()
286 287 local channel = fake.new()
287 288 local current = create( channel )
289 290 current.submit( 'first\nsecond', { capture = false, } )
292 + local prompt = entry.list( current.buffer )[1]
291 293 local lines = transcript.lines( current.buffer )
292 - assert.is_true( vim.list_contains( lines, '> first' ) )
293 - assert.is_true( vim.list_contains( lines, '> second' ) )
295 + assert.equal( 'prompt', prompt.kind )
296 + assert.equal( 'first\nsecond', table.concat( vim.list_slice( lines, prompt.first, prompt.last ), '\n' ) )
297 + end )
299 + it( 'yields the original message text when an answer is copied', function()
300 + local channel = fake.new()
301 + local current = create( channel )
302 + local authored = '# Heading\n\nSome **bold** text and `code`.\n\n```lua\nlocal x = 1\n```'
304 + channel.frame { type = 'message_start', message = { role = 'assistant', }, }
305 + for piece in authored:gmatch '[^\n]*\n?' do
306 + if piece ~= '' then
307 + channel.frame {
308 + type = 'message_update',
309 + assistantMessageEvent = { type = 'text_delta', contentIndex = 0, delta = piece, },
311 + end
312 + end
313 + channel.flush()
315 + local answer = entry.list( current.buffer )[1]
316 + local lines = transcript.lines( current.buffer )
317 + local copied = table.concat( vim.list_slice( lines, answer.first, answer.last ), '\n' )
319 + assert.equal( 'answer', answer.kind )
320 + assert.equal( authored, copied )
321 + end )
323 + it( 'frames entries with decoration rather than text', function()
324 + local channel = fake.new()
325 + local current = create( channel )
327 + current.submit( 'do it', { capture = false, } )
328 + channel.frame { type = 'tool_execution_start', toolCallId = 'c1', toolName = 'read', args = { path = 'a.lua', }, }
329 + channel.flush()
331 + local rendered = table.concat( transcript.lines( current.buffer ), '\n' )
332 + for _, framing in ipairs { '> ', '\u{25b8} ', '## pi', ' ! ', } do
333 + assert.is_nil( rendered:find( framing, 1, true ) )
334 + end
336 + assert.is_true( #render.marks( current.buffer ) > 0 )
337 + end )
339 + it( 'makes every part of a turn an addressable entry', function()
340 + local channel = fake.new()
341 + local current = create( channel )
343 + current.submit( 'do the thing', { capture = false, } )
344 + channel.frame { type = 'message_start', message = { role = 'assistant', }, }
345 + channel.frame {
346 + type = 'tool_execution_start',
347 + toolCallId = 'c1',
348 + toolName = 'read',
349 + args = { path = 'greet.lua', },
351 + channel.frame { type = 'tool_execution_end', toolCallId = 'c1', toolName = 'read', isError = false, }
352 + channel.frame {
353 + type = 'message_update',
354 + assistantMessageEvent = { type = 'text_delta', contentIndex = 0, delta = 'all done', },
356 + channel.frame { type = 'agent_settled', }
357 + channel.flush()
359 + local kinds = {}
360 + for _, item in ipairs( entry.list( current.buffer ) ) do
361 + table.insert( kinds, item.kind )
362 + end
363 + assert.same( { 'prompt', 'tool', 'answer', }, kinds )
365 + local lines = transcript.lines( current.buffer )
366 + for _, item in ipairs( entry.list( current.buffer ) ) do
367 + assert.is_true( item.first >= 1 and item.last <= #lines )
368 + assert.is_true( item.first <= item.last )
369 + end
370 + end )
372 + it( 'keeps a tool entry addressable with its call and result', function()
373 + local channel = fake.new()
374 + local current = create( channel )
376 + channel.frame {
377 + type = 'tool_execution_start',
378 + toolCallId = 'c1',
379 + toolName = 'edit',
380 + args = { path = 'greet.lua', },
382 + channel.frame {
383 + type = 'tool_execution_end',
384 + toolCallId = 'c1',
385 + toolName = 'edit',
386 + isError = false,
387 + result = { content = { { type = 'text', text = 'edited', }, }, },
389 + channel.flush()
391 + local tool = entry.list( current.buffer )[1]
392 + assert.equal( 'tool', tool.kind )
393 + assert.equal( 'edit', tool.meta.tool )
394 + assert.equal( 'greet.lua', tool.meta.args.path )
395 + assert.is_table( tool.meta.result )
396 + assert.is_false( tool.meta.failed )
397 + end )
399 + it( 'shows a tool result collapsed on arrival without changing history', function()
400 + local channel = fake.new()
401 + local current = create( channel )
402 + local win = transcript.show( current.buffer )
404 + channel.frame {
405 + type = 'tool_execution_start',
406 + toolCallId = 'c1',
407 + toolName = 'read',
408 + args = { path = 'a.lua', },
410 + channel.frame {
411 + type = 'tool_execution_end',
412 + toolCallId = 'c1',
413 + toolName = 'read',
414 + isError = false,
415 + result = { content = { { type = 'text', text = 'line one\nline two\nline three', }, }, },
417 + channel.flush()
419 + local tool = entry.list( current.buffer )[1]
420 + local lines = transcript.lines( current.buffer )
422 + assert.equal( 'tool', tool.kind )
423 + assert.is_true( tool.last > tool.first )
424 + assert.is_true( vim.list_contains( lines, 'line two' ) )
426 + vim.api.nvim_win_call( win, function()
427 + assert.is_true( fold.closed( tool.first ) )
428 + end )
430 + -- Collapsing is presentation: nothing was submitted, queued, or recorded.
431 + assert.same( { steering = {}, follow_up = {}, }, current.status.queue )
432 + assert.same( {}, current.submissions )
433 + assert.same( {}, commands_of( channel, 'prompt' ) )
434 + end )
436 + it( 'records a rejected submission as its own entry', function()
437 + local channel = fake.new()
438 + local current = create( channel )
439 + current.submit( 'do it', { capture = false, } )
441 + local id = commands_of( channel, 'prompt' )[1].id
442 + channel.frame { id = id, type = 'response', command = 'prompt', success = false, error = 'nope', }
443 + channel.flush()
445 + local listed = entry.list( current.buffer )
446 + local failure = listed[#listed]
447 + assert.equal( 'error', failure.kind )
448 + assert.equal( 'nope', failure.meta.error )
296 451 it( 'surfaces a rejected submission in the transcript', function()