@@ -89,13 +89,15 @@ response_start="$(wc -c <"$capture")"
8989printf '%s\r' "$NEMOCLAW_LAUNCH_PROMPT" >&3
9090
9191reply_seen=0
92- has_exact_reply () {
92+ normalized_response () {
9393 tail -c "+$((response_start + 1))" "$capture" \
9494 | sed -E $'s/\x1B][^\x07\x1B]*(\x07|\x1B\\\\)//g' \
9595 | sed -E $'s/\x1B\\[[0-?]*[ -\\/]*[@-~]//g' \
9696 | tr '\r' '\n' \
97- | LC_ALL=C tr -d '\000-\010\013\014\016-\037\177' \
98- | awk -v expected="$NEMOCLAW_LAUNCH_EXPECTED_REPLY" '
97+ | LC_ALL=C tr -d '\000-\010\013\014\016-\037\177'
98+ }
99+ has_exact_reply() {
100+ normalized_response | awk -v expected="$NEMOCLAW_LAUNCH_EXPECTED_REPLY" '
99101 {
100102 line = $0
101103 sub(/^[[:space:]]+/, "", line)
@@ -105,6 +107,20 @@ has_exact_reply() {
105107 END { exit found ? 0 : 1 }
106108 '
107109}
110+ has_post_reply_ready() {
111+ normalized_response | awk \
112+ -v expected="$NEMOCLAW_LAUNCH_EXPECTED_REPLY" \
113+ -v ready="$NEMOCLAW_LAUNCH_READY_TEXT" '
114+ {
115+ line = $0
116+ sub(/^[[:space:]]+/, "", line)
117+ sub(/[[:space:]]+$/, "", line)
118+ if (line == expected) reply = 1
119+ if (reply && index(line, ready) != 0) found = 1
120+ }
121+ END { exit found ? 0 : 1 }
122+ '
123+ }
108124for _ in {1..180}; do
109125 if has_exact_reply; then
110126 reply_seen=1
@@ -116,6 +132,24 @@ for _ in {1..180}; do
116132 sleep 1
117133done
118134
135+ if [[ "$reply_seen" = 1 && -n "$NEMOCLAW_LAUNCH_READY_TEXT" ]]; then
136+ post_reply_ready_seen=0
137+ for _ in {1..60}; do
138+ if has_post_reply_ready; then
139+ post_reply_ready_seen=1
140+ break
141+ fi
142+ if ! kill -0 "$session_pid" 2>/dev/null; then
143+ break
144+ fi
145+ sleep 1
146+ done
147+ if [[ "$post_reply_ready_seen" != 1 ]]; then
148+ echo "launch did not return to the expected TUI state after the reply" >&2
149+ exit 1
150+ fi
151+ fi
152+
119153if [[ -n "$NEMOCLAW_LAUNCH_EXIT_COMMAND" ]]; then
120154 printf '%s\r' "$NEMOCLAW_LAUNCH_EXIT_COMMAND" >&3
121155else
0 commit comments