forked from manaflow-ai/cmux
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprograma-bash-integration.bash
More file actions
1124 lines (1003 loc) · 37.7 KB
/
Copy pathprograma-bash-integration.bash
File metadata and controls
1124 lines (1003 loc) · 37.7 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
# cmux shell integration for bash
# Cache which send tool is available to avoid repeated PATH lookups.
_PROGRAMA_SEND_TOOL=""
_cmux_detect_send_tool() {
if command -v ncat >/dev/null 2>&1; then
_PROGRAMA_SEND_TOOL=ncat
elif command -v socat >/dev/null 2>&1; then
_PROGRAMA_SEND_TOOL=socat
elif command -v nc >/dev/null 2>&1; then
_PROGRAMA_SEND_TOOL=nc
fi
}
# Detection deferred to after _cmux_fix_path (end of file).
_cmux_send() {
local payload="$1"
case "$_PROGRAMA_SEND_TOOL" in
ncat)
printf '%s\n' "$payload" | ncat -w 1 -U "$PROGRAMA_SOCKET_PATH" --send-only
;;
socat)
printf '%s\n' "$payload" | socat -T 1 - "UNIX-CONNECT:$PROGRAMA_SOCKET_PATH" >/dev/null 2>&1
;;
nc)
if printf '%s\n' "$payload" | nc -N -U "$PROGRAMA_SOCKET_PATH" >/dev/null 2>&1; then
:
else
printf '%s\n' "$payload" | nc -w 1 -U "$PROGRAMA_SOCKET_PATH" >/dev/null 2>&1 || true
fi
;;
esac
}
_cmux_socket_is_unix() {
[[ -n "$PROGRAMA_SOCKET_PATH" && -S "$PROGRAMA_SOCKET_PATH" ]]
}
_cmux_relay_cli_path() {
if [[ -n "${PROGRAMA_BUNDLED_CLI_PATH:-}" && -x "${PROGRAMA_BUNDLED_CLI_PATH}" ]]; then
printf '%s\n' "${PROGRAMA_BUNDLED_CLI_PATH}"
return 0
fi
command -v cmux 2>/dev/null
}
_cmux_socket_uses_remote_relay() {
[[ -n "$PROGRAMA_SOCKET_PATH" ]] || return 1
[[ "$PROGRAMA_SOCKET_PATH" == /* ]] && return 1
[[ "$PROGRAMA_SOCKET_PATH" == *:* ]] || return 1
[[ -n "$(_cmux_relay_cli_path)" ]]
}
_cmux_has_port_scan_transport() {
_cmux_socket_is_unix && return 0
_cmux_socket_uses_remote_relay
}
_cmux_json_escape() {
local value="$1"
value="${value//\\/\\\\}"
value="${value//\"/\\\"}"
value="${value//$'\n'/\\n}"
value="${value//$'\r'/\\r}"
value="${value//$'\t'/\\t}"
printf '%s\n' "$value"
}
# Build a single-line v2 JSON-RPC request frame for the direct-socket
# (fire-and-forget) path. `params_json` must already be a well-formed JSON
# object string (see call sites, which use _cmux_json_escape on any
# user-controlled values before interpolating them).
_cmux_json_rpc_frame() {
local method="$1"
local params_json="$2"
printf '%s\n' "{\"id\":1,\"method\":\"$method\",\"params\":$params_json}"
}
_cmux_relay_rpc_bg() {
local method="$1"
local params="$2"
local relay_cli=""
_cmux_socket_uses_remote_relay || return 1
relay_cli="$(_cmux_relay_cli_path)" || return 1
{
"$relay_cli" rpc "$method" "$params" >/dev/null 2>&1 || true
} >/dev/null 2>&1 &
disown 2>/dev/null || true
}
_cmux_relay_rpc() {
local method="$1"
local params="$2"
local relay_cli=""
local response=""
_cmux_socket_uses_remote_relay || return 1
# Relay `cmux rpc` exits nonzero on server error. The real remote CLI prints
# only the JSON result payload on success, while some test stubs return the
# full `{"ok":...}` envelope. Retry only on explicit `ok:false`.
relay_cli="$(_cmux_relay_cli_path)" || return 1
response="$("$relay_cli" rpc "$method" "$params" 2>/dev/null)" || return 1
response="${response//$'\n'/}"
response="${response//$'\r'/}"
[[ "$response" == *'"ok":false'* || "$response" == *'"ok": false'* ]] && return 1
return 0
}
_cmux_relay_workspace_id() {
if [[ -n "$PROGRAMA_WORKSPACE_ID" ]]; then
printf '%s\n' "$PROGRAMA_WORKSPACE_ID"
return 0
fi
[[ -n "$PROGRAMA_TAB_ID" ]] || return 1
printf '%s\n' "$PROGRAMA_TAB_ID"
}
_cmux_report_tty_via_relay() {
_cmux_socket_uses_remote_relay || return 1
local workspace_id=""
workspace_id="$(_cmux_relay_workspace_id)" || return 1
[[ -n "$_PROGRAMA_TTY_NAME" ]] || return 1
local tty_name_json params
tty_name_json="$(_cmux_json_escape "$_PROGRAMA_TTY_NAME")"
params="{\"workspace_id\":\"$workspace_id\",\"tty_name\":\"$tty_name_json\""
if [[ -n "$PROGRAMA_PANEL_ID" ]]; then
params+=",\"surface_id\":\"$PROGRAMA_PANEL_ID\""
fi
params+="}"
_cmux_relay_rpc "surface.report_tty" "$params"
}
_cmux_ports_kick_via_relay() {
local reason="${1:-command}"
_cmux_socket_uses_remote_relay || return 1
local workspace_id=""
workspace_id="$(_cmux_relay_workspace_id)" || return 1
local params="{\"workspace_id\":\"$workspace_id\",\"reason\":\"$reason\""
if [[ -n "$PROGRAMA_PANEL_ID" ]]; then
params+=",\"surface_id\":\"$PROGRAMA_PANEL_ID\""
fi
params+="}"
_cmux_relay_rpc_bg "surface.ports_kick" "$params"
}
_cmux_restore_scrollback_once() {
local path="${PROGRAMA_RESTORE_SCROLLBACK_FILE:-}"
[[ -n "$path" ]] || return 0
unset PROGRAMA_RESTORE_SCROLLBACK_FILE
if [[ -r "$path" ]]; then
/bin/cat -- "$path" 2>/dev/null || true
/bin/rm -f -- "$path" >/dev/null 2>&1 || true
fi
}
_cmux_restore_scrollback_once
_PROGRAMA_CLAUDE_WRAPPER="${_PROGRAMA_CLAUDE_WRAPPER:-}"
_cmux_install_claude_wrapper() {
local integration_dir="${PROGRAMA_SHELL_INTEGRATION_DIR:-}"
local existing_type=""
[[ -n "$integration_dir" ]] || return 0
integration_dir="${integration_dir%/}"
local bundle_dir="${integration_dir%/shell-integration}"
local wrapper_path="$bundle_dir/bin/claude"
[[ -x "$wrapper_path" ]] || return 0
existing_type="$(type -t claude 2>/dev/null || true)"
case "$existing_type" in
alias|function)
return 0
;;
esac
# Keep the bundled claude wrapper ahead of later PATH mutations. Install it
# via eval so an existing `alias claude=...` cannot break parsing.
_PROGRAMA_CLAUDE_WRAPPER="$wrapper_path"
unalias claude >/dev/null 2>&1 || true
eval 'claude() { "$_PROGRAMA_CLAUDE_WRAPPER" "$@"; }'
}
_cmux_install_claude_wrapper
_cmux_now() {
printf '%s\n' "${EPOCHSECONDS:-$SECONDS}"
}
# Throttle heavy work to avoid prompt latency.
_PROGRAMA_PWD_LAST_PWD="${_PROGRAMA_PWD_LAST_PWD:-}"
_PROGRAMA_GIT_LAST_PWD="${_PROGRAMA_GIT_LAST_PWD:-}"
_PROGRAMA_GIT_LAST_RUN="${_PROGRAMA_GIT_LAST_RUN:-0}"
_PROGRAMA_GIT_JOB_PID="${_PROGRAMA_GIT_JOB_PID:-}"
_PROGRAMA_GIT_JOB_STARTED_AT="${_PROGRAMA_GIT_JOB_STARTED_AT:-0}"
_PROGRAMA_GIT_HEAD_LAST_PWD="${_PROGRAMA_GIT_HEAD_LAST_PWD:-}"
_PROGRAMA_GIT_HEAD_PATH="${_PROGRAMA_GIT_HEAD_PATH:-}"
_PROGRAMA_GIT_HEAD_SIGNATURE="${_PROGRAMA_GIT_HEAD_SIGNATURE:-}"
_PROGRAMA_PR_POLL_PID="${_PROGRAMA_PR_POLL_PID:-}"
_PROGRAMA_PR_POLL_PWD="${_PROGRAMA_PR_POLL_PWD:-}"
_PROGRAMA_PR_LAST_BRANCH="${_PROGRAMA_PR_LAST_BRANCH:-}"
_PROGRAMA_PR_NO_PR_BRANCH="${_PROGRAMA_PR_NO_PR_BRANCH:-}"
_PROGRAMA_PR_POLL_INTERVAL="${_PROGRAMA_PR_POLL_INTERVAL:-45}"
_PROGRAMA_PR_FORCE="${_PROGRAMA_PR_FORCE:-0}"
_PROGRAMA_PR_DEBUG="${_PROGRAMA_PR_DEBUG:-0}"
_PROGRAMA_ASYNC_JOB_TIMEOUT="${_PROGRAMA_ASYNC_JOB_TIMEOUT:-20}"
_PROGRAMA_PORTS_LAST_RUN="${_PROGRAMA_PORTS_LAST_RUN:-0}"
_PROGRAMA_SHELL_ACTIVITY_LAST="${_PROGRAMA_SHELL_ACTIVITY_LAST:-}"
_PROGRAMA_TTY_NAME="${_PROGRAMA_TTY_NAME:-}"
_PROGRAMA_TTY_REPORTED="${_PROGRAMA_TTY_REPORTED:-0}"
_PROGRAMA_TMUX_PUSH_SIGNATURE="${_PROGRAMA_TMUX_PUSH_SIGNATURE:-}"
_PROGRAMA_TMUX_PULL_SIGNATURE="${_PROGRAMA_TMUX_PULL_SIGNATURE:-}"
_PROGRAMA_TMUX_SYNC_KEYS=(
PROGRAMA_BUNDLED_CLI_PATH
PROGRAMA_BUNDLE_ID
CMUXD_UNIX_PATH
CMUXTERM_REPO_ROOT
PROGRAMA_DEBUG_LOG
PROGRAMA_LOAD_GHOSTTY_ZSH_INTEGRATION
PROGRAMA_PORT
PROGRAMA_PORT_END
PROGRAMA_PORT_RANGE
PROGRAMA_REMOTE_DAEMON_ALLOW_LOCAL_BUILD
PROGRAMA_SHELL_INTEGRATION
PROGRAMA_SHELL_INTEGRATION_DIR
PROGRAMA_SOCKET_ENABLE
PROGRAMA_SOCKET_MODE
PROGRAMA_SOCKET_PATH
PROGRAMA_TAB_ID
PROGRAMA_TAG
PROGRAMA_WORKSPACE_ID
)
_PROGRAMA_TMUX_SURFACE_SCOPED_KEYS=(
PROGRAMA_PANEL_ID
PROGRAMA_SURFACE_ID
)
_cmux_tmux_sync_key_is_managed() {
local candidate="$1"
local key
for key in "${_PROGRAMA_TMUX_SYNC_KEYS[@]}"; do
[[ "$key" == "$candidate" ]] && return 0
done
return 1
}
_cmux_tmux_shell_env_signature() {
local key value first=1
for key in "${_PROGRAMA_TMUX_SYNC_KEYS[@]}"; do
value="${!key}"
[[ -n "$value" ]] || continue
if (( first )); then
printf '%s=%s' "$key" "$value"
first=0
else
printf '\037%s=%s' "$key" "$value"
fi
done
}
_cmux_tmux_publish_cmux_environment() {
[[ -z "$TMUX" ]] || return 0
command -v tmux >/dev/null 2>&1 || return 0
local signature
signature="$(_cmux_tmux_shell_env_signature)"
[[ -n "$signature" ]] || return 0
[[ "$signature" == "$_PROGRAMA_TMUX_PUSH_SIGNATURE" ]] && return 0
local key value
for key in "${_PROGRAMA_TMUX_SYNC_KEYS[@]}"; do
value="${!key}"
[[ -n "$value" ]] || continue
tmux set-environment -g "$key" "$value" >/dev/null 2>&1 || return 0
done
for key in "${_PROGRAMA_TMUX_SURFACE_SCOPED_KEYS[@]}"; do
tmux set-environment -gu "$key" >/dev/null 2>&1 || return 0
done
_PROGRAMA_TMUX_PUSH_SIGNATURE="$signature"
}
_cmux_tmux_refresh_cmux_environment() {
[[ -n "$TMUX" ]] || return 0
command -v tmux >/dev/null 2>&1 || return 0
local output filtered line key value did_change=0
output="$(tmux show-environment -g 2>/dev/null)" || return 0
while IFS= read -r line; do
[[ "$line" == PROGRAMA_* ]] || continue
key="${line%%=*}"
_cmux_tmux_sync_key_is_managed "$key" || continue
filtered+="${line}"$'\n'
done <<< "$output"
[[ -n "$filtered" ]] || return 0
[[ "$filtered" == "$_PROGRAMA_TMUX_PULL_SIGNATURE" ]] && return 0
while IFS= read -r line; do
[[ "$line" == PROGRAMA_* ]] || continue
key="${line%%=*}"
_cmux_tmux_sync_key_is_managed "$key" || continue
value="${line#*=}"
if [[ "${!key}" != "$value" ]]; then
printf -v "$key" '%s' "$value"
export "$key"
did_change=1
fi
done <<< "$filtered"
_PROGRAMA_TMUX_PULL_SIGNATURE="$filtered"
if (( did_change )); then
_PROGRAMA_TTY_REPORTED=0
_PROGRAMA_SHELL_ACTIVITY_LAST=""
_PROGRAMA_PWD_LAST_PWD=""
_PROGRAMA_GIT_LAST_PWD=""
_PROGRAMA_GIT_HEAD_LAST_PWD=""
_PROGRAMA_GIT_HEAD_PATH=""
_PROGRAMA_GIT_HEAD_SIGNATURE=""
_PROGRAMA_PR_FORCE=1
_cmux_stop_pr_poll_loop
fi
}
_cmux_tmux_sync_cmux_environment() {
if [[ -n "$TMUX" ]]; then
_cmux_tmux_refresh_cmux_environment
else
_cmux_tmux_publish_cmux_environment
fi
}
_cmux_git_resolve_head_path() {
# Resolve the HEAD file path without invoking git (fast; works for worktrees).
local dir="$PWD"
while :; do
if [[ -d "$dir/.git" ]]; then
printf '%s\n' "$dir/.git/HEAD"
return 0
fi
if [[ -f "$dir/.git" ]]; then
local line gitdir
IFS= read -r line < "$dir/.git" || line=""
if [[ "$line" == gitdir:* ]]; then
gitdir="${line#gitdir:}"
gitdir="${gitdir## }"
gitdir="${gitdir%% }"
[[ -n "$gitdir" ]] || return 1
[[ "$gitdir" != /* ]] && gitdir="$dir/$gitdir"
printf '%s\n' "$gitdir/HEAD"
return 0
fi
fi
[[ "$dir" == "/" || -z "$dir" ]] && break
dir="$(dirname "$dir")"
done
return 1
}
_cmux_git_head_signature() {
local head_path="$1"
[[ -n "$head_path" && -r "$head_path" ]] || return 1
local line
IFS= read -r line < "$head_path" || return 1
printf '%s\n' "$line"
}
_cmux_report_tty_payload() {
[[ -n "$PROGRAMA_TAB_ID" ]] || return 0
[[ -n "$_PROGRAMA_TTY_NAME" ]] || return 0
local workspace_id="" tty_name_json params
workspace_id="$(_cmux_relay_workspace_id)" || workspace_id="$PROGRAMA_TAB_ID"
tty_name_json="$(_cmux_json_escape "$_PROGRAMA_TTY_NAME")"
params="{\"workspace_id\":\"$workspace_id\",\"tty_name\":\"$tty_name_json\""
if [[ -z "$TMUX" ]]; then
[[ -n "$PROGRAMA_PANEL_ID" ]] || return 0
params+=",\"surface_id\":\"$PROGRAMA_PANEL_ID\""
fi
params+="}"
_cmux_json_rpc_frame "surface.report_tty" "$params"
}
_cmux_report_tty_once() {
# Send the TTY name to the app once per session so the batched port scanner
# knows which TTY belongs to this panel.
(( _PROGRAMA_TTY_REPORTED )) && return 0
_cmux_has_port_scan_transport || return 0
if _cmux_socket_is_unix; then
local payload=""
payload="$(_cmux_report_tty_payload)"
[[ -n "$payload" ]] || return 0
_PROGRAMA_TTY_REPORTED=1
{
_cmux_send "$payload"
} >/dev/null 2>&1 & disown
else
[[ -n "$_PROGRAMA_TTY_NAME" ]] || return 0
# Keep the first relay TTY report synchronous so the server can resolve
# the target surface before command-start kicks begin their scan burst.
_cmux_report_tty_via_relay || return 0
_PROGRAMA_TTY_REPORTED=1
fi
}
_cmux_report_shell_activity_state() {
local state="$1"
[[ -n "$state" ]] || return 0
[[ -S "$PROGRAMA_SOCKET_PATH" ]] || return 0
[[ -n "$PROGRAMA_TAB_ID" ]] || return 0
[[ -n "$PROGRAMA_PANEL_ID" ]] || return 0
[[ "$_PROGRAMA_SHELL_ACTIVITY_LAST" == "$state" ]] && return 0
_PROGRAMA_SHELL_ACTIVITY_LAST="$state"
local workspace_id="" state_json params
workspace_id="$(_cmux_relay_workspace_id)" || workspace_id="$PROGRAMA_TAB_ID"
state_json="$(_cmux_json_escape "$state")"
params="{\"workspace_id\":\"$workspace_id\",\"surface_id\":\"$PROGRAMA_PANEL_ID\",\"state\":\"$state_json\"}"
(
_cmux_send "$(_cmux_json_rpc_frame "surface.report_shell_state" "$params")"
) >/dev/null 2>&1 &
disown 2>/dev/null
}
_cmux_ports_kick() {
local reason="${1:-command}"
# Lightweight: just tell the app to run a batched scan for this panel.
# The app coalesces kicks across all panels and runs a single ps+lsof.
_cmux_has_port_scan_transport || return 0
[[ -n "$PROGRAMA_TAB_ID" ]] || return 0
if _cmux_socket_is_unix; then
[[ -n "$PROGRAMA_PANEL_ID" ]] || return 0
fi
_PROGRAMA_PORTS_LAST_RUN="$(_cmux_now)"
if _cmux_socket_is_unix; then
local workspace_id="" reason_json params
workspace_id="$(_cmux_relay_workspace_id)" || workspace_id="$PROGRAMA_TAB_ID"
reason_json="$(_cmux_json_escape "$reason")"
params="{\"workspace_id\":\"$workspace_id\",\"surface_id\":\"$PROGRAMA_PANEL_ID\",\"reason\":\"$reason_json\"}"
{
_cmux_send "$(_cmux_json_rpc_frame "surface.ports_kick" "$params")"
} >/dev/null 2>&1 & disown
else
_cmux_ports_kick_via_relay "$reason"
fi
}
_cmux_clear_pr_for_panel() {
[[ -S "$PROGRAMA_SOCKET_PATH" ]] || return 0
[[ -n "$PROGRAMA_TAB_ID" ]] || return 0
[[ -n "$PROGRAMA_PANEL_ID" ]] || return 0
local workspace_id="" params
workspace_id="$(_cmux_relay_workspace_id)" || workspace_id="$PROGRAMA_TAB_ID"
params="{\"workspace_id\":\"$workspace_id\",\"surface_id\":\"$PROGRAMA_PANEL_ID\"}"
# Synchronous: must arrive before the next report_pr from the poll loop.
_cmux_send "$(_cmux_json_rpc_frame "surface.clear_pr" "$params")"
}
_cmux_pr_output_indicates_no_pull_request() {
local output="$1"
output="$(printf '%s' "$output" | tr '[:upper:]' '[:lower:]')"
[[ "$output" == *"no pull requests found"* \
|| "$output" == *"no pull request found"* \
|| "$output" == *"no pull requests associated"* \
|| "$output" == *"no pull request associated"* ]]
}
_cmux_github_repo_slug_for_path() {
local repo_path="$1"
local remote_url="" path_part=""
[[ -n "$repo_path" ]] || return 0
remote_url="$(git -C "$repo_path" remote get-url origin 2>/dev/null)"
[[ -n "$remote_url" ]] || return 0
case "$remote_url" in
git@github.com:*)
path_part="${remote_url#git@github.com:}"
;;
ssh://git@github.com/*)
path_part="${remote_url#ssh://git@github.com/}"
;;
https://github.com/*)
path_part="${remote_url#https://github.com/}"
;;
http://github.com/*)
path_part="${remote_url#http://github.com/}"
;;
git://github.com/*)
path_part="${remote_url#git://github.com/}"
;;
*)
return 0
;;
esac
path_part="${path_part%.git}"
[[ "$path_part" == */* ]] || return 0
printf '%s\n' "$path_part"
}
_cmux_pr_cache_prefix() {
[[ -n "$PROGRAMA_PANEL_ID" ]] || return 1
printf '%s\n' "/tmp/cmux-pr-cache-${PROGRAMA_PANEL_ID}"
}
_cmux_pr_force_signal_path() {
[[ -n "$PROGRAMA_PANEL_ID" ]] || return 1
printf '%s\n' "/tmp/cmux-pr-force-${PROGRAMA_PANEL_ID}"
}
_cmux_pr_debug_log() {
(( _PROGRAMA_PR_DEBUG )) || return 0
local branch="$1"
local event="$2"
local now
now="$(_cmux_now)"
printf '%s\tbranch=%s\tevent=%s\n' "$now" "$branch" "$event" >> /tmp/cmux-pr-debug.log
}
_cmux_pr_cache_clear() {
local prefix=""
prefix="$(_cmux_pr_cache_prefix 2>/dev/null || true)"
if [[ -n "$prefix" ]]; then
/bin/rm -f -- \
"${prefix}.branch" \
"${prefix}.repo" \
"${prefix}.result" \
"${prefix}.timestamp" \
"${prefix}.no-pr-branch" \
>/dev/null 2>&1 || true
fi
_PROGRAMA_PR_LAST_BRANCH=""
_PROGRAMA_PR_NO_PR_BRANCH=""
}
_cmux_pr_request_probe() {
local signal_path=""
signal_path="$(_cmux_pr_force_signal_path 2>/dev/null || true)"
[[ -n "$signal_path" ]] || return 0
: >| "$signal_path"
}
_cmux_report_pr_for_path() {
local repo_path="$1"
local force_probe="${2:-0}"
[[ -n "$repo_path" ]] || {
_cmux_pr_cache_clear
_cmux_clear_pr_for_panel
return 0
}
[[ -d "$repo_path" ]] || {
_cmux_pr_cache_clear
_cmux_clear_pr_for_panel
return 0
}
[[ -S "$PROGRAMA_SOCKET_PATH" ]] || return 0
[[ -n "$PROGRAMA_TAB_ID" ]] || return 0
[[ -n "$PROGRAMA_PANEL_ID" ]] || return 0
local branch repo_slug="" gh_output="" gh_error="" err_file="" gh_status number state url status_opt=""
local now prefix="" branch_file="" repo_file="" result_file="" timestamp_file="" no_pr_branch_file=""
local cache_branch="" cache_result="" cache_no_pr_branch=""
local -a gh_repo_args=()
now="$(_cmux_now)"
branch="$(git -C "$repo_path" branch --show-current 2>/dev/null)"
if [[ -z "$branch" ]] || ! command -v gh >/dev/null 2>&1; then
_cmux_pr_debug_log "$branch" "cache-miss:clear"
_cmux_pr_cache_clear
_cmux_clear_pr_for_panel
return 0
fi
prefix="$(_cmux_pr_cache_prefix 2>/dev/null || true)"
if [[ -n "$prefix" ]]; then
branch_file="${prefix}.branch"
repo_file="${prefix}.repo"
result_file="${prefix}.result"
timestamp_file="${prefix}.timestamp"
no_pr_branch_file="${prefix}.no-pr-branch"
[[ -r "$branch_file" ]] && cache_branch="$(<"$branch_file")"
[[ -r "$result_file" ]] && cache_result="$(<"$result_file")"
[[ -r "$no_pr_branch_file" ]] && cache_no_pr_branch="$(<"$no_pr_branch_file")"
fi
_PROGRAMA_PR_LAST_BRANCH="$cache_branch"
_PROGRAMA_PR_NO_PR_BRANCH="$cache_no_pr_branch"
if [[ "$cache_branch" == "$branch" && -n "$cache_result" ]]; then
_cmux_pr_debug_log "$branch" "cache-refresh"
else
_cmux_pr_debug_log "$branch" "cache-miss"
fi
repo_slug="$(_cmux_github_repo_slug_for_path "$repo_path")"
if [[ -n "$repo_slug" ]]; then
gh_repo_args=(--repo "$repo_slug")
fi
err_file="$(/usr/bin/mktemp "${TMPDIR:-/tmp}/cmux-gh-pr-view.XXXXXX" 2>/dev/null || true)"
[[ -n "$err_file" ]] || return 1
gh_output="$(
builtin cd "$repo_path" 2>/dev/null \
&& gh pr view "$branch" \
"${gh_repo_args[@]}" \
--json number,state,url \
--jq '[.number, .state, .url] | @tsv' \
2>"$err_file"
)"
gh_status=$?
if [[ -f "$err_file" ]]; then
gh_error="$("/bin/cat" -- "$err_file" 2>/dev/null || true)"
/bin/rm -f -- "$err_file" >/dev/null 2>&1 || true
fi
if (( gh_status != 0 )) || [[ -z "$gh_output" ]]; then
if (( gh_status == 0 )) && [[ -z "$gh_output" ]]; then
if [[ -n "$prefix" ]]; then
printf '%s\n' "$branch" >| "$branch_file"
printf '%s\n' "$repo_path" >| "$repo_file"
printf '%s\n' "$now" >| "$timestamp_file"
printf '%s\n' "none" >| "$result_file"
printf '%s\n' "$branch" >| "$no_pr_branch_file"
fi
_PROGRAMA_PR_LAST_BRANCH="$branch"
_PROGRAMA_PR_NO_PR_BRANCH="$branch"
_cmux_clear_pr_for_panel
return 0
fi
if _cmux_pr_output_indicates_no_pull_request "$gh_error"; then
if [[ -n "$prefix" ]]; then
printf '%s\n' "$branch" >| "$branch_file"
printf '%s\n' "$repo_path" >| "$repo_file"
printf '%s\n' "$now" >| "$timestamp_file"
printf '%s\n' "none" >| "$result_file"
printf '%s\n' "$branch" >| "$no_pr_branch_file"
fi
_PROGRAMA_PR_LAST_BRANCH="$branch"
_PROGRAMA_PR_NO_PR_BRANCH="$branch"
_cmux_clear_pr_for_panel
return 0
fi
# Always scope PR detection to the exact current branch. Preserve the
# last-known PR badge when gh fails transiently, then retry on the next
# background poll instead of showing a mismatched PR.
return 1
fi
IFS=$'\t' read -r number state url <<< "$gh_output"
if [[ -z "$number" || -z "$url" ]]; then
return 1
fi
case "$state" in
MERGED) status_opt="merged" ;;
OPEN) status_opt="open" ;;
CLOSED) status_opt="closed" ;;
*) return 1 ;;
esac
if [[ -n "$prefix" ]]; then
printf '%s\n' "$branch" >| "$branch_file"
printf '%s\n' "$repo_path" >| "$repo_file"
printf '%s\n' "$now" >| "$timestamp_file"
printf '%s\t%s\t%s\t%s\n' "pr" "$number" "$state" "$url" >| "$result_file"
/bin/rm -f -- "$no_pr_branch_file" >/dev/null 2>&1 || true
fi
_PROGRAMA_PR_LAST_BRANCH="$branch"
_PROGRAMA_PR_NO_PR_BRANCH=""
local workspace_id="" branch_json url_json params
workspace_id="$(_cmux_relay_workspace_id)" || workspace_id="$PROGRAMA_TAB_ID"
branch_json="$(_cmux_json_escape "$branch")"
url_json="$(_cmux_json_escape "$url")"
params="{\"workspace_id\":\"$workspace_id\",\"surface_id\":\"$PROGRAMA_PANEL_ID\",\"number\":$number,\"url\":\"$url_json\",\"state\":\"$status_opt\",\"branch\":\"$branch_json\"}"
_cmux_send "$(_cmux_json_rpc_frame "surface.report_pr" "$params")"
}
_cmux_child_pids() {
local parent_pid="$1"
[[ -n "$parent_pid" ]] || return 0
/bin/ps -ax -o pid= -o ppid= 2>/dev/null | /usr/bin/awk -v parent="$parent_pid" '$2 == parent { print $1 }'
}
_cmux_kill_process_tree() {
local pid="$1"
local signal="${2:-TERM}"
local child_pid=""
[[ -n "$pid" ]] || return 0
while IFS= read -r child_pid; do
[[ -n "$child_pid" ]] || continue
[[ "$child_pid" == "$pid" ]] && continue
_cmux_kill_process_tree "$child_pid" "$signal"
done < <(_cmux_child_pids "$pid")
kill "-$signal" "$pid" >/dev/null 2>&1 || true
}
_cmux_run_pr_probe_with_timeout() {
local repo_path="$1"
local force_probe="${2:-0}"
local probe_pid=""
local started_at=""
local now=""
started_at="$(_cmux_now)"
now=$started_at
(
_cmux_report_pr_for_path "$repo_path" "$force_probe"
) &
probe_pid=$!
while kill -0 "$probe_pid" >/dev/null 2>&1; do
sleep 1
now="$(_cmux_now)"
if (( _PROGRAMA_ASYNC_JOB_TIMEOUT > 0 )) && (( now - started_at >= _PROGRAMA_ASYNC_JOB_TIMEOUT )); then
_cmux_kill_process_tree "$probe_pid" TERM
sleep 0.2
if kill -0 "$probe_pid" >/dev/null 2>&1; then
_cmux_kill_process_tree "$probe_pid" KILL
sleep 0.2
fi
if ! kill -0 "$probe_pid" >/dev/null 2>&1; then
wait "$probe_pid" >/dev/null 2>&1 || true
fi
return 1
fi
done
wait "$probe_pid"
}
_cmux_halt_pr_poll_loop() {
if [[ -n "$_PROGRAMA_PR_POLL_PID" ]]; then
# Process-group kill: background jobs are process-group leaders, so
# negative PID kills the loop + all descendants (gh, sleep) without
# the synchronous /bin/ps + awk of tree-kill (~5-13ms).
kill -KILL -- -"$_PROGRAMA_PR_POLL_PID" 2>/dev/null || true
fi
local signal_path=""
signal_path="$(_cmux_pr_force_signal_path 2>/dev/null || true)"
[[ -n "$signal_path" ]] && /bin/rm -f -- "$signal_path" >/dev/null 2>&1 || true
_PROGRAMA_PR_POLL_PID=""
_PROGRAMA_PR_POLL_PWD=""
}
_cmux_stop_pr_poll_loop() {
_cmux_halt_pr_poll_loop
_cmux_pr_cache_clear
}
_cmux_start_pr_poll_loop() {
[[ -S "$PROGRAMA_SOCKET_PATH" ]] || return 0
[[ -n "$PROGRAMA_TAB_ID" ]] || return 0
[[ -n "$PROGRAMA_PANEL_ID" ]] || return 0
local watch_pwd="${1:-$PWD}"
local force_restart="${2:-0}"
local watch_shell_pid="$$"
local interval="${_PROGRAMA_PR_POLL_INTERVAL:-45}"
if [[ "$force_restart" != "1" && "$watch_pwd" == "$_PROGRAMA_PR_POLL_PWD" && -n "$_PROGRAMA_PR_POLL_PID" ]] \
&& kill -0 "$_PROGRAMA_PR_POLL_PID" 2>/dev/null; then
return 0
fi
if [[ -n "$_PROGRAMA_PR_POLL_PID" ]] && kill -0 "$_PROGRAMA_PR_POLL_PID" 2>/dev/null; then
_cmux_halt_pr_poll_loop
else
_PROGRAMA_PR_POLL_PID=""
fi
_PROGRAMA_PR_POLL_PWD="$watch_pwd"
(
local signal_path=""
signal_path="$(_cmux_pr_force_signal_path 2>/dev/null || true)"
while :; do
kill -0 "$watch_shell_pid" 2>/dev/null || break
local force_probe=0
if [[ -n "$signal_path" && -f "$signal_path" ]]; then
force_probe=1
/bin/rm -f -- "$signal_path" >/dev/null 2>&1 || true
fi
_cmux_run_pr_probe_with_timeout "$watch_pwd" "$force_probe" || true
local slept=0
while (( slept < interval )); do
kill -0 "$watch_shell_pid" 2>/dev/null || exit 0
if [[ -n "$signal_path" && -f "$signal_path" ]]; then
break
fi
sleep 1
slept=$(( slept + 1 ))
done
done
) >/dev/null 2>&1 &
_PROGRAMA_PR_POLL_PID=$!
disown 2>/dev/null
}
_cmux_bash_cleanup() {
_cmux_stop_pr_poll_loop
}
_cmux_command_starts_nested_shell() {
local cmd="$1"
local -a words=()
read -r -a words <<< "$cmd"
local index=0
local word base
while (( index < ${#words[@]} )); do
word="${words[index]}"
case "$word" in
*=*)
index=$(( index + 1 ))
continue ;;
exec|command|builtin|noglob|time)
index=$(( index + 1 ))
continue ;;
env)
index=$(( index + 1 ))
while (( index < ${#words[@]} )); do
word="${words[index]}"
case "$word" in
-*|*=*)
index=$(( index + 1 ))
continue ;;
esac
break
done
continue ;;
esac
base="${word##*/}"
case "$base" in
bash|zsh|sh|fish|nu|nix-shell)
return 0 ;;
nix)
local next_index=$(( index + 1 ))
local next_word="${words[next_index]:-}"
case "$next_word" in
develop|shell)
return 0 ;;
esac ;;
esac
return 1
done
return 1
}
_cmux_preexec_command() {
local cmd="${1:-${BASH_COMMAND:-}}"
_cmux_tmux_sync_cmux_environment
local programa_has_unix_socket=0
_cmux_socket_is_unix && programa_has_unix_socket=1
(( programa_has_unix_socket )) || _cmux_has_port_scan_transport || return 0
[[ -n "$PROGRAMA_TAB_ID" ]] || return 0
if [[ -z "$_PROGRAMA_TTY_NAME" ]]; then
local t
t="$(tty 2>/dev/null || true)"
t="${t##*/}"
[[ -n "$t" && "$t" != "not a tty" ]] && _PROGRAMA_TTY_NAME="$t"
fi
_cmux_report_shell_activity_state running
_cmux_report_tty_once
_cmux_ports_kick command
_cmux_halt_pr_poll_loop
if _cmux_command_starts_nested_shell "$cmd"; then
return 0
fi
}
_cmux_bash_preexec_hook() {
_cmux_preexec_command "$@"
}
_cmux_prompt_command() {
_cmux_tmux_sync_cmux_environment
local programa_has_unix_socket=0
_cmux_socket_is_unix && programa_has_unix_socket=1
(( programa_has_unix_socket )) || _cmux_has_port_scan_transport || return 0
[[ -n "$PROGRAMA_TAB_ID" ]] || return 0
if [[ -z "$_PROGRAMA_TTY_NAME" ]]; then
local t
t="$(tty 2>/dev/null || true)"
t="${t##*/}"
[[ "$t" != "not a tty" ]] && _PROGRAMA_TTY_NAME="$t"
fi
if [[ -n "$PROGRAMA_PANEL_ID" ]]; then
_cmux_report_shell_activity_state prompt
fi
_cmux_report_tty_once
local now
now="$(_cmux_now)"
if (( ! programa_has_unix_socket )); then
if (( now - _PROGRAMA_PORTS_LAST_RUN >= 10 )); then
_cmux_ports_kick refresh
fi
return 0
fi
[[ -n "$PROGRAMA_PANEL_ID" ]] || return 0
local pwd="$PWD"
# Post-wake socket writes can occasionally leave a probe process wedged.
# If one probe is stale, clear the guard so fresh async probes can resume.
if [[ -n "$_PROGRAMA_GIT_JOB_PID" ]]; then
if ! kill -0 "$_PROGRAMA_GIT_JOB_PID" 2>/dev/null; then
_PROGRAMA_GIT_JOB_PID=""
_PROGRAMA_GIT_JOB_STARTED_AT=0
elif (( _PROGRAMA_GIT_JOB_STARTED_AT > 0 )) && (( now - _PROGRAMA_GIT_JOB_STARTED_AT >= _PROGRAMA_ASYNC_JOB_TIMEOUT )); then
_PROGRAMA_GIT_JOB_PID=""
_PROGRAMA_GIT_JOB_STARTED_AT=0
fi
fi
# Resolve TTY name once.
if [[ -z "$_PROGRAMA_TTY_NAME" ]]; then
local t
t="$(tty 2>/dev/null || true)"
t="${t##*/}"
[[ "$t" != "not a tty" ]] && _PROGRAMA_TTY_NAME="$t"
fi
_cmux_report_tty_once
# CWD: keep the app in sync with the actual shell directory.
if [[ "$pwd" != "$_PROGRAMA_PWD_LAST_PWD" ]]; then
_PROGRAMA_PWD_LAST_PWD="$pwd"
(
local workspace_id="" pwd_json params
workspace_id="$(_cmux_relay_workspace_id)" || workspace_id="$PROGRAMA_TAB_ID"
pwd_json="$(_cmux_json_escape "$pwd")"
params="{\"workspace_id\":\"$workspace_id\",\"surface_id\":\"$PROGRAMA_PANEL_ID\",\"path\":\"$pwd_json\"}"
_cmux_send "$(_cmux_json_rpc_frame "surface.report_pwd" "$params")"
) >/dev/null 2>&1 &
disown 2>/dev/null
fi
# Branch can change via aliases/tools while an older probe is still in flight.
# Track .git/HEAD content so we can restart stale probes immediately.
local git_head_changed=0
if [[ "$pwd" != "$_PROGRAMA_GIT_HEAD_LAST_PWD" ]]; then
_PROGRAMA_GIT_HEAD_LAST_PWD="$pwd"
_PROGRAMA_GIT_HEAD_PATH="$(_cmux_git_resolve_head_path 2>/dev/null || true)"
_PROGRAMA_GIT_HEAD_SIGNATURE=""
fi
if [[ -n "$_PROGRAMA_GIT_HEAD_PATH" ]]; then
local head_signature
head_signature="$(_cmux_git_head_signature "$_PROGRAMA_GIT_HEAD_PATH" 2>/dev/null || true)"
if [[ -n "$head_signature" ]]; then
if [[ -z "$_PROGRAMA_GIT_HEAD_SIGNATURE" ]]; then
# The first observed HEAD value is just the session baseline.
# Treating it as a branch change clears restore-seeded PR badges
# before the first background probe can confirm the current PR.
_PROGRAMA_GIT_HEAD_SIGNATURE="$head_signature"
elif [[ "$head_signature" != "$_PROGRAMA_GIT_HEAD_SIGNATURE" ]]; then
_PROGRAMA_GIT_HEAD_SIGNATURE="$head_signature"
git_head_changed=1
# Also invalidate the PR poller so it refreshes with the new branch.
_PROGRAMA_PR_FORCE=1
fi
fi
fi
# Git branch/dirty can change without a directory change (e.g. `git checkout`),
# so update on every prompt (still async + de-duped by the running-job check).
# When pwd changes (cd into a different repo), kill the old probe and start fresh
# so the sidebar picks up the new branch immediately.
if [[ -n "$_PROGRAMA_GIT_JOB_PID" ]] && kill -0 "$_PROGRAMA_GIT_JOB_PID" 2>/dev/null; then
if [[ "$pwd" != "$_PROGRAMA_GIT_LAST_PWD" || "$git_head_changed" == "1" ]]; then
kill "$_PROGRAMA_GIT_JOB_PID" >/dev/null 2>&1 || true
_PROGRAMA_GIT_JOB_PID=""
_PROGRAMA_GIT_JOB_STARTED_AT=0
fi
fi
if [[ -z "$_PROGRAMA_GIT_JOB_PID" ]] || ! kill -0 "$_PROGRAMA_GIT_JOB_PID" 2>/dev/null; then
_PROGRAMA_GIT_LAST_PWD="$pwd"
_PROGRAMA_GIT_LAST_RUN=$now
(
# Skip git operations if not in a git repository to avoid TCC prompts
git rev-parse --git-dir >/dev/null 2>&1 || exit 0
local branch dirty=false workspace_id="" params
branch=$(git branch --show-current 2>/dev/null)
workspace_id="$(_cmux_relay_workspace_id)" || workspace_id="$PROGRAMA_TAB_ID"