Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
28 changes: 21 additions & 7 deletions lua/opencode/ui/output_window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<number, boolean>
Expand All @@ -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<string, integer>?
---@type vim.fn.winrestview.dict?
local view
for _, range in ipairs(ranges) do
if range.to <= line_count then
Expand All @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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({})
Expand Down
2 changes: 1 addition & 1 deletion lua/opencode/ui/session_picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
107 changes: 107 additions & 0 deletions tests/manual/streaming_input.lua
Original file line number Diff line number Diff line change
@@ -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', '<F5>', 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', '<F6>', 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)
85 changes: 85 additions & 0 deletions tests/manual/streaming_input.py
Original file line number Diff line number Diff line change
@@ -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)
Loading
Loading