From 33966090118da8b36f58959516c55fbfca8e7a72 Mon Sep 17 00:00:00 2001 From: jensenojs Date: Mon, 16 Nov 2026 16:08:59 +0800 Subject: [PATCH] fix(ui): keep background fold maintenance out of Normal mode state.ui.output_folds authoritative fold ranges | derive v output window folds window-local, rebuilt while user types | +-- old: :normal! zE / zd -> global State = Normal | -> ModeChanged i:n, attributed to output buf +-- new: foldmethod recompute / rebuild recorded ranges -> mode untouched Folds are window-local, mode is global. Deriving folds for a background window used to emit a mode change to listeners of the input window, which is where the input source switch hangs. Clearing by foldmethod recompute and repairing by rebuilding the recorded ranges keeps that derivation inside the output window. Cost: O(rendered lines) instead of O(folds); 1.65us per 1000 rendered lines, unchanged in the default lazy window. Tests: real-UI mode/key observers and a PTY streaming run wired into CI assert no ModeChanged, no synthetic Normal keys, and that an intentional is still reported. Fixes #567 --- .github/workflows/tests.yml | 4 + Makefile | 5 +- lua/opencode/ui/output_window.lua | 28 ++++-- lua/opencode/ui/session_picker.lua | 2 +- tests/manual/streaming_input.lua | 107 ++++++++++++++++++++++ tests/manual/streaming_input.py | 85 ++++++++++++++++++ tests/unit/fold_display_spec.lua | 138 +++++++++++++++++++++++++++++ 7 files changed, 360 insertions(+), 9 deletions(-) create mode 100644 tests/manual/streaming_input.lua create mode 100644 tests/manual/streaming_input.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a6aff918..c720539c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -153,6 +153,10 @@ jobs: - name: Run tests run: make test + - name: Preserve input during streamed rendering + if: always() + run: make test-input-streaming + # # For sshing in to debug GH actions # - name: Setup tmate session # uses: mxschmitt/action-tmate@v3 diff --git a/Makefile b/Makefile index 04fd144e..ef799003 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ TOPOLOGY_REQUIREMENTS := scripts/dependency-topology/requirements.txt TOPOLOGY_PYTHON ?= $(if $(shell command -v uv),uv run --quiet --no-project --with-requirements $(TOPOLOGY_REQUIREMENTS) python3,$(PYTHON)) TOPOLOGY_BASE ?= origin/main -.PHONY: help check test test-minimal test-unit test-replay typecheck format-check format replay replay-regenerate topology topology-diff topology-check +.PHONY: help check test test-minimal test-unit test-replay test-input-streaming typecheck format-check format replay replay-regenerate topology topology-diff topology-check help: ## List development commands @awk 'BEGIN { FS = ":.*## " } /^[a-z-]+:.*## / { printf " %-20s %s\n", $$1, $$2 }' $(MAKEFILE_LIST) @@ -34,6 +34,9 @@ test-unit: ## Run unit tests test-replay: ## Run automated replay tests $(MAKE) test TEST=replay +test-input-streaming: ## Exercise streamed rendering while typing through a real PTY + $(PYTHON) tests/manual/streaming_input.py $(ARGS) + typecheck: ## Check Lua types (optional ARGS passed to emmylua_check) ./check_types.sh $(ARGS) diff --git a/lua/opencode/ui/output_window.lua b/lua/opencode/ui/output_window.lua index 4df52819..cddefe48 100644 --- a/lua/opencode/ui/output_window.lua +++ b/lua/opencode/ui/output_window.lua @@ -54,9 +54,18 @@ local function build_fold_state(folds) return fold_state end -local function clear_manual_folds(win) +---Clear native folds without running Normal commands or consuming typeahead. +---@param win integer +function M.clear_manual_folds(win) vim.api.nvim_win_call(win, function() - vim.cmd('silent! normal! zE') + local nestmax = vim.wo.foldnestmax + -- Indent folds update during Insert; expr/marker folds defer until it ends. + -- Recompute with depth zero, then retain the empty fold tree in manual mode. + vim.wo.foldnestmax = 0 + vim.wo.foldmethod = 'indent' + vim.fn.foldlevel(1) + vim.wo.foldmethod = 'manual' + vim.wo.foldnestmax = nestmax end) end @@ -555,7 +564,7 @@ function M.get_open_fold_starts(win, buf, ranges) return was_open end ----Repair only the supplied folds after a buffer rewrite, leaving history alone. +---Repair native folds against the recorded ranges after a buffer rewrite. ---Existing open folds stay open; a pre-write snapshot restores deleted open folds. ---@param ranges {from: integer, to: integer}[] ---@param was_open? table @@ -566,7 +575,7 @@ function M.repair_folds(ranges, was_open) end vim.api.nvim_win_call(windows.output_win, function() local line_count = vim.api.nvim_buf_line_count(windows.output_buf) - ---@type table? + ---@type vim.fn.winrestview.dict? local view for _, range in ipairs(ranges) do if range.to <= line_count then @@ -576,7 +585,12 @@ function M.repair_folds(ranges, was_open) if not exists or (closed_start ~= -1 and (closed_start ~= range.from or closed_end ~= range.to)) then view = view or vim.fn.winsaveview() if exists then - vim.cmd(range.from .. 'normal! zd') + -- Neovim has no Ex command for deleting one manual fold. Rebuild + -- only when a surviving fold has wrong bounds; missing folds below + -- still use the incremental path. + M.set_folds(state.ui.get_output_folds().ranges, true, was_open) + vim.fn.winrestview(view) + return end vim.cmd(range.from .. ',' .. range.to .. 'fold') if was_open and was_open[range.from] then @@ -619,7 +633,7 @@ function M.set_folds(fold_ranges, force, open_starts) vim.api.nvim_win_call(win, function() local view = preserve_view and vim.fn.winsaveview() or nil - clear_manual_folds(win) + M.clear_manual_folds(win) local line_count = vim.api.nvim_buf_line_count(buf) local fold_commands = {} @@ -896,7 +910,7 @@ end function M.clear() local windows = state.windows if M.mounted(windows) then - clear_manual_folds(windows.output_win) + M.clear_manual_folds(windows.output_win) end state.ui.clear_output_folds() M.set_lines({}) diff --git a/lua/opencode/ui/session_picker.lua b/lua/opencode/ui/session_picker.lua index 582b916e..e0993ab4 100644 --- a/lua/opencode/ui/session_picker.lua +++ b/lua/opencode/ui/session_picker.lua @@ -186,7 +186,7 @@ local function render_preview_buffer(target, formatted) vim.api.nvim_set_option_value('foldcolumn', '1', { win = 0 }) vim.api.nvim_set_option_value('statuscolumn', '', { win = 0 }) vim.api.nvim_set_option_value('foldmethod', 'manual', { win = 0 }) - vim.cmd('silent! normal! zE') -- clear existing manual folds + output_window.clear_manual_folds(vim.api.nvim_get_current_win()) local line_count = vim.api.nvim_buf_line_count(bufnr) for _, range in ipairs(formatted.fold_ranges) do if range.from <= line_count and range.to <= line_count then diff --git a/tests/manual/streaming_input.lua b/tests/manual/streaming_input.lua new file mode 100644 index 00000000..fd6f9222 --- /dev/null +++ b/tests/manual/streaming_input.lua @@ -0,0 +1,107 @@ +-- Driven by streaming_input.py through a real terminal; replay replaces only transport. +vim.opt.rtp:append(vim.env.STREAM_TEST_ROOT .. '/deps/plenary.nvim') +local helpers = require('tests.helpers') +local state = require('opencode.state') +local config = require('opencode.config') +config.setup(vim.deepcopy(config.defaults)) +config.ui.output.tools.fold_exclude = {} +config.ui.output.tools.folding_threshold = 2 +helpers.replay_setup() +local events = helpers.load_test_data('tests/data/mcp-tool.json') +state.session.set_active(helpers.get_session_from_events(events)) +helpers.wait_for_replay_ready() +helpers.replay_event(table.remove(events, 1)) -- Establish session metadata before typing. +assert(require('tests.manual.renderer_replay').wait_for_idle(), 'Initial renderer did not settle') +local windows = state.windows --[[@as OpencodeWindowState & {input_win: integer, input_buf: integer, output_win: integer, output_buf: integer}]] +vim.api.nvim_set_current_win(windows.input_win) + +---@type {phase: integer, requests: table[], focus_events: table[], internal_normal_keys: string[], checkpoints: table[], terminal: boolean, left_input?: boolean, mode?: string, input?: string, focused?: boolean, current_win?: integer, input_win?: integer, events?: integer, input_buf?: integer, remaining_tools?: integer, error?: string} +local report = { + phase = 0, + requests = {}, + focus_events = {}, + internal_normal_keys = {}, + checkpoints = {}, + terminal = #vim.api.nvim_list_uis() > 0, +} +local function publish() + report.mode = vim.fn.mode(1) + report.input = table.concat(vim.api.nvim_buf_get_lines(windows.input_buf, 0, -1, false), '\n') + report.focused = vim.api.nvim_get_current_win() == windows.input_win + report.current_win = vim.api.nvim_get_current_win() + report.input_win = windows.input_win + vim.fn.writefile({ vim.json.encode(report) }, vim.env.STREAM_TEST_REPORT .. '.new') + vim.uv.fs_rename(vim.env.STREAM_TEST_REPORT .. '.new', vim.env.STREAM_TEST_REPORT) +end +-- Stand-in for the user's i:* listener: record the write request, never change the OS IME. +vim.api.nvim_create_autocmd('ModeChanged', { + pattern = 'i:n', + callback = function(event) + report.requests[#report.requests + 1] = { source = 'ABC', buf = event.buf, phase = report.phase } + report.left_input = true + end, +}) +-- :noautocmd can hide ModeChanged while :normal still consumes stuffed keys in n. +vim.on_key(function(key, typed) + if not report.left_input and typed == '' and vim.fn.mode(1) == 'n' then + report.internal_normal_keys[#report.internal_normal_keys + 1] = vim.fn.keytrans(key) + end +end, vim.api.nvim_create_namespace('stream-input-internal-keys')) +vim.api.nvim_create_autocmd({ 'WinEnter', 'WinLeave', 'BufEnter', 'BufLeave' }, { + callback = function(event) + if report.phase > 0 then + report.focus_events[#report.focus_events + 1] = { event = event.event, buf = event.buf, phase = report.phase } + end + end, +}) +events[#events + 1] = { + type = 'message.part.removed', + properties = { sessionID = 'ses_mcp_test_1', messageID = 'msg_mcp_asst1', partID = 'prt_mcp_tool2' }, +} +vim.keymap.set('i', '', function() + vim.defer_fn(function() + local ok, err = xpcall(function() + report.phase = report.phase + 1 + helpers.replay_event(events[report.phase]) + local folds = state.ui.get_output_folds().ranges + local closed = vim.api.nvim_win_call(windows.output_win, function() + local count = 0 + for _, range in ipairs(folds) do + if vim.fn.foldclosed(range.from) ~= -1 then + count = count + 1 + end + end + return count + end) + report.checkpoints[#report.checkpoints + 1] = { + event = events[report.phase].type, + mode = vim.fn.mode(1), + focused = vim.api.nvim_get_current_win() == windows.input_win, + folds = #folds, + closed = closed, + lines = vim.api.nvim_buf_line_count(windows.output_buf), + } + local observation = assert(state.session.active_observation()):read() + if report.phase == #events then + report.remaining_tools = 0 + for _, part in ipairs(assert(observation.entries_by_id.msg_mcp_asst1).content) do + if part.kind == 'tool' then + report.remaining_tools = report.remaining_tools + 1 + end + end + end + end, debug.traceback) + if not ok then + report.error = err + end + publish() + end, 40) +end) +vim.keymap.set('n', '', publish) +report.events = #events +report.input_buf = windows.input_buf +vim.schedule(function() + vim.api.nvim_set_current_win(windows.input_win) + vim.cmd('startinsert') + vim.defer_fn(publish, 50) +end) diff --git a/tests/manual/streaming_input.py b/tests/manual/streaming_input.py new file mode 100644 index 00000000..56ca8224 --- /dev/null +++ b/tests/manual/streaming_input.py @@ -0,0 +1,85 @@ +"""PTY regression: python3 tests/manual/streaming_input.py [--root UPSTREAM_TREE]. + +Runs the real SSE replay/renderer while typing; records a fake ABC request rather +than touching the host input method. Requires nvim and deps/plenary.nvim. +""" +import argparse +import fcntl +import json +import os +from pathlib import Path +import pty +import select +import struct +import subprocess +import tempfile +import termios +import time + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[2]) +args = parser.parse_args() +root = args.root.resolve() +scenario = Path(__file__).with_suffix(".lua").resolve() +with tempfile.TemporaryDirectory(prefix="opencode-stream-", dir=os.environ.get("TMPDIR")) as directory: + report_path = Path(directory) / "report.json" + master, slave = pty.openpty() + fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack("HHHH", 40, 140, 0, 0)) + env = dict(os.environ, TERM="xterm-256color", STREAM_TEST_ROOT=str(root), + STREAM_TEST_REPORT=str(report_path)) + env.pop("VIM", None) + env.pop("VIMRUNTIME", None) + process = subprocess.Popen( + ["nvim", "-u", "tests/minimal/init.lua", "-i", "NONE", "-n", + "-c", "luafile " + str(scenario)], + cwd=root, env=env, stdin=slave, stdout=slave, stderr=slave, + ) + os.close(slave) + typed = "" + transcript = bytearray() + + def checkpoint(predicate, typing=False): + global typed + deadline = time.monotonic() + 8 + next_key = time.monotonic() + while time.monotonic() < deadline: + readable, _, _ = select.select([master], [], [], .01) + if readable: + transcript.extend(os.read(master, 65536)) + if report_path.exists(): + report = json.loads(report_path.read_text()) + assert not report.get("error"), report.get("error") + if predicate(report): + return report + if typing and time.monotonic() >= next_key: + os.write(master, "中x".encode()) + typed += "中x" + next_key = time.monotonic() + .01 + raise AssertionError("terminal checkpoint timeout: " + repr(bytes(transcript[-1500:]))) + + try: + report = checkpoint(lambda r: r["phase"] == 0) + assert report["terminal"] + for phase in range(1, report["events"] + 1): + os.write(master, b"\x1b[15~") + report = checkpoint(lambda r: r["phase"] == phase, typing=True) + assert report["mode"] == "i" and report["focused"], report + assert report["requests"] == [], report + assert report["focus_events"] == [], report + assert report["internal_normal_keys"] == [], report + # A final key is a fence: all preceding terminal text must be present. + os.write(master, b"\x1b") + os.write(master, b"\x1b[17~") + report = checkpoint(lambda r: r["mode"] == "n") + assert report["input"] == typed, (len(report["input"]), len(typed)) + assert len(report["requests"]) == 1 and report["requests"][0]["buf"] == report["input_buf"], report + assert max(c["closed"] for c in report["checkpoints"]) > 0, report + assert report["remaining_tools"] == 1, report + print(json.dumps({"passed": True, "events": report["events"], "typed_bytes": len(typed.encode()), + "maintenance_ABC_requests": 0, "intentional_Esc_requests": 1, + "internal_normal_keys": report["internal_normal_keys"], + "checkpoints": report["checkpoints"]}, indent=2)) + finally: + process.kill() + process.wait(timeout=5) + os.close(master) diff --git a/tests/unit/fold_display_spec.lua b/tests/unit/fold_display_spec.lua index b5649aeb..87527bab 100644 --- a/tests/unit/fold_display_spec.lua +++ b/tests/unit/fold_display_spec.lua @@ -41,6 +41,144 @@ describe('fold display with a real Neovim UI', function() vim.fn.jobstop(channel) end) + local function start_input() + exec([[ + output.set_lines({ 'title', 'first', 'second', 'third', 'tail', 'old', 'history' }) + output.set_folds({ { from = 2, to = 3 }, { from = 6, to = 7 } }) + output_win = vim.api.nvim_get_current_win() + vim.cmd('vnew') + input_win = vim.api.nvim_get_current_win() + input_buf = vim.api.nvim_get_current_buf() + vim.bo.filetype = 'opencode' + mode_changes = {} + vim.api.nvim_create_autocmd('ModeChanged', { + pattern = 'i:n', + callback = function() mode_changes[#mode_changes + 1] = vim.api.nvim_get_current_buf() end, + }) + maintenance_keys = {} + vim.on_key(function(key) + if vim.fn.mode(1) == 'n' then + maintenance_keys[#maintenance_keys + 1] = vim.fn.keytrans(key) + end + end, vim.api.nvim_create_namespace('fold-maintenance-keys')) + ]]) + vim.rpcrequest(channel, 'nvim_input', 'i') + assert.is_true(vim.wait(1000, function() + return exec([[return vim.fn.mode(1)]]) == 'i' + end)) + exec([[maintenance_keys = {}]]) + end + + local function assert_input_untouched() + assert.same( + { true, 'i', {}, {} }, + exec([[ + return { vim.api.nvim_get_current_win() == input_win, vim.fn.mode(1), mode_changes, maintenance_keys } + ]]) + ) + end + + it('rebuilds and clears output folds without notifying input mode listeners', function() + start_input() + exec([[ + ctx = require('opencode.ui.renderer.ctx').current() + buffer = require('opencode.ui.renderer.buffer') + ctx.render_state:set_part({ id = 'stream', kind = 'text' }, 'message', 'stream', 0, 6) + ctx.formatted_parts.stream = { fold_ranges = { { from = 2, to = 4 } } } + buffer.update_part_folds('stream') + ]]) + assert_input_untouched() + assert.equals( + 4, + exec([[ + return vim.api.nvim_win_call(output_win, function() return vim.fn.foldclosedend(2) end) + ]]) + ) + exec([[ + vim.api.nvim_win_call(output_win, function() vim.cmd('2foldopen!') end) + ctx.formatted_parts.stream.fold_ranges[1].to = 5 + buffer.update_part_folds('stream') + ]]) + assert_input_untouched() + assert.equals( + -1, + exec([[ + return vim.api.nvim_win_call(output_win, function() return vim.fn.foldclosed(2) end) + ]]) + ) + exec([[buffer.remove_part_now('stream')]]) + assert_input_untouched() + exec([[output.clear()]]) + assert_input_untouched() + assert.equals( + 0, + exec([[ + return vim.api.nvim_win_call(output_win, function() return vim.fn.foldlevel(2) end) + ]]) + ) + vim.rpcrequest(channel, 'nvim_input', vim.api.nvim_replace_termcodes('', true, false, true)) + assert.is_true(vim.wait(1000, function() + return exec([[return #mode_changes]]) == 1 + end)) + assert.is_true(exec([[return mode_changes[1] == input_buf]])) + end) + + it('repairs a changed fold without disturbing input or open history folds', function() + start_input() + exec([[ + vim.api.nvim_win_call(output_win, function() vim.cmd('6foldopen!') end) + require('opencode.state').ui.set_output_folds({ + ranges = { { from = 2, to = 4 }, { from = 6, to = 7 } }, + }) + output.repair_folds({ { from = 2, to = 4 } }) + ]]) + assert_input_untouched() + assert.same( + { 4, -1 }, + exec([[ + return vim.api.nvim_win_call(output_win, function() + return { vim.fn.foldclosedend(2), vim.fn.foldclosed(6) } + end) + ]]) + ) + end) + + it('renders asynchronous session previews without synthetic input mode changes', function() + start_input() + exec([[ + local connection = require('tests.unit.services_spec_support').mock_connection() + local observation = connection:observe({ id = 'preview' }) + observation._state.entries_by_id = { + message = { id = 'message', kind = 'assistant', content = { + { id = 'text', kind = 'text', text = 'Background preview' }, + } }, + } + observation._state.entry_order = { 'message' } + observation._state.sync.messages = { state = 'current' } + observation.watch = function(self, _, changed) + vim.schedule(function() changed(self) end) + return function() end + end + local opts + require('opencode.ui.base_picker').pick = function(value) opts = value end + require('opencode.ui.session_picker').pick({ { id = 'preview', title = 'Preview' } }, function() end) + local buf = vim.api.nvim_win_get_buf(output_win) + opts.preview_fn({ id = 'preview' }, { + is_valid = function() return true end, + get_bufnr = function() return buf end, + set_lines = function(_, lines) output.set_lines(lines) end, + with_window = function(_, fn) vim.api.nvim_win_call(output_win, fn) end, + }) + ]]) + assert.is_true(vim.wait(1000, function() + return exec([[ + return table.concat(vim.api.nvim_buf_get_lines( + vim.api.nvim_win_get_buf(output_win), 0, -1, false), '\n'):find('Background preview', 1, true) ~= nil + ]]) + end)) + assert_input_untouched() + end) + it('keeps tool borders beside both input and result fold summaries', function() exec([[ local formatter = require('opencode.ui.formatter')