Skip to content

Commit 1d212b3

Browse files
authored
Merge pull request #496 from OpenCoven/fix/408-classic-quit-save
fix(cli): keep a Classic turn's prompt and output when quitting mid-turn
2 parents 8f7f544 + f95ea30 commit 1d212b3

6 files changed

Lines changed: 431 additions & 60 deletions

File tree

‎docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Clear the current conversation history and start a fresh session. The session fi
7777
### /exit
7878
**Aliases:** `quit`, `q`
7979

80-
Exit the Coven Code REPL, like pressing `Ctrl+D` twice on an empty prompt. Finished turns are already saved. Typed while a turn runs, `/exit` waits for the turn to end, like any queued message. With `COVEN_CODE_UI=next`, quitting with `Ctrl+D` while a turn is still running stops it and saves it with your prompt and what it delivered so far. If it has not stopped within 2 seconds, for example while a tool ignores the stop, only your prompt from that turn is saved. It then says the turn stopped, names each tool with no reported result, noting any last shown waiting for its turn or awaiting approval, and says what those did is unknown, names any finished tool whose result is not in the saved session, and gives the command to resume the session when the save succeeded. Quitting while auto-compaction runs prints only that command, or that the session was not saved. Classic does not save a running turn.
80+
Exit the Coven Code REPL, like pressing `Ctrl+D` twice on an empty prompt. Finished turns are already saved. Typed while a turn runs, `/exit` waits for the turn to end, like any queued message. Quitting with `Ctrl+D` while a turn is still running stops it and saves it with your prompt and what it delivered so far. If it has not stopped within 2 seconds, for example while a tool ignores the stop, only your prompt from that turn is saved. With `COVEN_CODE_UI=next`, it then says the turn stopped, names each tool with no reported result, noting any last shown waiting for its turn or awaiting approval, and says what those did is unknown, names any finished tool whose result is not in the saved session, and gives the command to resume the session when the save succeeded. Quitting while auto-compaction runs waits up to 2 seconds for it, in both renderers. If it finishes in time, the compacted conversation is saved; otherwise the conversation is saved as it was before. In Next, quitting then prints only the command to resume, or that the session was not saved. Classic prints nothing after it exits, whether or not the save succeeded.
8181

8282
```
8383
/exit

‎scripts/tui-tests/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ screen, and runs `run.sh`. No secrets or network access required.
7676
| `cases/23_classic_zero_size.sh` | Zero and tiny sizes | Classic starts at 0x0, 0x40 and 120x0 and survives 120x0 with the exit toast, 0x40, 0x0, 1x1 and 20x6, then draws a full frame at 120x40, exits 0 and restores the terminal; tmux cannot make a zero-sized window, so `zero_size_pty.py` drives a raw PTY and needs `python3` |
7777
| `cases/25_next_unicode_composer.sh` | Unicode in the Next composer | CJK, right-to-left text, a combining mark and an emoji ZWJ sequence typed through tmux stay unsent, and all but the emoji (drawn differently by tmux versions) are checked on screen; Left and Backspace act on whole CJK characters; no turn reaches the provider (`fake_anthropic.py`) before Enter, and it and the saved session get exactly the edited text. tmux sends committed characters only, so input-method preedit is not covered, and editing moves by Unicode scalar, so the ZWJ emoji and combining mark are not edited; needs `python3` |
7878
| `cases/26_classic_cancel.sh` | Cancelling a turn | In Classic (`COVEN_CODE_UI` unset) and Next, `fake_anthropic.py` drives a real turn. Escape while an approved Bash command appends a line every second: once the status no longer says cancelling, at most 3 more lines were written than at the Escape, and the file stops growing. Escape while text is still arriving (a burst of 200,000 text deltas, then a stream held open, so the turn is still running in the runtime): the status acknowledges it, the backlog still arriving does not bring the running turn back within 10 s, and the status settles to `Cancelled`; needs `python3` |
79+
| `cases/27_classic_quit_save.sh` | Quitting mid-turn | In Classic (`COVEN_CODE_UI` unset) and Next, `fake_anthropic.py` drives a real turn. Ctrl+D twice while a held stream is still sending text exits, leaves the alternate screen, restores the cursor and turns mouse reporting off, and the saved session holds the prompt and the text delivered so far. Only Next prints its report and the resume command after exiting. Ctrl+D twice while an approved `sleep 20` runs exits within 8 s and saves the prompt; needs `python3` |
7980

8081
## Configuration
8182

@@ -122,7 +123,9 @@ paste. The distinction matters: only the second shape engages the TUI's
122123
paste-burst detector.
123124

124125
Helpers from [`lib.sh`](lib.sh): `tui_start` / `tui_stop`, `tui_keys`,
125-
`tui_type`, `tui_paste`, `tui_settle`, `tui_capture`, `wait_for`, and the assertions
126+
`tui_type`, `tui_paste`, `tui_settle`, `tui_capture`, `wait_for`,
127+
`tui_quit_and_check` (press quit keys, then check the exit and the terminal
128+
restore), and the assertions
126129
`assert_contains` / `assert_absent` / `assert_matches` / `assert_eq`. For
127130
headless checks, call `run_bin <args...>` and read `$RUN_OUT` / `$RUN_RC`.
128131

‎scripts/tui-tests/cases/18_next_event_flood.sh‎

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,6 @@ _flood_saved_count() { # home
1616
| wc -l | tr -d ' '
1717
}
1818

19-
# Wall-clock time in tenths of a second.
20-
_flood_now_tenths() { python3 -c 'import time; print(int(time.time() * 10))'; }
21-
22-
# Press the quit keys, wait for the TUI to exit, then check it gave the terminal back.
23-
# Sets FLOOD_QUIT_TENTHS to how long the exit took after the last key, in tenths
24-
# of a second by the wall clock. Each pass of the wait also runs tmux, so the
25-
# pass count gives up later than 30 s; it only stops a hung exit.
26-
_flood_quit() { # how, keys...
27-
local how="$1" key passes=0 running start
28-
shift
29-
# The running process's name, whatever the binary under test is called.
30-
running="$(_tmux display-message -p -t "$TUI_SESSION" '#{pane_current_command}')"
31-
for key in "$@"; do tui_keys "$key"; start="$(_flood_now_tenths)"; sleep 0.25; done
32-
while [ "$passes" -lt 300 ] && \
33-
[ "$(_tmux display-message -p -t "$TUI_SESSION" '#{pane_current_command}')" = "$running" ]; do
34-
sleep 0.1; passes=$((passes + 1))
35-
done
36-
FLOOD_QUIT_TENTHS=$(($(_flood_now_tenths) - start))
37-
if [ "$passes" -lt 300 ]; then _pass "$how exits (after about $((FLOOD_QUIT_TENTHS / 10))s)"
38-
else _fail "$how exits" "$(tui_capture)"; fi
39-
assert_eq "$(_tmux display-message -p -t "$TUI_SESSION" '#{alternate_on}')" "0" \
40-
"$how leaves the alternate screen"
41-
assert_eq "$(_tmux display-message -p -t "$TUI_SESSION" '#{cursor_flag}')" "1" \
42-
"$how restores the cursor"
43-
assert_eq "$(_tmux display-message -p -t "$TUI_SESSION" '#{mouse_any_flag}')" "0" \
44-
"$how turns mouse reporting off"
45-
}
46-
4719
_flood_wait_saved() { # home, count
4820
local waited=0
4921
while [ "$waited" -lt 100 ] && [ "$(_flood_saved_count "$1")" -lt "$2" ]; do
@@ -127,7 +99,7 @@ _next_event_flood_journey() {
12799
tui_type "flood: 300001"; sleep 0.3; tui_keys Enter
128100
wait_for "working" 20 >/dev/null
129101
wait_for "flood line" 20 >/dev/null
130-
_flood_quit "Ctrl+D twice during a burst" C-d C-d
102+
tui_quit_and_check "Ctrl+D twice during a burst" C-d C-d
131103
local saved
132104
saved="$(find "$home" -type f -path '*/sessions/*.json' -exec grep -l 'flood: 300001' {} + 2>/dev/null)"
133105
if [ -n "$saved" ]; then _pass "quitting during a burst keeps the prompt"
@@ -145,7 +117,7 @@ _next_event_flood_journey() {
145117
# A fast build may have settled by the first look, hence "cancelling" or "Cancelled".
146118
if wait_for "ancel" 5; then _pass "Ctrl+C cancels during the burst"
147119
else _fail "Ctrl+C cancels during the burst" "$(tui_capture)"; fi
148-
_flood_quit "Ctrl+C twice more during a burst" C-c C-c
120+
tui_quit_and_check "Ctrl+C twice more during a burst" C-c C-c
149121

150122
# ---- quitting while a tool ignores the stop still exits within the bound.
151123
# Quitting waits up to 2 s for the turn; 8 s leaves room for a loaded host and
@@ -157,11 +129,11 @@ _next_event_flood_journey() {
157129
tui_type "run: sleep 20"; sleep 0.3; tui_keys Enter
158130
if wait_for "Approve Bash" 15; then
159131
sleep 0.7; tui_keys y; sleep 1.5
160-
_flood_quit "Ctrl+D twice while a tool ignores the stop" C-d C-d
161-
if [ "$FLOOD_QUIT_TENTHS" -lt 80 ]; then
132+
tui_quit_and_check "Ctrl+D twice while a tool ignores the stop" C-d C-d
133+
if [ "$TUI_QUIT_TENTHS" -lt 80 ]; then
162134
_pass "quitting exits within 8 s while a turn does not stop"
163135
else
164-
_fail "quitting exits within 8 s while a turn does not stop" "took about $((FLOOD_QUIT_TENTHS / 10))s"
136+
_fail "quitting exits within 8 s while a turn does not stop" "took about $((TUI_QUIT_TENTHS / 10))s"
165137
fi
166138
saved="$(find "$home" -type f -path '*/sessions/*.json' -exec grep -l 'run: sleep 20' {} + 2>/dev/null)"
167139
if [ -n "$saved" ]; then _pass "a turn that did not stop in time keeps its prompt"
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env bash
2+
# shellcheck shell=bash
3+
#
4+
# Quitting during a turn, in Classic and in Next (#408). A local fake of the
5+
# Messages API drives real turns through the real client and query loop.
6+
# Quitting mid-turn with Ctrl+D twice must exit, give the terminal back, stop
7+
# the turn and save its prompt and the output it delivered, and must not wait
8+
# long while an approved command runs. Only Next reports on the turn after it
9+
# exits. (A unit test covers a turn that ignores the stop: the exit waits 2 s
10+
# at most.) Classic runs with COVEN_CODE_UI unset;
11+
# Next, which case 18 also covers, with COVEN_CODE_UI=next. Time limits are
12+
# generous because this also runs on debug builds and loaded hosts.
13+
14+
register_case tc_classic_quit_save
15+
16+
# The saved session file that names `text`, if any.
17+
_cqs_saved_with() { # home, text
18+
find "$1" -type f -path '*/sessions/*.json' -exec grep -lF -- "$2" {} + 2>/dev/null | head -n 1
19+
}
20+
21+
# Start the TUI for one renderer against the fake API, with its own home.
22+
_cqs_launch() { # renderer, home, port, project
23+
local var ui=""
24+
TUI_LAUNCH_PREFIX="cd $(printf '%q' "$4") && env"
25+
for var in OPENAI_API_KEY COVEN_CODE_PROVIDER COVEN_CODE_API_BASE COVEN_CODE_HOME \
26+
COVEN_CODE_TEST_HOME COVEN_CODE_CLAUDE_BIN COVEN_PARENT COVEN_DAEMON_SOCKET COVEN_CODE_UI; do
27+
TUI_LAUNCH_PREFIX+=" -u $var"
28+
done
29+
if [ "$1" = next ]; then ui="COVEN_CODE_UI=next"; TUI_BOOT_STRING="preview"
30+
else TUI_BOOT_STRING="Coven v"; fi
31+
# shellcheck disable=SC2086 # an empty selector adds nothing
32+
TUI_LAUNCH_PREFIX+=" $(printf '%q ' "HOME=$2" "COVEN_HOME=$2/.coven" $ui \
33+
ANTHROPIC_API_KEY=fake-key "ANTHROPIC_BASE_URL=http://127.0.0.1:$3")"
34+
tui_start
35+
}
36+
37+
tc_classic_quit_save() {
38+
describe "Quit during a turn in each renderer"
39+
if ! have_tmux; then _skip "tmux not installed"; return 0; fi
40+
if ! command -v python3 >/dev/null 2>&1; then _skip "python3 not installed"; return 0; fi
41+
42+
local scratch; scratch="$(mktemp -d)"
43+
mkdir -p "$scratch/project" "$scratch/markers"
44+
python3 -B "${BASH_SOURCE[0]%/*}/../fake_anthropic.py" \
45+
"$scratch/port" "$scratch/requests.log" "$scratch/markers" 2>/dev/null &
46+
local fake_pid=$! waited=0 r home port
47+
while [ "$waited" -lt 50 ] && [ ! -s "$scratch/port" ]; do
48+
sleep 0.1; waited=$((waited + 1))
49+
done
50+
local saved_prefix="$TUI_LAUNCH_PREFIX" saved_boot="$TUI_BOOT_STRING"
51+
52+
if [ -s "$scratch/port" ]; then
53+
port="$(cat "$scratch/port")"
54+
for r in classic next; do
55+
home="$scratch/home-$r"
56+
mkdir -p "$home/.coven-code"
57+
printf '%s\n' '{"hasCompletedOnboarding": true}' >"$home/.coven-code/settings.json"
58+
_cqs_held "$r" "$home" "$port" "$scratch/project"
59+
tui_stop
60+
_cqs_command "$r" "$home" "$port" "$scratch/project"
61+
tui_stop
62+
done
63+
else
64+
_fail "the fake API starts"
65+
fi
66+
kill "$fake_pid" 2>/dev/null
67+
wait "$fake_pid" 2>/dev/null
68+
TUI_LAUNCH_PREFIX="$saved_prefix" TUI_BOOT_STRING="$saved_boot"
69+
rm -rf "$scratch"
70+
}
71+
72+
# Quitting while text is still arriving keeps the prompt and the answer so far.
73+
# The fake holds the stream open after its burst, so the turn is still running
74+
# when the keys land.
75+
_cqs_held() { # renderer, home, port, project
76+
local r="$1" home="$2" saved pane
77+
_cqs_launch "$@" || return 0
78+
tui_type "flood: 2000 hold"; sleep 0.3; tui_keys Enter
79+
if ! wait_for "held line" 30; then
80+
_fail "$r: the held turn starts" "$(tui_capture)"; return 0
81+
fi
82+
tui_quit_and_check "$r: Ctrl+D twice during a turn" C-d C-d
83+
# The pane now shows the shell, with whatever the TUI printed as it exited:
84+
# Next's report on the turn and the resume command; nothing from Classic.
85+
pane="$(tui_capture)"
86+
if [ "$r" = next ]; then
87+
assert_contains "$pane" "Quit during a turn" "$r: the exit report follows the exit"
88+
assert_contains "$pane" "Pick it up with" "$r: the resume command follows the exit"
89+
else
90+
assert_absent "$pane" "Quit during a turn" "$r: no exit report follows the exit"
91+
assert_absent "$pane" "Pick it up with" "$r: no resume command follows the exit"
92+
fi
93+
saved="$(_cqs_saved_with "$home" "flood: 2000 hold")"
94+
if [ -n "$saved" ]; then _pass "$r: quitting during a turn keeps the prompt"
95+
else _fail "$r: quitting during a turn keeps the prompt" "no saved session names it"; fi
96+
if [ -n "$saved" ] && grep -q 'flood line' "$saved"; then
97+
_pass "$r: quitting during a turn keeps the answer so far"
98+
else
99+
_fail "$r: quitting during a turn keeps the answer so far" "${saved:-no saved session}"
100+
fi
101+
}
102+
103+
# Quitting while an approved command runs exits promptly; 8 s leaves room for a
104+
# loaded host. The command stops with the turn here, so this does not reach the
105+
# 2 s bound. The prompt must be saved.
106+
_cqs_command() { # renderer, home, port, project
107+
local r="$1" home="$2" saved
108+
_cqs_launch "$@" || return 0
109+
tui_type "run: sleep 20"; sleep 0.3; tui_keys Enter
110+
if ! wait_for "allow once" 15; then
111+
_fail "$r: a Bash call asks for approval" "$(tui_capture)"; return 0
112+
fi
113+
sleep 0.7; tui_keys y; sleep 1.5
114+
tui_quit_and_check "$r: Ctrl+D twice while a command runs" C-d C-d
115+
if [ "$TUI_QUIT_TENTHS" -lt 80 ]; then
116+
_pass "$r: quitting exits within 8 s while a command runs"
117+
else
118+
_fail "$r: quitting exits within 8 s while a command runs" "took about $((TUI_QUIT_TENTHS / 10))s"
119+
fi
120+
saved="$(_cqs_saved_with "$home" "run: sleep 20")"
121+
if [ -n "$saved" ]; then _pass "$r: quitting while a command runs keeps the prompt"
122+
else _fail "$r: quitting while a command runs keeps the prompt" "no saved session names it"; fi
123+
}

‎scripts/tui-tests/lib.sh‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,35 @@ wait_absent() {
221221

222222
tui_session_alive() { _tmux has-session -t "$TUI_SESSION" 2>/dev/null; }
223223

224+
# Wall-clock time in tenths of a second. Needs python3.
225+
tui_now_tenths() { python3 -c 'import time; print(int(time.time() * 10))'; }
226+
227+
# tui_quit_and_check <how> <keys...>
228+
# Press the quit keys, wait for the TUI to exit, then check it gave the terminal
229+
# back. Sets TUI_QUIT_TENTHS to how long the exit took after the last key, in
230+
# tenths of a second by the wall clock. Each pass of the wait also runs tmux, so
231+
# the pass count gives up later than 30 s; it only stops a hung exit.
232+
tui_quit_and_check() {
233+
local how="$1" key passes=0 running start
234+
shift
235+
# The running process's name, whatever the binary under test is called.
236+
running="$(_tmux display-message -p -t "$TUI_SESSION" '#{pane_current_command}')"
237+
for key in "$@"; do tui_keys "$key"; start="$(tui_now_tenths)"; sleep 0.25; done
238+
while [ "$passes" -lt 300 ] && \
239+
[ "$(_tmux display-message -p -t "$TUI_SESSION" '#{pane_current_command}')" = "$running" ]; do
240+
sleep 0.1; passes=$((passes + 1))
241+
done
242+
TUI_QUIT_TENTHS=$(($(tui_now_tenths) - start))
243+
if [ "$passes" -lt 300 ]; then _pass "$how exits (after about $((TUI_QUIT_TENTHS / 10))s)"
244+
else _fail "$how exits" "$(tui_capture)"; fi
245+
assert_eq "$(_tmux display-message -p -t "$TUI_SESSION" '#{alternate_on}')" "0" \
246+
"$how leaves the alternate screen"
247+
assert_eq "$(_tmux display-message -p -t "$TUI_SESSION" '#{cursor_flag}')" "1" \
248+
"$how restores the cursor"
249+
assert_eq "$(_tmux display-message -p -t "$TUI_SESSION" '#{mouse_any_flag}')" "0" \
250+
"$how turns mouse reporting off"
251+
}
252+
224253
# tui_start [extra binary args...]
225254
# Spins up a fresh tmux session, waits for the shell, launches the binary,
226255
# and blocks until the TUI has drawn (or fails the current assertion).

0 commit comments

Comments
 (0)