-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-layout.sh
More file actions
executable file
·864 lines (759 loc) · 26.3 KB
/
Copy pathdev-layout.sh
File metadata and controls
executable file
·864 lines (759 loc) · 26.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
#!/usr/bin/env bash
# Herdr dev layout: sticky agent pane (left 50%) + review/explorer/terminal tabs.
# shellcheck disable=SC2016
set -euo pipefail
HERDR="${HERDR_BIN_PATH:-herdr}"
STATE_VERSION=1
TABS=(review explorer terminal)
_plugin_root() {
if [[ -n "${HERDR_PLUGIN_ROOT:-}" ]]; then
printf '%s' "$HERDR_PLUGIN_ROOT"
else
cd "$(dirname "${BASH_SOURCE[0]}")" && pwd
fi
}
# shellcheck source=/dev/null
source "$(_plugin_root)/config-reader.sh"
_state_dir() {
if [[ -n "${HERDR_PLUGIN_STATE_DIR:-}" ]]; then
printf '%s' "$HERDR_PLUGIN_STATE_DIR"
return 0
fi
printf '%s' "${XDG_STATE_HOME:-${HOME}/.local/state}/herdr/plugins/agentic-dev.dev-layout"
}
_state_path() {
local workspace_id="$1"
mkdir -p "$(_state_dir)"
printf '%s/%s.json' "$(_state_dir)" "$workspace_id"
}
_agent_cmd() {
if declare -F agentic_dev_agent_command >/dev/null 2>&1; then
agentic_dev_agent_command
else
printf '%s' "cursor-agent"
fi
}
_editor() {
if declare -F agentic_dev_layout_editor >/dev/null 2>&1; then
agentic_dev_layout_editor
else
printf '%s' "${EDITOR:-nvim}"
fi
}
_reviewer() {
if declare -F agentic_dev_layout_review >/dev/null 2>&1; then
agentic_dev_layout_review
else
printf '%s' "tuicr"
fi
}
# Known review tools get a launch recipe; anything else is run as-is.
_review_launch() {
local cmd
cmd="$(_reviewer)"
case "$cmd" in
tuicr) printf '%s' "tuicr" ;;
hunk) printf '%s' "hunk diff" ;;
*) printf '%s' "$cmd" ;;
esac
}
# Known editors get a launch recipe; a bare binary opens `.`; a command with
# arguments is run as-is.
_explorer_launch() {
local editor
editor="$(_editor)"
case "$editor" in
nvim|vim|neovim|fresh) printf '%s' "$editor ." ;;
tode) printf '%s' "tode" ;;
nano) printf '%s' "nano" ;;
*' '*) printf '%s' "$editor" ;;
*) printf '%s' "$editor ." ;;
esac
}
_herdr_json() {
"$HERDR" "$@" 2>/dev/null
}
_jq() {
jq -r "$@"
}
# Interactive pane shell: $SHELL when it is executable.
# Linux accounts are usually bash; modern macOS is zsh. Never hardcode bash —
# macOS /bin/bash prints "update your account to use zsh" in every pane.
_login_shell() {
local shell="${SHELL:-}"
if [[ -n "$shell" && -x "$shell" ]]; then
printf '%s' "$shell"
return 0
fi
if [[ -n "$shell" ]]; then
local resolved
resolved="$(command -v "$shell" 2>/dev/null || true)"
if [[ -n "$resolved" && -x "$resolved" ]]; then
printf '%s' "$resolved"
return 0
fi
fi
if [[ "$(uname -s)" == Darwin && -x /bin/zsh ]]; then
printf '%s' "/bin/zsh"
return 0
fi
printf '%s' "bash"
}
_pane_run_login() {
local pane="$1" cmd="$2" sh
sh="$(_login_shell)"
_herdr_json pane run "$pane" "$(printf '%q' "$sh") -li -c $(printf '%q' "$cmd")" >/dev/null || true
}
_tool_command() {
local tab="$1" sh
sh="$(printf '%q' "$(_login_shell)")"
case "$tab" in
review) printf '%s' "$(_review_launch); exec ${sh} -li" ;;
explorer) printf '%s' "$(_explorer_launch); exec ${sh} -li" ;;
terminal) printf '%s' "clear; exec ${sh} -li" ;;
*) return 1 ;;
esac
}
_focused_workspace_id() {
_herdr_json workspace list \
| _jq '.result.workspaces[] | select(.focused == true) | .workspace_id' \
| head -1
}
_workspace_id_by_label() {
local label="$1"
_herdr_json workspace list \
| _jq --arg label "$label" '.result.workspaces[] | select(.label == $label) | .workspace_id' \
| head -1
}
_workspace_id_by_cwd() {
local cwd="$1"
local ws_id pane_cwd
while IFS= read -r ws_id; do
[[ -n "$ws_id" ]] || continue
pane_cwd="$(_herdr_json pane list --workspace "$ws_id" \
| _jq '.result.panes[0].cwd // empty' \
| head -1)"
if [[ "$pane_cwd" == "$cwd" ]]; then
printf '%s' "$ws_id"
return 0
fi
done < <(_herdr_json workspace list | _jq '.result.workspaces[].workspace_id')
return 1
}
_pane_exists() {
local pane_id="$1"
[[ -n "$pane_id" ]] || return 1
_herdr_json pane get "$pane_id" >/dev/null 2>&1
}
# Serialize layout mutations per workspace. Concurrent create/apply (e.g. worktrunk
# post-start + wtc/dev attach) otherwise race and open duplicate agent panes.
# flock is util-linux (Linux); macOS has no flock(1), so fall back to mkdir.
_has_flock() {
command -v flock >/dev/null 2>&1
}
_layout_lock_acquire() {
local workspace_id="$1"
local lockfile waited pid
mkdir -p "$(_state_dir)"
if _has_flock; then
lockfile="$(_state_dir)/${workspace_id}.lock"
exec 9>"$lockfile"
flock 9
LAYOUT_LOCK_KIND="flock"
return 0
fi
LAYOUT_LOCKDIR="$(_state_dir)/${workspace_id}.lock.d"
LAYOUT_LOCK_KIND="mkdir"
waited=0
while ! mkdir "$LAYOUT_LOCKDIR" 2>/dev/null; do
pid="$(cat "$LAYOUT_LOCKDIR/pid" 2>/dev/null || true)"
if [[ -n "$pid" ]] && ! kill -0 "$pid" 2>/dev/null; then
rm -rf "$LAYOUT_LOCKDIR"
continue
fi
sleep 0.05
waited=$((waited + 1))
if (( waited > 200 )); then
echo "dev-layout: timed out waiting for layout lock" >&2
return 1
fi
done
printf '%s\n' "$$" >"$LAYOUT_LOCKDIR/pid"
}
_layout_lock_release() {
case "${LAYOUT_LOCK_KIND:-}" in
flock)
flock -u 9 2>/dev/null || true
exec 9>&- 2>/dev/null || true
;;
mkdir)
[[ -n "${LAYOUT_LOCKDIR:-}" ]] || return 0
rm -rf "$LAYOUT_LOCKDIR"
LAYOUT_LOCKDIR=""
;;
esac
LAYOUT_LOCK_KIND=""
}
_extra_panes_on_tab() {
local workspace_id="$1" tab_id="$2" tool_pane="$3"
_herdr_json pane list --workspace "$workspace_id" \
| _jq --arg tab "$tab_id" --arg tool "$tool_pane" \
'.result.panes[] | select(.tab_id == $tab and .pane_id != $tool) | .pane_id'
}
_close_orphan_agent_panes() {
local workspace_id="$1" tab_id="$2" tool_pane="$3" keep_pane="${4:-}"
local pane_id
while IFS= read -r pane_id; do
[[ -n "$pane_id" ]] || continue
[[ "$pane_id" == "$keep_pane" ]] && continue
_herdr_json pane close "$pane_id" >/dev/null 2>&1 || true
done < <(_extra_panes_on_tab "$workspace_id" "$tab_id" "$tool_pane")
}
_state_quarantine() {
local workspace_id="$1" path="$2"
local quarantine_dir target
quarantine_dir="$(_state_dir)/quarantine"
mkdir -p "$quarantine_dir"
target="$(mktemp "$quarantine_dir/$workspace_id.json.XXXXXX")"
if ! mv "$path" "$target"; then
rm -f "$target"
return 1
fi
printf 'dev-layout: quarantined invalid state: %s\n' "$target" >&2
}
_state_probe() {
local workspace_id="$1"
local path filename_workspace_id
path="$(_state_path "$workspace_id")"
[[ -f "$path" ]] || return 1
filename_workspace_id="${path##*/}"
filename_workspace_id="${filename_workspace_id%.json}"
if jq -e \
--arg workspace_id "$filename_workspace_id" \
--argjson version "$STATE_VERSION" \
'type == "object"
and (.version | type == "number")
and .version == $version
and (.workspace_id | type == "string")
and .workspace_id == $workspace_id
and (.label | type == "string")
and (.workdir | type == "string")
and (.agent_pane_id | type == "string")
and (.active_tab | type == "string")
and (.tabs | type == "object")' \
"$path" >/dev/null 2>&1; then
cat "$path"
return 0
fi
if ! _state_quarantine "$filename_workspace_id" "$path"; then
printf 'dev-layout: unable to quarantine invalid state; removing active record: %s\n' "$path" >&2
rm -f "$path"
fi
return 1
}
_state_load() {
_state_probe "$1"
}
_state_save() {
local workspace_id="$1"
local json="$2"
printf '%s' "$json" > "$(_state_path "$workspace_id")"
}
_state_delete() {
local workspace_id="$1"
rm -f "$(_state_path "$workspace_id")"
}
_state_init() {
local workspace_id="$1" label="$2" workdir="$3"
jq -n \
--arg workspace_id "$workspace_id" \
--arg label "$label" \
--arg workdir "$workdir" \
--argjson version "$STATE_VERSION" \
'{
workspace_id: $workspace_id,
label: $label,
workdir: $workdir,
version: $version,
agent_pane_id: "",
active_tab: "review",
tabs: {}
}'
}
_state_set_tab() {
local state="$1" tab="$2" tab_id="$3" tool_pane_id="$4"
printf '%s' "$state" | jq \
--arg tab "$tab" \
--arg tab_id "$tab_id" \
--arg tool_pane_id "$tool_pane_id" \
'.tabs[$tab] = {tab_id: $tab_id, tool_pane_id: $tool_pane_id}'
}
_state_get_tab_id() {
local state="$1" tab="$2"
printf '%s' "$state" | _jq --arg tab "$tab" '.tabs[$tab].tab_id // empty'
}
_state_get_tool_pane() {
local state="$1" tab="$2"
printf '%s' "$state" | _jq --arg tab "$tab" '.tabs[$tab].tool_pane_id // empty'
}
_ensure_workspace() {
local label="$1" workdir="$2"
local workspace_id
workspace_id="$(_workspace_id_by_label "$label")"
if [[ -z "$workspace_id" ]]; then
workspace_id="$(_workspace_id_by_cwd "$workdir" 2>/dev/null || true)"
fi
if [[ -n "$workspace_id" ]]; then
_herdr_json workspace rename "$workspace_id" "$label" >/dev/null || true
printf '%s' "$workspace_id"
return 0
fi
workspace_id="$(_herdr_json workspace create --cwd "$workdir" --label "$label" --no-focus \
| _jq '.result.workspace.workspace_id')"
printf '%s' "$workspace_id"
}
_ensure_tab() {
local workspace_id="$1" tab="$2" workdir="$3" state="$4"
local tab_id tool_pane_id existing
tab_id="$(_state_get_tab_id "$state" "$tab")"
if [[ -n "$tab_id" ]]; then
tool_pane_id="$(_state_get_tool_pane "$state" "$tab")"
if _pane_exists "$tool_pane_id"; then
printf '%s\t%s' "$tab_id" "$tool_pane_id"
return 0
fi
fi
existing="$(_herdr_json tab list --workspace "$workspace_id" \
| _jq --arg tab "$tab" '.result.tabs[] | select(.label == $tab) | .tab_id' \
| head -1)"
if [[ -n "$existing" ]]; then
tool_pane_id="$(_herdr_json pane list --workspace "$workspace_id" \
| _jq --arg tab "$existing" '.result.panes[] | select(.tab_id == $tab) | .pane_id' \
| head -1)"
if _pane_exists "$tool_pane_id"; then
printf '%s\t%s' "$existing" "$tool_pane_id"
return 0
fi
fi
local create_json tool_cmd
tool_cmd="$(_tool_command "$tab")"
create_json="$(_herdr_json tab create --workspace "$workspace_id" --cwd "$workdir" --label "$tab" --no-focus)"
tab_id="$(printf '%s' "$create_json" | _jq '.result.tab.tab_id')"
tool_pane_id="$(printf '%s' "$create_json" | _jq '.result.root_pane.pane_id')"
_pane_run_login "$tool_pane_id" "$tool_cmd"
printf '%s\t%s' "$tab_id" "$tool_pane_id"
}
_pane_is_shell() {
local pane="$1"
local json agent
json="$(_herdr_json pane get "$pane")" || return 1
agent="$(printf '%s' "$json" | _jq '.result.pane.agent // .result.agent // empty')"
[[ -z "$agent" || "$agent" == "null" ]]
}
_maybe_start_agent_pane() {
local pane="$1" start_agent="${2:-1}"
[[ "$start_agent" == "1" ]] || return 0
[[ -n "$pane" ]] || return 0
_pane_is_shell "$pane" || return 0
_herdr_json pane run "$pane" "$(_agent_cmd)" >/dev/null || true
}
_ensure_agent_pane() {
local workspace_id="$1" workdir="$2" state="$3" tab="$4" start_agent="${5:-1}"
local agent_pane tool_pane tab_id existing
tab_id="$(_state_get_tab_id "$state" "$tab")"
tool_pane="$(_state_get_tool_pane "$state" "$tab")"
[[ -n "$tab_id" && -n "$tool_pane" ]] || return 1
agent_pane="$(printf '%s' "$state" | _jq '.agent_pane_id // empty')"
if _pane_exists "$agent_pane"; then
_close_orphan_agent_panes "$workspace_id" "$tab_id" "$tool_pane" "$agent_pane"
_maybe_start_agent_pane "$agent_pane" "$start_agent"
printf '%s' "$agent_pane"
return 0
fi
# Reclaim a leftover split from a raced create before opening another.
existing="$(_extra_panes_on_tab "$workspace_id" "$tab_id" "$tool_pane" | head -1)"
if [[ -n "$existing" ]] && _pane_exists "$existing"; then
_close_orphan_agent_panes "$workspace_id" "$tab_id" "$tool_pane" "$existing"
_maybe_start_agent_pane "$existing" "$start_agent"
printf '%s' "$existing"
return 0
fi
# Split by pane id; tab focus would steal session focus on a background subspace.
agent_pane="$(_herdr_json pane split "$tool_pane" --direction right --ratio 0.5 --cwd "$workdir" --no-focus \
| _jq '.result.pane.pane_id')"
_herdr_json pane swap --source-pane "$agent_pane" --target-pane "$tool_pane" >/dev/null 2>&1 || true
if [[ "$start_agent" == "1" ]]; then
_herdr_json pane run "$agent_pane" "$(_agent_cmd)" >/dev/null || true
fi
printf '%s' "$agent_pane"
}
_attach_agent_to_tab() {
local state="$1" tab="$2"
local agent_pane tab_id tool_pane agent_tab
agent_pane="$(printf '%s' "$state" | _jq '.agent_pane_id // empty')"
tab_id="$(_state_get_tab_id "$state" "$tab")"
tool_pane="$(_state_get_tool_pane "$state" "$tab")"
[[ -n "$agent_pane" && -n "$tab_id" && -n "$tool_pane" ]] || return 0
_pane_exists "$agent_pane" || return 0
_pane_exists "$tool_pane" || return 0
agent_tab="$(_herdr_json pane get "$agent_pane" | _jq '.result.pane.tab_id // .result.tab_id // empty' 2>/dev/null || \
_herdr_json pane list | _jq --arg p "$agent_pane" '.result.panes[] | select(.pane_id == $p) | .tab_id')"
if [[ "$agent_tab" == "$tab_id" ]]; then
return 0
fi
_herdr_json pane move "$agent_pane" --tab "$tab_id" --split right --ratio 0.5 --target-pane "$tool_pane" --no-focus >/dev/null
_herdr_json pane swap --source-pane "$agent_pane" --target-pane "$tool_pane" >/dev/null 2>&1 || true
}
_workspace_is_new() {
local workspace_id="$1"
local tabs panes
tabs="$(_herdr_json tab list --workspace "$workspace_id" | _jq '.result.tabs | length')"
panes="$(_herdr_json pane list --workspace "$workspace_id" | _jq '.result.panes | length')"
[[ "$tabs" =~ ^[0-9]+$ && "$panes" =~ ^[0-9]+$ ]] || return 1
(( tabs <= 1 && panes <= 1 ))
}
_confirm_reset_layout() {
local reply
case "${WT_HERDR_LAYOUT_RESET:-}" in
1|yes|true) return 0 ;;
0|no|false) return 1 ;;
esac
if [[ -r /dev/tty ]]; then
printf 'Reset this workspace to the sticky-agent layout?\nExtra tabs and panes will be closed. [y/N] ' >/dev/tty
read -r reply </dev/tty || return 1
[[ "$reply" =~ ^[Yy]$ ]]
return
fi
return 1
}
# Leave one tab and one pane, then drop plugin state so ensure rebuilds the layout.
_layout_reset() {
local workspace_id="$1" keep_tab keep_pane tab_id pane_id
keep_tab="$(_herdr_json workspace get "$workspace_id" | _jq '.result.workspace.active_tab_id // empty')"
[[ -n "$keep_tab" ]] || keep_tab="$(_herdr_json tab list --workspace "$workspace_id" \
| _jq '.result.tabs[0].tab_id // empty')"
[[ -n "$keep_tab" ]] || {
_state_delete "$workspace_id"
return 0
}
keep_pane="$(_herdr_json pane list --workspace "$workspace_id" \
| _jq --arg tab "$keep_tab" '.result.panes[] | select(.tab_id == $tab) | .pane_id' \
| head -1)"
while IFS= read -r tab_id; do
[[ -n "$tab_id" && "$tab_id" != "$keep_tab" ]] || continue
_herdr_json tab close "$tab_id" >/dev/null 2>&1 || true
done < <(_herdr_json tab list --workspace "$workspace_id" \
| _jq --arg keep "$keep_tab" '.result.tabs[] | select(.tab_id != $keep) | .tab_id')
if [[ -n "$keep_pane" ]]; then
while IFS= read -r pane_id; do
[[ -n "$pane_id" && "$pane_id" != "$keep_pane" ]] || continue
_herdr_json pane close "$pane_id" >/dev/null 2>&1 || true
done < <(_herdr_json pane list --workspace "$workspace_id" \
| _jq --arg tab "$keep_tab" --arg keep "$keep_pane" \
'.result.panes[] | select(.tab_id == $tab and .pane_id != $keep) | .pane_id')
fi
_state_delete "$workspace_id"
}
_layout_ensure() {
local start_agent="${1:-1}"
local label="${WT_HERDR_LABEL:-}" workdir="${WT_HERDR_WORKDIR:-}"
local workspace_id state tab tab_id tool_pane agent_pane line
if [[ -z "$label" || -z "$workdir" ]]; then
workspace_id="${HERDR_WORKSPACE_ID:-$(_focused_workspace_id)}"
[[ -n "$workspace_id" ]] || { echo "dev-layout: no workspace context" >&2; exit 1; }
state="$(_state_load "$workspace_id" 2>/dev/null || true)"
if [[ -n "$state" ]]; then
label="$(printf '%s' "$state" | _jq '.label')"
workdir="$(printf '%s' "$state" | _jq '.workdir')"
else
label="$(_herdr_json workspace get "$workspace_id" | _jq '.result.workspace.label // empty')"
workdir="$(_herdr_json pane list --workspace "$workspace_id" \
| _jq '.result.panes[0].cwd // empty' | head -1)"
workdir="${workdir:-$PWD}"
label="${label:-$workdir}"
fi
fi
workspace_id="$(_ensure_workspace "$label" "$workdir")"
export HERDR_WORKSPACE_ID="$workspace_id"
_layout_lock_acquire "$workspace_id"
# shellcheck disable=SC2064
trap '_layout_lock_release' RETURN
if ! _workspace_is_new "$workspace_id"; then
if _confirm_reset_layout; then
_layout_reset "$workspace_id"
fi
fi
state="$(_state_load "$workspace_id" 2>/dev/null || _state_init "$workspace_id" "$label" "$workdir")"
state="$(printf '%s' "$state" | jq --arg wid "$workspace_id" --arg label "$label" --arg workdir "$workdir" \
'.workspace_id = $wid | .label = $label | .workdir = $workdir')"
if [[ -z "$(_state_get_tab_id "$state" "review")" ]]; then
local root_tab root_pane
root_tab="$(_herdr_json workspace get "$workspace_id" | _jq '.result.workspace.active_tab_id // empty')"
root_pane="$(_herdr_json pane list --workspace "$workspace_id" \
| _jq '.result.panes[0].pane_id // empty' | head -1)"
if [[ -n "$root_tab" && -n "$root_pane" ]]; then
_herdr_json tab rename "$root_tab" review >/dev/null 2>&1 || true
_pane_run_login "$root_pane" "$(_tool_command review)"
state="$(_state_set_tab "$state" review "$root_tab" "$root_pane")"
fi
fi
for tab in "${TABS[@]}"; do
if [[ "$tab" == "review" && -n "$(_state_get_tab_id "$state" "review")" ]]; then
continue
fi
line="$(_ensure_tab "$workspace_id" "$tab" "$workdir" "$state")"
tab_id="${line%%$'\t'*}"
tool_pane="${line#*$'\t'}"
state="$(_state_set_tab "$state" "$tab" "$tab_id" "$tool_pane")"
done
agent_pane="$(_ensure_agent_pane "$workspace_id" "$workdir" "$state" "review" "$start_agent")"
state="$(printf '%s' "$state" | jq --arg agent "$agent_pane" '.agent_pane_id = $agent')"
_state_save "$workspace_id" "$state"
printf '%s' "$state"
}
_dev_workspace_id() {
if [[ -n "${HERDR_WORKSPACE_ID:-}" ]]; then
printf '%s' "$HERDR_WORKSPACE_ID"
return 0
fi
_focused_workspace_id
}
_dev_state() {
local workspace_id
workspace_id="$(_dev_workspace_id)"
[[ -n "$workspace_id" ]] || return 1
_state_probe "$workspace_id"
}
_select_tab() {
local tab="$1"
local state workspace_id tab_id
state="$(_dev_state)" || return 0
workspace_id="$(printf '%s' "$state" | _jq '.workspace_id')"
tab_id="$(_state_get_tab_id "$state" "$tab")"
[[ -n "$tab_id" ]] || return 0
_attach_agent_to_tab "$state" "$tab"
_herdr_json tab focus "$tab_id" >/dev/null
if [[ "$tab" != "agent" ]]; then
_herdr_json pane focus --direction right >/dev/null 2>&1 || true
fi
state="$(printf '%s' "$state" | jq --arg tab "$tab" '.active_tab = $tab')"
_state_save "$workspace_id" "$state"
}
_focus_agent() {
local state workspace_id tab agent_pane tab_id
state="$(_dev_state)" || return 0
workspace_id="$(printf '%s' "$state" | _jq '.workspace_id')"
tab="$(printf '%s' "$state" | _jq '.active_tab // "review"')"
agent_pane="$(printf '%s' "$state" | _jq '.agent_pane_id // empty')"
tab_id="$(_state_get_tab_id "$state" "$tab")"
if ! _pane_exists "$agent_pane"; then
local workdir
workdir="$(printf '%s' "$state" | _jq '.workdir')"
agent_pane="$(_ensure_agent_pane "$workspace_id" "$workdir" "$state" "$tab" 1 2>/dev/null || true)"
if [[ -n "$agent_pane" ]]; then
state="$(printf '%s' "$state" | jq --arg agent "$agent_pane" '.agent_pane_id = $agent')"
_state_save "$workspace_id" "$state"
fi
else
_maybe_start_agent_pane "$agent_pane" 1
_attach_agent_to_tab "$state" "$tab"
fi
[[ -z "$tab_id" ]] || _herdr_json tab focus "$tab_id" >/dev/null
if _pane_exists "$agent_pane"; then
_herdr_json agent focus "$agent_pane" >/dev/null 2>&1 \
|| _herdr_json pane focus --direction left >/dev/null 2>&1 \
|| true
fi
}
_select_tab_index() {
local index="$1" semantic="$2"
local workspace_id tab_id
if _dev_state >/dev/null 2>&1; then
_select_tab "$semantic"
return 0
fi
workspace_id="$(_dev_workspace_id)"
[[ -n "$workspace_id" ]] || return 0
tab_id="$(_herdr_json tab list --workspace "$workspace_id" \
| _jq --argjson index "$index" '.result.tabs[$index].tab_id // empty')"
[[ -n "$tab_id" ]] || return 0
_herdr_json tab focus "$tab_id" >/dev/null
}
_workspace_is_live() {
local workspace_id="$1"
local list_json found
list_json="$(_herdr_json workspace list)" || return 2
found="$(printf '%s' "$list_json" | _jq --arg id "$workspace_id" \
'.result.workspaces[]? | select(.workspace_id == $id) | .workspace_id' \
| head -1)" || return 2
[[ -n "$found" ]]
}
_reconcile_live_state() {
local state="$1"
local agent_pane tab tool_pane next
next="$state"
agent_pane="$(printf '%s' "$next" | _jq '.agent_pane_id // empty')"
if [[ -n "$agent_pane" ]] && ! _pane_exists "$agent_pane"; then
next="$(printf '%s' "$next" | jq '.agent_pane_id = ""')"
fi
for tab in "${TABS[@]}"; do
tool_pane="$(_state_get_tool_pane "$next" "$tab")"
if [[ -n "$tool_pane" ]] && ! _pane_exists "$tool_pane"; then
next="$(printf '%s' "$next" | jq --arg tab "$tab" \
'if (.tabs[$tab] | type) == "object" then .tabs[$tab].tool_pane_id = "" else . end')"
fi
done
printf '%s' "$next"
}
_on_startup() {
local state_dir path workspace_id state reconciled live_status
state_dir="$(_state_dir)"
[[ -d "$state_dir" ]] || return 0
shopt -s nullglob
for path in "$state_dir"/*.json; do
[[ -f "$path" ]] || continue
workspace_id="${path##*/}"
workspace_id="${workspace_id%.json}"
state="$(_state_probe "$workspace_id" 2>/dev/null || true)"
[[ -n "$state" ]] || continue
live_status=0
_workspace_is_live "$workspace_id" || live_status=$?
if [[ "$live_status" -eq 1 ]]; then
_state_delete "$workspace_id"
continue
fi
if [[ "$live_status" -ne 0 ]]; then
# Workspace list failed: preserve state rather than delete.
continue
fi
reconciled="$(_reconcile_live_state "$state")"
if [[ "$reconciled" != "$state" ]]; then
_state_save "$workspace_id" "$reconciled"
fi
done
shopt -u nullglob
}
_on_pane_exited() {
local pane_id="${1:-}"
local workspace_id state agent_pane
[[ -n "$pane_id" ]] || return 0
for path in "$(_state_dir)"/*.json; do
[[ -f "$path" ]] || continue
state="$(cat "$path")"
agent_pane="$(printf '%s' "$state" | _jq '.agent_pane_id // empty')"
if [[ "$agent_pane" == "$pane_id" ]]; then
workspace_id="$(printf '%s' "$state" | _jq '.workspace_id')"
state="$(printf '%s' "$state" | jq '.agent_pane_id = ""')"
_state_save "$workspace_id" "$state"
fi
done
}
_on_workspace_closed() {
local workspace_id="${1:-}"
[[ -n "$workspace_id" ]] || return 0
_state_delete "$workspace_id"
}
_resolve_context() {
if [[ -n "${WT_HERDR_LABEL:-}" && -n "${WT_HERDR_WORKDIR:-}" ]]; then
return 0
fi
if [[ -n "${HERDR_WORKSPACE_ID:-}" ]]; then
return 0
fi
local focused
focused="$(_focused_workspace_id)"
[[ -n "$focused" ]] && export HERDR_WORKSPACE_ID="$focused"
}
# Submit WT_HERDR_AGENT_PROMPT via herdr agent prompt once the TUI is detected.
# Do not wait for idle before submit: a freshly started agent is often working/unknown.
# Skip when the pane is already working/blocked so post-start + handoff create cannot
# double-submit the same task (Composer paste / follow-up queue).
_prompt_if_requested() {
local state="$1"
local pane timeout_ms wait_ms interval_ms waited=0 agent status
[[ -n "${WT_HERDR_AGENT_PROMPT:-}" ]] || return 0
pane="$(printf '%s' "$state" | _jq '.agent_pane_id // empty')"
if [[ -z "$pane" ]]; then
echo "dev-layout: no agent_pane_id; cannot submit prompt" >&2
return 1
fi
timeout_ms="${WT_HERDR_AGENT_READY_TIMEOUT_MS:-30000}"
wait_ms="${WT_HERDR_AGENT_PROMPT_WAIT_MS:-15000}"
interval_ms=100
while true; do
agent="$(_herdr_json pane get "$pane" | _jq '.result.pane.agent // .result.agent // empty')"
if [[ -n "$agent" ]]; then
break
fi
if (( waited >= timeout_ms )); then
echo "dev-layout: agent TUI did not start on pane $pane (still a shell after ${timeout_ms}ms)" >&2
return 1
fi
sleep 0.1
waited=$((waited + interval_ms))
done
status="$(_herdr_json pane get "$pane" | _jq '.result.pane.agent_status // empty')"
if [[ "$status" == "working" || "$status" == "blocked" ]]; then
echo "dev-layout: skipping prompt; pane $pane already $status" >&2
return 0
fi
# --wait --until working fails loud if text only lands in the composer.
if ! "$HERDR" agent prompt "$pane" "$WT_HERDR_AGENT_PROMPT" \
--wait --until working --timeout "$wait_ms" >/dev/null; then
echo "dev-layout: herdr agent prompt failed for pane $pane (prompt did not reach working)" >&2
return 1
fi
}
main() {
local cmd="${1:-}"
[[ -n "$cmd" ]] || cmd="${HERDR_PLUGIN_ACTION_ID:-}"
[[ -n "$cmd" ]] || cmd="${HERDR_PLUGIN_EVENT:-}"
case "$cmd" in
create)
local state
_resolve_context
state="$(_layout_ensure 1)"
if [[ -z "${WT_HERDR_NO_ATTACH:-}" ]]; then
_select_tab review
fi
_prompt_if_requested "$state"
;;
apply)
_resolve_context
_layout_ensure 1 >/dev/null
if [[ -z "${WT_HERDR_NO_ATTACH:-}" ]]; then
_select_tab review
fi
;;
startup)
_on_startup
;;
focus_agent)
_resolve_context
_focus_agent
;;
select_review) _resolve_context; _select_tab review ;;
select_explorer) _resolve_context; _select_tab explorer ;;
select_terminal) _resolve_context; _select_tab terminal ;;
alt_review) _resolve_context; _select_tab_index 0 review ;;
alt_explorer) _resolve_context; _select_tab_index 1 explorer ;;
alt_terminal) _resolve_context; _select_tab_index 2 terminal ;;
event_pane_exited|pane.exited)
local pane_id
pane_id="$(printf '%s' "${HERDR_PLUGIN_EVENT_JSON:-{}}" | _jq '.pane_id // .pane.pane_id // empty' 2>/dev/null || true)"
_on_pane_exited "$pane_id"
;;
event_workspace_closed|workspace.closed)
local workspace_id
workspace_id="$(printf '%s' "${HERDR_PLUGIN_EVENT_JSON:-{}}" | _jq '.workspace_id // .workspace.workspace_id // empty' 2>/dev/null || true)"
_on_workspace_closed "$workspace_id"
;;
*)
echo "dev-layout: unknown command '$cmd'" >&2
exit 1
;;
esac
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main "$@"
fi