-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtctl
More file actions
executable file
·1231 lines (1116 loc) · 31.3 KB
/
Copy pathtctl
File metadata and controls
executable file
·1231 lines (1116 loc) · 31.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="/tmp/tctl-sessions"
mkdir -p "$ROOT_DIR"
print_help() {
cat <<'HELP'
tctl -- unified terminal control wrapper
Usage:
tctl launch <command> [options]
tctl -s <session> <action> [args...]
tctl sessions
Actions:
type <text> Send literal text
press <key> [keys...] Send a key chord (e.g. press shift enter)
wait <pattern> [--timeout] Block until text or /regex/ appears
wait-idle [--timeout] Block until output stabilizes
snapshot [--trim] Print cleaned text from the session
screenshot [-o <path>] Capture compositor PNG (true-input only)
provenance Print launch repo root, branch, and commit
record start <path> Start video recording (true-input only)
record stop Stop video recording
close Tear down the session
Launch options:
-s, --session <name> Session name (default: default)
--backend <name> tuistory | true-input | ghostty | kitty | alacritty
--terminal <name> Override terminal for true-input
--cols <n> Columns (default: 120)
--rows <n> Rows (default: 36)
--cwd <path> Working directory (tuistory)
--repo-root <path> Git worktree root for provenance + droid-dev launches
--env <KEY=VALUE> Environment variable (repeatable)
--tmux Wrap the command in tmux with RGB-safe tuistory defaults
--record <path> Record from launch (.cast or .mp4)
Notes:
tuistory recording wraps the PTY so it must be set at launch.
true-input auto-selects: ghostty > kitty > alacritty.
Each true-input session gets an isolated Wayland runtime directory.
HELP
}
die() {
echo "tctl: $*" >&2
exit 1
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "required command not found: $1"
}
runtime_dir_value() {
if [[ -n "${XDG_RUNTIME_DIR:-}" ]]; then
printf '%s\n' "$XDG_RUNTIME_DIR"
else
printf '/run/user/%s\n' "$(id -u)"
fi
}
session_slug() {
printf '%s\n' "${1//[^[:alnum:]]/-}"
}
session_runtime_dir() {
printf '%s/tctl/%s\n' "$(runtime_dir_value)" "$(session_slug "$1")"
}
wait_for_wayland_socket() {
local pid="$1"
local socket_path="$2"
local timeout_ms="$3"
local deadline=$(( $(date +%s%3N) + timeout_ms ))
while (( $(date +%s%3N) <= deadline )); do
[[ -S "$socket_path" ]] && return 0
kill -0 "$pid" >/dev/null 2>&1 || return 1
sleep 0.05
done
return 1
}
collect_tree_pids() {
local root="$1"
local pid ppid
local -A children=()
while read -r pid ppid; do
children["$ppid"]+=" $pid"
done < <(ps -eo pid=,ppid= 2>/dev/null || true)
local -a result=("$root") queue=("$root")
local current
while (( ${#queue[@]} )); do
current="${queue[0]}"
queue=("${queue[@]:1}")
for pid in ${children[$current]:-}; do
result+=("$pid")
queue+=("$pid")
done
done
printf '%s\n' "${result[@]}"
}
# Tear down a true-input compositor and everything under it. Descendant
# enumeration alone misses processes that script(1) moved to a new session;
# a process-group kill alone misses those same processes AND anything
# reparented to init after the leader died. Do both, then escalate.
terminate_true_input_stack() {
local root="$1"
local grace_ms="${2:-2000}"
[[ -n "$root" ]] || return 0
local -a targets=()
mapfile -t targets < <(collect_tree_pids "$root")
local pid
kill -TERM -- "-$root" >/dev/null 2>&1 || true
for pid in "${targets[@]}"; do
kill -TERM "$pid" >/dev/null 2>&1 || true
done
local deadline=$(( $(date +%s%3N) + grace_ms ))
local alive=1
while (( $(date +%s%3N) <= deadline )); do
alive=0
kill -0 -- "-$root" >/dev/null 2>&1 && alive=1
if (( ! alive )); then
for pid in "${targets[@]}"; do
if kill -0 "$pid" >/dev/null 2>&1; then
alive=1
break
fi
done
fi
(( alive )) || break
sleep 0.05
done
if (( alive )); then
kill -KILL -- "-$root" >/dev/null 2>&1 || true
for pid in "${targets[@]}"; do
kill -KILL "$pid" >/dev/null 2>&1 || true
done
fi
}
# SIGINT first so wf-recorder finalizes the container; escalate if it hangs.
terminate_recorder_pid() {
local pid="$1"
local grace_ms="${2:-3000}"
[[ -n "$pid" ]] || return 0
kill -INT "$pid" >/dev/null 2>&1 || true
local deadline=$(( $(date +%s%3N) + grace_ms ))
while kill -0 "$pid" >/dev/null 2>&1 && (( $(date +%s%3N) <= deadline )); do
sleep 0.05
done
if kill -0 "$pid" >/dev/null 2>&1; then
kill -KILL "$pid" >/dev/null 2>&1 || true
fi
}
pid_is_self_or_ancestor() {
local candidate="$1"
local cur=$$ ppid
while [[ -n "$cur" && "$cur" != "0" && "$cur" != "1" ]]; do
[[ "$cur" == "$candidate" ]] && return 0
ppid="$(ps -o ppid= -p "$cur" 2>/dev/null | tr -d '[:space:]')" || return 1
[[ "$ppid" != "$cur" ]] || return 1
cur="$ppid"
done
return 1
}
# Belt-and-suspenders for sessions whose meta lost the compositor PID (a
# pre-fix launch bug left CAGE_PID empty for every recorded session). Match
# only the session's runner scripts ($dir/run-*.sh) -- matching the bare dir
# path would also hit unrelated processes that merely mention it in argv
# (an inspecting shell, an editor) -- and never kill ourselves or a caller.
terminate_session_strays() {
local session="$1"
local dir
dir="$(session_dir "$session")"
[[ -n "$dir" ]] || return 0
local stray
while read -r stray; do
[[ -n "$stray" ]] || continue
pid_is_self_or_ancestor "$stray" && continue
terminate_true_input_stack "$stray" 1000
done < <(pgrep -f -- "$dir/run-" 2>/dev/null || true)
}
quote_sh() {
printf '%q' "$1"
}
session_dir() {
printf '%s/%s\n' "$ROOT_DIR" "$1"
}
command_file() {
printf '%s/command.txt\n' "$(session_dir "$1")"
}
runner_file() {
printf '%s/run-child.sh\n' "$(session_dir "$1")"
}
tmux_config_file() {
printf '%s/tmux.conf\n' "$(session_dir "$1")"
}
tmux_runner_file() {
printf '%s/run-tmux-child.sh\n' "$(session_dir "$1")"
}
logged_runner_file() {
printf '%s/run-logged-child.sh\n' "$(session_dir "$1")"
}
tuistory_recording_runner_file() {
printf '%s/run-tuistory-recording.sh\n' "$(session_dir "$1")"
}
asciinema_pid_file() {
printf '%s/asciinema.pid\n' "$(session_dir "$1")"
}
provenance_file() {
printf '%s/provenance\n' "$(session_dir "$1")"
}
meta_file() {
printf '%s/meta\n' "$(session_dir "$1")"
}
load_meta() {
local session="$1"
local meta
meta="$(meta_file "$session")"
[[ -f "$meta" ]] || die "unknown session: $session"
# shellcheck disable=SC1090
source "$meta"
}
write_meta() {
local session="$1"
local dir
dir="$(session_dir "$session")"
mkdir -p "$dir"
cat > "$(meta_file "$session")" <<META
SESSION=$(quote_sh "${SESSION:-$session}")
BACKEND=$(quote_sh "${BACKEND:-}")
TERMINAL=$(quote_sh "${TERMINAL:-}")
COMMAND=$(quote_sh "${COMMAND:-}")
COLS=$(quote_sh "${COLS:-120}")
ROWS=$(quote_sh "${ROWS:-36}")
CWD=$(quote_sh "${CWD:-}")
WAYLAND_DISPLAY_NAME=$(quote_sh "${WAYLAND_DISPLAY_NAME:-}")
RUNTIME_DIR=$(quote_sh "${RUNTIME_DIR:-}")
RUNNER_FILE=$(quote_sh "${RUNNER_FILE:-}")
LOGGED_RUNNER_FILE=$(quote_sh "${LOGGED_RUNNER_FILE:-}")
LOG_FILE=$(quote_sh "${LOG_FILE:-}")
TMUX_SOCKET_NAME=$(quote_sh "${TMUX_SOCKET_NAME:-}")
RECORD_PATH=$(quote_sh "${RECORD_PATH:-}")
RECORDER_PID=$(quote_sh "${RECORDER_PID:-}")
CAGE_PID=$(quote_sh "${CAGE_PID:-}")
WARMED_UP=$(quote_sh "${WARMED_UP:-0}")
REPO_ROOT=$(quote_sh "${REPO_ROOT:-}")
META
}
command_uses_droid_dev() {
local command="$1"
local token normalized tokens=()
read -r -a tokens <<< "$command"
for token in "${tokens[@]}"; do
if [[ "$token" == "env" ]]; then
continue
fi
if [[ "$token" == *=* && "$token" != */*=* ]]; then
continue
fi
normalized="${token##*/}"
normalized="${normalized//\"/}"
normalized="${normalized//\'/}"
normalized="${normalized%%[;&|()<>]*}"
if [[ "$normalized" == "droid-dev" ]]; then
return 0
fi
done
return 1
}
launch_env_value() {
local expected_key="$1"
shift
local kv
for kv in "$@"; do
if [[ "${kv%%=*}" == "$expected_key" ]]; then
printf '%s\n' "${kv#*=}"
return 0
fi
done
return 1
}
require_git_worktree_root() {
local repo_root="$1"
[[ "$repo_root" = /* ]] || die "--repo-root must be an absolute path: $repo_root"
[[ -d "$repo_root" ]] || die "--repo-root does not exist: $repo_root"
git -C "$repo_root" rev-parse --show-toplevel >/dev/null 2>&1 \
|| die "--repo-root is not a git worktree: $repo_root"
}
write_provenance() {
local session="$1"
local repo_root="$2"
local file branch commit
file="$(provenance_file "$session")"
if [[ -z "$repo_root" ]]; then
rm -f "$file"
return 0
fi
branch="$(git -C "$repo_root" rev-parse --abbrev-ref HEAD 2>/dev/null || printf 'unknown')"
commit="$(git -C "$repo_root" rev-parse HEAD 2>/dev/null || printf 'unknown')"
cat > "$file" <<PROVENANCE
repo_root=$repo_root
branch=$branch
commit=$commit
PROVENANCE
}
resolve_backend() {
local backend="$1"
local terminal_override="$2"
case "$backend" in
tuistory)
BACKEND="tuistory"
TERMINAL=""
;;
true-input)
BACKEND="true-input"
if [[ -n "$terminal_override" ]]; then
TERMINAL="$terminal_override"
elif command -v ghostty >/dev/null 2>&1; then
TERMINAL="ghostty"
elif command -v kitty >/dev/null 2>&1; then
TERMINAL="kitty"
elif command -v alacritty >/dev/null 2>&1; then
TERMINAL="alacritty"
else
die "no true-input terminal found (ghostty, kitty, alacritty)"
fi
;;
ghostty|kitty|alacritty)
BACKEND="true-input"
TERMINAL="$backend"
;;
*)
die "unsupported backend: $backend"
;;
esac
}
save_session_state() {
SESSION="$1"
write_meta "$1"
}
write_session_runner() {
local session="$1"
local command="$2"
local cwd="$3"
shift 3
local envs=("$@")
local dir runner command_path
dir="$(session_dir "$session")"
runner="$(runner_file "$session")"
command_path="$(command_file "$session")"
mkdir -p "$dir"
printf '%s\n' "$command" > "$command_path"
{
echo '#!/usr/bin/env bash'
echo 'set -euo pipefail'
printf 'COMMAND_FILE=%q\n' "$command_path"
if [[ -n "$cwd" ]]; then
printf 'cd %q\n' "$cwd"
fi
local kv name value
for kv in "${envs[@]}"; do
[[ "$kv" == *=* ]] || die "env must be KEY=VALUE: $kv"
name="${kv%%=*}"
value="${kv#*=}"
[[ "$name" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] \
|| die "invalid environment variable name: $name"
printf 'export %s=%q\n' "$name" "$value"
done
# shellcheck disable=SC2016
echo 'if [[ -z "${TERM:-}" ]] || ! infocmp "$TERM" >/dev/null 2>&1; then'
echo ' export TERM=xterm-256color'
echo 'fi'
# Force color output in virtual PTYs: Node.js/chalk suppresses colors
# when the PTY doesn't advertise support. FORCE_COLOR=3 = truecolor.
# Prepend as env-prefix on exec so they override anything the login
# shell profile may have set (e.g. FORCE_COLOR=0 from CI tooling).
# shellcheck disable=SC2016
echo 'exec env FORCE_COLOR=3 COLORTERM=truecolor bash -lc "$(< "$COMMAND_FILE")"'
} > "$runner"
chmod +x "$runner"
RUNNER_FILE="$runner"
}
write_tmux_runner() {
local session="$1"
local runner tmux_config tmux_runner tmux_socket_name
runner="${RUNNER_FILE:-$(runner_file "$session")}"
tmux_config="$(tmux_config_file "$session")"
tmux_runner="$(tmux_runner_file "$session")"
tmux_socket_name="tctl-$(session_slug "$session")"
cat > "$tmux_config" <<'TMUX'
set -g default-terminal "tmux-256color"
set -qas terminal-features ",xterm-256color:RGB"
set -as terminal-overrides ",xterm-256color:Tc"
set-environment -g COLORTERM truecolor
set -g escape-time 50
set -g mode-keys vi
TMUX
{
echo '#!/usr/bin/env bash'
echo 'set -euo pipefail'
printf 'RUNNER_FILE=%q\n' "$runner"
printf 'TMUX_CONFIG=%q\n' "$tmux_config"
printf 'TMUX_SOCKET_NAME=%q\n' "$tmux_socket_name"
printf 'TMUX_SESSION_NAME=%q\n' "$tmux_socket_name"
echo 'export TERM=xterm-256color'
echo 'exec tmux -f "$TMUX_CONFIG" -L "$TMUX_SOCKET_NAME" new-session -s "$TMUX_SESSION_NAME" "$RUNNER_FILE"'
} > "$tmux_runner"
chmod +x "$tmux_runner"
RUNNER_FILE="$tmux_runner"
TMUX_SOCKET_NAME="$tmux_socket_name"
}
write_logged_runner() {
local session="$1"
local log_file="$2"
local runner logged_runner
runner="${RUNNER_FILE:-$(runner_file "$session")}"
logged_runner="$(logged_runner_file "$session")"
{
echo '#!/usr/bin/env bash'
echo 'set -euo pipefail'
printf 'LOG_FILE=%q\n' "$log_file"
printf 'RUNNER_FILE=%q\n' "$runner"
# shellcheck disable=SC2016
echo 'exec script -q -f -O "$LOG_FILE" -c "$RUNNER_FILE"'
} > "$logged_runner"
chmod +x "$logged_runner"
LOGGED_RUNNER_FILE="$logged_runner"
}
write_tuistory_recording_runner() {
local session="$1"
local record_path="$2"
local runner pid_file wrapper
runner="${RUNNER_FILE:-$(runner_file "$session")}"
pid_file="$(asciinema_pid_file "$session")"
wrapper="$(tuistory_recording_runner_file "$session")"
{
echo '#!/usr/bin/env bash'
echo 'set -euo pipefail'
printf 'RUNNER_FILE=%q\n' "$runner"
printf 'RECORD_PATH=%q\n' "$record_path"
printf 'PID_FILE=%q\n' "$pid_file"
cat <<'SH'
# IMPORTANT: run asciinema in the foreground so it owns the session TTY.
# Running it in the background breaks stdin forwarding to interactive TUIs
# (Ink/React), causing typed keys to be echoed by the outer PTY instead of
# reaching the child process.
rm -f "$PID_FILE"
echo "$$" > "$PID_FILE"
exec asciinema rec --overwrite --command "$RUNNER_FILE" "$RECORD_PATH"
SH
} > "$wrapper"
chmod +x "$wrapper"
printf '%s\n' "$wrapper"
}
pid_is_expected_asciinema_recording() {
local pid="$1"
local expected_record_path="$2"
[[ "$pid" =~ ^[0-9]+$ ]] || return 1
local cmdline=""
if [[ -r "/proc/$pid/cmdline" ]]; then
cmdline="$(tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null || true)"
else
cmdline="$(ps -p "$pid" -o command= 2>/dev/null || true)"
fi
[[ -n "$cmdline" ]] || return 1
[[ "$cmdline" == *"asciinema"* ]] || return 1
[[ "$cmdline" == *" rec "* ]] || return 1
if [[ -n "$expected_record_path" ]]; then
[[ "$cmdline" == *"$expected_record_path"* ]] || return 1
fi
return 0
}
cleanup_tuistory_asciinema() {
local session="$1"
local record_path="$2"
local pid_file pid
pid_file="$(asciinema_pid_file "$session")"
pid="$(cat "$pid_file" 2>/dev/null || true)"
if [[ -n "$pid" ]] && kill -0 "$pid" >/dev/null 2>&1; then
if pid_is_expected_asciinema_recording "$pid" "$record_path"; then
kill -TERM "$pid" >/dev/null 2>&1 || true
local deadline=$(( $(date +%s%3N) + 2000 ))
while kill -0 "$pid" >/dev/null 2>&1 && (( $(date +%s%3N) <= deadline )); do
sleep 0.05
done
if kill -0 "$pid" >/dev/null 2>&1; then
kill -KILL "$pid" >/dev/null 2>&1 || true
fi
fi
fi
rm -f "$pid_file"
if [[ -z "$record_path" ]]; then
return 0
fi
local orphan_pid orphan_ppid orphan_comm orphan_args
while read -r orphan_pid orphan_ppid orphan_comm orphan_args; do
[[ "$orphan_ppid" == "1" ]] || continue
[[ "$orphan_comm" == "asciinema" || "$orphan_args" == *"asciinema"* ]] || continue
[[ "$orphan_args" == *" rec "* ]] || continue
[[ "$orphan_args" == *"$record_path"* ]] || continue
kill -TERM "$orphan_pid" >/dev/null 2>&1 || true
local deadline=$(( $(date +%s%3N) + 2000 ))
while kill -0 "$orphan_pid" >/dev/null 2>&1 && (( $(date +%s%3N) <= deadline )); do
sleep 0.05
done
if kill -0 "$orphan_pid" >/dev/null 2>&1; then
kill -KILL "$orphan_pid" >/dev/null 2>&1 || true
fi
done < <(ps -eo pid=,ppid=,comm=,args= 2>/dev/null || true)
}
launch_tuistory() {
local session="$1"
local cols="$2"
local rows="$3"
local record_path="$4"
require_cmd tuistory
local launch_cmd="$RUNNER_FILE"
if [[ -n "$record_path" ]]; then
require_cmd asciinema
cleanup_tuistory_asciinema "$session" "$record_path"
launch_cmd="$(write_tuistory_recording_runner "$session" "$record_path")"
fi
local args=(launch "$launch_cmd" -s "$session" --cols "$cols" --rows "$rows")
tuistory "${args[@]}"
}
launch_true_input() {
local session="$1"
local record_path="$2"
require_cmd cage
require_cmd wtype
require_cmd script
require_cmd setsid
require_cmd "$TERMINAL"
# Fail before the compositor starts, not after: a die inside the
# recording path would strand a live cage.
[[ -z "$record_path" ]] || require_cmd wf-recorder
local dir log_file terminal_cmd runtime_dir socket_path
dir="$(session_dir "$session")"
runtime_dir="$(session_runtime_dir "$session")"
socket_path="$runtime_dir/wayland-0"
log_file="$dir/pty.log"
mkdir -p "$dir"
: > "$log_file"
rm -rf "$runtime_dir"
mkdir -p "$runtime_dir"
chmod 700 "$runtime_dir"
write_logged_runner "$session" "$log_file"
case "$TERMINAL" in
ghostty)
terminal_cmd=(ghostty --window-decoration=false --confirm-close-surface=false -e "$LOGGED_RUNNER_FILE")
;;
kitty)
terminal_cmd=(kitty "$LOGGED_RUNNER_FILE")
;;
alacritty)
terminal_cmd=(alacritty -e "$LOGGED_RUNNER_FILE")
;;
*)
die "unsupported true-input terminal: $TERMINAL"
;;
esac
WAYLAND_DISPLAY_NAME="wayland-0"
RUNTIME_DIR="$runtime_dir"
LOG_FILE="$log_file"
RECORD_PATH="$record_path"
RECORDER_PID=""
CAGE_PID=""
WARMED_UP="0"
save_session_state "$session"
# setsid: cage leads its own process group, so teardown can group-kill
# the whole stack even after members reparent to init.
XDG_RUNTIME_DIR="$runtime_dir" \
WLR_BACKENDS="${WLR_BACKENDS:-headless}" \
WLR_LIBINPUT_NO_DEVICES="${WLR_LIBINPUT_NO_DEVICES:-1}" \
setsid cage -- "${terminal_cmd[@]}" >/dev/null 2>&1 &
CAGE_PID="$!"
if ! wait_for_wayland_socket "$CAGE_PID" "$socket_path" 5000; then
terminate_true_input_stack "$CAGE_PID" 1000
die "true-input compositor did not create $socket_path"
fi
# Persist CAGE_PID before anything below calls load_meta: the recording
# path reloads the meta file and would clobber it back to empty.
save_session_state "$session"
if [[ -n "$record_path" ]]; then
start_true_input_recording "$session" "$record_path"
fi
}
strip_log() {
local log_file="$1"
python3 - "$log_file" <<'PY'
import re
import sys
from pathlib import Path
path = Path(sys.argv[1])
if not path.exists():
raise SystemExit(0)
text = path.read_bytes().decode('utf-8', 'replace')
text = text.replace('\r\n', '\n').replace('\r', '\n')
text = re.sub(r'^Script started on .*$\n?', '', text, flags=re.M)
text = re.sub(r'^Script done on .*$\n?', '', text, flags=re.M)
text = re.sub(r'\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)', '', text)
text = re.sub(r'\x1bP.*?\x1b\\', '', text, flags=re.S)
text = re.sub(r'\x1b\[[0-?]*[ -/]*[@-~]', '', text)
text = re.sub(r'\x1b[@-_]', '', text)
text = text.replace('\x08', '')
text = ''.join(ch for ch in text if ch in ('\n', '\t') or ord(ch) >= 32)
print(text, end='')
PY
}
trim_text() {
python3 -c 'import sys
text = sys.stdin.read()
lines = text.splitlines()
while lines and not lines[-1].strip():
lines.pop()
print("\\n".join(line.rstrip() for line in lines))'
}
wait_for_pattern_in_log() {
local log_file="$1"
local pattern="$2"
local timeout_ms="$3"
local deadline=$(( $(date +%s%3N) + timeout_ms ))
while (( $(date +%s%3N) <= deadline )); do
local snapshot
snapshot="$(strip_log "$log_file")"
if SNAPSHOT_TEXT="$snapshot" python3 - "$pattern" <<'PY'
import re
import sys
import os
pattern = sys.argv[1]
text = os.environ.get("SNAPSHOT_TEXT", "")
if len(pattern) >= 2 and pattern.startswith('/') and pattern.rfind('/') > 0:
last = pattern.rfind('/')
body = pattern[1:last]
flags_str = pattern[last+1:]
flags = 0
if 'i' in flags_str:
flags |= re.I
matched = re.search(body, text, flags) is not None
else:
matched = pattern in text
raise SystemExit(0 if matched else 1)
PY
then
return 0
fi
sleep 0.1
done
return 1
}
wait_for_idle_in_log() {
local log_file="$1"
local timeout_ms="$2"
local deadline=$(( $(date +%s%3N) + timeout_ms ))
local stable_for=0
local last_size=-1
while (( $(date +%s%3N) <= deadline )); do
local size=0
[[ -f "$log_file" ]] && size=$(stat -c %s "$log_file")
if [[ "$size" == "$last_size" ]]; then
stable_for=$(( stable_for + 100 ))
if (( stable_for >= 500 )); then
return 0
fi
else
stable_for=0
last_size="$size"
fi
sleep 0.1
done
return 1
}
true_input_env() {
export WAYLAND_DISPLAY="$WAYLAND_DISPLAY_NAME"
export XDG_RUNTIME_DIR="${RUNTIME_DIR:-$(runtime_dir_value)}"
export WLR_BACKENDS="${WLR_BACKENDS:-headless}"
export WLR_LIBINPUT_NO_DEVICES="${WLR_LIBINPUT_NO_DEVICES:-1}"
}
ensure_true_input_warmup() {
local session="$1"
if [[ "${WARMED_UP:-0}" != "1" ]]; then
true_input_env
wtype -M shift -m shift >/dev/null 2>&1 || true
WARMED_UP="1"
save_session_state "$session"
fi
}
translate_key_name() {
case "$1" in
enter|return) printf 'Return' ;;
esc|escape) printf 'Escape' ;;
tab) printf 'Tab' ;;
space) printf 'space' ;;
backspace) printf 'BackSpace' ;;
delete) printf 'Delete' ;;
insert) printf 'Insert' ;;
up) printf 'Up' ;;
down) printf 'Down' ;;
left) printf 'Left' ;;
right) printf 'Right' ;;
home) printf 'Home' ;;
end) printf 'End' ;;
pageup) printf 'Page_Up' ;;
pagedown) printf 'Page_Down' ;;
*) printf '%s' "$1" ;;
esac
}
is_modifier() {
case "$1" in
ctrl|control|alt|shift|meta|super) return 0 ;;
*) return 1 ;;
esac
}
true_input_press() {
local keys=("$@")
true_input_env
if [[ ${#keys[@]} -eq 1 ]]; then
local key="${keys[0]}"
if [[ ${#key} -eq 1 ]]; then
wtype "$key"
return
fi
fi
local mods=()
local nonmods=()
local k
for k in "${keys[@]}"; do
if is_modifier "$k"; then
case "$k" in
control) mods+=(ctrl) ;;
super) mods+=(meta) ;;
*) mods+=("$k") ;;
esac
else
nonmods+=("$k")
fi
done
if [[ ${#nonmods[@]} -eq 0 ]]; then
die "press requires at least one non-modifier key"
fi
if [[ ${#nonmods[@]} -gt 1 ]]; then
die "true-input press currently supports one key chord at a time"
fi
local key_name="${nonmods[0]}"
if [[ ${#mods[@]} -eq 0 && ${#key_name} -eq 1 ]]; then
wtype "$key_name"
return
fi
local args=()
for k in "${mods[@]}"; do
args+=(-M "$k")
done
args+=(-k "$(translate_key_name "$key_name")")
local idx=$(( ${#mods[@]} - 1 ))
while (( idx >= 0 )); do
args+=(-m "${mods[$idx]}")
idx=$(( idx - 1 ))
done
wtype "${args[@]}"
}
start_true_input_recording() {
local session="$1"
local output="$2"
load_meta "$session"
[[ "$BACKEND" == "true-input" ]] || die "record start is only supported for true-input sessions; for tuistory, relaunch with --record"
[[ -z "$RECORDER_PID" ]] || die "recording already active for session: $session"
require_cmd wf-recorder
true_input_env
mkdir -p "$(dirname "$output")"
WAYLAND_DISPLAY="$WAYLAND_DISPLAY_NAME" wf-recorder -f "$output" >/dev/null 2>&1 &
RECORDER_PID="$!"
RECORD_PATH="$output"
save_session_state "$session"
}
stop_true_input_recording() {
local session="$1"
load_meta "$session"
[[ "$BACKEND" == "true-input" ]] || die "tuistory recordings stop when the session exits; use close to finalize the cast"
[[ -n "$RECORDER_PID" ]] || die "no active recorder for session: $session"
terminate_recorder_pid "$RECORDER_PID"
RECORDER_PID=""
save_session_state "$session"
}
cmd_launch() {
[[ $# -ge 1 ]] || die "launch requires a command string"
local command="$1"
shift
local session="default"
local backend="tuistory"
local terminal_override=""
local cols="120"
local rows="36"
local cwd=""
local repo_root=""
local tmux_enabled="0"
local record_path=""
local envs=()
while [[ $# -gt 0 ]]; do
case "$1" in
-s|--session)
session="$2"
shift 2
;;
--backend)
backend="$2"
shift 2
;;
--terminal)
terminal_override="$2"
shift 2
;;
--cols)
cols="$2"
shift 2
;;
--rows)
rows="$2"
shift 2
;;
--cwd)
cwd="$2"
shift 2
;;
--repo-root)
repo_root="$2"
shift 2
;;
--env)
envs+=("$2")
shift 2
;;
--tmux)
tmux_enabled="1"
shift
;;
--record)
record_path="$2"
shift 2
;;
-h|--help)
print_help
exit 0
;;
*)
die "unknown launch option: $1"
;;
esac
done
if [[ -z "$repo_root" ]]; then
repo_root="$(launch_env_value DROID_DEV_REPO_ROOT "${envs[@]}" || true)"
fi
if [[ -n "$repo_root" ]]; then
require_git_worktree_root "$repo_root"
if [[ -n "$cwd" && "$cwd" != "$repo_root" ]]; then
die "--cwd must match --repo-root when --repo-root is set"
fi
cwd="$repo_root"
fi
if command_uses_droid_dev "$command"; then
[[ -n "$repo_root" ]] \
|| die "droid-dev launches require --repo-root <worktree> or --env DROID_DEV_REPO_ROOT=<worktree>"
if ! launch_env_value DROID_DEV_REPO_ROOT "${envs[@]}" >/dev/null 2>&1; then
envs+=("DROID_DEV_REPO_ROOT=$repo_root")
fi
fi
[[ ! -e "$(session_dir "$session")/meta" ]] || die "session already exists: $session"
resolve_backend "$backend" "$terminal_override"
if [[ "$tmux_enabled" == "1" && "$BACKEND" != "tuistory" ]]; then
die "--tmux is only supported with --backend tuistory"
fi
write_session_runner "$session" "$command" "$cwd" "${envs[@]}"
TMUX_SOCKET_NAME=""
if [[ "$tmux_enabled" == "1" ]]; then
require_cmd tmux
write_tmux_runner "$session"
fi
SESSION="$session"
COMMAND="$command"
COLS="$cols"
ROWS="$rows"
CWD="$cwd"
WAYLAND_DISPLAY_NAME=""
LOG_FILE=""
LOGGED_RUNNER_FILE=""
RECORD_PATH="$record_path"
RECORDER_PID=""
CAGE_PID=""
RUNTIME_DIR=""
WARMED_UP="0"
REPO_ROOT="$repo_root"
save_session_state "$session"
write_provenance "$session" "$repo_root"
if [[ "$BACKEND" == "tuistory" ]]; then
launch_tuistory "$session" "$cols" "$rows" "$record_path"
else
launch_true_input "$session" "$record_path"
fi
}
cmd_sessions() {
find "$ROOT_DIR" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort
}
cmd_provenance() {
local session="$1"
load_meta "$session"
local file
file="$(provenance_file "$session")"
[[ -f "$file" ]] || die "no launch provenance recorded for session: $session"
cat "$file"
}