Skip to content

fix(terminal): start a new snacks terminal for an open during close - #354

Merged
ThomasK33 merged 2 commits into
mainfrom
fix/snacks-open-during-close
Oct 31, 2026
Merged

ThomasK33 merged 2 commits into
mainfrom
fix/snacks-open-during-close

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Why

The goal is to clear the open follow-up issues (#350, #325, #326, #335, #342), one small reviewed PR per bug. Part of #350. This PR handles item 3 and the snacks comment from item 2.

With the snacks provider, close waits up to 1 s for the Claude job to exit, and that wait runs scheduled callbacks. A scheduled open, for example a vim.scheduled :ClaudeCode or require("claudecode.terminal").open(), found the closing terminal still tracked. It showed that terminal, and close then wiped it, so that open showed nothing. #349 fixed the same case for the native provider only.

close now stops tracking the instance before it waits, as native does. The closing instance's TermClose and BufWipeout handlers clear the tracked terminal only while it is still that instance, so they keep the new terminal. Without that guard, the new terminal would lose its tracking as soon as the old buffer is wiped.

The comment in M.close said that the wait "ends those writes". That is not true for a job that stopped reading its input. The comment now says what happens: a job that reads its input, like Claude, takes the pending writes and exits. A job that stopped reading (for example yes, or cat after a large multi-line send) keeps them pending, the wait times out, and Neovim can still hang (neovim/neovim#42508). This PR does not mitigate that.

What changed

  • lua/claudecode/terminal/snacks.lua:
    • M.close sets terminal = nil before the wait.
    • setup_terminal_events clears terminal only when it still points at that instance.
    • The comment in M.close is rewritten.
  • tests/unit/terminal/snacks_close_spec.lua: the mock queues vim.schedule callbacks and runs them in vim.wait before the job exits, as Neovim does. New tests schedule an open before close, with auto_close off and on.
  • lua/claudecode/terminal/README.md: the snacks.close and setup_terminal_events contract lines describe the new behavior.

Scope

Only the snacks provider's close and its two event handlers. Open, hide, show, toggle, and the native and external providers do not change. Item 2's hang has no code change here, because it is upstream.

Blast Radius

Users of the snacks terminal provider. A plain :ClaudeCodeClose works as before. An open that runs during the close wait now starts a new Claude terminal instead of showing nothing. While close waits, get_active_bufnr() returns nil instead of the closing buffer.

Verification

I ran a headless script with snacks.nvim 882c996 (the commit in fixtures/snacks-explorer/lazy-lock.json) on the runtimepath, the snacks provider, and terminal_cmd = "cat". It schedules an open, then calls close, three ways: open(), :ClaudeCode, and :ClaudeCodeFocus. A fourth check is a plain close.

Code Neovim Result
main 63c9645 0.10.4, 0.12.6 1 of 4 pass. Each scheduled open saw the closing buffer still tracked. After close, no terminal is tracked or shown.
this PR 71f6a37 0.10.4, 0.11.6, 0.12.3, 0.12.6 4 of 4 pass, 3 of 3 runs per version. The old job is stopped, and the new terminal is tracked, running, and shown. Close takes 3 to 7 ms.

Item 2 comment check, with this PR on Neovim 0.10.4 and 0.12.6, both providers, a multi-line send and then close:

  • cat and yes, 23 KB: close returns after the 1 s timeout, and the next vim.wait never returns (killed by timeout).
  • A node process that reads its input, 228 KB: close returns in 54 to 72 ms, and the event loop keeps running.

Unit tests: the test commit bfa7d27 fails the 2 new tests against main's snacks.lua, and 71f6a37 makes them pass. Each half of the fix alone (only terminal = nil, or only the handler guard) still fails both tests. mise run check (0 warnings), mise run test (763 successes, 0 failures), stylua --check, and prettier --check pass on the head commit.


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: xhigh

close waits for the Claude job to exit, and that wait runs callbacks
scheduled earlier. An open among them reused the closing terminal, so
that open showed nothing. The mock now queues vim.schedule callbacks
and runs them in vim.wait before the job exits, as Neovim does. The new
tests fail until the fix in the next commit.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
snacks close waits up to 1 s for the Claude job to exit, and the wait
runs scheduled callbacks. A scheduled open (for example a vim.schedule'd
:ClaudeCode) found the closing instance still tracked, showed it, and
close then wiped it, so that open showed nothing.

close now stops tracking the instance before it waits, as the native
provider does since #349. The closing instance's TermClose and
BufWipeout handlers clear the tracked terminal only while it is still
that instance, so they keep the new terminal.

The close comment no longer claims that the wait ends all pending
writes. A job that stopped reading its input keeps them pending, the
wait times out, and Neovim can still hang (neovim/neovim#42508).

Part of #350.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T21:21:45.800072Z 71f6a37 Manual request
🔒 Security Review ✅ Completed 2026-10-31T21:21:43.248290Z 71f6a37 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 71f6a3785b

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 71f6a3785b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

ThomasK33 added a commit that referenced this pull request Oct 31, 2026
…rites

The close comment said the wait ends pending writes to the job. That
holds for a job that reads its input, like Claude. A job that stopped
reading (yes, or cat after a large multi-line send) keeps the writes
pending, the wait times out, and Neovim can still hang
(neovim/neovim#42508). The comment now says so, as #354 does for
snacks. Comment only.

Part of #350.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
@ThomasK33
ThomasK33 added this pull request to the merge queue Oct 31, 2026
Merged via the queue into main with commit 51d49af Oct 31, 2026
5 checks passed
@ThomasK33
ThomasK33 deleted the fix/snacks-open-during-close branch October 31, 2026 21:38
ThomasK33 added a commit that referenced this pull request Oct 31, 2026
Brings in #354 so covered.lock can record its snacks.lua.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
ThomasK33 added a commit that referenced this pull request Oct 31, 2026
The only was/now line is terminal/snacks.lua. #354 changes how the
snacks provider tracks its terminal while it closes, which cannot reach
diff state, an openDiff answer, a disconnect, or a diff command.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
@github-actions github-actions Bot mentioned this pull request Oct 31, 2026
hareki pushed a commit to hareki/claudecode.nvim that referenced this pull request Nov 1, 2026
…t exits (coder#352)

## Why

The operator goal is to clear the open follow-up issues in this
repository (coder#350, coder#325, coder#326, coder#335, coder#342) with small reviewed PRs. This
PR covers item 4 of coder#350, the two native-terminal comments in its item
6, and the native half of the comment problem in its item 2.

After a module reload (for example `:Lazy reload`), the new native
module adopts the running Claude terminal. The job's `on_exit` callback
belongs to the old module copy, so the new module never learned that the
job exited. It kept tracking the exited buffer, and `:ClaudeCode` or
`:ClaudeCodeOpen` showed the dead terminal instead of starting a new
session. With `auto_close`, a window that the new module had opened for
the terminal also stayed open after the exit.

The fix runs the same exit handling for an adopted terminal as for one
the module started. Adoption now adds a buffer-local `TermClose` handler
that calls that handling.

## What changed

- `lua/claudecode/terminal/native.lua`:
- The body of `on_exit` moves unchanged into `forget_exited_job(job_id,
auto_close)`. It still does nothing unless the module tracks that job,
then clears the state and, with `auto_close`, closes the tracked window
(never the last window). `on_exit` calls it.
- `adopt_existing_terminal` adds a buffer-local, once-only `TermClose`
handler. The handler schedules `forget_exited_job` for the adopted job
with the module's `auto_close` setting.
- The `close_terminal` comment now says the buffer delete closes the
terminal's windows except the last one. It no longer says the close wait
ends pending writes. It says that a job that reads its input, like
Claude, takes them during the wait, and that a job that stopped reading
(`yes`, or `cat` after a large multi-line send) keeps them pending, so
Neovim can still hang (neovim/neovim#42508). coder#354 makes the same change
for snacks.
- `lua/claudecode/terminal.lua`: the `send_to_terminal` comment no
longer says the native provider loses the job id of a recovered
terminal. It now says that the `'channel'` fallback covers a Neovim that
stops setting the deprecated `b:terminal_job_id`.
- `lua/claudecode/terminal/README.md`: the adoption and on-exit lines
describe the exit handling for adopted terminals.
- `tests/unit/native_terminal_toggle_spec.lua`: three new tests, all
failing on main.

## Scope

Only the native provider's exit handling for adopted terminals, and two
comments. Owned terminals run the same code as before, moved into a
function. Snacks and the other coder#350 items are separate PRs.

## Blast Radius

Users of the native provider who reload the plugin inside a running
Neovim. After the reload, an adopted Claude session that exits on its
own is forgotten, so the next `:ClaudeCode` starts a new session. With
`auto_close`, the window the new module tracks closes, as it does for a
terminal the plugin started. Without a reload, nothing changes. The old
module copy still runs its own `on_exit` too. Both calls check that
their module tracks the job, and a window that one of them closed is
skipped by the other.

## Verification

A headless script runs the real plugin on Neovim 0.12.3 and 0.10.4. It
calls `require("claudecode").setup` with the native provider, then uses
`:ClaudeCode`, `:ClaudeCodeOpen`, and `:ClaudeCodeClose`. It reloads the
plugin by clearing every `claudecode` module from `package.loaded` and
running `require` and `setup` again. The terminal command is a stand-in
that exits when it reads one line. The script sends it a line, like a
user typing `/exit`. Each run is bounded by `timeout -k 3 20`.

| Check (both `auto_close` values unless noted) | main `63c9645` | this
branch `a51beb0` |
| ------------------------------------------------------------- |
---------------------------------------------------------------------- |
------------------------------------------------------ |
| Reload, `:ClaudeCode` adopts, job exits, then `:ClaudeCode` | dead
terminal shown, no live process | new terminal with a live process |
| Same, then `:ClaudeCodeOpen` | dead terminal shown | new terminal with
a live process |
| Reload, adopt, hide and show, job exits (`auto_close = true`) | window
still shows the dead terminal, next `:ClaudeCode` shows nothing | window
closes, next `:ClaudeCode` starts a new session |
| No reload, job exits, then `:ClaudeCode` | new session | new session |
| Reload, adopt, `:ClaudeCodeClose`, then `:ClaudeCode` | process
stopped, new session | process stopped, new session |

Per Neovim version, 10 checks pass on this branch. On main, 6 of them
fail. Both versions give the same results. The close and adoption script
from coder#349 still passes all 23 of its checks per version on this branch.
Its extra run of the known upstream hang (coder#350 item 2) still hangs, as
on main.

Unit tests: the test commit `c33c44e` fails 3 tests on main's code. With
`auto_close` true and false, `simple_toggle` returns the exited buffer.
With `auto_close`, the re-shown window still shows the exited terminal.
The fix commit `bc0fba4` makes them pass. `838a1cc` and `a51beb0` change
comments only. On the head, `mise run check` reports 0 warnings, `mise
run test` gives 764 successes and 0 failures, and `stylua --check` and
`prettier --check` pass on the changed files.

Part of coder#350.

---

_Generated with [`xum`](https://github.com/coder/xum) • Model:
`anthropic:claude-opus-5-5` • Thinking: `xhigh`_
hareki pushed a commit to hareki/claudecode.nvim that referenced this pull request Nov 1, 2026
…adoption change (coder#359)

## Why

The goal is to clear the open follow-up issues (coder#350, coder#325, coder#326, coder#335,
coder#342), one small reviewed PR per bug. Part of coder#350. This PR handles item
6 (the diff part) and item 5.

Item 6: the proposed buffer of a native diff has a cleanup autocmd for
`BufDelete` and `BufWipeout` with `once = true`. `once` applies to each
event separately, and a wipe fires both events, so the cleanup ran
twice. When the session folder was not empty, "Error removing temp
directory" showed twice, and the folder delete ran twice. The callback
now runs the cleanup only on its first call.

Item 5: coder#349 changed which terminals the native provider adopts, and the
CHANGELOG did not say so. A terminal from an older plugin version or a
session file is no longer adopted, and the next open starts a second
Claude session. The `[Unreleased]` Bug Fixes section now has that line.

## What changed

- `lua/claudecode/diff.lua` (`M._open_native_diff`): a `cleaned_up` flag
in the cleanup autocmd's callback. The events, the group, and
`cleanup_temp_file` stay as they are.
- `tests/unit/diff_spec.lua`: the test helper fires the cleanup autocmd
once per registered event, as a wipe does in Neovim. A new test leaves a
stray file in the session folder, wipes the buffer, and expects one
warning.
- `lua/claudecode/README.md`: the cleanup sentence says it runs once and
warns once.
- `CHANGELOG.md`: one Bug Fixes line for coder#349, with the upgrade note.

## Scope

Only the temp-file cleanup autocmd of the native diff, and one CHANGELOG
line. The openDiff tool (`open_diff_blocking`) writes no temp files and
does not change. The other diff.lua work for coder#325 is a separate PR, and
this change touches 7 lines of `_open_native_diff` (6 added, 1 removed)
to avoid conflicts with it.

## Blast Radius

Users of the native diff whose session folder cannot be removed. They
now see one warning per failed delete instead of two. A folder that
empties normally is still removed without a notification.

`verify-diff-lifecycle/check.sh` reads STALE on main already
(`diff.lua`, `init.lua`, `native.lua` and `snacks.lua` changed since the
record at `919881e`). This PR adds no changed block or call site to that
list, only the `diff.lua` file hash. The change cannot reach diff state,
an openDiff answer, a disconnect, or a diff command. The model still
passes (`main` and `live` configs), and I did not re-record
`covered.lock`.

## Verification

I ran a headless script on Neovim 0.8.3, 0.10.4, 0.11.6, and 0.12.6 (`-u
NONE`, `XDG_CACHE_HOME` in a temp dir). It calls `open_diff`, puts a
stray file in the session folder, and closes the proposed buffer three
ways: `:bwipeout!`, `:bdelete!`, and closing its window
(`bufhidden=wipe`). It counts WARN notifications that contain "Error
removing temp directory", and calls of `vim.fn.delete(dir, "d")`.

| Code | Each of the 3 ways, on every version |
| --- | --- |
| main `63c9645` | 2 warnings, 2 folder deletes |
| this PR `c695a89` | 1 warning, 1 folder delete |

In both, the temp file is removed and the buffer is gone.

With a read-only session folder (`chmod 555`), both deletes fail. Then
`:bwipeout!` shows 4 warnings on main (each of the two twice) and 2 on
this PR (one for the file, one for the folder), on 0.10.4, 0.12.3, and
0.12.6. The README now says the cleanup warns once for each delete that
fails.

Unit tests: the test commit `87aad49` fails the new test against main's
`diff.lua` (2 warnings, expected 1), and `c695a89` makes it pass. `mise
run check` (0 warnings), `mise run test` (767 successes, 0 failures,
after merging main with coder#352 and coder#354), `stylua --check`, and `prettier
--check` pass on the head commit.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model:
`anthropic:claude-opus-5-5` • Thinking: `xhigh`_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant